有没有办法在loopbackjs中替换内置的模型Endpoint方法?

时间:2023-01-15 20:35:02

I am trying to replace the User model's built-in Create method to add some extra custom logic and reformat the response data according to my need. Is there any way to do it?

我试图替换User模型的内置Create方法来添加一些额外的自定义逻辑,并根据我的需要重新格式化响应数据。有什么办法吗?

Thanks in advance

提前致谢

1 个解决方案

#1


0  

You need to declare a new model. Then, afterwards, manually add a base property for it to inherit from the built-in User model.

您需要声明一个新模型。然后,手动为其添加基本属性以从内置用户模型继承。

/common/models/custom-user.json

/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"
      }
    }
   ...
   ...
   }
}

Then you can override builtin routes on

然后你可以覆盖内置路由

/common/models/custom-user.js

/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;
    };
});

#1


0  

You need to declare a new model. Then, afterwards, manually add a base property for it to inherit from the built-in User model.

您需要声明一个新模型。然后,手动为其添加基本属性以从内置用户模型继承。

/common/models/custom-user.json

/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"
      }
    }
   ...
   ...
   }
}

Then you can override builtin routes on

然后你可以覆盖内置路由

/common/models/custom-user.js

/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;
    };
});