-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Labels
RepositoryIssues related to @loopback/repository packageIssues related to @loopback/repository packagefeaturefeature parity
Milestone
Description
Suggestion
ATM, toObject() method is implemented via toJSON and thus converts object values to JSON representation. This is problematic e.g. for Date and ObjectID values.
I am proposing to modify toObject() to preserve type of Date, ObjectID and other non-model values. Of course, if a model instance has a property set to a value of another Model, that property must be recursively converted via toObject to a plain object.
Use Cases
Consider the following test demonstrating the problem:
@model()
class Order extends Entity {
@property({
type: 'string',
id: true,
generated: true,
})
id: string;
@property()
createdAt: Date;
}
const orderRepo = // ...;
const DATE = new Date(...);
const created = orderRepo.create({
createdAt: DATE;
});
expect(created.toObject()).to.deepEqual({
id: created.id,
createdAt: DATE,
});The assertion above fails now.
Examples
The test example above should pass.
Acceptance criteria
-
.toObject()is updated to handleDate,ObjectID, and other non-model values, and handle recursion into fields that are alsoModelobjects. See : https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/model.js#L422 - Update or add mocha test cases in https://github.com/strongloop/loopback-datasource-juggler/tree/master/test
- Update documentation
Metadata
Metadata
Assignees
Labels
RepositoryIssues related to @loopback/repository packageIssues related to @loopback/repository packagefeaturefeature parity