环回自定义连接器实现

发布于 2025-01-12 14:26:49 字数 3774 浏览 0 评论 0原文

我正在尝试实现一个自定义环回连接器,但我不清楚这一切是如何工作的。

这是我的模型:

{
  "customer": {
    "dataSource": "qb",
    "public": false
  },
  "company": {
    "dataSource": "qb",
    "public": true
  },
  "payment": {
    "dataSource": "qb",
    "public": false
  },
  "invoice": {
    "dataSource": "qb",
    "public": false
  }
}

模型中最重要的部分(为了节省空间)是

{
  "relations": {
    "company": {
      "type": "belongsTo",
      "model": "company",
      "foreignKey": "id",
      "primaryKey": "id"
    }
  }
}

And,在 company.json 中

{
  "name": "company",
  "plural": "companies",
  "base": "Model",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "customers": {
      "type": "hasMany",
      "model": "customer",
      "foreignKey": "customerId"
    },
    "payments": {
      "type": "hasMany",
      "model": "payment",
      "foreignKey": "customerId"
    },
    "invoices": {
      "type": "hasMany",
      "model": "customer",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}

,正如预期的那样,它会生成如下 URL: /companies/${id}/customers/${fk}

因此,我尝试使用 swagger UI 并提交: GET /companies/4620816365214377730/customers/456

我的问题现在有 2 倍:

  1. 它每次都会调用我的连接器上的 all 函数 - 立即,这没有意义。我已经给了它 2 个特定的 ID,为什么它可能想要所有任何东西?
  2. 我管理了上述内容并生成了所要求的结果,但随后环回报告了 404:
{
  "error": {
    "statusCode": 404,
    "name": "Error",
    "message": "could not find a model with id 4620816365214377730",
    "code": "MODEL_NOT_FOUND",
    "stack": "Error: could not find a model with id 4620816365214377730"
  }
}

所以,我绝对不明白 - callback 中的第一个参数是错误,第二个参数是结果。我已经将其硬编码为正确的(我认为)

我如何实现简单的 CRUD?为什么它不调用我的 findById 函数?我到处都有断点

const {Connector: connector} = require('loopback-connector')
const util = require("util");


exports.initialize = function initializeDataSource(dataSource, callback) {
  dataSource.connector = new QbConnector(dataSource.settings);
  dataSource.connector.dataSource = dataSource;

};
exports.QbConnector = QbConnector

function QbConnector(settings, datasource) {
  connector.call(this, 'quickbooks', settings)
  this.datasource = datasource
  this.client = require(`./qb`)(require('./axios'))
}
util.inherits(QbConnector, connector);

// connector.defineAliases(QbConnector.prototype, 'find', 'findById');
QbConnector.prototype.create = function(data, callback) {
  console.log()
}

QbConnector.prototype.replaceOrCreate = function(model, data, options, cb) {
  console.log()
}

QbConnector.prototype.findOne = function (filter,cb) {
  console.log()
}
QbConnector.prototype.all = function(model, filter, callback) {
  this.client[model]?.get(filter.where.id)
    ?.then(data => callback(null,{id: filter.where.id}))
    ?.catch(e => callback(JSON.stringify(e.response.data,null,4)))
}
QbConnector.prototype.count = function (whereClause,callback) {
  console.log()
}
QbConnector.prototype.save = function(model, data, options, cb)  {
  console.log()
}
QbConnector.prototype.findById = function (id, filter, options) {
  console.log()
}

当我进入回调时,它的定义是一个有保证的错误(我看到的消息)

(function anonymous(number, plural, select, pluralFuncs, fmt
) {
return function(d) { return "could not find a model with id " + d["0"]; }
})

I am trying to implement a custom loopback connector and it's not clear to me how this all works.

Here are my models:

{
  "customer": {
    "dataSource": "qb",
    "public": false
  },
  "company": {
    "dataSource": "qb",
    "public": true
  },
  "payment": {
    "dataSource": "qb",
    "public": false
  },
  "invoice": {
    "dataSource": "qb",
    "public": false
  }
}

The most important part to the model (and to save space) is

{
  "relations": {
    "company": {
      "type": "belongsTo",
      "model": "company",
      "foreignKey": "id",
      "primaryKey": "id"
    }
  }
}

And, in company.json

{
  "name": "company",
  "plural": "companies",
  "base": "Model",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "customers": {
      "type": "hasMany",
      "model": "customer",
      "foreignKey": "customerId"
    },
    "payments": {
      "type": "hasMany",
      "model": "payment",
      "foreignKey": "customerId"
    },
    "invoices": {
      "type": "hasMany",
      "model": "customer",
      "foreignKey": "customerId"
    }
  },
  "acls": [],
  "methods": {}
}

which, as expected, produces URLs like:
/companies/${id}/customers/${fk}

So, I try the swagger UI and submit: GET /companies/4620816365214377730/customers/456

The problem I have is now 2 fold:

  1. It calls the all function on my connector every time - right away, that doesn't make sense. I've given it 2 specific ID's why would it possible want all of anything?
  2. I managed the above and produced the results asked, but then loopback reports a 404:
{
  "error": {
    "statusCode": 404,
    "name": "Error",
    "message": "could not find a model with id 4620816365214377730",
    "code": "MODEL_NOT_FOUND",
    "stack": "Error: could not find a model with id 4620816365214377730"
  }
}

So, I definitely don't get it - the first param in callback is the err, and the second is the result. I have literally hardcoded it to be right (I think)

How do I implement simple CRUD? Why does it not call my findById function? I have breakpoints everywhere

const {Connector: connector} = require('loopback-connector')
const util = require("util");


exports.initialize = function initializeDataSource(dataSource, callback) {
  dataSource.connector = new QbConnector(dataSource.settings);
  dataSource.connector.dataSource = dataSource;

};
exports.QbConnector = QbConnector

function QbConnector(settings, datasource) {
  connector.call(this, 'quickbooks', settings)
  this.datasource = datasource
  this.client = require(`./qb`)(require('./axios'))
}
util.inherits(QbConnector, connector);

// connector.defineAliases(QbConnector.prototype, 'find', 'findById');
QbConnector.prototype.create = function(data, callback) {
  console.log()
}

QbConnector.prototype.replaceOrCreate = function(model, data, options, cb) {
  console.log()
}

QbConnector.prototype.findOne = function (filter,cb) {
  console.log()
}
QbConnector.prototype.all = function(model, filter, callback) {
  this.client[model]?.get(filter.where.id)
    ?.then(data => callback(null,{id: filter.where.id}))
    ?.catch(e => callback(JSON.stringify(e.response.data,null,4)))
}
QbConnector.prototype.count = function (whereClause,callback) {
  console.log()
}
QbConnector.prototype.save = function(model, data, options, cb)  {
  console.log()
}
QbConnector.prototype.findById = function (id, filter, options) {
  console.log()
}

When I step into the callback it's definition is a guaranteed error (the message I am seeing)

(function anonymous(number, plural, select, pluralFuncs, fmt
) {
return function(d) { return "could not find a model with id " + d["0"]; }
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文