1 year ago

#259761

test-img

Christian Bongiorno

Loopback custom connector implementation

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"]; }
})

loopbackjs

0 Answers

Your Answer

Accepted video resources