-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Robust handling of ObjectID type for MongoDB #3720
Comments
Loopback 3 has AccessToken _id of type string. |
there is something confused by setting
as above say, if I get a model User,and I call the this.userRepository.create(user),it will return a User object instance,UserInstance. Expectaccording to my understand of citing criteria,I expect
Actural
this make me confused about the criteria cited above. please give me some instruction. |
MongoDB is tricky - see #1875
ObjectID
type for primary keys.ObjectID
is represented as astring
when converted to JSON'some-id' !== ObjectID('some-id')
.As a result, both PK and FK properties must use
ObjectID
as the type, and coercion must be applied where necessary.Ideally, I'd like LB4 to define MongoDB PK and FKs as follows:
{type: 'string', mongodb: {dataType: 'ObjectID'}}
Even better,
dataType: 'ObjectID'
should be automatically applied by the connector for PK and FKs referencing ObjectID PKs.For example:
For v1, I suppose we can ask developers to provide dataType manually.
With this setup in place,
id
andcategoryId
properties should be always returned as strings from DAO and connector methods.Related discussions
dataType: 'mongodb'
: feat: adds mongodb: {dataType: 'ObjectID'} to model properties loopback-connector-mongodb#517 and fix: ObjectID data type preservation loopback-connector-mongodb#525Acceptance criteria
For every property defined as
{type: 'string', mongodb: {dataType: 'ObjectID'}}
, including properties defined in nested/embedded models:filter.where
, but alsofindById
andreplaceById
), it converts string values to ObjectID. The conversion is applied to non-trivial conditions too, e.g.{where: {id: { inq: ['my-objectid-1', 'my-objectid-2'] }}}
Documentation page for MongoDB users explaining extra configuration needed
Blog post announcing the improvements
Tasks
The text was updated successfully, but these errors were encountered: