Skip to content
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
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ sql:
- **`migration`** `(array)` - Migrations specific configurations.

- **`sourceType`** `(string)` - Type of migration file. Value `defaults` to sql. - **example**: javascript, typescript.
- TypeScript migrations are compiled on the fly with `ts-node`. With TypeScript 6+ your project needs a `tsconfig.json` (e.g. `module: nodenext`, an explicit `rootDir`) - see the [TypeScript samples](#examples).
- TypeScript migrations are compiled on the fly with `ts-node`. With TypeScript 6+ your project needs a `tsconfig.json` (e.g. `module: nodenext`, an explicit `rootDir`) - see the [TypeScript samples](#examples), which include a working `tsconfig.json`.
- **`tableName`** `(string)` - Custom name for table to store migrations meta data.

- **`connectionResolver`** (`string`) - Connection resolver file name optional if connections are resolved using `connections.sync-db.json`.
Expand Down Expand Up @@ -381,11 +381,8 @@ await db.transaction(async trx => {

## Examples

1. [Node MSSQL JavaScript Sample](examples/node-app-mssql)
2. [Node MSSQL TypeScript Sample](examples/node-app-mssql-ts)
3. [Node MSSQL Programmatic Usage Sample](examples/node-mssql-programmatic-use)
4. [Node PostgreSQL JavaScript Sample](examples/node-app-pg)
5. [Node PostgreSQL TypeScript Sample](examples/node-app-pg-ts)
1. [Node MSSQL TypeScript Sample](examples/node-app-mssql-ts) - includes [programmatic usage](examples/node-app-mssql-ts/src/sync-db.ts)
2. [Node PostgreSQL TypeScript Sample](examples/node-app-pg-ts) - includes [programmatic usage](examples/node-app-pg-ts/src/sync-db.ts)

## Changelog

Expand Down
2 changes: 1 addition & 1 deletion examples/node-app-mssql-ts/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18-slim
FROM node:24-slim

WORKDIR /app/

Expand Down
8 changes: 8 additions & 0 deletions examples/node-app-mssql-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ $ docker-compose up -d mssql
$ docker-compose up app
```

## Programmatic usage

`sync-db` can also be used from code instead of the CLI. See [src/sync-db.ts](src/sync-db.ts), which loads `sync-db.yml` and the connections and runs `synchronize`:

```bash
$ yarn synchronize:programmatic
```

## Output

```bash
Expand Down
8 changes: 6 additions & 2 deletions examples/node-app-mssql-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "node-app-mssql",
"name": "node-app-mssql-ts",
"description": "Sample project using sync-db",
"scripts": {
"synchronize": "../../bin/run-dev.sh synchronize",
"synchronize:programmatic": "ts-node src/sync-db.ts",
"clean": "../../bin/run-dev.sh prune",
"migrate:list": "../../bin/run-dev.sh migrate-list",
"start": "node src/index.js",
Expand All @@ -16,5 +17,8 @@
"ts-node": "^10.9.2",
"typescript": "^6.0.3"
},
"license": "MIT"
"license": "MIT",
"devDependencies": {
"@types/node": "^22"
}
}
24 changes: 24 additions & 0 deletions examples/node-app-mssql-ts/src/sync-db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Demonstrates using sync-db programmatically (instead of the CLI) to synchronize the database.
*/
import { synchronize, loadConfig, resolveConnections } from '@leapfrogtechnology/sync-db';

(async () => {
try {
const config = await loadConfig(); // Load sync-db.yml
const connections = await resolveConnections(config); // Load connections.sync-db.json

// Invoke the synchronize operation.
const results = await synchronize(config, connections);
const failed = results.filter(({ success }) => !success);

for (const { connectionId, success, timeElapsed } of results) {
console.log(`${success ? '✓' : '✖'} ${connectionId} (${timeElapsed}s)`);
}

process.exit(failed.length === 0 ? 0 : -1);
} catch (error) {
console.error(error);
process.exit(-1);
}
})();
3 changes: 2 additions & 1 deletion examples/node-app-mssql-ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"module": "nodenext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
"skipLibCheck": true,
"types": ["node"]
},
"include": ["src/**/*"]
}
12 changes: 12 additions & 0 deletions examples/node-app-mssql-ts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,13 @@
dependencies:
undici-types "~8.9.0"

"@types/node@^22":
version "22.20.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.20.5.tgz#afa7bde6da81b8b83ea4e2f020487e9b148c11fd"
integrity sha512-U2+DNr+wSjpsTS/wZGYHq7GcwfuSmKiKvoPvK22zwTlRhU91yOniN4qRR5KhIjvif7ysw/dz/hKmfDH0Ris4aA==
dependencies:
undici-types "~6.21.0"

"@types/readable-stream@^4.0.0":
version "4.0.25"
resolved "https://registry.yarnpkg.com/@types/readable-stream/-/readable-stream-4.0.25.tgz#2987b68f413ca24ad151f644cf6545277bd87338"
Expand Down Expand Up @@ -1349,6 +1356,11 @@ typescript@^6.0.3:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-6.0.3.tgz#90251dc007916e972786cb94d74d15b185577d21"
integrity sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw==

undici-types@~6.21.0:
version "6.21.0"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.21.0.tgz#691d00af3909be93a7faa13be61b3a5b50ef12cb"
integrity sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==

undici-types@~8.9.0:
version "8.9.0"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-8.9.0.tgz#e240d97c8b5d85e5347ce73d25865c7906c1ec9f"
Expand Down
2 changes: 0 additions & 2 deletions examples/node-app-mssql/.gitignore

This file was deleted.

9 changes: 0 additions & 9 deletions examples/node-app-mssql/Dockerfile

This file was deleted.

105 changes: 0 additions & 105 deletions examples/node-app-mssql/README.md

This file was deleted.

15 changes: 0 additions & 15 deletions examples/node-app-mssql/connections.sync-db.json.example

This file was deleted.

17 changes: 0 additions & 17 deletions examples/node-app-mssql/docker-compose.yml

This file was deleted.

18 changes: 0 additions & 18 deletions examples/node-app-mssql/package.json

This file was deleted.

42 changes: 0 additions & 42 deletions examples/node-app-mssql/src/index.js

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

8 changes: 0 additions & 8 deletions examples/node-app-mssql/src/sql/function/dbo/square.sql

This file was deleted.

8 changes: 0 additions & 8 deletions examples/node-app-mssql/src/sql/function/dbo/sum.sql

This file was deleted.

8 changes: 0 additions & 8 deletions examples/node-app-mssql/src/sql/function/utils/product.sql

This file was deleted.

Loading