Skip to content

Commit

Permalink
Merge pull request #112 from leapfrogtechnology/node-app-pg-ts
Browse files Browse the repository at this point in the history
Make running the example a trivial process
  • Loading branch information
cham11ng authored Mar 22, 2021
2 parents 9ef6488 + 479f5a0 commit 522ef57
Show file tree
Hide file tree
Showing 17 changed files with 1,011 additions and 1,256 deletions.
3 changes: 0 additions & 3 deletions examples/node-app-pg-ts/.babelrc

This file was deleted.

4 changes: 2 additions & 2 deletions examples/node-app-pg-ts/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
From node:10
From node:14-slim
WORKDIR /app/
COPY . .
RUN yarn
CMD yarn sync-db && yarn start
CMD yarn sync-db synchronize && yarn start
64 changes: 64 additions & 0 deletions examples/node-app-pg-ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Node PostgreSQL Example with TypeScript Migrations

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

## Overview

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

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

## Setup

Setup will require [docker](https://docs.docker.com/engine/) and [docker-compose](https://docs.docker.com/compose/gettingstarted/).

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](docker-compose.yml).

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

## Run

Run the docker-compose services in order.

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

## Output

```bash
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 (0s)
example-app | [✓] Migrations - up to date (0.02s)
example-app | [✓] Synchronization - completed (0.13s)
example-app |
example-app | Synchronization complete for 1 / 1 connection(s). (0.25s)
example-app |
example-app | Done in 0.54s.
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 | [ 'sync' ]
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-21T00:00:00.000Z
example-app | Done in 0.11s.
example-app exited with code 0
```
15 changes: 0 additions & 15 deletions examples/node-app-pg-ts/connections.sync-db.json.docker.example

This file was deleted.

8 changes: 4 additions & 4 deletions examples/node-app-pg-ts/connections.sync-db.json.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"connections": [
{
"id": "laudio_test_db",
"client": "postgresql",
"id": "db1",
"client": "pg",
"connection": {
"host": "localhost",
"host": "db",
"port": 5432,
"user": "testdb",
"user": "test",
"database": "testdb",
"password": "secret"
}
Expand Down
7 changes: 4 additions & 3 deletions examples/node-app-pg-ts/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
version: '3.3'
services:
db:
image: 'postgres'
image: 'postgres:13-alpine'
container_name: 'example-pg'
ports:
- '5432:5432'
environment:
DATABASE_URL: ${DATABASE_URL}
POSTGRES_DB: 'mydatabasename'
POSTGRES_DB: 'testdb'
POSTGRES_USER: 'test'
POSTGRES_PASSWORD: 'secret'
app:
container_name: 'example-app'
build: .
3 changes: 1 addition & 2 deletions examples/node-app-pg-ts/knexfile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require('@babel/register');
const { connections } = require('./connections.sync-db.json');

module.exports = {
client: 'postgresql',
client: 'pg',
connection: connections[0],
migrations: {
directory: './src/migrations',
Expand Down
12 changes: 4 additions & 8 deletions examples/node-app-pg-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@
"migrate:list": "../../bin/run-dev.sh migrate-list",
"migrate": "../../bin/run-dev.sh migrate-latest",
"rollback": "../../bin/run-dev.sh migrate-rollback",
"start": " babel-node src/index.js",
"start": "node src/index.js",
"make": "knex --knexfile=knexfile.js migrate:make"
},
"license": "MIT",
"dependencies": {
"@leapfrogtechnology/sync-db": "1.0.0-beta.8",
"knex": "^0.20.3",
"pg": "^7.14.0",
"@leapfrogtechnology/sync-db": "1.0.0-beta.10",
"knex": "^0.95.2",
"pg": "^8.5.1",
"ts-node": "^9.1.1",
"typescript": "^4.2.3"
},
"devDependencies": {
"@babel/core": "^7.7.4",
"@babel/node": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"@babel/register": "^7.7.4",
"eslint": "^6.7.2",
"eslint-config-leapfrog": "^2.0.1"
}
Expand Down
37 changes: 24 additions & 13 deletions examples/node-app-pg-ts/src/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
/**
* Demonstrates use of sync-db to create functions, procedures and views in PostgresSql.
*/
const knex = require('knex');
const { connections } = require('../connections.sync-db.json');
const dbConfig = require('../knexfile').connection.connection;

(async () => {
try {
const db = knex({
client: 'postgresql',
connection: connections.find(({ id }) => id === 'db1')
// PostgreSQL connection only works with a connection string.
// Time wasted = 60 minutes
const db = require('knex')({
client: 'pg',
connection: `postgresql://${dbConfig.user}:${dbConfig.password}@${dbConfig.host}:${dbConfig.port}/${dbConfig.database}`
});

const tables = await db.raw('SELECT * FROM utils.vw_table_names');
const users = await db.raw('SELECT * FROM utils.vw_user_names');
const [{ result: product }] = await db.raw('SELECT utils.product(6, 7) AS result;');
const [{ result: sum }] = await db.raw('SELECT dbo.sum(6, 7) AS result;');
const [{ result: square }] = await db.raw('SELECT dbo.square(6) AS result;');
const [{ result: date }] = await db.raw('EXEC utils.get_date;');
const { rows: tables } = await db.raw('SELECT * FROM utils.vw_table_names');
const { rows: users } = await db.raw('SELECT * FROM utils.vw_user_names');

const {
rows: [{ result: sum }]
} = await db.raw('SELECT public.sum(6, 7) AS result;');
const {
rows: [{ result: square }]
} = await db.raw('SELECT public.square(6) AS result;');

const {
rows: [{ result: product }]
} = await db.raw('SELECT utils.product(6, 7) AS result;');
const {
rows: [{ result: date }]
} = await db.raw('SELECT utils.get_date() AS result;');

console.log(
'\nList of table names in the database:\n',
Expand All @@ -28,8 +39,8 @@ const { connections } = require('../connections.sync-db.json');
);
console.log('\nCalculations:\n', {
'Sum of 6 and 7': sum,
'Product of 6 and 7': product,
'Square of 6': square
'Square of 6': square,
'Product of 6 and 7': product
});
console.log('\nCurrent date time:', date);

Expand Down
8 changes: 4 additions & 4 deletions examples/node-app-pg-ts/src/sql/function/public/sum.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- Calculate square of a number.
--
CREATE FUNCTION public.sum(INTEGER, INTEGER) RETURNS INTEGER
AS 'SELECT $1 + $2;'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
AS 'SELECT $1 + $2;'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
8 changes: 8 additions & 0 deletions examples/node-app-pg-ts/src/sql/function/utils/get_date.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--
-- Procedure that returns current date.
--
CREATE FUNCTION utils.get_date() RETURNS DATE
AS 'SELECT NOW()'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
8 changes: 4 additions & 4 deletions examples/node-app-pg-ts/src/sql/function/utils/product.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- Calculate multiplication of two numbers.
--
CREATE FUNCTION utils.product(INTEGER, INTEGER) RETURNS INTEGER
AS 'SELECT $1 * $2;'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
AS 'SELECT $1 * $2;'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
9 changes: 0 additions & 9 deletions examples/node-app-pg-ts/src/sql/procedure/utils/get_date.sql

This file was deleted.

4 changes: 2 additions & 2 deletions examples/node-app-pg-ts/src/sql/view/utils/vw_table_names.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
--
CREATE VIEW utils.vw_table_names
AS (
SELECT table_name
SELECT table_name AS name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_type = 'BASE TABLE'
AND table_type = 'BASE TABLE'
);
6 changes: 3 additions & 3 deletions examples/node-app-pg-ts/src/sql/view/utils/vw_user_names.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

--
-- View to fetch server login names.
--
CREATE VIEW utils.vw_user_names
AS (
SELECT DISTINCT usename as username
FROM pg_stat_activity WHERE usename IS NOT NULL
SELECT DISTINCT usename AS name
FROM pg_stat_activity
WHERE usename IS NOT NULL
);
2 changes: 1 addition & 1 deletion examples/node-app-pg-ts/sync-db.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sql:
# Create objects in utils schema
- schema/utils.sql
- function/utils/product.sql
- procedure/utils/get_date.sql
- function/utils/get_date.sql
- view/utils/vw_table_names.sql
- view/utils/vw_user_names.sql

Expand Down
Loading

0 comments on commit 522ef57

Please sign in to comment.