Sails Js-Waterline:序列化、解析和初始化模型
Sailjs-Waterline: Serialize, Parse and initialize an Model
假设我找到了使用下一个命令的用户:
User.findOne(id).exec(function(err, user){
redis.set(JSON.stringify(user), id);
})
之后我从 redis 加载我的对象
redis.get(id, function(err, reply){
if(!err && reply) {
var user = JSON.parse(reply);
// here methods like user.save() or any of defined manually by developer is unavailable
//
} else {
..
}
})
用户模型示例:
module.exports = {
attributes : {
// Simple attribute:
// name: 'STRING',
// Or for more flexibility:
// phoneNumber: {
// type: 'STRING',
// defaultValue: '555-555-5555'
// }
email : {
type: 'string',
unique: true
},
// ...
verifyPass: function(pass, callback) {
var obj = this.toObject();
if (callback) {
return Password.compare(pass, obj.local.password, callback);
}
return Password.compareSync(pass, obj.local.password);
},
// retrieve profile for sending to front end
getProfile: function() {
var obj = this.toObject();
var profile = {};
profile.id = obj.id;
// ...
return profile;
},
每当我从 json 解析水线模型时,我都需要所有这些方法才能工作。有没有办法在根本不触发数据库的情况下对其进行初始化。如果我可以调用 user.save().
也很好
不幸的是,目前没有关于此的记录 public API,但您可以使用,
var user = new PersonCollection._model(values, {showJoins: true});
看看这对你有用!
假设我找到了使用下一个命令的用户:
User.findOne(id).exec(function(err, user){
redis.set(JSON.stringify(user), id);
})
之后我从 redis 加载我的对象
redis.get(id, function(err, reply){
if(!err && reply) {
var user = JSON.parse(reply);
// here methods like user.save() or any of defined manually by developer is unavailable
//
} else {
..
}
})
用户模型示例:
module.exports = {
attributes : {
// Simple attribute:
// name: 'STRING',
// Or for more flexibility:
// phoneNumber: {
// type: 'STRING',
// defaultValue: '555-555-5555'
// }
email : {
type: 'string',
unique: true
},
// ...
verifyPass: function(pass, callback) {
var obj = this.toObject();
if (callback) {
return Password.compare(pass, obj.local.password, callback);
}
return Password.compareSync(pass, obj.local.password);
},
// retrieve profile for sending to front end
getProfile: function() {
var obj = this.toObject();
var profile = {};
profile.id = obj.id;
// ...
return profile;
},
每当我从 json 解析水线模型时,我都需要所有这些方法才能工作。有没有办法在根本不触发数据库的情况下对其进行初始化。如果我可以调用 user.save().
也很好不幸的是,目前没有关于此的记录 public API,但您可以使用,
var user = new PersonCollection._model(values, {showJoins: true});
看看这对你有用!