Foo.first(1, function (err, model) {
// Check if there was an error with the DB
if (err) throw new Error('Uh oh, something broke');
// If there was no error, but no model was found it must be missing
if (!err && !model) throw new Error('Foo not found');
// Update the model's name property
model.name = "New name!";
// Once we're done updating properties we can call save on the model.
// Save will send the current model data to the DB you specified
model.save(function (err, updatedModel) {
if (err) throw new Error('Could not save the model');
console.log("The model was updated!");
});
});
After I use this model lib, I find that I can not avoid finding a row when I want to update a row. I think that the better way is to avoid the SELECT.
Update a row , I usually use the raw SQL just like : Update table Set column=value WHERE ID = value.
Foo.first(1, function (err, model) { // Check if there was an error with the DB if (err) throw new Error('Uh oh, something broke');
// If there was no error, but no model was found it must be missing if (!err && !model) throw new Error('Foo not found');
// Update the model's name property model.name = "New name!";
// Once we're done updating properties we can call save on the model. // Save will send the current model data to the DB you specified model.save(function (err, updatedModel) { if (err) throw new Error('Could not save the model'); console.log("The model was updated!"); }); });
After I use this model lib, I find that I can not avoid finding a row when I want to update a row. I think that the better way is to avoid the SELECT. Update a row , I usually use the raw SQL just like : Update table Set
column
=value WHERE ID = value.