geddy / model

Datastore-agnostic ORM in JavaScript
265 stars 55 forks source link

Can you only updateProperties per model? #258

Open OscarGodson opened 9 years ago

OscarGodson commented 9 years ago

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.

mde commented 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

danfinlay commented 9 years ago

Whoah! That's fantastic! I can't believe I hadn't seen this.

mde commented 9 years ago

Is this not documented? I thought it was.

mde commented 9 years ago

Delete does the same thing.

danfinlay commented 9 years ago

I haven't seen it in any docs, and I did a brief additional scan.

mde commented 9 years ago

I remember a bunch of back-and-forth about this before with delete, probably because it's not documented.

danfinlay commented 9 years ago

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.

OscarGodson commented 9 years ago

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).

mde commented 9 years ago

Weird, and seemingly a bug. It should already know what its type is.

mde commented 9 years ago

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.