Skip to content
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

Can you only updateProperties per model? #258

Open
OscarGodson opened this issue Apr 1, 2015 · 10 comments
Open

Can you only updateProperties per model? #258

OscarGodson opened this issue Apr 1, 2015 · 10 comments

Comments

@OscarGodson
Copy link
Member

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
Copy link
Contributor

mde commented Apr 1, 2015

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
Copy link
Contributor

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

@mde
Copy link
Contributor

mde commented Apr 1, 2015

Is this not documented? I thought it was.

@mde
Copy link
Contributor

mde commented Apr 1, 2015

Delete does the same thing.

@danfinlay
Copy link
Contributor

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

@mde
Copy link
Contributor

mde commented Apr 1, 2015

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

@danfinlay
Copy link
Contributor

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
Copy link
Member Author

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
Copy link
Contributor

mde commented Apr 1, 2015

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

@mde
Copy link
Contributor

mde commented Apr 1, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants