有没有办法取代loopbackjs中的内置模型端点方法?
我试图替换用户模型的内置Create方法,添加一些额外的自定义逻辑,并根据需要重新格式化响应数据。有什么办法吗有没有办法取代loopbackjs中的内置模型端点方法?,loopbackjs,Loopbackjs,我试图替换用户模型的内置Create方法,添加一些额外的自定义逻辑,并根据需要重新格式化响应数据。有什么办法吗 提前感谢您需要申报新型号。然后,手动添加一个base属性,使其从内置用户模型继承 /common/models/custom user.json { "name": "customUser", "base": "User", "options": { "idInjection": false, "postgresql": { "schema":
提前感谢您需要申报新型号。然后,手动添加一个
base
属性,使其从内置用户模型继承
/common/models/custom user.json
{
"name": "customUser",
"base": "User",
"options": {
"idInjection": false,
"postgresql": {
"schema": "public",
"table": "user"
}
},
"dataSource": "mypostgresDS",
"properties": {
"id": {
"type": "number",
"postgresql": {
"columnName": "id",
"dataType": "integer"
}
}
...
...
}
}
然后可以覆盖上的内置管线
/common/models/custom user.js
module.exports = function (customUser) {
// avoid looking for properties that your new model doesn't have
var excludedProperties = [
'realm',
'emailVerified',
'verificationToken',
'credentials',
'challenges',
'lastUpdated',
'created'
];
// Remove the properties from base User model that doesn't have mapped columns
excludedProperties.forEach(function (p) {
delete customUser.definition.rawProperties[p];
delete customUser.definition.properties[p];
delete customUser.prototype[p];
});
customUser.prototype.createAccessToken = function (ttl, cb) {
var user = this;
if (ttl === undefined) ttl = 259200;
var timenow = new Date().toISOString();
console.log('ttl will be ', ttl, ' current time is ' + timenow);
user.accessTokens.create({
ttl: ttl
}, cb);
};
customUser.prototype.checkPassword = function (password, stored_hash) {
var user = this;
console.log('I will overwrite this check to always return true');
return true;
};
});
我会试试看,然后告诉你结果