Skip to content

Commit

Permalink
Some fixes in example for ts with node
Browse files Browse the repository at this point in the history
  • Loading branch information
samirsilwal committed Mar 22, 2021
1 parent 522ef57 commit 4b2fe5e
Show file tree
Hide file tree
Showing 9 changed files with 1,176 additions and 1,679 deletions.
3 changes: 0 additions & 3 deletions examples/node-app-mssql-ts/.babelrc

This file was deleted.

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

WORKDIR /app/

COPY . .

RUN yarn

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

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

## 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
```
## Overview

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

**Synchronization**
- 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.

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

```
$ yarn synchronize
```

```
Synchronizing...
▸ testdb1
[✓] Synchronization - started
[✓] Synchronization - pruned (0.02s)
[✓] Migrations - 2 run (0.03s)
- 20191124213750_create_users_table
- 20191124213800_create_tasks_table
[✓] Synchronization - completed (0.15s)
▸ testdb2
[✓] Synchronization - started
[✓] Synchronization - pruned (0.01s)
[✓] Migrations - 2 run (0.02s)
- 20191124213750_create_users_table
- 20191124213800_create_tasks_table
[✓] Synchronization - completed (0.13s)
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-ts/connections.sync-db.json.docker

This file was deleted.

10 changes: 5 additions & 5 deletions examples/node-app-mssql-ts/connections.sync-db.json.example
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"connections": [
{
"id": "testdb1",
"id": "db1",
"client": "mssql",
"connection": {
"host": "localhost",
"host": "db",
"port": 1433,
"user": "db1user",
"database": "testdb1",
"password": "password"
"user": "test",
"database": "tempdb",
"password": "Secret@123"
}
}
]
Expand Down
5 changes: 3 additions & 2 deletions examples/node-app-mssql-ts/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ version: '3.3'

services:
mssql:
container_name: 'example-mssql'
container_name: 'example-mssql-ts'
image: 'mcr.microsoft.com/mssql/server:2017-GA-ubuntu'
ports:
- '1433:1433'
environment:
ACCEPT_EULA: 'Y'
SA_PASSWORD: ${DB_PASSWORD}
SA_PASSWORD: 'Password@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-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@
"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-beta.8",
"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",
"ts-node": "^9.1.1",
"typescript": "^4.2.3"
},
"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
6 changes: 3 additions & 3 deletions examples/node-app-mssql-ts/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 All @@ -29,8 +29,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
Loading

0 comments on commit 4b2fe5e

Please sign in to comment.