Skip to content
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

fix: Always use lean Mongoose documents #102

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 8 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ declare module 'apollo-datasource-mongodb' {
Collection as MongooseCollection,
Document,
Model as MongooseModel,
LeanDocument,
MongooseDocument
} from 'mongoose'

export type Collection<T, U = MongoCollection<T>> = T extends Document
Expand All @@ -28,11 +30,13 @@ declare module 'apollo-datasource-mongodb' {
| (string | number | boolean | ObjectId)[]
}

type MongooseDocumentOrMongoCollection<T> = MongoCollection<T> | MongooseDocument

export interface Options {
ttl: number
}

export class MongoDataSource<TData, TContext = any> extends DataSource<
export class MongoDataSource<TData extends MongooseDocumentOrMongoCollection<any>, TContext = any> extends DataSource<
Copy link
Member

Choose a reason for hiding this comment

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

Why MongoCollection? It seems like TData is used for the document type:

    protected collection: Collection<TData>
    protected model: Model<TData>

Copy link
Author

Choose a reason for hiding this comment

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

I will drop this part of the code, it is an unnecessary type constraint. I think it was added because of the way we used it internally, but for general usage, it isn't necessary.

TContext
> {
protected context: TContext
Expand All @@ -44,17 +48,17 @@ declare module 'apollo-datasource-mongodb' {
findOneById(
id: ObjectId | string,
options?: Options
): Promise<TData | null | undefined>
): Promise<LeanDocument<TData> | null | undefined>

findManyByIds(
ids: (ObjectId | string)[],
options?: Options
): Promise<(TData | null | undefined)[]>
): Promise<(LeanDocument<TData> | null | undefined)[]>

findByFields(
fields: Fields,
options?: Options
): Promise<(TData | null | undefined)[]>
): Promise<(LeanDocument<TData> | null | undefined)[]>

deleteFromCacheById(id: ObjectId | string): Promise<void>
deleteFromCacheByFields(fields: Fields): Promise<void>
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down