Skip to content

Files

Latest commit

5243607 · May 2, 2023

History

History
This branch is 110 commits behind leapfrogtechnology/sync-db:master.

node-app-pg

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Dec 30, 2022
Dec 19, 2019
Dec 23, 2019
May 2, 2023
Mar 23, 2021
Mar 23, 2021
Mar 23, 2021
Mar 23, 2021
May 1, 2023
Dec 30, 2022
May 1, 2023

Node PostgreSQL Example with JavaScript Migrations

Sample project for sync-db using PostgreSQL and migrations written in JavaScript.

Overview

This sync-db example does a few things in order:

  • Prunes database objects inside of src/sql from the specified database connection(s) if it exists.
  • Executes knex database migrations inside of src/migrations directory written in JavaScript.
  • Creates database objects inside of src/sql directory using sync-db. These database objects are created in the order specified in sync-db.yml.
  • Executes a node script to check if the synchronized objects can be executed and prints the result.

Setup

Setup will require docker and docker-compose.

Configure database connection(s) in connections.sync-db.json by copying connections.sync-db.json.example. For ease of use, the example app will work without making any changes to connections.sync-db.json. Throwaway database credentials have been set in docker-compose.yml.

$ cp connections.sync-db.json.example connections.sync-db.json

Run

Run the docker-compose services in order.

$ docker-compose up -d db
$ docker-compose up app

Output

example-app | yarn run v1.22.5
example-app | $ /app/node_modules/.bin/sync-db synchronize
example-app | Synchronizing...
example-app |
example-app |  ▸ db1
example-app |    [✓] Synchronization - started
example-app |    [✓] Synchronization - pruned (0.01s)
example-app |    [✓] Migrations - up to date (0.04s)
example-app |    [✓] Synchronization - completed (0.22s)
example-app |
example-app | Synchronization complete for 1 / 1 connection(s). (0.43s)
example-app |
example-app | Done in 1.00s.
example-app | yarn run v1.22.5
example-app | $ node src/index.js
example-app |
example-app | List of table names in the database:
example-app |  [ 'knex_migrations', 'knex_migrations_lock', 'users', 'todos' ]
example-app |
example-app | List of user names in the database:
example-app |  [ 'test' ]
example-app |
example-app | Calculations:
example-app |  { 'Sum of 6 and 7': 13, 'Square of 6': 36, 'Product of 6 and 7': 42 }
example-app |
example-app | Current date time: 2021-03-23T00:00:00.000Z
example-app | Done in 0.41s.