Skip to content

Commit 4a1aadb

Browse files
crobinson42jmdobry
authored andcommitted
feat(): add adapter option mongoDriverOpts, add default option ignoreUndefined (#32)
1 parent f6805aa commit 4a1aadb

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

src/index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@ const DEFAULTS = {
3232
* @type {string}
3333
* @default mongodb://localhost:27017
3434
*/
35-
uri: 'mongodb://localhost:27017'
35+
uri: 'mongodb://localhost:27017',
36+
37+
/**
38+
* MongoDB Driver options
39+
*
40+
* @name MongoDBAdapter#mongoDriverOpts
41+
* @type {object}
42+
* @default { ignoreUndefined: true }
43+
*/
44+
mongoDriverOpts: {
45+
ignoreUndefined: true
46+
}
3647
}
3748

3849
const COUNT_OPTS_DEFAULTS = {}
@@ -185,7 +196,7 @@ export function MongoDBAdapter (opts) {
185196
utils.fillIn(this.removeOpts, REMOVE_OPTS_DEFAULTS)
186197

187198
this.client = new utils.Promise((resolve, reject) => {
188-
MongoClient.connect(opts.uri, (err, db) => {
199+
MongoClient.connect(opts.uri, opts.mongoDriverOpts, (err, db) => {
189200
if (err) {
190201
return reject(err)
191202
}

test/update.test.js

+31-20
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
1+
import * as JSData from 'js-data'
2+
13
describe('MongoDBAdapter#find', function () {
24
var adapter
35

4-
// create a record to test update against
5-
before(function () {
6-
// var id
7-
8-
// adapter = this.$$adapter
9-
// User = this.$$user
10-
11-
// return adapter.findAll(User, {
12-
// name: 'John'
13-
// }).then(function (users) {
14-
// assert.equal(users.length, 0)
15-
// return adapter.create(User, {name: 'John'})
16-
// }).then(function (user) {
17-
// id = user._id
18-
// return adapter.find(User, id.toString())
19-
// }).then(function (user) {
20-
// assert.objectsEqual(user, {_id: id, name: 'John'})
21-
// })
22-
})
23-
246
beforeEach(function () {
257
adapter = this.$$adapter
268
// User = this.$$user
@@ -29,4 +11,33 @@ describe('MongoDBAdapter#find', function () {
2911
it('should not support updateMany', function () {
3012
return assert.throws(adapter.updateMany)
3113
})
14+
15+
it('should ignore undefined when resource has schema', function () {
16+
var id
17+
18+
const schema = new JSData.Schema({
19+
'type': 'object',
20+
'properties': {
21+
'name': {
22+
'type': 'string'
23+
},
24+
'b': {
25+
'type': 'string'
26+
}
27+
}
28+
})
29+
const User = this.$$store.defineMapper('user', { schema: schema })
30+
31+
return adapter.findAll(User, {
32+
name: 'John'
33+
}).then(function (users) {
34+
assert.equal(users.length, 0)
35+
return adapter.create(User, {name: 'John'})
36+
}).then(function (user) {
37+
id = user._id
38+
return adapter.find(User, id.toString())
39+
}).then(function (user) {
40+
assert.objectsEqual(user, {_id: id, name: 'John'})
41+
})
42+
})
3243
})

0 commit comments

Comments
 (0)