tommybananas / finale

Create flexible REST endpoints and controllers from Sequelize models in your Express app
187 stars 36 forks source link

Multiple Datacenters #21

Closed rickysullivan closed 6 years ago

rickysullivan commented 6 years ago

I have multiple data centres (MSSQL) which all have the same database, username, passwords and the same table name & schema.

I'm trying to use the same express instance to map different end points that correspond to each Sequalize instance.

servers.forEach(server => {
  let sequelizeInstance = new Sequelize(database, username, password, {
    ...options,
    host: server.address
  });

  let Client = sequelizeInstance.define(
    "Client",
    {
      Client: { primaryKey: true, type: Sequelize.STRING },
      DatabaseName: Sequelize.STRING
    },
    { timestamps: false, freezeTableName: true }
  );

  finale.initialize({
    app: app,
    sequelize: sequelizeInstance
  });

  finale.resource({
    model: Client,
    pagination: false,
    endpoints: [`${server.site}/clients`, `${server.site}/clients/:id`],
    actions: ["list", "read"]
  });

  db[server.site] = {
    sequelize: sequelizeInstance
  };
});

I've also tried setting a base in the initialize():

  finale.initialize({
    app: app,
    base: server.site,
    sequelize: sequelizeInstance
  });

app._router.stack returns:

[ { path: 'nz/clients', method: 'get' },
  { path: 'nz/clients/:id', method: 'get' },
  { path: 'au/clients', method: 'get' },
  { path: 'au/clients/:id', method: 'get' },
  { path: 'ca/clients', method: 'get' },
  { path: 'ca/clients/:id', method: 'get' },
  { path: 'za/clients', method: 'get' },
  { path: 'za/clients/:id', method: 'get' } ]

But performing a GET request to http://localhost:62946/nz/clients returns Cannot GET /nz/clients

I had no trouble using a single instance of Sequalize with finale-rest. What am I doing wrong?

rickysullivan commented 6 years ago

I see the error of my ways:

endpoints: [`/${server.site}/clients`, `/${server.site}/clients/:id`]

The endpoints or the base needed to be prefixed with /