Loopback-next: Loopback 4, Model, PostgresDB schema name

Created on 11 Jan 2019  路  4Comments  路  Source: strongloop/loopback-next

I use Loopback 4, TypeScript with PostgreSQL DB, I have a requirement to mention the postgresql schema name for the tables,

CREATE TABLE registry.vendors (
  id VARCHAR(50) not null default registry.id_generator()  PRIMARY KEY,
  name VARCHAR (50) UNIQUE NOT NULL
);

Model is defined as below

@model({
  name: 'vendors'
// name: 'registry.vendors' not working
})
export class Vendor extends Entity {
  @property({
    type: 'string',
    id: true,
    //required: true,
  })
  id: string;
....
}

Loopback 4 always look for table 'public.vendors' not 'registry', I don't find any documentation on lb4 website https://loopback.io/doc/en/lb4/Model.html related to this, how to explicitly mention the schema name in the model/loopback so that the lookup can be done on schema name, not the default 'public'

Repository PostgreSQL question

Most helpful comment

@gopalakrishnan-subramani, I believe it is similar to this issue https://github.com/strongloop/loopback-next/issues/2134. You can set the schema name and table name in the settings in your model TS file.

Cross posting my comment :

@model({
  settings: {
    postgresql: {
      table: 'customer',
      schema: 'registry' ////////// here is the line
    },
  },
})
export class Customer2 extends Entity {

For details, please see reference in this docs page: https://loopback.io/doc/en/lb3/Creating-a-database-schema-from-models.html

All 4 comments

@gopalakrishnan-subramani, I believe it is similar to this issue https://github.com/strongloop/loopback-next/issues/2134. You can set the schema name and table name in the settings in your model TS file.

Cross posting my comment :

@model({
  settings: {
    postgresql: {
      table: 'customer',
      schema: 'registry' ////////// here is the line
    },
  },
})
export class Customer2 extends Entity {

For details, please see reference in this docs page: https://loopback.io/doc/en/lb3/Creating-a-database-schema-from-models.html

@dhmlau,

Thank you so much, it solved the issues.

Closing the issue.

Hi, @dhmlau is it possible to specify what Postgres schema to use for queries at runtime? I have a use case where after authorization, I get the schema name from the user object and that would determine what schema on my Postgres DB to query. I'm building a multi-tenant application.

is it possible to specify what Postgres schema to use for queries at runtime? I have a use case where after authorization, I get the schema name from the user object and that would determine what schema on my Postgres DB to query. I'm building a multi-tenant application.

I opened a new issue to discuss this use case and posted few ideas what to try.

See https://github.com/strongloop/loopback-next/issues/5056

Was this page helpful?
0 / 5 - 0 ratings

Related issues

teambitcodeGIT picture teambitcodeGIT  路  3Comments

marioestradarosa picture marioestradarosa  路  3Comments

shahulhameedp picture shahulhameedp  路  3Comments

mightytyphoon picture mightytyphoon  路  3Comments

teambitcodeGIT picture teambitcodeGIT  路  3Comments