Open OscarGodson opened 9 years ago
You can use the static update
method on the constructor -- pass it a params object (or an instance with all the properties set the way you want), and a query object which will return all the instances you want to update:
https://github.com/geddy/model/blob/master/lib/index.js#L504
Whoah! That's fantastic! I can't believe I hadn't seen this.
Is this not documented? I thought it was.
Delete does the same thing.
I haven't seen it in any docs, and I did a brief additional scan.
I remember a bunch of back-and-forth about this before with delete, probably because it's not documented.
This'll be easy to document. I say leave this open or another issue to document those, it's a nice easy one for anyone to contribute.
I'd do it now but I'm craaaazy swamped.
Took some digging but I finally got it to work. I feel like I'm doing it wrong tho. To get this to work I noticed in the code the type
of model needs to be passed in with the data
despite already being an instance of model with a type, like this:
NotificationModel.update({read: true, type: 'Notification'}, { userId: id, read: false }, function (err, notificationModels) {
If you don't pass this you get an error (because of this line: https://github.com/geddy/model/blob/master/lib/adapters/sql/base.js#L132).
Weird, and seemingly a bug. It should already know what its type is.
Ah, that assumes you're using an actual instance for the data object, which was the original use -- you can also arrive at the update
method via the save
instance method on an previously created-and-saved item. This should be pretty easily fixable.
If I want to update a bunch of models (like getting them from
.all()
) can I do it with updateProperties? That doesn't seem to work.