InvoDB/index.js

50 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2021-11-24 23:42:10 +08:00
const core = require("./build/Release/invodb.node");
2021-11-12 15:58:21 +08:00
function database(filename) {
core.database(filename);
}
2021-11-22 19:11:18 +08:00
function collection(collectionName) {
2021-11-12 15:58:21 +08:00
function exist() { return core.exists(collectionName); }
function create() { core.create(collectionName); }
function insert(object) {
if(!core.exists(collectionName)) throw `Collection ${collectionName} doesn't exists!`;
let json = JSON.stringify(object);
core.insert(collectionName, json);
}
function remove(object) {
if(!core.exists(collectionName)) throw `Collection ${collectionName} doesn't exists!`;
let json = JSON.stringify(object);
core.remove(collectionName, json);
}
2021-12-05 22:45:55 +08:00
function find(object) {
2021-11-12 15:58:21 +08:00
if(!core.exists(collectionName)) throw `Collection ${collectionName} doesn't exists!`;
let json = JSON.stringify(object);
2021-11-13 12:53:14 +08:00
let res = [];
for(let str of core.query(collectionName, json)) {
res.push(JSON.parse(str));
}
return res;
2021-11-12 15:58:21 +08:00
}
2021-12-05 22:45:55 +08:00
function findOne(object) {
let res = find(object);
if(res.length >= 1) return res[0];
return undefined;
}
2021-11-12 15:58:21 +08:00
return {
exist: exist,
create: create,
insert: insert,
remove: remove,
2021-12-05 22:45:55 +08:00
find: find,
findOne: findOne,
2021-11-25 00:24:39 +08:00
update: insert
2021-11-12 15:58:21 +08:00
}
}
module.exports = {
database: database,
2021-11-22 19:11:18 +08:00
collection: collection
2021-11-12 15:58:21 +08:00
};