-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from leapfrogtechnology/node-app-pg-ts
Make running the example a trivial process
- Loading branch information
Showing
17 changed files
with
1,011 additions
and
1,256 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
examples/node-app-pg-ts/connections.sync-db.json.docker.example
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.