Skip to content

Commit 28fe6bf

Browse files
author
高鸣飞
committed
[feature] mongo findOneAndUpdate & count
1 parent 5ce5b07 commit 28fe6bf

File tree

7 files changed

+208
-28
lines changed

7 files changed

+208
-28
lines changed

.travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ services:
99
- redis
1010
after_success:
1111
- nyc report --reporter=text-lcov | coveralls
12+
deploy:
13+
provider: npm
14+
15+
api_key: 438c9fe0-e2d6-4e7e-8559-ad7c8eed53a2
16+
on:
17+
branch: master

README.md

+29-11
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,21 @@ database driver with extended features.
9292
## feature list
9393
* changelog/oplog
9494
* auto release timeout connection
95+
* read-write access control
9596

9697
## api support
9798

98-
| Method | MySQL | MongoDB | Redis |
99-
| :------- |:------:| :------:| :----:|
100-
| select ||||
101-
| selectEx || × | × |
102-
| insert ||||
103-
| update ||||
104-
| delete ||||
105-
| find | × || × |
106-
| aggregate| × || × |
99+
| Method | MySQL | MongoDB | Redis |
100+
| :------- |:------:| :------:| :----:|
101+
| select ||||
102+
| selectEx || × | × |
103+
| insert ||||
104+
| update ||||
105+
| delete ||||
106+
| find | × || × |
107+
| aggregate | × || × |
108+
| count | × || × |
109+
| findOneAndUpdate| × || × |
107110

108111
## interface
109112

@@ -285,14 +288,14 @@ conn.delete(
285288
function(err, rows, info) {
286289
})
287290

288-
conn.find( // mongodb only
291+
conn.find( // mongodb only
289292
/* table */ 'table5',
290293
/* opt */ {fields: ['field1'], skip: 10, sort: 'field1'},
291294
/* where */ {field1: 10},
292295
function(err, rows, info) {
293296
})
294297

295-
conn.aggregate( // mongodb only
298+
conn.aggregate( // mongodb only
296299
/* table */ 'table5',
297300
/* opt */ {skip: 10, sort: 'name'},
298301
/* pipe */ [
@@ -302,6 +305,21 @@ conn.aggregate( // mongodb only
302305
function(err, rows, info) {
303306
})
304307

308+
conn.count( // mongodb only
309+
/* table */ 'table5',
310+
/* opt */ {skip: 10, limit: 50},
311+
/* where */ {field1: 10},
312+
function(err, rows, info) {
313+
})
314+
315+
conn.findOneAndUpdate( // mongodb only
316+
/* table */ 'table5',
317+
/* sets */ {field1: 20},
318+
/* where */ {field1: 10},
319+
function(err, rows, info) {
320+
})
321+
322+
305323
// Promise/async/await
306324
let result
307325
try {

lib/$/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ ALL_METHODS: [
1010
'selectEx',
1111
'find',
1212
'aggregate',
13+
'count',
14+
'findOneAndUpdate',
1315
],
1416

1517
util: require('util'),

lib/define/mongodb.js

+104-13
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ __destroy() {
4444
* @param {function} callback
4545
*/
4646
insert(table, sets, callback) {
47-
if (sets.constructor !== Object && sets.constructor !== Array) {
48-
return callback(new Error('invalid sets'))
47+
try {
48+
checkParams({table, sets, callback})
49+
} catch (err) {
50+
return callback(err)
4951
}
5052
if (this._readonly) {
5153
return errNotAllowd(callback)
@@ -65,8 +67,10 @@ insert(table, sets, callback) {
6567
}
6668

6769
update(table, sets, where, callback) {
68-
if (sets.constructor !== Object && sets.constructor !== Array) {
69-
return callback(new Error('invalid sets'))
70+
try {
71+
checkParams({table, sets, where, callback})
72+
} catch (err) {
73+
return callback(err)
7074
}
7175
if (this._readonly) {
7276
return errNotAllowd(callback)
@@ -91,6 +95,11 @@ update(table, sets, where, callback) {
9195
* @param {function} callback
9296
*/
9397
delete(table, where, callback) {
98+
try {
99+
checkParams({table, where, callback})
100+
} catch (err) {
101+
return callback(err)
102+
}
94103
if (this._readonly) {
95104
return errNotAllowd(callback)
96105
}
@@ -113,11 +122,13 @@ delete(table, where, callback) {
113122
* @param {function} callback
114123
*/
115124
select(table, fields, where, callback) {
125+
try {
126+
checkParams({table, fields, where, callback})
127+
} catch (err) {
128+
return callback(err)
129+
}
116130
let _fields = fields === '*' ? {} : fields
117131
this._conn.collection(table).find(where, _fields, (err, docs) => {
118-
if (err) {
119-
return callback(err)
120-
}
121132
return callback(err, docs, null)
122133
})
123134
}
@@ -132,14 +143,35 @@ select(table, fields, where, callback) {
132143
* @param {function} callback
133144
*/
134145
find(table, options, where, callback) {
146+
try {
147+
checkParams({table, options, where, callback})
148+
} catch (err) {
149+
return callback(err)
150+
}
135151
this._conn.collection(table).find(where, options, (err, docs) => {
136-
if (err) {
137-
return callback(err)
138-
}
139152
return callback(err, docs, null)
140153
})
141154
}
142155

156+
/**
157+
*
158+
* @param {stinrg} table
159+
* @param {object} sets
160+
* @param {object} where
161+
* @param {function} callback
162+
*/
163+
findOneAndUpdate(table, sets, where, callback) {
164+
try {
165+
checkParams({table, sets, where, callback})
166+
} catch (err) {
167+
return callback(err)
168+
}
169+
this._conn.collection(table).findOneAndUpdate(where, sets,
170+
(err, updated_doc) => {
171+
return callback(err, updated_doc, null)
172+
})
173+
}
174+
143175
/**
144176
* Mongodb.close()
145177
*/
@@ -156,20 +188,43 @@ release() {
156188
* @param {function} callback
157189
*/
158190
aggregate(table, options, pipeline, callback) {
191+
try {
192+
checkParams({table, options, pipeline, callback})
193+
} catch (err) {
194+
return callback(err)
195+
}
159196
this._conn.collection(table).aggregate(pipeline, options, (err, docs) => {
160-
if (err) {
161-
return callback(err)
162-
}
163197
return callback(err, docs, null)
164198
})
165199
}
166200

201+
/**
202+
* Mongodb.count()
203+
*
204+
* @param {string} table
205+
* @param {object} options
206+
* @param {object} where
207+
* @param {function} callback
208+
*/
209+
count(table, options, where, callback) {
210+
try {
211+
checkParams({table, options, where, callback})
212+
} catch (err) {
213+
return callback(err)
214+
}
215+
this._conn.collection(table).count(where, options, (err, count) => {
216+
return callback(err, count, null)
217+
})
218+
}
219+
220+
167221
}
168222

169223
const _ = require('lodash')
170224
const util = require('util')
171225
const debug = require('debug')('sqlx')
172226
const monk = require('monk')
227+
173228
function __log() {
174229
debug(util.format.apply(null, arguments))
175230
}
@@ -184,3 +239,39 @@ function errNotAllowd(callback) {
184239
return callback(new Error('not allowed'))
185240
}
186241

242+
// ------ validator -----
243+
const Ajv = require('ajv')
244+
const validator = (new Ajv()).compile({
245+
type: 'object',
246+
properties: {
247+
table: {type: 'string'},
248+
options: {type: 'object'},
249+
pipeline: {type: 'array'},
250+
where: {type: ['object', 'string']},
251+
sets: {type: ['object', 'array']},
252+
fields: {type: ['array', 'string']}
253+
},
254+
required: ['table'],
255+
})
256+
/**
257+
*
258+
* @param {object} params
259+
* @param {string} params.table
260+
* @param {string|Array} params.fileds
261+
* @param {object|Array} params.sets
262+
* @param {object} params.where
263+
* @param {object} params.options
264+
* @param {function} params.callback
265+
*/
266+
function checkParams(params) {
267+
for (let key of _.keys(params)) {
268+
if (util.isNullOrUndefined(params[key])) {
269+
throw new Error(`Missing parameter: ${key}`)
270+
}
271+
}
272+
if (!validator(params)) {
273+
throw new Error(validator.errors.map(err => {
274+
return err.dataPath + ' ' + err.message
275+
}))
276+
}
277+
}

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sqlx",
3-
"version": "4.2.0",
3+
"version": "4.3.0",
44
"description": "Database driver with extended features like mysql changelog/oplog, connection auto release.",
55
"main": "index.js",
66
"directories": {
@@ -28,6 +28,7 @@
2828
},
2929
"homepage": "https://github.com/yinrong/node-sqlx#readme",
3030
"dependencies": {
31+
"ajv": "^6.1.1",
3132
"async": "^2.0.1",
3233
"debug": "^2.2.0",
3334
"lodash": "^4.17.4",
@@ -57,5 +58,8 @@
5758
"text-summary"
5859
],
5960
"all": true
61+
},
62+
"publishConfig": {
63+
"access": "public"
6064
}
6165
}

test/mongodb.js

+51-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ it('insert', done => {
6060
], err => {throw err})
6161
})
6262

63+
it('count', done => {
64+
const client = sqlx.createClient()
65+
client.define('*', DBCONFIG_RW)
66+
const conn = client.getConnection(OPCONFIG)
67+
async.waterfall([
68+
(next) => {
69+
conn.count('table1', {
70+
limit: 1,
71+
}, {title: {$regex: /test/}}, next)
72+
},
73+
(rows, info, next) => {
74+
done()
75+
}
76+
], err => {throw err})
77+
})
78+
6379
it('find', done => {
6480
const client = sqlx.createClient()
6581
client.define('*', DBCONFIG_RW)
@@ -79,6 +95,21 @@ it('find', done => {
7995
], err => {throw err})
8096
})
8197

98+
it('findOneAndUpdate', done => {
99+
const client = sqlx.createClient()
100+
client.define('*', DBCONFIG_RW)
101+
const conn = client.getConnection(OPCONFIG)
102+
async.waterfall([
103+
(next) => {
104+
conn.findOneAndUpdate('table1', {title: 'test3'}, {title: 'test2'}, next)
105+
},
106+
(rows, info, next) => {
107+
assert.equal(rows.title, 'test3')
108+
done()
109+
}
110+
], err => {throw err})
111+
})
112+
82113
it('aggregate', done => {
83114
const client = sqlx.createClient()
84115
client.define('*', DBCONFIG_RW)
@@ -174,13 +205,13 @@ it('error-authority', done => {
174205
},
175206
(next) => {
176207
conn.insert('table1', 'error insert', err => {
177-
assert(err.message.match(/invalid sets/))
208+
assert(err.message.match(/sets should be object,array/))
178209
next()
179210
})
180211
},
181212
(next) => {
182213
conn.update('table1', 'error update', {}, err => {
183-
assert(err.message.match(/invalid sets/))
214+
assert(err.message.match(/sets should be object,array/))
184215
next()
185216
})
186217
},
@@ -196,6 +227,12 @@ it('error-operations', done => {
196227
client.define('*', DBCONFIG_RW)
197228
const conn = client.getConnection(OPCONFIG)
198229
async.waterfall([
230+
(next) => {
231+
conn.insert('error', undefined, err => {
232+
assert(err.message.match(/Missing/))
233+
next()
234+
})
235+
},
199236
// insert with wrong 'sets'
200237
(next) => {
201238
conn.insert('error', [0], err => {
@@ -234,6 +271,18 @@ it('error-operations', done => {
234271
// aggregate with wrong 'options' and 'pipeline'
235272
(next) => {
236273
conn.aggregate('error', [0], [0], err => {
274+
assert(err)
275+
next()
276+
})
277+
},
278+
(next) => {
279+
conn.count('error', [0], [0], err => {
280+
assert(err)
281+
next()
282+
})
283+
},
284+
(next) => {
285+
conn.findOneAndUpdate('error', [0], [0], err => {
237286
assert(err)
238287
done()
239288
})

0 commit comments

Comments
 (0)