loopback-ds-timestamp-mixin
loopback-ds-timestamp-mixin copied to clipboard
Idea: Backfill existing data with creation timestamp
This got me thinking if the mixin could auto-fill some timestamps for createdAt based on ObjectId ... exciting? see any merit? just ~meh?
Actually, I don't know if this will fly in your generic mixin which works for both sql and nosql but where would be a good place to handle upserts for mongo in your mixin with $currentDate and $setOnInsert so that folks don't have to worry about that stuff?
handle upserts for mongo in your mixin with $currentDate and $setOnInsert
💯 That might be a nice way to handle the upsert problem. In the generic sense there's no easy way unless query the database for the values before completing the upsert, something that invalidates the value of an upsert. However if those values were available without a query round trip and were the correct values to use I think it would make sense to create a shim.
I haven't done any investigation on this, so assuming those values are the right ones. I would probably create the following structure.
db.js
db/mongodb.js
Where the db.js file does the detection of the currently used database and provides either the mono module or no-op functions for other currently unsupported dbs; likely passing it the Model.
The monogodb file would probably just have one method exported, something like lookup which takes the ctx.instance (I don't think ctx.data would work) and adds the proper fields that would allow for an upsert with validation.
I'm still digesting your input ... in the meantime this snapshot represents the simplest set of from/to changes to get it done in code if the mixin doesn't take care of it, hoping to apply it to the mixin somehow.
