The PostgreSQL connector enables LoopBack applications to connect to PostgreSQL data sources.Note: This page was generated from the loopback-connector-postgresql/README.md.

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

loopback-connector-postgresql

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

For more information, see the documentation.NOTE: The PostgreSQL connector requires PostgreSQL 8.x or 9.x.

Installation

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

  1. $ npm install loopback-connector-postgresql --save

This installs the module from npm and adds it as a dependency to the application’s package.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 run npm install for you.

Creating a data source

Use the Data 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 the npm install command above for you.

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

/server/datasources.json

  1. "mydb": {
  2. "name": "mydb",
  3. "connector": "postgresql"
  4. "host": "mydbhost",
  5. "port": 5432,
  6. "url": "postgres://admin:admin@mydbhost:5432/db1?ssl=false",
  7. "database": "db1",
  8. "password": "admin",
  9. "user": "admin",
  10. "ssl": false
  11. }

Edit datasources.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 datasources.json. 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 of datasource.json:

  1. {
  2. "mypostgresdb": {
  3. "host": "mydbhost",
  4. "port": 5432,
  5. "url": "postgres://admin:password1@mydbhost:5432/db1?ssl=false",
  6. "database": "db1",
  7. "password": "password1",
  8. "name": "mypostgresdb",
  9. "user": "admin",
  10. "connector": "postgresql",
  11. "min": 5,
  12. "max": 200,
  13. "idleTimeoutMillis": 60000,
  14. "ssl": false
  15. }
  16. }

Check out node-pg-pool and node postgres pooling example for more information.

Properties

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

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

The PostgreSQL connector uses node-postgres as the driver. For moreinformation about configuration parameters, see node-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:

  1. {
  2. "postgres": {
  3. "host": "/var/run/postgresql/",
  4. "port": "5432",
  5. "database": "dbname",
  6. "username": "dbuser",
  7. "password": "dbpassword",
  8. "name": "postgres",
  9. "debug": true,
  10. "connector": "postgresql"
  11. }
  12. }

Defining models

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:

/common/models/model.json

  1. {
  2. "name": "Inventory",
  3. "options": {
  4. "idInjection": false,
  5. "postgresql": {
  6. "schema": "strongloop",
  7. "table": "inventory"
  8. }
  9. },
  10. "properties": {
  11. "id": {
  12. "type": "String",
  13. "required": false,
  14. "length": 64,
  15. "precision": null,
  16. "scale": null,
  17. "postgresql": {
  18. "columnName": "id",
  19. "dataType": "character varying",
  20. "dataLength": 64,
  21. "dataPrecision": null,
  22. "dataScale": null,
  23. "nullable": "NO"
  24. }
  25. },
  26. "productId": {
  27. "type": "String",
  28. "required": false,
  29. "length": 20,
  30. "precision": null,
  31. "scale": null,
  32. "id": 1,
  33. "postgresql": {
  34. "columnName": "product_id",
  35. "dataType": "character varying",
  36. "dataLength": 20,
  37. "dataPrecision": null,
  38. "dataScale": null,
  39. "nullable": "YES"
  40. }
  41. },
  42. "locationId": {
  43. "type": "String",
  44. "required": false,
  45. "length": 20,
  46. "precision": null,
  47. "scale": null,
  48. "id": 1,
  49. "postgresql": {
  50. "columnName": "location_id",
  51. "dataType": "character varying",
  52. "dataLength": 20,
  53. "dataPrecision": null,
  54. "dataScale": null,
  55. "nullable": "YES"
  56. }
  57. },
  58. "available": {
  59. "type": "Number",
  60. "required": false,
  61. "length": null,
  62. "precision": 32,
  63. "scale": 0,
  64. "postgresql": {
  65. "columnName": "available",
  66. "dataType": "integer",
  67. "dataLength": null,
  68. "dataPrecision": 32,
  69. "dataScale": 0,
  70. "nullable": "YES"
  71. }
  72. },
  73. "total": {
  74. "type": "Number",
  75. "required": false,
  76. "length": null,
  77. "precision": 32,
  78. "scale": 0,
  79. "postgresql": {
  80. "columnName": "total",
  81. "dataType": "integer",
  82. "dataLength": null,
  83. "dataPrecision": 32,
  84. "dataScale": 0,
  85. "nullable": "YES"
  86. }
  87. }
  88. }
  89. }

Type mapping

See LoopBack types for details on LoopBack’s data types.

LoopBack to PostgreSQL types

LoopBack TypePostgreSQL Type
StringJSONTextDefault VARCHAR2 Default length is 1024
NumberINTEGER
DateTIMESTAMP WITH TIME ZONE
BooleanBOOLEAN

PostgreSQL types to LoopBack

PostgreSQL TypeLoopBack Type
BOOLEANBoolean
VARCHARCHARACTER VARYINGCHARACTERCHARTEXT String
BYTEANode.js Buffer object
SMALLINTINTEGERBIGINTDECIMALNUMERICREALDOUBLESERIALBIGSERIALNumber
DATETIMESTAMPTIMEDate
POINTGeoPoint

Numeric Data Type

Note: The node.js driver for postgres by default casts Numeric type as a string on GET operation. This is to avoid data precision loss since Numeric types in postgres cannot be safely converted to JavaScript Number.

For details, see the corresponding driver 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:

  1. {
  2. "name": "Customer",
  3. "properties": {
  4. "address": {
  5. "type": "object",
  6. "postgresql": {
  7. "dataType": "json"
  8. }
  9. }
  10. }
  11. }

You can query the nested fields with dot notation:

  1. Customer.find({
  2. where: {
  3. 'address.state': 'California'
  4. },
  5. order: 'address.city'
  6. })

Discovery and auto-migration

Model discovery

The PostgreSQL connector supports model discovery that enables you to create LoopBack modelsbased on an existing database schema using the unified database discovery API. For more information on discovery, see Discovering models from relational databases.

Auto-migration

The PostgreSQL connector also supports auto-migration that enables you to create a database schemafrom LoopBack models using the LoopBack automigrate method.

For more information on auto-migration, see Creating a database schema from models for more information.

LoopBack PostgreSQL connector creates the following schema objects for a givenmodel: a table, for example, PRODUCT under the ‘public’ schema within the database.

The auto-migrate method:

  • Defines a primary key for the properties whose id property is true (or a positive number).
  • Creates a column with ‘SERIAL’ type if the generated property of the id property is true.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 options. 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.

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

  1. {
  2. "name": "Customer",
  3. "options": {
  4. "idInjection": false
  5. },
  6. "properties": {
  7. "id": {
  8. "type": "String",
  9. "length": 20,
  10. "id": 1
  11. },
  12. "name": {
  13. "type": "String",
  14. "required": false,
  15. "length": 40
  16. }
  17. }
  18. },
  19. {
  20. "name": "Order",
  21. "options": {
  22. "idInjection": false,
  23. "foreignKeys": {
  24. "fk_order_customerId": {
  25. "name": "fk_order_customerId",
  26. "entity": "Customer",
  27. "entityKey": "id",
  28. "foreignKey": "customerId"
  29. }
  30. }
  31. },
  32. "properties": {
  33. "id": {
  34. "type": "String",
  35. "length": 20,
  36. "id": 1
  37. },
  38. "customerId": {
  39. "type": "String",
  40. "length": 20
  41. },
  42. "description": {
  43. "type": "String",
  44. "required": false,
  45. "length": 40
  46. }
  47. }
  48. }

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
  1. POSTGRESQL_HOST=<HOST> POSTGRESQL_PORT=<PORT> POSTGRESQL_USER=<USER> POSTGRESQL_PASSWORD=<PASSWORD> POSTGRESQL_DATABASE=<DATABASE> CI=true npm test
  • Windows
  1. SET POSTGRESQL_HOST=<HOST> SET POSTGRESQL_PORT=<PORT> SET POSTGRESQL_USER=<USER> SET POSTGRESQL_PASSWORD=<PASSWORD> SET POSTGRESQL_DATABASE=<DATABASE> SET CI=true npm test

Docker

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

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

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

  • Run the test:
  1. npm test

Tags: connectors