node.js - Set default value to a model(Sails.js) -
i starting learn sails.js , want know if there simpler way set default value in model session variable? using waterlock authentication , setting user_id in session variable req.session.user_id
. have message model, want default 'from'
field session variable. there way in sails.js?
if using sail's default orm, waterline, model attributes have defaultsto option. supplied value may function. can @ waterline documentation.
sample model
module.exports = { attributes: { description: { type: 'string', defaultsto: 'no description.' }, email: { type: 'email', required: true, unique: true }, alias: { type: 'string', defaultsto: function(){ return this.email; } }, } };
if supplied value function, call function bound values in create.
for model.create(values)...
, if alias null/undefined, alias = defaultsto.call(values)
used value alias.
so use req.session.user_id
may have include during create. model.create({session: req.session,...})
. not sure, since not use waterlock. , i'm not don't think best way go it.
from experience these values ignored when using adapters such sails-mysql, if using sails-disk development, might see these values persisted. may want delete them before persist.
beforecreate: function(values, cb){ delete values['session']; cb(); }
Comments
Post a Comment