Skip to content

Commit

Permalink
Fixes in progamatic example
Browse files Browse the repository at this point in the history
  • Loading branch information
samirsilwal committed Mar 24, 2021
1 parent 8ae0cbc commit 35856b7
Show file tree
Hide file tree
Showing 10 changed files with 679 additions and 253 deletions.
3 changes: 0 additions & 3 deletions examples/node-mssql-programmatic-use/.babelrc

This file was deleted.

4 changes: 2 additions & 2 deletions examples/node-mssql-programmatic-use/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 sync-db && yarn start
CMD yarn sync-db synchronize && yarn start
125 changes: 55 additions & 70 deletions examples/node-mssql-programmatic-use/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,81 +16,66 @@ Configure database connection(s) in the `connections.sync-db.json`.
$ cp connections.sync-db.json.example connections.sync-db.json
```

## Running
## Run

Run the command below to perform a migration:

```
$ yarn migrate
```

Run `sync-db` to synchronize all database objects (views, functions, procedures, schemas, etc) in the configured database(s).

```
$ yarn sync-db
```

Run the sample node app.

```
$ yarn start
```

**Output**

```
List of table names in the database:
[ 'knex_migrations', 'knex_migrations_lock', 'users', 'tasks' ]
List of user names in the database:
[
'sa',
'public',
'sysadmin',
'securityadmin',
'serveradmin',
'setupadmin',
'processadmin',
'diskadmin',
'dbcreator',
'bulkadmin',
'##MS_SQLResourceSigningCertificate##',
'##MS_SQLReplicationSigningCertificate##',
'##MS_SQLAuthenticatorCertificate##',
'##MS_PolicySigningCertificate##',
'##MS_SmoExtendedSigningCertificate##',
'##MS_PolicyEventProcessingLogin##',
'##MS_PolicyTsqlExecutionLogin##',
'##MS_AgentSigningCertificate##',
'BUILTIN\\Administrators',
'NT AUTHORITY\\NETWORK SERVICE',
'NT AUTHORITY\\SYSTEM'
]
Calculations:
{ 'Sum of 6 and 7': 13, 'Product of 6 and 7': 42, 'Square of 6': 36 }
Current date time: 2019-11-28T10:09:36.507Z
```

## Docker

Set `DB_PASSWORD` (password for `SA` user) in environment. e.g.
Run the docker-compose services in order.

```bash
$ export DB_PASSWORD=Password@123
```

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`.

```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 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 | ▸ db1
example-app | [✓] Synchronization - started
example-app | [✓] Synchronization - pruned (0.04s)
example-app | [✓] Migrations - up to date (0.07s)
example-app | [✓] Synchronization - completed (0.14s)
example-app |
example-app | Synchronization complete for 1 / 1 connection(s). (1.01s)
example-app |
example-app | Done in 1.64s.
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', 'tasks' ]
example-app |
example-app | List of user names in the database:
example-app | [
example-app | 'sa',
example-app | 'public',
example-app | 'sysadmin',
example-app | 'securityadmin',
example-app | 'serveradmin',
example-app | 'setupadmin',
example-app | 'processadmin',
example-app | 'diskadmin',
example-app | 'dbcreator',
example-app | 'bulkadmin',
example-app | '##MS_SQLResourceSigningCertificate##',
example-app | '##MS_SQLReplicationSigningCertificate##',
example-app | '##MS_SQLAuthenticatorCertificate##',
example-app | '##MS_PolicySigningCertificate##',
example-app | '##MS_SmoExtendedSigningCertificate##',
example-app | '##MS_PolicyEventProcessingLogin##',
example-app | '##MS_PolicyTsqlExecutionLogin##',
example-app | '##MS_AgentSigningCertificate##',
example-app | 'BUILTIN\\Administrators',
example-app | 'NT AUTHORITY\\SYSTEM',
example-app | 'NT AUTHORITY\\NETWORK SERVICE'
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-24T11:36:39.230Z
example-app | Done in 0.95s.
example-app exited with code 0
```

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"connections": [
{
"id": "db1",
"host": "localhost",
"port": 1433,
"user": "db1user",
"database": "db1",
"password": "password",
"client": "mssql"
"client": "mssql",
"connection": {
"host": "localhost",
"port": 1433,
"user": "sa",
"database": "tempdb",
"password": "Password@123"
}
}
]
}
3 changes: 2 additions & 1 deletion examples/node-mssql-programmatic-use/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ services:
- '1433:1433'
environment:
ACCEPT_EULA: 'Y'
SA_PASSWORD: ${DB_PASSWORD}
SA_PASSWORD: 'Secret@123'

app:
network_mode: host
container_name: 'example-app'
build: .
1 change: 0 additions & 1 deletion examples/node-mssql-programmatic-use/knexfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require('@babel/register');
const { connections } = require('./connections.sync-db.json');

module.exports = {
Expand Down
13 changes: 5 additions & 8 deletions examples/node-mssql-programmatic-use/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@
"migrate:list": "../../bin/run-dev.sh migrate-list",
"migrate": "../../bin/run-dev.sh migrate-latest",
"rollback": "../../bin/run-dev.sh migrate-rollback",
"start": "yarn lint && babel-node src/index.js",
"start": "node src/index.js",
"make": "knex --knexfile=knexfile.js migrate:make"
},
"dependencies": {
"@leapfrogtechnology/sync-db": "^1.0.0-alpha.8",
"knex": "^0.19.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
6 changes: 3 additions & 3 deletions examples/node-mssql-programmatic-use/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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 === 'db1')
connection: connections.find(({ id }) => id === 'db1').connection
});

const tables = await db.raw('SELECT * FROM utils.vw_table_names');
Expand All @@ -30,8 +30,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 35856b7

Please sign in to comment.