Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PostgreSQL connector for LoopBack.

License

NotificationsYou must be signed in to change notification settings

loopbackio/loopback-connector-postgresql

PostgreSQL, is a popular open-source object-relational database.Theloopback-connector-postgresql module is the PostgreSQL connector for the LoopBack framework.

The PostgreSQL connector supports both LoopBack 3 and LoopBack 4. For more information, seeLoopBack 4 documentation,LoopBack 3 documentationandModule Long Term Support Policy below.

NOTE: The PostgreSQL connector requires PostgreSQL 8.x or 9.x.

Installation

In your application root directory, enter this command to install the connector:

$ npm install loopback-connector-postgresql --save

This installs the module from npm and adds it as a dependency to the application'spackage.json file.

If you create a PostgreSQL data source using the data source generator as described below, you don't have to do this, since the generator will runnpm install for you.

Creating a data source

For LoopBack 4 users, use the LoopBack 4Command-line interface to generate a DataSource with PostgreSQL connector to your LB4 application. Runlb4 datasource, it will prompt for configurations such as host, post, etc. that are required to connect to a PostgreSQL database.

After setting it up, the configuration can be found undersrc/datasources/<DataSourceName>.datasource.ts, which would look like this:

constconfig={name:'db',connector:'postgresql',url:'',host:'localhost',port:5432,user:'user',password:'pass',database:'testdb',};
For LoopBack 3 users

Use theData source generator to add a PostgreSQL data source to your application.The generator will prompt for the database server hostname, port, and other settingsrequired to connect to a PostgreSQL database. It will also run thenpm install command above for you.

The entry in the application's/server/datasources.json will look like this:

{% include code-caption.html content="/server/datasources.json" %}

"mydb":{"name":"mydb","connector":"postgresql""host":"mydbhost","port":5432,"url":"postgres://admin:admin@mydbhost:5432/db1?ssl=false","database":"db1","password":"admin","user":"admin","ssl":false}

Editdatasources.json to add other properties that enable you to connect the data source to a PostgreSQL database.

Connection Pool Settings

You can also specify connection pool settings in<DataSourceName>.datasource.ts ( ordatasources.json for LB3 users). For instance you can specify the minimum and the maximum pool size, and the maximum pool client's idle time before closing the client.

Example ofdb.datasource.ts:

constconfig={name:'db',connector:'postgresql',url:'',host:'localhost',port:5432,user:'user',password:'pass',database:'testdb',min:5,max:200,idleTimeoutMillis:60000,ssl:false};

Check outnode-pg-pool andnode postgres pooling example for more information.

Configuration options

PropertyTypeDescription
connectorString Connector name, either "loopback-connector-postgresql" or "postgresql"
databaseStringDatabase name
debugBooleanIf true, turn on verbose mode to debug database queries and lifecycle.
hostStringDatabase host name
passwordStringPassword to connect to database
portNumberDatabase TCP port
urlStringUse instead of thehost,port,user,password, anddatabaseproperties. For example:'postgres://test:mypassword@localhost:5432/dev'.
usernameStringUsername to connect to database
minIntegerMinimum number of clients in the connection pool
maxIntegerMaximum number of clients in the connection pool
idleTimeoutMillisIntegerMaximum time a client in the pool has to stay idle before closing it
sslBooleanWhether to try SSL/TLS to connect to server
defaultIdSortBoolean/StringSet tofalse to disable default sorting onid column(s). Set tonumericIdOnly to only apply to IDs with a number typeid.
allowExtendedOperatorsBooleanSet totrue to enable PostgreSQL-specific operators such ascontains. Learn more inExtended operators below.

NOTE: By default, the 'public' schema is used for all tables.

The PostgreSQL connector usesnode-postgres as the driver. For moreinformation about configuration parameters, seenode-postgres documentation.

Connecting to UNIX domain socket

A common PostgreSQL configuration is to connect to the UNIX domain socket/var/run/postgresql/.s.PGSQL.5432 instead of using the TCP/IP port. For example:

constconfig={name:'db',connector:'postgresql',url:'',host:'/var/run/postgresql/',port:5432,user:'user',password:'pass',database:'testdb',debug:true};

Defining models

LoopBack allows you to specify some database settings through the model definition and/or the property definition. These definitions would be mapped to the database. Please check out the CLIlb4 model for generating LB4 models. The following is a typical LoopBack 4 model that specifies the schema, table and column details through model definition and property definitions:

@model({settings:{postgresql:{schema:'public',table:'inventory'}},})exportclassInventoryextendsEntity{  @property({type:'number',required:true,scale:0,id:1,postgresql:{columnName:'id',dataType:'integer',dataLength:null,dataPrecision:null,dataScale:0,nullable:'NO',},})id:number;  @property({type:'string',postgresql:{columnName:'name',dataType:'text',dataLength:null,dataPrecision:null,dataScale:null,nullable:'YES',},})name?:string;  @property({type:'boolean',required:true,postgresql:{columnName:'available',dataType:'boolean',dataLength:null,dataPrecision:null,dataScale:null,nullable:'NO',},})available:boolean;constructor(data?:Partial<User>){super(data);}}
For LoopBack 3 users

The model definition consists of the following properties.

PropertyDefaultDescription
nameCamel-case of the database table nameName of the model.
optionsN/AModel level operations and mapping to PostgreSQL schema/table
propertiesN/AProperty definitions, including mapping to PostgreSQL column

For example:

{% include code-caption.html content="/common/models/model.json" %}

{"name":"Inventory","options":{"idInjection":false,"postgresql":{"schema":"strongloop","table":"inventory"}},"properties":{"id":{"type":"String","required":false,"length":64,"precision":null,"scale":null,"postgresql":{"columnName":"id","dataType":"character varying","dataLength":64,"dataPrecision":null,"dataScale":null,"nullable":"NO"}},"productId":{"type":"String","required":false,"length":20,"precision":null,"scale":null,"id":1,"postgresql":{"columnName":"product_id","dataType":"character varying","dataLength":20,"dataPrecision":null,"dataScale":null,"nullable":"YES"}},"locationId":{"type":"String","required":false,"length":20,"precision":null,"scale":null,"id":1,"postgresql":{"columnName":"location_id","dataType":"character varying","dataLength":20,"dataPrecision":null,"dataScale":null,"nullable":"YES"}},"available":{"type":"Number","required":false,"length":null,"precision":32,"scale":0,"postgresql":{"columnName":"available","dataType":"integer","dataLength":null,"dataPrecision":32,"dataScale":0,"nullable":"YES"}},"total":{"type":"Number","required":false,"length":null,"precision":32,"scale":0,"postgresql":{"columnName":"total","dataType":"integer","dataLength":null,"dataPrecision":32,"dataScale":0,"nullable":"YES"}}}}

To learn more about specifying database settings, please check the sectionData Mapping Properties.

Type mapping

See LoopBack 4 types (orLoopBack 3 types) fordetails on LoopBack's data types.

LoopBack to PostgreSQL types

LoopBack TypePostgreSQL Type
String
JSON
Text
Default
VARCHAR2
Default length is 1024
String[] VARCHAR2[]
NumberINTEGER
DateTIMESTAMP WITH TIME ZONE
BooleanBOOLEAN

Besides the basic LoopBack types, as we introduced above, you can also specify the database type for model properties. It would be mapped to the database (seeData Mapping Properties). For example, we would like the propertyprice to have database typedouble precision in the corresponding table in the database, we have specify it as following:

  @property({type:'number',postgresql:{dataType:'double precision',},})price?:number;
For LoopBack 3 users
"properties":{// .."price":{"type":"Number","postgresql":{"dataType":"double precision",}},

{% include warning.html content="Not all database types are supported for operating CRUD operations and queries with filters. For example, type Array cannot be filtered correctly, see GitHub issues:# 441 and# 342." %}

PostgreSQL types to LoopBack

PostgreSQL TypeLoopBack Type
BOOLEANBoolean
VARCHAR
CHARACTER VARYING
CHARACTER
CHAR
TEXT
String
BYTEANode.jsBuffer object
SMALLINT
INTEGER
BIGINT
DECIMAL
NUMERIC
REAL
DOUBLE PRECISION
FLOAT
SERIAL
BIGSERIAL
Number
DATE
TIMESTAMP
TIMESTAMP WITH TIME ZONE
TIMESTAMP WITHOUT TIME ZONE
TIME
TIME WITH TIME ZONE
TIME WITHOUT TIME ZONE
Date
POINTGeoPoint

Numeric Data Type

Note: Thenode.js driver for postgres by default castsNumeric type as a string onGET operation. This is to avoiddata precision loss sinceNumeric types in postgres cannot be safely converted to JavaScriptNumber.

For details, see the correspondingdriver issue.

Querying JSON fields

Note The fields you are querying should be setup to use the JSON postgresql data type - see Defining models

Assuming a model such as this:

  @property({type:'number',postgresql:{dataType:'double precision',},})price?:number;

You can query the nested fields with dot notation:

CustomerRepository.find({where:{address.state:'California',},order:'address.city',});

Extended operators

PostgreSQL supports the following PostgreSQL-specific operators:

Please note extended operators are disabled by default, you must enablethem at datasource level or model level by settingallowExtendedOperators totrue.

Operatorcontains

Thecontains operator allow you to query array properties and pick onlyrows where the stored value contains all of the items specified by the query.

The operator is implemented using PostgreSQLarray operator@>.

Note The fields you are querying must be setup to use the postgresql array data type - seeDefining models above.

Assuming a model such as this:

@model({settings:{allowExtendedOperators:true,}})classPost{  @property({type:['string'],postgresql:{dataType:'varchar[]',},})categories?:string[];}

You can query the tags fields as follows:

constposts=awaitpostRepository.find({where:{{categories:{'contains':['AA']},}}});

OperatorcontainedBy

Inverse of thecontains operator, thecontainedBy operator allow you to query array properties and pick onlyrows where the all the items in the stored value are contained by the query.

The operator is implemented using PostgreSQLarray operator<@.

Note The fields you are querying must be setup to use the postgresql array data type - seeDefining models above.

Assuming a model such as this:

@model({settings:{allowExtendedOperators:true,}})classPost{  @property({type:['string'],postgresql:{dataType:'varchar[]',},})categories?:string[];}

You can query the tags fields as follows:

constposts=awaitpostRepository.find({where:{{categories:{'containedBy':['AA']},}}});

OperatorcontainsAnyOf

ThecontainsAnyOf operator allow you to query array properties and pick onlyrows where the any of the items in the stored value matches any of the items in the query.

The operator is implemented using PostgreSQLarray overlap operator&&.

Note The fields you are querying must be setup to use the postgresql array data type - seeDefining models above.

Assuming a model such as this:

@model({settings:{allowExtendedOperators:true,}})classPost{  @property({type:['string'],postgresql:{dataType:'varchar[]',},})categories?:string[];}

You can query the tags fields as follows:

constposts=awaitpostRepository.find({where:{{categories:{'containsAnyOf':['AA']},}}});

Operatormatch

Thematch operator allows you to perform afull text search using the@@ operator in PostgreSQL.

Assuming a model such as this:

@model({settings:{allowExtendedOperators:true,}})classPost{  @property({type:'string',})content:string;}

You can query the content field as follows:

constposts=awaitpostRepository.find({where:{{content:{match:'someString'},}}});

Discovery and auto-migration

Model discovery

The PostgreSQL connector supportsmodel discovery that enables you to create LoopBack modelsbased on an existing database schema. Once you defined your datasource:

(Seedatabase discovery API for related APIs information)

Auto-migration

The PostgreSQL connector also supportsauto-migration that enables you to create a database schemafrom LoopBack models.

For example, based on the following model, the auto-migration method would create/alter existingcustomer table underpublic schema in the database. Tablecustomer would have two columns:name andid, whereid is also the primary key and has the default valueSERIAL as it has definition oftype: 'Number' andgenerated: true:

@model()exportclassCustomerextendsEntity{  @property({id:true,type:'Number',generated:true})id:number;  @property({type:'string'})name:string;}

By default, tables generated by the auto-migration are underpublic schema and named in lowercase.

Besides the basic model metadata, LoopBack allows you to specify part of the database schema definition via theproperty definition, which would be mapped to the database.

For example, based on the following model, after running the auto-migration script, a table namedCUSTOMER under schemamarket will be created. Moreover, you can also have different names for your property and the corresponding column. In the example, by specifying the column name, the propertyname will be mapped to thecustomer_name column. This is useful when your database has a different naming convention than LoopBack (camelCase).

@model(settings:{postgresql:{schema:'market',table:'CUSTOMER'},})exportclassCustomerextendsEntity{  @property({id:true,type:'Number',generated:true})id:number;  @property({type:'string',postgresql:{columnName:'customer_name'}})name:string;}

For how to run the script and more details:

(SeeLoopBack auto-migrate method for related APIs information)

Here are some limitations and tips:

  • If you definedgenerated: true in the id property, it generates integers by default. For auto-generated uuid, seeAuto-generated id property
  • Only the id property supports the auto-generation settinggenerated: true for now
  • Auto-migration doesn't create foreign key constraints by default. But they can be defined through the model definition. SeeAuto-migrate with foreign keys
  • Destroying models may result in errors due to foreign key integrity. First delete any related models by calling delete on models with relationships.

Auto-migrate/Auto-update models with foreign keys

Foreign key constraints can be defined in the model definition.

Note: The order of table creation is important. A referenced table must exist before creating a foreign key constraint.

Define your models and the foreign key constraints as follows:

customer.model.ts:

@model()exportclassCustomerextendsEntity{  @property({id:true,type:'Number',generated:true})id:number;  @property({type:'string'})name:string;}

order.model.ts:

@model({settings:{foreignKeys:{fk_order_customerId:{name:'fk_order_customerId',entity:'Customer',entityKey:'id',foreignKey:'customerId',onDelete:'CASCADE',onUpdate:'SET NULL'},},})exportclassOrderextendsEntity{  @property({id:true,type:'Number',generated:true})id:number;  @property({type:'string'})name:string;  @property({type:'Number'})customerId:number;}
For LoopBack 3 users
({"name":"Customer","options": {"idInjection":false  },"properties": {"id": {"type":"Number","id":1    },"name": {"type":"String","required":false    }  }},{"name":"Order","options": {"idInjection":false,"foreignKeys": {"fk_order_customerId": {"name":"fk_order_customerId","entity":"Customer","entityKey":"id","foreignKey":"customerId","onDelete":"CASCADE","onUpdate":"SET NULL"      }    }  },"properties": {"id": {"type":"Number""id":1    },"customerId": {"type":"Number"    },"description": {"type":"String","required":false    }  }})

{% include tip.html content="Removing or updating the value of `foreignKeys` will be updated or delete or update the constraints in the db tables. If there is a reference to an object being deleted then the `DELETE` will fail. Likewise if there is a create with an invalid FK id then the `POST` will fail. The `onDelete` and `onUpdate` properties are optional and will default to `NO ACTION`." %}

Auto-generated ids

Auto-migrate supports the automatic generation of property values for the id property. For PostgreSQL, the default id type isinteger. Thus if you havegenerated: true in the id property, it generates integers by default:

{id:true,type:'Number',required:false,generated:true// enables auto-generation}

It is common to use UUIDs as the primary key in PostgreSQL instead of integers. You can enable it with either the following ways:

  • use uuid that isgenerated by your LB application by settingdefaultFn: uuid:
  @property({id:true,type:'string'defaultFn:'uuid',// generated: true,  -> not needed})  id:string;
  • use PostgreSQL built-in (extension and) uuid functions:
  @property({id:true,type:'String',required:false,// settings below are neededgenerated:true,useDefaultIdType:false,postgresql:{dataType:'uuid',},})  id:string;

The setting usesuuid-ossp extension anduuid_generate_v4() function as default.

If you'd like to use other extensions and functions, you can do:

  @property({id:true,type:'String',required:false,// settings below are neededgenerated:true,useDefaultIdType:false,postgresql:{dataType:'uuid',extension:'myExtension',defaultFn:'myuuid'},})  id:string;

WARNING: It is the users' responsibility to make sure the provided extension and function are valid.

Module Long Term Support Policy

This module adopts theModule Long Term Support (LTS) policy, with the following End Of Life (EOL) dates:

VersionStatusPublishedEOL
5.xCurrentApr 2020Apr 2023(minimum)
3.xActive LTSMar 2017Apr 2022

Learn more about our LTS plan indocs.

Running tests

Own instance

If you have a local or remote PostgreSQL instance and would like to use that to run the test suite, use the following command:

  • Linux
POSTGRESQL_HOST=<HOST> POSTGRESQL_PORT=<PORT> POSTGRESQL_USER=<USER> POSTGRESQL_PASSWORD=<PASSWORD> POSTGRESQL_DATABASE=<DATABASE> CI=true npmtest
  • Windows
SET POSTGRESQL_HOST=<HOST> SET POSTGRESQL_PORT=<PORT> SET POSTGRESQL_USER=<USER> SET POSTGRESQL_PASSWORD=<PASSWORD> SET POSTGRESQL_DATABASE=<DATABASE> SET CI=true npmtest

Docker

If you do not have a local PostgreSQL instance, you can also run the test suite with very minimal requirements.

  • Assuming you haveDocker installed, run the following script which would spawn a PostgreSQL instance on your local:
source setup.sh<HOST><PORT><USER><PASSWORD><DATABASE>

where<HOST>,<PORT>,<USER>,<PASSWORD> and<DATABASE> are optional parameters. The default values arelocalhost,5432,root,pass andtestdb respectively.

  • Run the test:
npmtest

About

PostgreSQL connector for LoopBack.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors69


[8]ページ先頭

©2009-2025 Movatter.jp