Friday, 15 March 2013

database - Optional Changes to Model in NodeJS -



database - Optional Changes to Model in NodeJS -

i'm using nodejs sequelizejs suppose question apply other situations.

an object comes in method properties attempting update model pull database (if find match).

var changes = { firstname: "foo", loginpin: 0000 }

the model contains many more properties sake of give-and-take here example.

var existingemployeemodel = { firstname: "bar", middleinitial: "f", loginpin: 1111, email: "foo@bar.com" }

so right @ processing this:

if (changes.firstname) existingemployeemodel.firstname = changes.firstname;

is there improve way this? ocassionally may .trim(), etc can't imagine ideal.

for example, lodash apply http://lodash.com/docs#assign function similar. phone call follows:

_.assign(existingemployeemodel, changes);

it should re-create properties changes model.

if not want add together dependency, can utilize one:

function apply(model, changes) { object.keys(changes).foreach(function (key) { if ('string' === typeof changes[key]) { model[key] = changes[key].trim(); } else { model[key] = changes[key]; } }); }

done.

database node.js postgresql sequelize.js

No comments:

Post a Comment