A brief tutorial on using the LoopBack PostgreSQL connector.Note: This page was generated from the loopback-example-database/README.md.

loopback-example-database

A tutorial for basic database related features.

Overview

Topics covered

  • Data sources
    • Creating
    • Configuring
  • Models
    • Creating
  • Automigration
  • Discovery

Database specific tutorials

Database specific tutorials are on separate branches. The master branch containsthe tutorial for MongoDB.

BranchConnector
masterMongoDB
mssqlMicrosoft SQL Server
mysqlMySQL
oracleOracle
postgresqlPostgreSQL

For example, to view the PostgreSQL example:

  1. git clone https://github.com/strongloop/loopback-example-database
  2. cd loopback-example-database
  3. git checkout postgresql

Prerequisites

Before starting this tutorial, make sure you have the following installed:

  • Node
  • LoopBack CLI tools; see lb

Running the example

  1. git clone https://github.com/strongloop/loopback-example-database
  2. cd loopback-example-database
  3. npm install
  4. npm start

Tutorial - PostgreSQL

1. Create a new LoopBack app

App info

  • Name: loopback-example-database
  • Dir to contain the project: loopback-example-database
  1. lb app loopback-example-database
  2. ... # follow the prompts

2. Install the LoopBack PostgreSQL connector

  1. cd loopback-example-database
  2. npm install --save loopback-connector-postgresql

3. Create a data source

Data source info

  • Data source name: accountDS
  • Select the connector for accountDS: PostgreSQL
  1. lb datasource accountDS
  2. ... # follow the prompts

This creates a new data source named accountDS that uses the PostgreSQLconnector.

4. Configure the data source

For the purposes of this example, we will use a preconfigured StrongLoopPostgreSQL server. Edit server/datasources.json to set the PostgreSQL configs:

  1. {
  2. ...
  3. "accountDS": {
  4. "name": "accountDS",
  5. "connector": "postgresql",
  6. "host": "demo.strongloop.com",
  7. "port": 5432,
  8. "database": "demo",
  9. "username": "demo",
  10. "password": "L00pBack"
  11. }
  12. }

Feel free to use your own local PostgreSQL instance. Simply change the configsabove to match your own.

5. Create a new model

Model Info

  • Model name: Account
  • Attach Account to: accountDS (postgresql)
  • Base class: PersistedModel
  • Expose via REST: Yes
  • Custom plural form: Leave blank
  • Properties:
    • email
      • String
      • Not required
    • createdAt
      • Date
      • Not required
    • lastModifiedAt
      • Date
      • Not required
  1. lb model Account
  2. ... # follow the prompts

6. Create the collection with sample data - Automigration

With the account model configured, we can generate the correspondingPostgreSQL table using the info from the Account metadata in common/models/account.jsonvia auto-migration.

Start by creating a dir to store general-purpose scripts:

  1. mkdir bin

Inside that dir, create a script named automigrate.js.To create the Account collection and create two sample accounts, run:

  1. node bin/automigrate.js

WARNING

The automigrate function creates a new collection if it doesn’t exist. Ifthe collection already exists, it will be destroyed and it’s data will bedeleted. If you want to keep this data, use autoupdate instead.

You should see:

  1. Created: { email: 'john.doe@ibm.com',
  2. createdAt: Fri Oct 23 2015 17:14:03 GMT-0700 (PDT),
  3. lastModifiedAt: Fri Oct 23 2015 17:14:03 GMT-0700 (PDT),
  4. id: 1 }
  5. Created: { email: 'jane.doe@ibm.com',
  6. createdAt: Fri Oct 23 2015 17:14:03 GMT-0700 (PDT),
  7. lastModifiedAt: Fri Oct 23 2015 17:14:03 GMT-0700 (PDT),
  8. id: 2 }

7. View data using the explorer

Projects scaffolded via slc loopback come with loopback-component-explorerpreconfigured. From the project root, start the server:

  1. node .

Then to view the existing account data, browse to localhost:3000/explorer andclick:

  • GET /Accounts
  • Try it out!You should see:
  1. [
  2. {
  3. "email": "john.doe@ibm.com",
  4. "createdAt": "2015-10-24T00:14:03.567Z",
  5. "lastModifiedAt": "2015-10-24T00:14:03.567Z",
  6. "id": 1
  7. },
  8. {
  9. "email": "jane.doe@ibm.com",
  10. "createdAt": "2015-10-24T00:14:03.567Z",
  11. "lastModifiedAt": "2015-10-24T00:14:03.567Z",
  12. "id": 2
  13. }
  14. ]

Try out some of the other endpoints to get a feel for how explorer works.

8. Add a script to perform discover the database schema

Discoveryis the process of reverse engineering a LoopBack model from an existingdatabase schema.

Create a script name discover-schema.js. Then run this script todiscover the schema from the existing Account table:

  1. node bin/discover-schema

You should see:

  1. {
  2. "name": "Account",
  3. "options": {
  4. "idInjection": false,
  5. "postgresql": {
  6. "schema": "public",
  7. "table": "account"
  8. }
  9. },
  10. "properties": {
  11. "email": {
  12. "type": "String",
  13. "required": false,
  14. "length": 1024,
  15. "precision": null,
  16. "scale": null,
  17. "postgresql": {
  18. "columnName": "email",
  19. "dataType": "character varying",
  20. "dataLength": 1024,
  21. "dataPrecision": null,
  22. "dataScale": null,
  23. "nullable": "YES"
  24. }
  25. },
  26. "createdat": {
  27. "type": "String",
  28. "required": false,
  29. "length": null,
  30. "precision": null,
  31. "scale": null,
  32. "postgresql": {
  33. "columnName": "createdat",
  34. "dataType": "timestamp with time zone",
  35. "dataLength": null,
  36. "dataPrecision": null,
  37. "dataScale": null,
  38. "nullable": "YES"
  39. }
  40. },
  41. "lastmodifiedat": {
  42. "type": "String",
  43. "required": false,
  44. "length": null,
  45. "precision": null,
  46. "scale": null,
  47. "postgresql": {
  48. "columnName": "lastmodifiedat",
  49. "dataType": "timestamp with time zone",
  50. "dataLength": null,
  51. "dataPrecision": null,
  52. "dataScale": null,
  53. "nullable": "YES"
  54. }
  55. },
  56. "id": {
  57. "type": "Number",
  58. "required": true,
  59. "length": null,
  60. "precision": 32,
  61. "scale": 0,
  62. "id": 1,
  63. "postgresql": {
  64. "columnName": "id",
  65. "dataType": "integer",
  66. "dataLength": null,
  67. "dataPrecision": 32,
  68. "dataScale": 0,
  69. "nullable": "NO"
  70. }
  71. }
  72. }
  73. }

9. Add a script to discover and build models

When retrieving the scheme is not enough, you can discover and build LoopBackmodels in one step.

Create a script named discover-and-build-models.js.Then run:

  1. node bin/discover-and-build-models

You should see:

  1. [ { email: 'john.doe@ibm.com',
  2. createdat: Fri Oct 23 2015 17:39:50 GMT-0700 (PDT),
  3. lastmodifiedat: Fri Oct 23 2015 17:39:50 GMT-0700 (PDT),
  4. id: 1 },
  5. { email: 'jane.doe@ibm.com',
  6. createdat: Fri Oct 23 2015 17:39:50 GMT-0700 (PDT),
  7. lastmodifiedat: Fri Oct 23 2015 17:39:50 GMT-0700 (PDT),
  8. id: 2 } ]

See the official docsfor more info.


More LoopBack examples

Tags: example_app