@chidchanun/bcp 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -20
- package/docs/api-manifest.json +2 -2
- package/docs/api-reference.md +21 -2
- package/docs/database-migrations.md +79 -19
- package/docs/database.md +197 -28
- package/docs/docs-web-manifest.json +4 -3
- package/docs/platform-manifest.json +14 -4
- package/docs/releases/0.2.3.md +77 -0
- package/package.json +1 -1
- package/packages/cli/src/database-migrations.ts +91 -21
- package/packages/client/src/database-mysql.ts +291 -0
- package/packages/client/src/database-postgresql.ts +355 -0
- package/packages/client/src/database-sqlite.ts +445 -0
- package/packages/client/src/database.mjs +709 -57
- package/packages/client/src/database.ts +347 -182
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 1,
|
|
3
3
|
"framework": "bcp",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.3",
|
|
5
5
|
"releaseState": "unreleased",
|
|
6
|
-
"baseline": "
|
|
6
|
+
"baseline": "database-platform-v2",
|
|
7
7
|
"runtime": {
|
|
8
8
|
"node": ">=24.11.0",
|
|
9
9
|
"react": "19",
|
|
@@ -46,6 +46,11 @@
|
|
|
46
46
|
"middlewareV2": true,
|
|
47
47
|
"jwtCookieSessions": true,
|
|
48
48
|
"databaseMigrations": true,
|
|
49
|
+
"databaseAdapterContract": true,
|
|
50
|
+
"databasePostgresql": true,
|
|
51
|
+
"databaseSqlite": true,
|
|
52
|
+
"databaseLifecycleV2": true,
|
|
53
|
+
"databaseMigrationProviderConsistency": true,
|
|
49
54
|
"validation": true,
|
|
50
55
|
"structuredErrors": true,
|
|
51
56
|
"logging": true,
|
|
@@ -61,6 +66,11 @@
|
|
|
61
66
|
"configurationDiagnostics": true,
|
|
62
67
|
"configCheckCli": true
|
|
63
68
|
},
|
|
69
|
+
"databaseProviders": [
|
|
70
|
+
"mysql",
|
|
71
|
+
"postgresql",
|
|
72
|
+
"sqlite"
|
|
73
|
+
],
|
|
64
74
|
"storageProviders": [
|
|
65
75
|
"local",
|
|
66
76
|
"amazon-s3",
|
|
@@ -68,7 +78,7 @@
|
|
|
68
78
|
"s3-compatible"
|
|
69
79
|
],
|
|
70
80
|
"compatibility": {
|
|
71
|
-
"previousBaseline": "0.2.
|
|
81
|
+
"previousBaseline": "0.2.2",
|
|
72
82
|
"intentionalBreakingChangesFromPreviousBaseline": false,
|
|
73
83
|
"migrationGuide": "migration-0.2.md"
|
|
74
84
|
},
|
|
@@ -81,6 +91,6 @@
|
|
|
81
91
|
"apiReference": "api-reference.md",
|
|
82
92
|
"environmentValidation": "environment-validation.md",
|
|
83
93
|
"migrationGuide": "migration-0.2.md",
|
|
84
|
-
"releaseNotes": "releases/0.2.
|
|
94
|
+
"releaseNotes": "releases/0.2.3.md"
|
|
85
95
|
}
|
|
86
96
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# BCP Framework 0.2.3
|
|
2
|
+
|
|
3
|
+
## Database Platform v2
|
|
4
|
+
|
|
5
|
+
BCP `0.2.3` turns the framework database layer into a provider-neutral SQL platform while preserving the existing `bcp/database` application API.
|
|
6
|
+
|
|
7
|
+
### Database adapter contract
|
|
8
|
+
|
|
9
|
+
- `BcpDatabase` delegates connection, query, execute, transaction and disconnect behavior through `DatabaseAdapter`.
|
|
10
|
+
- Custom adapters can be injected directly or through lazy factories.
|
|
11
|
+
- Provider implementations are separated into MySQL, PostgreSQL and SQLite runtime modules.
|
|
12
|
+
- Provider drivers remain optional dependencies and are loaded lazily.
|
|
13
|
+
|
|
14
|
+
### PostgreSQL
|
|
15
|
+
|
|
16
|
+
- Added built-in PostgreSQL support through `pg`.
|
|
17
|
+
- `DATABASE_URL=postgresql://...` selects PostgreSQL automatically.
|
|
18
|
+
- `DB_DRIVER=postgresql`, `postgres` and `pg` are supported provider names.
|
|
19
|
+
- Query parameters use PostgreSQL `$1`, `$2`, ... placeholders.
|
|
20
|
+
- Transactions use dedicated pool clients with `BEGIN`, `COMMIT`, `ROLLBACK` and guaranteed client release.
|
|
21
|
+
|
|
22
|
+
### SQLite
|
|
23
|
+
|
|
24
|
+
- Added built-in SQLite support through `better-sqlite3`.
|
|
25
|
+
- Supports file paths, `:memory:`, `sqlite:` and `file:` database locations.
|
|
26
|
+
- Common `.sqlite`, `.sqlite3` and `.db` `DATABASE_URL` values infer SQLite automatically.
|
|
27
|
+
- File-backed databases create missing parent directories before opening the native database.
|
|
28
|
+
- Adapter operations are serialized so async transaction callbacks do not interleave unrelated operations on one SQLite connection.
|
|
29
|
+
|
|
30
|
+
### Connection lifecycle
|
|
31
|
+
|
|
32
|
+
- Added explicit `database.connect()`.
|
|
33
|
+
- Added explicit `database.disconnect()`.
|
|
34
|
+
- Existing `database.close()` remains backward compatible and aliases the disconnect lifecycle.
|
|
35
|
+
- Disconnecting resets the facade so a later operation can initialize a fresh provider connection.
|
|
36
|
+
- Failed adapter initialization resets cleanly and can be retried.
|
|
37
|
+
- `close()` is safe after failed adapter initialization.
|
|
38
|
+
|
|
39
|
+
### Migration consistency
|
|
40
|
+
|
|
41
|
+
The existing CLI remains unchanged:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
bcp db create create_users
|
|
45
|
+
bcp db migrate
|
|
46
|
+
bcp db status
|
|
47
|
+
bcp db rollback
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Migration bookkeeping now selects its internal SQL dialect from the active database provider:
|
|
51
|
+
|
|
52
|
+
- MySQL uses `AUTO_INCREMENT` and `?` placeholders.
|
|
53
|
+
- PostgreSQL uses `BIGSERIAL` and `$1`, `$2` placeholders.
|
|
54
|
+
- SQLite uses `INTEGER PRIMARY KEY AUTOINCREMENT` and `?` placeholders.
|
|
55
|
+
|
|
56
|
+
BCP only makes the internal `_bcp_migrations` bookkeeping provider-aware. Application migration SQL is intentionally not translated between SQL dialects.
|
|
57
|
+
|
|
58
|
+
### Packaging and validation
|
|
59
|
+
|
|
60
|
+
- Added Database Platform v2 package smoke coverage.
|
|
61
|
+
- Packed framework validation checks provider modules, bundled optional-driver references, lifecycle APIs and migration dialect support.
|
|
62
|
+
- Added unit coverage for SQLite option resolution, provider inference, lifecycle reconnect behavior, failed adapter initialization and migration dialects.
|
|
63
|
+
|
|
64
|
+
## Compatibility
|
|
65
|
+
|
|
66
|
+
`0.2.3` does not intentionally remove public application entrypoints from the `0.2.2` baseline.
|
|
67
|
+
|
|
68
|
+
Existing MySQL applications can continue using:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import {
|
|
72
|
+
db,
|
|
73
|
+
createDatabase,
|
|
74
|
+
} from "bcp/database";
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
without changing their application-facing database calls.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chidchanun/bcp",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "BCP Framework - a React full-stack framework with file-based routing, SSR, APIs, middleware, islands, caching and standalone production builds.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -4,8 +4,10 @@ import {
|
|
|
4
4
|
pathToFileURL,
|
|
5
5
|
} from "node:url";
|
|
6
6
|
|
|
7
|
-
import
|
|
8
|
-
|
|
7
|
+
import {
|
|
8
|
+
resolveDatabaseOptions,
|
|
9
|
+
type DatabaseDriver,
|
|
10
|
+
type TransactionDatabase,
|
|
9
11
|
} from "../../client/src/database.js";
|
|
10
12
|
|
|
11
13
|
export type DatabaseMigrationAction =
|
|
@@ -35,6 +37,7 @@ export interface DatabaseMigrationCommandOptions {
|
|
|
35
37
|
action: DatabaseMigrationAction;
|
|
36
38
|
name?: string;
|
|
37
39
|
database?: MigrationDatabase;
|
|
40
|
+
driver?: DatabaseDriver;
|
|
38
41
|
now?: Date;
|
|
39
42
|
}
|
|
40
43
|
|
|
@@ -65,6 +68,12 @@ interface MigrationModule {
|
|
|
65
68
|
) => Promise<void> | void;
|
|
66
69
|
}
|
|
67
70
|
|
|
71
|
+
interface MigrationDialect {
|
|
72
|
+
createTableSql: string;
|
|
73
|
+
insertMigrationSql: string;
|
|
74
|
+
deleteMigrationSql: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
68
77
|
const MIGRATION_DIRECTORY =
|
|
69
78
|
"migrations";
|
|
70
79
|
const MIGRATION_TABLE =
|
|
@@ -105,8 +114,15 @@ export async function runDatabaseMigrationCommand(
|
|
|
105
114
|
);
|
|
106
115
|
}
|
|
107
116
|
|
|
117
|
+
const dialect =
|
|
118
|
+
getMigrationDialect(
|
|
119
|
+
options.driver ??
|
|
120
|
+
resolveDatabaseOptions().driver
|
|
121
|
+
);
|
|
122
|
+
|
|
108
123
|
await ensureMigrationTable(
|
|
109
|
-
database
|
|
124
|
+
database,
|
|
125
|
+
dialect
|
|
110
126
|
);
|
|
111
127
|
|
|
112
128
|
if (
|
|
@@ -129,14 +145,16 @@ export async function runDatabaseMigrationCommand(
|
|
|
129
145
|
) {
|
|
130
146
|
await migratePending(
|
|
131
147
|
rootDirectory,
|
|
132
|
-
database
|
|
148
|
+
database,
|
|
149
|
+
dialect
|
|
133
150
|
);
|
|
134
151
|
return;
|
|
135
152
|
}
|
|
136
153
|
|
|
137
154
|
await rollbackLatestBatch(
|
|
138
155
|
rootDirectory,
|
|
139
|
-
database
|
|
156
|
+
database,
|
|
157
|
+
dialect
|
|
140
158
|
);
|
|
141
159
|
}
|
|
142
160
|
|
|
@@ -197,13 +215,13 @@ export function createMigrationFile(
|
|
|
197
215
|
`export async function up(\n` +
|
|
198
216
|
` db: TransactionDatabase\n` +
|
|
199
217
|
`): Promise<void> {\n` +
|
|
200
|
-
` // Write
|
|
218
|
+
` // Write provider-compatible forward SQL here.\n` +
|
|
201
219
|
` void db;\n` +
|
|
202
220
|
`}\n\n` +
|
|
203
221
|
`export async function down(\n` +
|
|
204
222
|
` db: TransactionDatabase\n` +
|
|
205
223
|
`): Promise<void> {\n` +
|
|
206
|
-
` // Write
|
|
224
|
+
` // Write provider-compatible rollback SQL here.\n` +
|
|
207
225
|
` void db;\n` +
|
|
208
226
|
`}\n`;
|
|
209
227
|
|
|
@@ -363,7 +381,8 @@ export async function getMigrationStatus(
|
|
|
363
381
|
|
|
364
382
|
async function migratePending(
|
|
365
383
|
rootDirectory: string,
|
|
366
|
-
database: MigrationDatabase
|
|
384
|
+
database: MigrationDatabase,
|
|
385
|
+
dialect: MigrationDialect
|
|
367
386
|
): Promise<void> {
|
|
368
387
|
const files =
|
|
369
388
|
listMigrationFiles(
|
|
@@ -421,11 +440,11 @@ async function migratePending(
|
|
|
421
440
|
|
|
422
441
|
await database.transaction(
|
|
423
442
|
async (transaction) => {
|
|
424
|
-
await module.up
|
|
443
|
+
await module.up(
|
|
425
444
|
transaction
|
|
426
445
|
);
|
|
427
446
|
await transaction.execute(
|
|
428
|
-
|
|
447
|
+
dialect.insertMigrationSql,
|
|
429
448
|
[
|
|
430
449
|
migration.name,
|
|
431
450
|
batch,
|
|
@@ -442,7 +461,8 @@ async function migratePending(
|
|
|
442
461
|
|
|
443
462
|
async function rollbackLatestBatch(
|
|
444
463
|
rootDirectory: string,
|
|
445
|
-
database: MigrationDatabase
|
|
464
|
+
database: MigrationDatabase,
|
|
465
|
+
dialect: MigrationDialect
|
|
446
466
|
): Promise<void> {
|
|
447
467
|
const applied =
|
|
448
468
|
await readAppliedMigrations(
|
|
@@ -520,11 +540,11 @@ async function rollbackLatestBatch(
|
|
|
520
540
|
|
|
521
541
|
await database.transaction(
|
|
522
542
|
async (transaction) => {
|
|
523
|
-
await module.down
|
|
543
|
+
await module.down(
|
|
524
544
|
transaction
|
|
525
545
|
);
|
|
526
546
|
await transaction.execute(
|
|
527
|
-
|
|
547
|
+
dialect.deleteMigrationSql,
|
|
528
548
|
[
|
|
529
549
|
record.name,
|
|
530
550
|
]
|
|
@@ -539,15 +559,11 @@ async function rollbackLatestBatch(
|
|
|
539
559
|
}
|
|
540
560
|
|
|
541
561
|
async function ensureMigrationTable(
|
|
542
|
-
database: MigrationDatabase
|
|
562
|
+
database: MigrationDatabase,
|
|
563
|
+
dialect: MigrationDialect
|
|
543
564
|
): Promise<void> {
|
|
544
565
|
await database.execute(
|
|
545
|
-
|
|
546
|
-
`id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, ` +
|
|
547
|
-
`name VARCHAR(255) NOT NULL UNIQUE, ` +
|
|
548
|
-
`batch INT UNSIGNED NOT NULL, ` +
|
|
549
|
-
`applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP` +
|
|
550
|
-
`) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`
|
|
566
|
+
dialect.createTableSql
|
|
551
567
|
);
|
|
552
568
|
}
|
|
553
569
|
|
|
@@ -561,6 +577,60 @@ async function readAppliedMigrations(
|
|
|
561
577
|
);
|
|
562
578
|
}
|
|
563
579
|
|
|
580
|
+
function getMigrationDialect(
|
|
581
|
+
driver: DatabaseDriver
|
|
582
|
+
): MigrationDialect {
|
|
583
|
+
if (
|
|
584
|
+
driver === "postgresql"
|
|
585
|
+
) {
|
|
586
|
+
return {
|
|
587
|
+
createTableSql:
|
|
588
|
+
`CREATE TABLE IF NOT EXISTS ${MIGRATION_TABLE} (` +
|
|
589
|
+
`id BIGSERIAL PRIMARY KEY, ` +
|
|
590
|
+
`name VARCHAR(255) NOT NULL UNIQUE, ` +
|
|
591
|
+
`batch INTEGER NOT NULL, ` +
|
|
592
|
+
`applied_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP` +
|
|
593
|
+
`)`,
|
|
594
|
+
insertMigrationSql:
|
|
595
|
+
`INSERT INTO ${MIGRATION_TABLE} (name, batch) VALUES ($1, $2)`,
|
|
596
|
+
deleteMigrationSql:
|
|
597
|
+
`DELETE FROM ${MIGRATION_TABLE} WHERE name = $1`,
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
if (
|
|
602
|
+
driver === "sqlite"
|
|
603
|
+
) {
|
|
604
|
+
return {
|
|
605
|
+
createTableSql:
|
|
606
|
+
`CREATE TABLE IF NOT EXISTS ${MIGRATION_TABLE} (` +
|
|
607
|
+
`id INTEGER PRIMARY KEY AUTOINCREMENT, ` +
|
|
608
|
+
`name TEXT NOT NULL UNIQUE, ` +
|
|
609
|
+
`batch INTEGER NOT NULL, ` +
|
|
610
|
+
`applied_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP` +
|
|
611
|
+
`)`,
|
|
612
|
+
insertMigrationSql:
|
|
613
|
+
`INSERT INTO ${MIGRATION_TABLE} (name, batch) VALUES (?, ?)`,
|
|
614
|
+
deleteMigrationSql:
|
|
615
|
+
`DELETE FROM ${MIGRATION_TABLE} WHERE name = ?`,
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
return {
|
|
620
|
+
createTableSql:
|
|
621
|
+
`CREATE TABLE IF NOT EXISTS ${MIGRATION_TABLE} (` +
|
|
622
|
+
`id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, ` +
|
|
623
|
+
`name VARCHAR(255) NOT NULL UNIQUE, ` +
|
|
624
|
+
`batch INT UNSIGNED NOT NULL, ` +
|
|
625
|
+
`applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP` +
|
|
626
|
+
`) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`,
|
|
627
|
+
insertMigrationSql:
|
|
628
|
+
`INSERT INTO ${MIGRATION_TABLE} (name, batch) VALUES (?, ?)`,
|
|
629
|
+
deleteMigrationSql:
|
|
630
|
+
`DELETE FROM ${MIGRATION_TABLE} WHERE name = ?`,
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
|
|
564
634
|
async function loadMigrationModule(
|
|
565
635
|
migration: LoadedMigration
|
|
566
636
|
): Promise<Required<MigrationModule>> {
|
|
@@ -595,7 +665,7 @@ async function loadMigrationModule(
|
|
|
595
665
|
module.up,
|
|
596
666
|
down:
|
|
597
667
|
module.down,
|
|
598
|
-
}
|
|
668
|
+
} as Required<MigrationModule>;
|
|
599
669
|
}
|
|
600
670
|
|
|
601
671
|
function normalizeMigrationName(
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DatabaseAdapter,
|
|
3
|
+
DatabaseParameters,
|
|
4
|
+
ResolvedDatabaseOptions,
|
|
5
|
+
TransactionDatabase,
|
|
6
|
+
} from "./database.js";
|
|
7
|
+
|
|
8
|
+
interface MysqlExecutor {
|
|
9
|
+
query(
|
|
10
|
+
sql: string,
|
|
11
|
+
parameters?: DatabaseParameters
|
|
12
|
+
): Promise<[unknown, unknown]>;
|
|
13
|
+
execute(
|
|
14
|
+
sql: string,
|
|
15
|
+
parameters?: DatabaseParameters
|
|
16
|
+
): Promise<[unknown, unknown]>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface MysqlConnection extends MysqlExecutor {
|
|
20
|
+
beginTransaction(): Promise<void>;
|
|
21
|
+
commit(): Promise<void>;
|
|
22
|
+
rollback(): Promise<void>;
|
|
23
|
+
release(): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface MysqlPool extends MysqlExecutor {
|
|
27
|
+
getConnection(): Promise<MysqlConnection>;
|
|
28
|
+
end(): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface MysqlModule {
|
|
32
|
+
createPool(
|
|
33
|
+
options: Record<string, unknown>
|
|
34
|
+
): MysqlPool;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const MYSQL_MODULE_SPECIFIER =
|
|
38
|
+
"mysql2/promise";
|
|
39
|
+
|
|
40
|
+
export function createMysqlDatabaseAdapter(
|
|
41
|
+
options: ResolvedDatabaseOptions
|
|
42
|
+
): DatabaseAdapter {
|
|
43
|
+
return new MysqlDatabaseAdapter(
|
|
44
|
+
options
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
class MysqlDatabaseAdapter
|
|
49
|
+
implements DatabaseAdapter {
|
|
50
|
+
readonly driver = "mysql";
|
|
51
|
+
|
|
52
|
+
private readonly options:
|
|
53
|
+
ResolvedDatabaseOptions;
|
|
54
|
+
|
|
55
|
+
private poolPromise:
|
|
56
|
+
Promise<MysqlPool> | null =
|
|
57
|
+
null;
|
|
58
|
+
|
|
59
|
+
constructor(
|
|
60
|
+
options: ResolvedDatabaseOptions
|
|
61
|
+
) {
|
|
62
|
+
this.options =
|
|
63
|
+
options;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async connect(): Promise<void> {
|
|
67
|
+
if (!this.poolPromise) {
|
|
68
|
+
this.poolPromise =
|
|
69
|
+
createMysqlPool(
|
|
70
|
+
this.options
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
await this.poolPromise;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async query<T = unknown>(
|
|
78
|
+
sql: string,
|
|
79
|
+
parameters?: DatabaseParameters
|
|
80
|
+
): Promise<T> {
|
|
81
|
+
assertSql(
|
|
82
|
+
sql
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const pool =
|
|
86
|
+
await this.getPool();
|
|
87
|
+
const [rows] =
|
|
88
|
+
await pool.query(
|
|
89
|
+
sql,
|
|
90
|
+
parameters
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
return rows as T;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async execute<T = unknown>(
|
|
97
|
+
sql: string,
|
|
98
|
+
parameters?: DatabaseParameters
|
|
99
|
+
): Promise<T> {
|
|
100
|
+
assertSql(
|
|
101
|
+
sql
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
const pool =
|
|
105
|
+
await this.getPool();
|
|
106
|
+
const [result] =
|
|
107
|
+
await pool.execute(
|
|
108
|
+
sql,
|
|
109
|
+
parameters
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
return result as T;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async transaction<T>(
|
|
116
|
+
callback: (
|
|
117
|
+
database: TransactionDatabase
|
|
118
|
+
) => Promise<T>
|
|
119
|
+
): Promise<T> {
|
|
120
|
+
assertTransactionCallback(
|
|
121
|
+
callback
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const pool =
|
|
125
|
+
await this.getPool();
|
|
126
|
+
const connection =
|
|
127
|
+
await pool.getConnection();
|
|
128
|
+
|
|
129
|
+
await connection.beginTransaction();
|
|
130
|
+
|
|
131
|
+
const transactionDatabase:
|
|
132
|
+
TransactionDatabase = {
|
|
133
|
+
query: async <R = unknown>(
|
|
134
|
+
sql: string,
|
|
135
|
+
parameters?: DatabaseParameters
|
|
136
|
+
): Promise<R> => {
|
|
137
|
+
assertSql(
|
|
138
|
+
sql
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
const [rows] =
|
|
142
|
+
await connection.query(
|
|
143
|
+
sql,
|
|
144
|
+
parameters
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
return rows as R;
|
|
148
|
+
},
|
|
149
|
+
execute: async <R = unknown>(
|
|
150
|
+
sql: string,
|
|
151
|
+
parameters?: DatabaseParameters
|
|
152
|
+
): Promise<R> => {
|
|
153
|
+
assertSql(
|
|
154
|
+
sql
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
const [result] =
|
|
158
|
+
await connection.execute(
|
|
159
|
+
sql,
|
|
160
|
+
parameters
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
return result as R;
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
const result =
|
|
169
|
+
await callback(
|
|
170
|
+
transactionDatabase
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
await connection.commit();
|
|
174
|
+
|
|
175
|
+
return result;
|
|
176
|
+
} catch (error) {
|
|
177
|
+
try {
|
|
178
|
+
await connection.rollback();
|
|
179
|
+
} catch {
|
|
180
|
+
// Preserve the original transaction error.
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
throw error;
|
|
184
|
+
} finally {
|
|
185
|
+
connection.release();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async disconnect(): Promise<void> {
|
|
190
|
+
const pendingPool =
|
|
191
|
+
this.poolPromise;
|
|
192
|
+
|
|
193
|
+
this.poolPromise =
|
|
194
|
+
null;
|
|
195
|
+
|
|
196
|
+
if (!pendingPool) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
let pool:
|
|
201
|
+
MysqlPool;
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
pool =
|
|
205
|
+
await pendingPool;
|
|
206
|
+
} catch {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
await pool.end();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private async getPool():
|
|
214
|
+
Promise<MysqlPool> {
|
|
215
|
+
await this.connect();
|
|
216
|
+
|
|
217
|
+
return this.poolPromise as
|
|
218
|
+
Promise<MysqlPool>;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function createMysqlPool(
|
|
223
|
+
options: ResolvedDatabaseOptions
|
|
224
|
+
): Promise<MysqlPool> {
|
|
225
|
+
let mysqlModule:
|
|
226
|
+
MysqlModule;
|
|
227
|
+
|
|
228
|
+
try {
|
|
229
|
+
mysqlModule =
|
|
230
|
+
await import(
|
|
231
|
+
MYSQL_MODULE_SPECIFIER
|
|
232
|
+
) as unknown as MysqlModule;
|
|
233
|
+
} catch (error) {
|
|
234
|
+
throw new Error(
|
|
235
|
+
"BCP Database: MySQL requires the optional dependency mysql2. Install it with `npm install mysql2` or create the app with the MySQL preset.",
|
|
236
|
+
{
|
|
237
|
+
cause:
|
|
238
|
+
error,
|
|
239
|
+
}
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return mysqlModule.createPool({
|
|
244
|
+
host:
|
|
245
|
+
options.host,
|
|
246
|
+
port:
|
|
247
|
+
options.port,
|
|
248
|
+
user:
|
|
249
|
+
options.user,
|
|
250
|
+
password:
|
|
251
|
+
options.password,
|
|
252
|
+
database:
|
|
253
|
+
options.database,
|
|
254
|
+
waitForConnections:
|
|
255
|
+
options.waitForConnections,
|
|
256
|
+
connectionLimit:
|
|
257
|
+
options.connectionLimit,
|
|
258
|
+
queueLimit:
|
|
259
|
+
options.queueLimit,
|
|
260
|
+
charset:
|
|
261
|
+
options.charset,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function assertSql(
|
|
266
|
+
sql: string
|
|
267
|
+
): void {
|
|
268
|
+
if (
|
|
269
|
+
typeof sql !== "string" ||
|
|
270
|
+
sql.trim() === ""
|
|
271
|
+
) {
|
|
272
|
+
throw new TypeError(
|
|
273
|
+
"BCP Database: SQL must be a non-empty string."
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function assertTransactionCallback(
|
|
279
|
+
callback: unknown
|
|
280
|
+
): asserts callback is (
|
|
281
|
+
database: TransactionDatabase
|
|
282
|
+
) => Promise<unknown> {
|
|
283
|
+
if (
|
|
284
|
+
typeof callback !==
|
|
285
|
+
"function"
|
|
286
|
+
) {
|
|
287
|
+
throw new TypeError(
|
|
288
|
+
"BCP Database: transaction callback must be a function."
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
}
|