From e9a304a951ff4a5448191268e13c315bdace6e1c Mon Sep 17 00:00:00 2001 From: Jonas Meier Date: Thu, 2 Sep 2021 11:38:18 +0200 Subject: [PATCH 1/3] fix: Always use lean Mongoose documents With hydrated Mongoose documents they aren't serializable. Therefore caching can't be used, as it serializes the documents. --- index.d.ts | 12 ++++++++---- package-lock.json | 9 +++++++++ src/cache.js | 5 ++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index f06b4dd..290e8f0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,6 +5,8 @@ declare module 'apollo-datasource-mongodb' { Collection as MongooseCollection, Document, Model as MongooseModel, + LeanDocument, + MongooseDocument } from 'mongoose' export type Collection> = T extends Document @@ -28,11 +30,13 @@ declare module 'apollo-datasource-mongodb' { | (string | number | boolean | ObjectId)[] } + type MongooseDocumentOrMongoCollection = MongoCollection | MongooseDocument + export interface Options { ttl: number } - export class MongoDataSource extends DataSource< + export class MongoDataSource, TContext = any> extends DataSource< TContext > { protected context: TContext @@ -44,17 +48,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/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 From 4f1c8f11fb654b660f4b0ec4cc9bc07f23b58653 Mon Sep 17 00:00:00 2001 From: Jonas Meier Date: Sat, 27 Aug 2022 17:05:14 +0200 Subject: [PATCH 2/3] Fixed test --- src/__tests__/datasource.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 () => { From b96a86d5681f24ea9a74a20b6b05b63ef39c90a9 Mon Sep 17 00:00:00 2001 From: Jonas Meier Date: Sat, 27 Aug 2022 17:29:30 +0200 Subject: [PATCH 3/3] Removed unnecessary typ constraint --- index.d.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index 290e8f0..0fcb083 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,8 +5,7 @@ declare module 'apollo-datasource-mongodb' { Collection as MongooseCollection, Document, Model as MongooseModel, - LeanDocument, - MongooseDocument + LeanDocument } from 'mongoose' export type Collection> = T extends Document @@ -30,13 +29,11 @@ declare module 'apollo-datasource-mongodb' { | (string | number | boolean | ObjectId)[] } - type MongooseDocumentOrMongoCollection = MongoCollection | MongooseDocument - export interface Options { ttl: number } - export class MongoDataSource, TContext = any> extends DataSource< + export class MongoDataSource extends DataSource< TContext > { protected context: TContext