Skip to content

feat(): add adapter option mongoDriverOpts, add default option ignoreUndefined #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,18 @@ const DEFAULTS = {
* @type {string}
* @default mongodb://localhost:27017
*/
uri: 'mongodb://localhost:27017'
uri: 'mongodb://localhost:27017',

/**
* MongoDB Driver options
*
* @name MongoDBAdapter#mongoDriverOpts
* @type {object}
* @default { ignoreUndefined: true }
*/
mongoDriverOpts: {
ignoreUndefined: true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have missed the db property:

{
  "db": {
    "ignoreUndefined": true
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danivek Depends on which doc you're looking at.. they both are accepted.

ref.:
mongoclient.connect
mongoclient

}
}

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

this.client = new utils.Promise((resolve, reject) => {
MongoClient.connect(opts.uri, (err, db) => {
MongoClient.connect(opts.uri, opts.mongoDriverOpts, (err, db) => {
if (err) {
return reject(err)
}
Expand Down
51 changes: 31 additions & 20 deletions test/update.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,8 @@
import * as JSData from 'js-data'

describe('MongoDBAdapter#find', function () {
var adapter

// create a record to test update against
before(function () {
// var id

// adapter = this.$$adapter
// User = this.$$user

// return adapter.findAll(User, {
// name: 'John'
// }).then(function (users) {
// assert.equal(users.length, 0)
// return adapter.create(User, {name: 'John'})
// }).then(function (user) {
// id = user._id
// return adapter.find(User, id.toString())
// }).then(function (user) {
// assert.objectsEqual(user, {_id: id, name: 'John'})
// })
})

beforeEach(function () {
adapter = this.$$adapter
// User = this.$$user
Expand All @@ -29,4 +11,33 @@ describe('MongoDBAdapter#find', function () {
it('should not support updateMany', function () {
return assert.throws(adapter.updateMany)
})

it('should ignore undefined when resource has schema', function () {
var id

const schema = new JSData.Schema({
'type': 'object',
'properties': {
'name': {
'type': 'string'
},
'b': {
'type': 'string'
}
}
})
const User = this.$$store.defineMapper('user', { schema: schema })

return adapter.findAll(User, {
name: 'John'
}).then(function (users) {
assert.equal(users.length, 0)
return adapter.create(User, {name: 'John'})
}).then(function (user) {
id = user._id
return adapter.find(User, id.toString())
}).then(function (user) {
assert.objectsEqual(user, {_id: id, name: 'John'})
})
})
})