Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes in mssql & pg node examples with JS migrations #116

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions examples/node-app-mssql/.babelrc

This file was deleted.

4 changes: 2 additions & 2 deletions examples/node-app-mssql/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM node:10
FROM node:14-slim

WORKDIR /app/

COPY . .

RUN yarn

CMD yarn synchronize && yarn start
CMD yarn sync-db synchronize && yarn start
195 changes: 87 additions & 108 deletions examples/node-app-mssql/README.md
Original file line number Diff line number Diff line change
@@ -1,126 +1,105 @@
# Node MSSQL Example (JavaScript)
# Node MSSQL Example with JavaScript Migrations

Sample project for Microsoft SQL Server.
Sample project for sync-db using MSSQL and migrations written in JavaScript.

## Setup

Install dependencies.

```bash
$ yarn
```

Configure database connection(s) in the `connections.sync-db.json`.

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

## Running

**Synchronization**

Run `synchronize` to run migrations and synchronize all database objects (views, functions, procedures, schemas, etc) in the configured database(s).

```
$ yarn synchronize
```

```
Synchronizing...
## Overview

▸ testdb1
[✓] Synchronization - started
[✓] Synchronization - pruned (0.02s)
[✓] Migrations - 2 run (0.03s)
- 20191124213750_create_users_table
- 20191124213800_create_tasks_table
[✓] Synchronization - completed (0.15s)
This `sync-db` example does a few things in order:

▸ testdb2
[✓] Synchronization - started
[✓] Synchronization - pruned (0.01s)
[✓] Migrations - 2 run (0.02s)
- 20191124213750_create_users_table
- 20191124213800_create_tasks_table
[✓] Synchronization - completed (0.13s)
- 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 JavaScript.
- 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.

Synchronization complete for 2 / 2 connection(s). (0.51s)
```

**Running Sample App**

Run the sample node app.

```
$ yarn start
```

**Output**

```
List of users:
[ 'user1@example.com',
'user2@example.com',
'user3@example.com',
'user4@example.com' ]

List of completed tasks:
[ { id: 21,
user_id: 1,
title: 'Task 1',
description: 'This is task 1.',
is_complete: true,
created_at: null,
updated_at: null },
{ id: 27,
user_id: 2,
title: 'Task 7',
description: 'This is task 7.',
is_complete: true,
created_at: null,
updated_at: null },
{ id: 30,
user_id: 2,
title: 'Task 10',
description: 'This is task 10.',
is_complete: true,
created_at: null,
updated_at: null },
{ id: 35,
user_id: 3,
title: 'Task 15',
description: 'This is task 15.',
is_complete: true,
created_at: null,
updated_at: null } ]

Calculations:
{ 'Sum of 6 and 7': 13,
'Product of 6 and 7': 42,
'Square of 6': 36 }

Current date time: 2020-04-25T19:02:38.320Z
```
## Setup

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

Set `DB_PASSWORD` (password for `SA` user) in environment. e.g.
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
$ export DB_PASSWORD=Password@123
$ cp connections.sync-db.json.example connections.sync-db.json
```

Configure database connection(s) in the `connections.sync-db.json`. Use same `password` as `DB_PASSWORD`
Note: `host` has to be the service name of docker container for `mssql`.
## Run

Run the docker-compose services in order.

```bash
$ cp connections.sync-db.json.docker connections.sync-db.json
$ docker-compose up -d db
$ docker-compose up app
```

Then run (in order).
## Output

```bash
$ docker-compose up -d mssql
$ docker-compose up app
example-app | yarn run v1.22.5
example-app | $ /app/node_modules/.bin/sync-db synchronize
example-app | Synchronizing...
example-app |
example-app | ▸ testdb1
example-app | [✓] Synchronization - started
example-app | [✓] Synchronization - pruned (0.05s)
example-app | [✓] Migrations - up to date (0.06s)
example-app | [✓] Synchronization - completed (0.23s)
example-app |
example-app | Synchronization complete for 1 / 1 connection(s). (1.07s)
example-app |
example-app | Done in 1.99s.
example-app | yarn run v1.22.5
example-app | $ node src/index.js
example-app |
example-app | List of users:
example-app | [
example-app | 'user1@example.com',
example-app | 'user2@example.com',
example-app | 'user3@example.com',
example-app | 'user4@example.com'
example-app | ]
example-app |
example-app | List of completed tasks:
example-app | [
example-app | {
example-app | id: 21,
example-app | title: 'Task 1',
example-app | user_id: 1,
example-app | description: 'This is task 1.',
example-app | is_complete: true,
example-app | created_at: 2021-03-22T09:06:27.906Z,
example-app | updated_at: 2021-03-22T09:06:27.906Z
example-app | },
example-app | {
example-app | id: 27,
example-app | title: 'Task 7',
example-app | user_id: 2,
example-app | description: 'This is task 7.',
example-app | is_complete: true,
example-app | created_at: 2021-03-22T09:06:27.906Z,
example-app | updated_at: 2021-03-22T09:06:27.906Z
example-app | },
example-app | {
example-app | id: 30,
example-app | title: 'Task 10',
example-app | user_id: 2,
example-app | description: 'This is task 10.',
example-app | is_complete: true,
example-app | created_at: 2021-03-22T09:06:27.906Z,
example-app | updated_at: 2021-03-22T09:06:27.906Z
example-app | },
example-app | {
example-app | id: 35,
example-app | title: 'Task 15',
example-app | user_id: 3,
example-app | description: 'This is task 15.',
example-app | is_complete: true,
example-app | created_at: 2021-03-22T09:06:27.906Z,
example-app | updated_at: 2021-03-22T09:06:27.906Z
example-app | }
example-app | ]
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-22T09:06:28.930Z
example-app exited with code 0
```
15 changes: 0 additions & 15 deletions examples/node-app-mssql/connections.sync-db.json.docker

This file was deleted.

6 changes: 3 additions & 3 deletions examples/node-app-mssql/connections.sync-db.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"connection": {
"host": "localhost",
"port": 1433,
"user": "db1user",
"database": "testdb1",
"password": "password"
"user": "sa",
"database": "tempdb",
"password": "Secret@123"
}
}
]
Expand Down
3 changes: 2 additions & 1 deletion examples/node-app-mssql/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ services:
- '1433:1433'
environment:
ACCEPT_EULA: 'Y'
SA_PASSWORD: ${DB_PASSWORD}
SA_PASSWORD: 'Secret@123'

app:
network_mode: host
container_name: 'example-app'
environment:
DEBUG: 'sync-db*'
Expand Down
13 changes: 5 additions & 8 deletions examples/node-app-mssql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@
"synchronize": "../../bin/run-dev.sh synchronize",
"clean": "../../bin/run-dev.sh prune",
"migrate:list": "../../bin/run-dev.sh migrate-list",
"start": "yarn lint && babel-node src/index.js",
"start": "node src/index.js",
"migrate": "../../bin/run-dev.sh migrate-latest",
"rollback": "../../bin/run-dev.sh migrate-rollback"
},
"dependencies": {
"@leapfrogtechnology/sync-db": "^1.0.0-experimental.9",
"knex": "^0.20.2",
"mssql": "^5.1.0"
"@leapfrogtechnology/sync-db": "^1.0.0-beta.10",
"knex": "^0.95.2",
"mssql": "^6.3.1",
"tedious": "^11.0.6"
},
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.7.4",
"@babel/node": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"@babel/register": "^7.7.4",
"eslint": "^6.2.2",
"eslint-config-leapfrog": "^2.0.1"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/node-app-mssql/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { connections } = require('../connections.sync-db.json');
// Getting knex instance of mssql database with id db1.
const db = knex({
client: 'mssql',
connection: connections.find(({ id }) => id === 'testdb1')
connection: connections.find(({ id }) => id === 'testdb1').connection
});

const tasks = await db.raw('SELECT * FROM utils.vw_tasks');
Expand Down
Loading