diff --git a/index.d.ts b/index.d.ts index f06b4dd..0fcb083 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,6 +5,7 @@ declare module 'apollo-datasource-mongodb' { Collection as MongooseCollection, Document, Model as MongooseModel, + LeanDocument } from 'mongoose' export type Collection> = T extends Document @@ -44,17 +45,17 @@ declare module 'apollo-datasource-mongodb' { findOneById( id: ObjectId | string, options?: Options - ): Promise + ): Promise | null | undefined> findManyByIds( ids: (ObjectId | string)[], options?: Options - ): Promise<(TData | null | undefined)[]> + ): Promise<(LeanDocument | null | undefined)[]> findByFields( fields: Fields, options?: Options - ): Promise<(TData | null | undefined)[]> + ): Promise<(LeanDocument | null | undefined)[]> deleteFromCacheById(id: ObjectId | string): Promise deleteFromCacheByFields(fields: Fields): Promise diff --git a/package-lock.json b/package-lock.json index 698f62a..02f3d0e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14463,6 +14463,15 @@ "strip-ansi": "^5.1.0" } }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "dev": true, + "requires": { + "safe-buffer": "~5.1.0" + } + }, "strip-ansi": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", diff --git a/src/__tests__/datasource.test.js b/src/__tests__/datasource.test.js index 14b1c76..25abc31 100644 --- a/src/__tests__/datasource.test.js +++ b/src/__tests__/datasource.test.js @@ -107,7 +107,7 @@ describe('Mongoose', () => { users.initialize() const user = await users.findOneById(alice._id) expect(user.name).toBe('Alice') - expect(user.id).toBe(alice._id.toString()) + expect(user._id.toString()).toBe(alice._id.toString()) }) test('Data Source with Collection', async () => { diff --git a/src/cache.js b/src/cache.js index d36fdc6..ba0d6bf 100644 --- a/src/cache.js +++ b/src/cache.js @@ -135,7 +135,10 @@ export const createCachingMethods = ({ collection, model, cache }) => { log('filter: ', filter) const findPromise = model - ? model.find(filter).exec() + ? model + .find(filter) + .lean() + .exec() : collection.find(filter).toArray() const results = await findPromise