-
Notifications
You must be signed in to change notification settings - Fork 253
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
Deserializing to custom targets (classes) #253
Conversation
Reverts #1698. With mongodb/js-bson#253 and other approaches to document-class mapping on the table, we'd like to have further discussion before committing to any one API approach. Reopens NODE-1450
Reverts #1698. With mongodb/js-bson#253 and other approaches to document-class mapping on the table, we'd like to have further discussion before committing to any one API approach. Reopens NODE-1450
Reverts #1698. With mongodb/js-bson#253 and other approaches to document-class mapping on the table, we'd like to have further discussion before committing to any one API approach. Reopens NODE-1450
Hey @rahbari, I too feel like it'd be nice to have this feature where (de)serialization happens. This is cool but I still would hope for some sort of way of controlling the object instantiation. Some potential ideas (typescript): class Author {
public name: string;
}
class Comment {
public static $bson = {
targets: {
author: Author
}
};
public author: Author;
public body: string;
}
class Article {
public static $bson = {
targets: {
author: Author,
comments: [Comment]
}
};
public comments: Comment[] = [];
public author: Author;
public body: string;
public createdAt: Date;
}
articles.find<Article>({}, { target: Article }) The only thing I see that may be potentially missing is if people wanted to do field name mapping so that "createdAt" is the class property but is stored as "ca" in the database. None-the-less, this would be a joy to have. :) I slapped this together and have to go, so hopefully it all makes sense. |
Playing with this code isn't intuitive right now. It's not passing the target option to the deserializer. |
Also, the document detection question you have is the problem I had when trying to implement mapping into BSON. :(.. So I'm not sure the best way. I wish ONLY documents went through (de)serialize functions. |
Reverts mongodb#1698. With mongodb/js-bson#253 and other approaches to document-class mapping on the table, we'd like to have further discussion before committing to any one API approach. Reopens NODE-1450
Thank you for submitting this PR! While our current development plan is at capacity, we will track this in Jira for future prioritization. |
This pull request tries to cover this feature request https://github.com/mongodb/js-bson/issues/211
I thought of two options for nested targeting:
1- one (which current code is based on) is using static props of the top target, for example consider following document in mongodb:
Assigning the top document to an Article object:
Assigning articles to an extended Array:
assigning articles items to a Comment class:
I used $ at the begging of field names to prevent confusion with Class self properties, and used $ for array items.
2- Using a custom data structure like this:
articles.find({}, {targets: {root: Article, Comments: {root: Comments, item: Comment}}});
I believe first one is nicer but it's more confusing and the whole hierarchy of data must be defines.
If the maintainers accepts the general idea then it can be discussed.
For now I used firstBatch and nextBatch to detect documents parsing, until someone suggest a better approach.
if you want to test this code please use my modified mongodb core driver (I've added necessary code to commands.js).