@salestrip/mongodb
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -119,3 +119,4 @@ 'use strict' | ||
dbModule.findOne = function(query, options = {}) { | ||
dbModule.findOne = function({id, ...rest}, options = {}) { | ||
const query = id ? {_id: new ObjectId(id), ...rest} : {...rest} | ||
return collection() | ||
@@ -122,0 +123,0 @@ .findOne(query, options) |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"main": "index.js", | ||
@@ -9,0 +9,0 @@ "author": "SalesTrip Limited", |
@@ -406,2 +406,28 @@ 'use strict' | ||
given('query contains string "id"', () => { | ||
it('returns matching document', async () => { | ||
const [project1, project2] = projects() | ||
await this.collection.insertMany([project1, project2]) | ||
const id = project1._id.toHexString() | ||
expect(typeof id).toBe('string') | ||
const results = await db('projects').findOne({id}) | ||
expect(results).not.toHaveProperty('_id') | ||
expect(results.name).toBe(project1.name) | ||
}) | ||
}) | ||
given('query contains id as ObjectId', () => { | ||
it('returns matching document', async () => { | ||
const [project1, project2] = projects() | ||
await this.collection.insertMany([project1, project2]) | ||
const id = project1._id | ||
expect(id).toBeInstanceOf(db.ObjectId) | ||
const results = await db('projects').findOne({id}) | ||
expect(results).toStrictEqual({ | ||
id: expect.any(String), | ||
name: project1.name | ||
}) | ||
}) | ||
}) | ||
it('returns null object', async () => { | ||
@@ -408,0 +434,0 @@ const results = await db('projects').findOne({name: 'whatever'}) |
32819
736