-
Notifications
You must be signed in to change notification settings - Fork 64
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
base: master
Are you sure you want to change the base?
Conversation
With hydrated Mongoose documents they aren't serializable. Therefore caching can't be used, as it serializes the documents.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for contributing this! This is breaking, correct? Users with code that depends on receiving a full model will need to change their code after updating?
Looks like npm test
is failing:
Ideally we'd also have tests with TypeScript code that use the index.d.ts
types (both Mongoose and Mongo) and makes sure it compiles.
index.d.ts
Outdated
export interface Options { | ||
ttl: number | ||
} | ||
|
||
export class MongoDataSource<TData, TContext = any> extends DataSource< | ||
export class MongoDataSource<TData extends MongooseDocumentOrMongoCollection<any>, TContext = any> extends DataSource< |
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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.
Yes, it is definitely a breaking change! I updated the test and the change indicates one of the breaking changes. The lean documents don't have these getters like What do you mean with tests in Typescript? Rewriting the existing tests in Typescript but leave the rest of the code in plain JS? |
It should be possible to implement this in a way that is backwards compatible. For example, the constructor could take an optional argument that defined a transform function that would be applied to the query result before being written to the cache. One could supply a function that calls We have a large codebase, and it's very difficult to be 100% sure that all of our code will still work with tl;dr Why making a breaking change when you can achieve the same end result compositionally, offer greater flexibility, and avoid the breaking change. |
Actually, now that I'm thinking about composition, it seems like the one solution might be to just wrap a |
Ah, nevermind. I see the library is already doing |
@lorensr I'm not sure if I'll have time for this, but if I put up a PR for a solution of this shape would you accept it? |
@doronrk Yeah, a backcompat way to achieve the same goal would be great, thanks |
@lorensr do you have a contribution guide anywhere? |
@doronrk nope, feel free to ask questions (@lorendsr or [email protected]) or submit a PR with one |
@lorensr , |
@doronrk passing on master for me: |
With hydrated Mongoose documents they aren't serializable.
Therefore caching can't be used, as it serializes the documents.
See #74 for why this is needed for Mongoose documents.