在 Waterline 中创建模型后设置默认值
Set a default value after the model was created in Waterline
是否可以在创建模型实体后为属性设置默认值?
该属性之前不存在。
使用defaultsTo.
When a record is created, if no value was supplied, the record will be
created with the specified defaultsTo value. The supplied value can
also be a function that waterline will run while creating the record.
attributes: {
phoneNumber: {
type: 'string',
defaultsTo: '111-222-3333'
},
orderNumber: {
type: 'text',
defaultsTo: function() {
return uuid.v4();
}
}
}
根据您的用例,您可能还会发现 Lifecycle callbacks 有用。
Lifecycle callbacks are functions that are automagically called before
or after certain model actions. For example, we sometimes use
lifecycle callbacks to automatically hash a password before creating
or updating an Account model.
是否可以在创建模型实体后为属性设置默认值?
该属性之前不存在。
使用defaultsTo.
When a record is created, if no value was supplied, the record will be created with the specified defaultsTo value. The supplied value can also be a function that waterline will run while creating the record.
attributes: {
phoneNumber: {
type: 'string',
defaultsTo: '111-222-3333'
},
orderNumber: {
type: 'text',
defaultsTo: function() {
return uuid.v4();
}
}
}
根据您的用例,您可能还会发现 Lifecycle callbacks 有用。
Lifecycle callbacks are functions that are automagically called before or after certain model actions. For example, we sometimes use lifecycle callbacks to automatically hash a password before creating or updating an Account model.