-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (45 loc) · 1.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const core = require("./build/Release/invodb.node");
function database(filename) {
core.database(filename);
}
function collection(collectionName) {
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);
}
function find(object) {
if(!core.exists(collectionName)) throw `Collection ${collectionName} doesn't exists!`;
let json = JSON.stringify(object);
let res = [];
for(let str of core.query(collectionName, json)) {
res.push(JSON.parse(str));
}
return res;
}
function findOne(object) {
let res = find(object);
if(res.length >= 1) return res[0];
return undefined;
}
return {
exist: exist,
create: create,
insert: insert,
remove: remove,
find: find,
findOne: findOne,
update: insert
}
}
module.exports = {
database: database,
collection: collection
};