gqsimplenedb 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -0
- package/app.coffee +96 -0
- package/app.js +133 -0
- package/db.coffee +0 -0
- package/package.json +32 -0
- package/test.coffee +158 -0
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# gqnedb
|
|
2
|
+
|
|
3
|
+
```
|
|
4
|
+
.______.
|
|
5
|
+
____ ______ ____ ____ __| _/\_ |__
|
|
6
|
+
/ ___\ / ____// \_/ __ \ / __ | | __ \
|
|
7
|
+
/ /_/ >< <_| | | \ ___// /_/ | | \_\ \
|
|
8
|
+
\___ / \__ |___| /\___ >____ | |___ /
|
|
9
|
+
/_____/ |__| \/ \/ \/ \/
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Description
|
|
13
|
+
|
|
14
|
+
* A wrapper module for nedb for crud operations.
|
|
15
|
+
|
|
16
|
+
## Getting Started
|
|
17
|
+
|
|
18
|
+
### Dependencies
|
|
19
|
+
```
|
|
20
|
+
"node_version": "18.20.2",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"nedb":"1.8.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"gqtest": ">=0.0.17",
|
|
26
|
+
"coffeescript": "2.7.0",
|
|
27
|
+
"nodemon": "3.1.10"
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
### Installing
|
|
31
|
+
|
|
32
|
+
* npm install --save gqnedb
|
|
33
|
+
* npm test
|
|
34
|
+
|
|
35
|
+
## Help
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
check out test.coffee for how to use the module
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Authors
|
|
42
|
+
|
|
43
|
+
glidev5
|
|
44
|
+
|
|
45
|
+
## Version History
|
|
46
|
+
|
|
47
|
+
* 0.1
|
|
48
|
+
* Initial Release
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
This project is licensed under the MIT License - see the LICENSE.md file for details
|
|
53
|
+
|
package/app.coffee
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Datastore = require 'nedb'
|
|
2
|
+
log=console.log
|
|
3
|
+
db=null
|
|
4
|
+
|
|
5
|
+
@createDB=(path)->
|
|
6
|
+
db = new Datastore { filename: (path||'./db/nedb.json'), autoload: true }
|
|
7
|
+
db.persistence.setAutocompactionInterval 120000
|
|
8
|
+
log("nedb loaded...")
|
|
9
|
+
return db
|
|
10
|
+
|
|
11
|
+
@getDB=()->
|
|
12
|
+
return db
|
|
13
|
+
|
|
14
|
+
cleanStr=(str)->
|
|
15
|
+
return str.trim().toLowerCase()
|
|
16
|
+
|
|
17
|
+
@handler=(db,opt)->
|
|
18
|
+
return (req,res,next)->
|
|
19
|
+
method=cleanStr req.method
|
|
20
|
+
if method=="get"
|
|
21
|
+
db.find req.body.query,(err,docs)->
|
|
22
|
+
if err
|
|
23
|
+
log err
|
|
24
|
+
res.send 404,"Server error."
|
|
25
|
+
else
|
|
26
|
+
res.send 200,docs
|
|
27
|
+
|
|
28
|
+
else if method=="head"
|
|
29
|
+
db.count req.body.query,(err,count)->
|
|
30
|
+
if err
|
|
31
|
+
log err
|
|
32
|
+
res.send 404,"Server error."
|
|
33
|
+
else
|
|
34
|
+
res.send 200,count
|
|
35
|
+
|
|
36
|
+
else if method=="post"
|
|
37
|
+
db.insert req.body.data,(err,result)->
|
|
38
|
+
if err
|
|
39
|
+
log err
|
|
40
|
+
res.send 404,"Server error."
|
|
41
|
+
else
|
|
42
|
+
res.send 200,result
|
|
43
|
+
|
|
44
|
+
else if method=="upsert"
|
|
45
|
+
db.update req.body.query,req.body.data,{upsert:true},(err,num,upsert)->
|
|
46
|
+
if err
|
|
47
|
+
log err
|
|
48
|
+
res.send 404,"Server error."
|
|
49
|
+
else
|
|
50
|
+
res.send 200,{num:num,upsert:upsert}
|
|
51
|
+
|
|
52
|
+
else if method=="all"||method=="put"||method=="update"||method=="patch" #use put to do entire doc, use patch to update
|
|
53
|
+
db.update req.body.query,req.body.data,{multi:true},(err,result)->
|
|
54
|
+
if err
|
|
55
|
+
log err
|
|
56
|
+
res.send 404,"Server error."
|
|
57
|
+
else
|
|
58
|
+
res.send 200,result
|
|
59
|
+
|
|
60
|
+
else if method=="delete"||method=="remove"||method=="del"
|
|
61
|
+
db.remove req.body.query,{multi:(!!req.body.multi)},(err,num)->
|
|
62
|
+
if err
|
|
63
|
+
log err
|
|
64
|
+
res.send 404,"Server error."
|
|
65
|
+
else
|
|
66
|
+
db.persistence.compactDatafile();
|
|
67
|
+
res.send 200,num
|
|
68
|
+
|
|
69
|
+
else if method=="index"
|
|
70
|
+
db.ensureIndex {fieldName:req.body.field},(err)->
|
|
71
|
+
if err
|
|
72
|
+
log err
|
|
73
|
+
res.send 404,"Server error."
|
|
74
|
+
else
|
|
75
|
+
res.send 200,"OK"
|
|
76
|
+
|
|
77
|
+
else if method=="uniqueindex"
|
|
78
|
+
db.ensureIndex {fieldName:req.body.field,unique:true,sparse:true},(err)->
|
|
79
|
+
if err
|
|
80
|
+
log err
|
|
81
|
+
res.send 404,"Server error."
|
|
82
|
+
else
|
|
83
|
+
res.send 200,"OK"
|
|
84
|
+
|
|
85
|
+
else if method=="opt" # other operations
|
|
86
|
+
if !!opt
|
|
87
|
+
opt req,res,next,db
|
|
88
|
+
else
|
|
89
|
+
next req,res
|
|
90
|
+
else
|
|
91
|
+
next req,res
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
package/app.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Generated by CoffeeScript 2.7.0
|
|
2
|
+
(function() {
|
|
3
|
+
var Datastore, cleanStr, db, log;
|
|
4
|
+
|
|
5
|
+
Datastore = require('nedb');
|
|
6
|
+
|
|
7
|
+
log = console.log;
|
|
8
|
+
|
|
9
|
+
db = null;
|
|
10
|
+
|
|
11
|
+
this.createDB = function(path) {
|
|
12
|
+
db = new Datastore({
|
|
13
|
+
filename: path || './db/nedb.json',
|
|
14
|
+
autoload: true
|
|
15
|
+
});
|
|
16
|
+
db.persistence.setAutocompactionInterval(120000);
|
|
17
|
+
log("nedb loaded...");
|
|
18
|
+
return db;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
this.getDB = function() {
|
|
22
|
+
return db;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
cleanStr = function(str) {
|
|
26
|
+
return str.trim().toLowerCase();
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
this.handler = function(db, opt) {
|
|
30
|
+
return function(req, res, next) {
|
|
31
|
+
var method;
|
|
32
|
+
method = cleanStr(req.method);
|
|
33
|
+
if (method === "get") {
|
|
34
|
+
return db.find(req.body.query, function(err, docs) {
|
|
35
|
+
if (err) {
|
|
36
|
+
log(err);
|
|
37
|
+
return res.send(404, "Server error.");
|
|
38
|
+
} else {
|
|
39
|
+
return res.send(200, docs);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
} else if (method === "head") {
|
|
43
|
+
return db.count(req.body.query, function(err, count) {
|
|
44
|
+
if (err) {
|
|
45
|
+
log(err);
|
|
46
|
+
return res.send(404, "Server error.");
|
|
47
|
+
} else {
|
|
48
|
+
return res.send(200, count);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
} else if (method === "post") {
|
|
52
|
+
return db.insert(req.body.data, function(err, result) {
|
|
53
|
+
if (err) {
|
|
54
|
+
log(err);
|
|
55
|
+
return res.send(404, "Server error.");
|
|
56
|
+
} else {
|
|
57
|
+
return res.send(200, result);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
} else if (method === "upsert") {
|
|
61
|
+
return db.update(req.body.query, req.body.data, {
|
|
62
|
+
upsert: true
|
|
63
|
+
}, function(err, num, upsert) {
|
|
64
|
+
if (err) {
|
|
65
|
+
log(err);
|
|
66
|
+
return res.send(404, "Server error.");
|
|
67
|
+
} else {
|
|
68
|
+
return res.send(200, {
|
|
69
|
+
num: num,
|
|
70
|
+
upsert: upsert
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
} else if (method === "all" || method === "put" || method === "update" || method === "patch") { //use put to do entire doc, use patch to update
|
|
75
|
+
return db.update(req.body.query, req.body.data, {
|
|
76
|
+
multi: true
|
|
77
|
+
}, function(err, result) {
|
|
78
|
+
if (err) {
|
|
79
|
+
log(err);
|
|
80
|
+
return res.send(404, "Server error.");
|
|
81
|
+
} else {
|
|
82
|
+
return res.send(200, result);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
} else if (method === "delete" || method === "remove" || method === "del") {
|
|
86
|
+
return db.remove(req.body.query, {
|
|
87
|
+
multi: !!req.body.multi
|
|
88
|
+
}, function(err, num) {
|
|
89
|
+
if (err) {
|
|
90
|
+
log(err);
|
|
91
|
+
return res.send(404, "Server error.");
|
|
92
|
+
} else {
|
|
93
|
+
db.persistence.compactDatafile();
|
|
94
|
+
return res.send(200, num);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
} else if (method === "index") {
|
|
98
|
+
return db.ensureIndex({
|
|
99
|
+
fieldName: req.body.field
|
|
100
|
+
}, function(err) {
|
|
101
|
+
if (err) {
|
|
102
|
+
log(err);
|
|
103
|
+
return res.send(404, "Server error.");
|
|
104
|
+
} else {
|
|
105
|
+
return res.send(200, "OK");
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
} else if (method === "uniqueindex") {
|
|
109
|
+
return db.ensureIndex({
|
|
110
|
+
fieldName: req.body.field,
|
|
111
|
+
unique: true,
|
|
112
|
+
sparse: true
|
|
113
|
+
}, function(err) {
|
|
114
|
+
if (err) {
|
|
115
|
+
log(err);
|
|
116
|
+
return res.send(404, "Server error.");
|
|
117
|
+
} else {
|
|
118
|
+
return res.send(200, "OK");
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
} else if (method === "opt") { // other operations
|
|
122
|
+
if (!!opt) {
|
|
123
|
+
return opt(req, res, next, db);
|
|
124
|
+
} else {
|
|
125
|
+
return next(req, res);
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
return next(req, res);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
}).call(this);
|
package/db.coffee
ADDED
|
File without changes
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "gqsimplenedb",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A wrapper module for nedb for crud operations.",
|
|
5
|
+
"repository": "git@github.com:goldfiction/gqnedb.git",
|
|
6
|
+
"private": false,
|
|
7
|
+
"main": "app.js",
|
|
8
|
+
"node_version": "18.20.2",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"nedb":"1.8.0"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"gqtest": ">=0.0.17",
|
|
14
|
+
"coffeescript": "2.7.0",
|
|
15
|
+
"nodemon": "3.1.10"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"start": "export NODE_OPTIONS=--openssl-legacy-provider; node --require coffeescript/register db.coffee",
|
|
19
|
+
"test": "node --require coffeescript/register test.coffee",
|
|
20
|
+
"test:watch": "nodemon --delay 2 -q -x \"npm test\"",
|
|
21
|
+
"test:slow": "mocha --openssl-legacy-provider --require coffeescript/register --no-color --reporter min -t 20000 test/test.slow.coffee",
|
|
22
|
+
"install:nobin": "npm install --no-bin-links --no-optional --omit=optional --ignore-scripts",
|
|
23
|
+
"install:prod": "npm install --production",
|
|
24
|
+
"install:prod2": "npm install --omit=dev --no-bin-links",
|
|
25
|
+
"git": "git add .; git commit -m \"update\"; echo gitted...",
|
|
26
|
+
"git:push": "git push scm; echo pushed...",
|
|
27
|
+
"git:setup": "git config --global user.name \"username\";git config --global user.email \"username@gmail.com\";git branch --set-upstream-to=origin/master master;",
|
|
28
|
+
"commit":"git push origin master && git push bitbucket master && git push nodegit master"
|
|
29
|
+
},
|
|
30
|
+
"author": "glidev5@gmail.com",
|
|
31
|
+
"license": "MIT"
|
|
32
|
+
}
|
package/test.coffee
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
app=require './app.coffee'
|
|
2
|
+
assert=require 'assert'
|
|
3
|
+
tests=require 'gqtest'
|
|
4
|
+
|
|
5
|
+
it=tests.it
|
|
6
|
+
xit=tests.xit
|
|
7
|
+
run=tests.doRun
|
|
8
|
+
th=this
|
|
9
|
+
db=null
|
|
10
|
+
opt=null
|
|
11
|
+
hand=null
|
|
12
|
+
next=null
|
|
13
|
+
log=console.log
|
|
14
|
+
|
|
15
|
+
getRandomInt=(min,max)->
|
|
16
|
+
return Math.floor(Math.random() * (max - min + 1)) + min
|
|
17
|
+
|
|
18
|
+
rndid=getRandomInt(1,10000)
|
|
19
|
+
|
|
20
|
+
# dummy test for code integrity
|
|
21
|
+
it "should be able to run",(done)->
|
|
22
|
+
done()
|
|
23
|
+
|
|
24
|
+
# create db
|
|
25
|
+
|
|
26
|
+
it "should be able to create db",(done)->
|
|
27
|
+
db=app.createDB()
|
|
28
|
+
opt=(req,res,next,db)->
|
|
29
|
+
res.send 200,"OK"
|
|
30
|
+
hand=app.handler db,opt
|
|
31
|
+
next=(req,res)->
|
|
32
|
+
res.send 200,"OK"
|
|
33
|
+
done()
|
|
34
|
+
|
|
35
|
+
it "should be able to clean db",(done)->
|
|
36
|
+
req={method:"delete",body:{query:{"index":rndid},multi:true}}
|
|
37
|
+
res={send:(status,result)->
|
|
38
|
+
log status
|
|
39
|
+
log result
|
|
40
|
+
assert.equal status,200
|
|
41
|
+
done()
|
|
42
|
+
}
|
|
43
|
+
hand req,res,next
|
|
44
|
+
|
|
45
|
+
it "should be able to make field unique",(done)->
|
|
46
|
+
req={method:"uniqueindex",body:{field:"index"}}
|
|
47
|
+
res={send:(status,result)->
|
|
48
|
+
log status
|
|
49
|
+
log result
|
|
50
|
+
assert.equal status,200
|
|
51
|
+
done()
|
|
52
|
+
}
|
|
53
|
+
hand req,res,next
|
|
54
|
+
|
|
55
|
+
# post
|
|
56
|
+
it "should be able to post new entry",(done)->
|
|
57
|
+
req={method:"post",body:{data:{index:rndid,value:"abc"}}}
|
|
58
|
+
res={send:(status,result)->
|
|
59
|
+
log status
|
|
60
|
+
log result
|
|
61
|
+
assert.equal status,200
|
|
62
|
+
assert.equal result.value,"abc"
|
|
63
|
+
done()
|
|
64
|
+
}
|
|
65
|
+
hand req,res,next
|
|
66
|
+
|
|
67
|
+
# get
|
|
68
|
+
it "should be able to get entry",(done)->
|
|
69
|
+
req={method:"get",body:{query:{index:rndid}}}
|
|
70
|
+
res={send:(status,result)->
|
|
71
|
+
log status
|
|
72
|
+
log result
|
|
73
|
+
assert.equal status,200
|
|
74
|
+
assert.equal result[0].value,"abc"
|
|
75
|
+
done()
|
|
76
|
+
}
|
|
77
|
+
hand req,res,next
|
|
78
|
+
|
|
79
|
+
# head
|
|
80
|
+
it "should be able to head entry",(done)->
|
|
81
|
+
req={method:"head",body:{query:{index:rndid}}}
|
|
82
|
+
res={send:(status,result)->
|
|
83
|
+
log status
|
|
84
|
+
log result
|
|
85
|
+
assert.equal status,200
|
|
86
|
+
assert.equal result,1
|
|
87
|
+
done()
|
|
88
|
+
}
|
|
89
|
+
hand req,res,next
|
|
90
|
+
|
|
91
|
+
# update
|
|
92
|
+
it "should be able to update entry",(done)->
|
|
93
|
+
req={method:"update",body:{query:{index:rndid},data:{$set:{value:"def"}}}}
|
|
94
|
+
res={send:(status,result)->
|
|
95
|
+
log status
|
|
96
|
+
log result
|
|
97
|
+
assert.equal status,200
|
|
98
|
+
assert.equal result,1
|
|
99
|
+
done()
|
|
100
|
+
}
|
|
101
|
+
hand req,res,next
|
|
102
|
+
|
|
103
|
+
# upsert
|
|
104
|
+
it "should be able to upsert entry",(done)->
|
|
105
|
+
req={method:"upsert",body:{query:{index:rndid},data:{index:rndid,value:"ghi"}}}
|
|
106
|
+
res={send:(status,result)->
|
|
107
|
+
log status
|
|
108
|
+
log result
|
|
109
|
+
assert.equal status,200
|
|
110
|
+
assert.equal result.num,1
|
|
111
|
+
done()
|
|
112
|
+
}
|
|
113
|
+
hand req,res,next
|
|
114
|
+
|
|
115
|
+
# delete
|
|
116
|
+
it "should be able to delete entry",(done)->
|
|
117
|
+
req={method:"delete",body:{query:{index:rndid},multi:true}}
|
|
118
|
+
res={send:(status,result)->
|
|
119
|
+
log status
|
|
120
|
+
log result
|
|
121
|
+
assert.equal status,200
|
|
122
|
+
assert.equal result,1
|
|
123
|
+
done()
|
|
124
|
+
}
|
|
125
|
+
hand req,res,next
|
|
126
|
+
|
|
127
|
+
# opt
|
|
128
|
+
it "should be able to do opt operation",(done)->
|
|
129
|
+
req={method:"opt",body:{data:{index:rndid}}}
|
|
130
|
+
res={send:(status,result)->
|
|
131
|
+
log status
|
|
132
|
+
log result
|
|
133
|
+
assert.equal status,200
|
|
134
|
+
assert.equal result,"OK"
|
|
135
|
+
done()
|
|
136
|
+
}
|
|
137
|
+
next=done
|
|
138
|
+
hand req,res,next
|
|
139
|
+
|
|
140
|
+
# no method
|
|
141
|
+
it "should be able to handle no method",(done)->
|
|
142
|
+
req={method:"none",body:{data:{index:rndid}}}
|
|
143
|
+
res={send:(status,result)->
|
|
144
|
+
log status
|
|
145
|
+
log result
|
|
146
|
+
assert.equal status,200
|
|
147
|
+
assert.equal result,"OK"
|
|
148
|
+
done()
|
|
149
|
+
}
|
|
150
|
+
next=(req,res)->
|
|
151
|
+
res.send 200,"OK"
|
|
152
|
+
hand req,res,next
|
|
153
|
+
|
|
154
|
+
xit "should be able to get abc",(done)->
|
|
155
|
+
assert.equal app.abc,"abc"
|
|
156
|
+
done()
|
|
157
|
+
|
|
158
|
+
run()
|