LoopBack 4 TodoList Application Tutorial - Running on Relational DatabasesIf you are running this example using a relational database, there are extrasteps in order to set up constraints in the database. We’re using PostgreSQL forillustration, but it would work for other relational databases in a similar way.

Create a new datasource

We’re going to update the DataSource db to connect to the PostgreSQL databaseinstead of the in-memory database.

First, remove the existing db, run:

  1. rm src/datasources/db.datasource.*

Then, create the datasource with the same name with the lb4 datasource commandand select PostgreSQL as the connector.

  1. $ lb4 datasource
  2. ? Datasource name: db
  3. ? Select the connector for db:
  4. Redis key-value connector (supported by StrongLoop)
  5. MongoDB (supported by StrongLoop)
  6. MySQL (supported by StrongLoop)
  7. PostgreSQL (supported by StrongLoop)
  8. Oracle (supported by StrongLoop)
  9. Microsoft SQL (supported by StrongLoop)
  10. REST services (supported by StrongLoop)
  11. ...

Specify the foreign key constraint in the models

Based on the model relations we’ve specified in the 3 models, the expecteddatabase tables look like below:database tables

Note:

There is ongoing work on supporting strong relations with referential integrity. For details, please see epic #2231.

Specify the foreign key constraints in Todo model

We will use the foreignKeys attribute to determine the constraints in thedatabase table.

In src/models/todo.model.ts, add the settings options in the @modeldecorator.

  1. @model({
  2. settings: {
  3. foreignKeys: {
  4. fk_todo_todoListId: {
  5. name: 'fk_todo_todoListId',
  6. entity: 'TodoList',
  7. entityKey: 'id',
  8. foreignKey: 'todolistid',
  9. },
  10. },
  11. },
  12. })
  13. export class Todo extends Entity {
  14. //etc.
  15. }

Check out the details forauto-migrate and auto-updatefor PostgreSQL connector. For other connectors, go to the correspondingconnector from theConnectors Referencesection.

Specify the foreign key constraints in TodoListImage model

Similar to the Todo model, we will specify the constraints in the settings.

  1. @model({
  2. settings: {
  3. foreignKeys: {
  4. fk_todoListImage_todoListId: {
  5. name: 'fk_todoListImage_todoListId',
  6. entity: 'TodoList',
  7. entityKey: 'id',
  8. foreignKey: 'todolistid',
  9. },
  10. },
  11. },
  12. })
  13. export class TodoListImage extends Entity {
  14. //etc.
  15. }

Database migration using npm run migrate command

The order of table creation is important. We are going to migrate TodoListmodel before the Todo and TodoListImage models.

In src/migrate.ts, modify this line:

  1. await app.migrateSchema({existingSchema});

to:

  1. await app.migrateSchema({
  2. existingSchema,
  3. // The order of table creation is important.
  4. // A referenced table must exist before creating a
  5. // foreign key constraint.
  6. // For PostgreSQL connector, it does not create tables in the
  7. // right order. Therefore, this change is needed.
  8. models: ['TodoList', 'Todo', 'TodoListImage'],
  9. });

Run the following commands:

  1. $ npm run build
  2. $ npm run migrate

The todolist, todo and todolistimage tables are created.

For details, see the Database migrations documentationpage.