@simplysm/orm-node 13.0.72 → 13.0.74

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.
Files changed (2) hide show
  1. package/README.md +479 -20
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -2,37 +2,496 @@
2
2
 
3
3
  Simplysm package - ORM module (node)
4
4
 
5
+ Provides Node.js database connection classes, a connection pool wrapper, and a top-level `createOrm` factory for running queries through `@simplysm/orm-common` `DbContext`.
6
+
7
+ Supports MSSQL, MySQL, and PostgreSQL. Database driver packages (`tedious`, `mysql2`, `pg`, `pg-copy-streams`) are optional peer dependencies — install only the ones you need.
8
+
5
9
  ## Installation
6
10
 
11
+ ```bash
7
12
  pnpm add @simplysm/orm-node
8
13
 
9
- **Peer Dependencies:** `mysql2` (optional), `pg` (optional), `pg-copy-streams` (optional), `tedious` (optional)
14
+ # Install driver(s) for the database(s) you use
15
+ pnpm add tedious # MSSQL / Azure SQL
16
+ pnpm add mysql2 # MySQL
17
+ pnpm add pg pg-copy-streams # PostgreSQL
18
+ ```
19
+
20
+ ## Main Modules
21
+
22
+ ### Core
23
+
24
+ #### `createOrm`
25
+
26
+ Node.js ORM factory function. Creates an `Orm` instance that binds a `DbContextDef` to a connection configuration and provides `connect` / `connectWithoutTransaction` helpers.
27
+
28
+ ```typescript
29
+ import { createOrm } from "@simplysm/orm-node";
30
+ import { defineDbContext, queryable } from "@simplysm/orm-common";
31
+
32
+ const MyDb = defineDbContext({
33
+ user: (db) => queryable(db, User),
34
+ });
35
+
36
+ const orm = createOrm(MyDb, {
37
+ dialect: "mysql",
38
+ host: "localhost",
39
+ port: 3306,
40
+ username: "root",
41
+ password: "password",
42
+ database: "mydb",
43
+ });
44
+
45
+ // Execute within a transaction
46
+ const users = await orm.connect(async (db) => {
47
+ return db.user().result();
48
+ });
49
+
50
+ // Execute without a transaction
51
+ const users2 = await orm.connectWithoutTransaction(async (db) => {
52
+ return db.user().result();
53
+ });
54
+ ```
55
+
56
+ **Signature**
57
+
58
+ ```typescript
59
+ function createOrm<TDef extends DbContextDef<any, any, any>>(
60
+ dbContextDef: TDef,
61
+ config: DbConnConfig,
62
+ options?: OrmOptions,
63
+ ): Orm<TDef>
64
+ ```
65
+
66
+ | Parameter | Type | Description |
67
+ |-----------|------|-------------|
68
+ | `dbContextDef` | `TDef` | DbContext definition created with `defineDbContext` |
69
+ | `config` | `DbConnConfig` | Database connection configuration |
70
+ | `options` | `OrmOptions` (optional) | Override `database` or `schema` from config |
71
+
72
+ ---
73
+
74
+ #### `createDbConn`
75
+
76
+ Low-level DB connection factory. Acquires a `PooledDbConn` from the internal connection pool for the given configuration. Creates a new pool on first call; reuses the same pool on subsequent calls with the same configuration.
77
+
78
+ ```typescript
79
+ import { createDbConn } from "@simplysm/orm-node";
80
+
81
+ const conn = await createDbConn({
82
+ dialect: "postgresql",
83
+ host: "localhost",
84
+ username: "postgres",
85
+ password: "secret",
86
+ database: "mydb",
87
+ });
88
+
89
+ await conn.connect();
90
+ await conn.beginTransaction();
91
+ // ... execute queries ...
92
+ await conn.commitTransaction();
93
+ await conn.close(); // returns connection to pool
94
+ ```
95
+
96
+ **Signature**
97
+
98
+ ```typescript
99
+ function createDbConn(config: DbConnConfig): Promise<DbConn>
100
+ ```
101
+
102
+ ---
103
+
104
+ #### `NodeDbContextExecutor`
105
+
106
+ `DbContextExecutor` implementation for the Node.js environment. Used internally by `createOrm`. Can be passed directly to `createDbContext` from `@simplysm/orm-common` when fine-grained control is needed.
107
+
108
+ ```typescript
109
+ import { NodeDbContextExecutor } from "@simplysm/orm-node";
110
+ import { createDbContext } from "@simplysm/orm-common";
111
+
112
+ const executor = new NodeDbContextExecutor({
113
+ dialect: "mssql",
114
+ host: "localhost",
115
+ username: "sa",
116
+ password: "Password1",
117
+ database: "mydb",
118
+ });
119
+
120
+ const db = createDbContext(MyDb, executor, { database: "mydb" });
121
+ await db.connect(async () => {
122
+ const rows = await db.user().result();
123
+ });
124
+ ```
125
+
126
+ **Constructor**
10
127
 
11
- ## Source Index
128
+ ```typescript
129
+ new NodeDbContextExecutor(config: DbConnConfig)
130
+ ```
12
131
 
13
- ### Types
132
+ **Methods** (all delegate to the underlying `DbConn`)
14
133
 
15
- | Source | Exports | Description | Test |
16
- |--------|---------|-------------|------|
17
- | `src/types/db-conn.ts` | `DB_CONN_CONNECT_TIMEOUT`, `DB_CONN_DEFAULT_TIMEOUT`, `DB_CONN_ERRORS`, `DbConn`, `DbPoolConfig`, `DbConnConfig`, `MysqlDbConnConfig`, `MssqlDbConnConfig`, `PostgresqlDbConnConfig`, `getDialectFromConfig` | Database connection interface, config types, and timeout constants for all dialects | `-` |
134
+ | Method | Description |
135
+ |--------|-------------|
136
+ | `connect()` | Acquire connection from pool and open it |
137
+ | `close()` | Close connection and return it to pool |
138
+ | `beginTransaction(isolationLevel?)` | Begin a transaction |
139
+ | `commitTransaction()` | Commit the current transaction |
140
+ | `rollbackTransaction()` | Roll back the current transaction |
141
+ | `executeParametrized(query, params?)` | Execute a parameterized SQL query |
142
+ | `bulkInsert(tableName, columnMetas, records)` | Bulk insert using the native bulk API |
143
+ | `executeDefs(defs, resultMetas?)` | Build and execute `QueryDef` array, parse results |
144
+
145
+ ---
146
+
147
+ #### `PooledDbConn`
148
+
149
+ `DbConn` wrapper that manages connection pool lifecycle. Returned by `createDbConn`. Calling `close()` returns the underlying physical connection to the pool rather than terminating it.
150
+
151
+ ```typescript
152
+ import { createDbConn } from "@simplysm/orm-node";
153
+
154
+ const conn = await createDbConn(config); // returns PooledDbConn
155
+ await conn.connect(); // acquires physical connection from pool
156
+ await conn.close(); // releases physical connection back to pool
157
+ ```
158
+
159
+ **Constructor**
160
+
161
+ ```typescript
162
+ new PooledDbConn(
163
+ pool: Pool<DbConn>,
164
+ initialConfig: DbConnConfig,
165
+ getLastCreateError?: () => Error | undefined,
166
+ )
167
+ ```
168
+
169
+ **Properties**
170
+
171
+ | Property | Type | Description |
172
+ |----------|------|-------------|
173
+ | `config` | `DbConnConfig` | Active connection configuration |
174
+ | `isConnected` | `boolean` | Whether a physical connection is currently held |
175
+ | `isInTransaction` | `boolean` | Whether a transaction is in progress |
176
+
177
+ **Methods**
178
+
179
+ | Method | Description |
180
+ |--------|-------------|
181
+ | `connect()` | Acquire connection from pool |
182
+ | `close()` | Roll back pending transaction (if any) and release connection to pool |
183
+ | `beginTransaction(isolationLevel?)` | Begin a transaction |
184
+ | `commitTransaction()` | Commit the current transaction |
185
+ | `rollbackTransaction()` | Roll back the current transaction |
186
+ | `execute(queries)` | Execute SQL string array |
187
+ | `executeParametrized(query, params?)` | Execute parameterized SQL query |
188
+ | `bulkInsert(tableName, columnMetas, records)` | Bulk insert using native bulk API |
189
+
190
+ ---
18
191
 
19
192
  ### Connections
20
193
 
21
- | Source | Exports | Description | Test |
22
- |--------|---------|-------------|------|
23
- | `src/connections/mssql-db-conn.ts` | `MssqlDbConn` | MSSQL database connection implementation using tedious | `-` |
24
- | `src/connections/mysql-db-conn.ts` | `MysqlDbConn` | MySQL database connection implementation using mysql2 | `-` |
25
- | `src/connections/postgresql-db-conn.ts` | `PostgresqlDbConn` | PostgreSQL database connection implementation using pg | `-` |
194
+ #### `MssqlDbConn`
26
195
 
27
- ### Core
196
+ MSSQL / Azure SQL database connection. Uses the `tedious` library. Requires `tedious` as a peer dependency.
197
+
198
+ ```typescript
199
+ import { MssqlDbConn } from "@simplysm/orm-node";
200
+ import tedious from "tedious";
201
+
202
+ const conn = new MssqlDbConn(tedious, {
203
+ dialect: "mssql",
204
+ host: "localhost",
205
+ username: "sa",
206
+ password: "Password1",
207
+ database: "mydb",
208
+ });
209
+
210
+ await conn.connect();
211
+ const results = await conn.execute(["SELECT 1 AS val"]);
212
+ await conn.close();
213
+ ```
214
+
215
+ **Constructor**
216
+
217
+ ```typescript
218
+ new MssqlDbConn(
219
+ tedious: typeof import("tedious"),
220
+ config: MssqlDbConnConfig,
221
+ )
222
+ ```
223
+
224
+ **Properties**
225
+
226
+ | Property | Type | Description |
227
+ |----------|------|-------------|
228
+ | `config` | `MssqlDbConnConfig` | Connection configuration |
229
+ | `isConnected` | `boolean` | Whether connected |
230
+ | `isInTransaction` | `boolean` | Whether transaction is in progress |
231
+
232
+ **Methods** — implements `DbConn` (see interface below)
233
+
234
+ ---
235
+
236
+ #### `MysqlDbConn`
237
+
238
+ MySQL database connection. Uses the `mysql2/promise` library. Requires `mysql2` as a peer dependency.
239
+
240
+ ```typescript
241
+ import { MysqlDbConn } from "@simplysm/orm-node";
242
+ import mysql2 from "mysql2/promise";
243
+
244
+ const conn = new MysqlDbConn(mysql2, {
245
+ dialect: "mysql",
246
+ host: "localhost",
247
+ username: "root",
248
+ password: "password",
249
+ database: "mydb",
250
+ });
251
+
252
+ await conn.connect();
253
+ const results = await conn.execute(["SELECT 1 AS val"]);
254
+ await conn.close();
255
+ ```
256
+
257
+ **Constructor**
258
+
259
+ ```typescript
260
+ new MysqlDbConn(
261
+ mysql2: typeof import("mysql2/promise"),
262
+ config: MysqlDbConnConfig,
263
+ )
264
+ ```
265
+
266
+ **Properties**
267
+
268
+ | Property | Type | Description |
269
+ |----------|------|-------------|
270
+ | `config` | `MysqlDbConnConfig` | Connection configuration |
271
+ | `isConnected` | `boolean` | Whether connected |
272
+ | `isInTransaction` | `boolean` | Whether transaction is in progress |
273
+
274
+ **Methods** — implements `DbConn` (see interface below)
275
+
276
+ ---
277
+
278
+ #### `PostgresqlDbConn`
279
+
280
+ PostgreSQL database connection. Uses the `pg` library and `pg-copy-streams` for bulk insert. Requires `pg` and `pg-copy-streams` as peer dependencies.
281
+
282
+ ```typescript
283
+ import { PostgresqlDbConn } from "@simplysm/orm-node";
284
+ import pg from "pg";
285
+ import pgCopyStreams from "pg-copy-streams";
286
+
287
+ const conn = new PostgresqlDbConn(pg, pgCopyStreams, {
288
+ dialect: "postgresql",
289
+ host: "localhost",
290
+ username: "postgres",
291
+ password: "secret",
292
+ database: "mydb",
293
+ });
294
+
295
+ await conn.connect();
296
+ const results = await conn.execute(["SELECT 1 AS val"]);
297
+ await conn.close();
298
+ ```
299
+
300
+ **Constructor**
301
+
302
+ ```typescript
303
+ new PostgresqlDbConn(
304
+ pg: typeof import("pg"),
305
+ pgCopyStreams: typeof import("pg-copy-streams"),
306
+ config: PostgresqlDbConnConfig,
307
+ )
308
+ ```
309
+
310
+ **Properties**
311
+
312
+ | Property | Type | Description |
313
+ |----------|------|-------------|
314
+ | `config` | `PostgresqlDbConnConfig` | Connection configuration |
315
+ | `isConnected` | `boolean` | Whether connected |
316
+ | `isInTransaction` | `boolean` | Whether transaction is in progress |
317
+
318
+ **Methods** — implements `DbConn` (see interface below)
319
+
320
+ ---
321
+
322
+ ## Types
323
+
324
+ ### `DbConn`
325
+
326
+ Low-level database connection interface implemented by `MssqlDbConn`, `MysqlDbConn`, and `PostgresqlDbConn`. Extends `EventEmitter<{ close: void }>`.
327
+
328
+ ```typescript
329
+ import type { DbConn } from "@simplysm/orm-node";
330
+ ```
331
+
332
+ | Member | Type | Description |
333
+ |--------|------|-------------|
334
+ | `config` | `DbConnConfig` | Connection configuration |
335
+ | `isConnected` | `boolean` | Whether connected |
336
+ | `isInTransaction` | `boolean` | Whether a transaction is in progress |
337
+ | `connect()` | `Promise<void>` | Establish connection |
338
+ | `close()` | `Promise<void>` | Close connection |
339
+ | `beginTransaction(isolationLevel?)` | `Promise<void>` | Begin transaction |
340
+ | `commitTransaction()` | `Promise<void>` | Commit transaction |
341
+ | `rollbackTransaction()` | `Promise<void>` | Roll back transaction |
342
+ | `execute(queries)` | `Promise<Record<string, unknown>[][]>` | Execute SQL string array |
343
+ | `executeParametrized(query, params?)` | `Promise<Record<string, unknown>[][]>` | Execute parameterized query |
344
+ | `bulkInsert(tableName, columnMetas, records)` | `Promise<void>` | Bulk insert using native bulk API |
345
+
346
+ ---
347
+
348
+ ### `Orm<TDef>`
349
+
350
+ Type of the object returned from `createOrm`.
351
+
352
+ ```typescript
353
+ import type { Orm } from "@simplysm/orm-node";
354
+ ```
355
+
356
+ | Member | Type | Description |
357
+ |--------|------|-------------|
358
+ | `dbContextDef` | `TDef` | DbContext definition |
359
+ | `config` | `DbConnConfig` | Connection configuration |
360
+ | `options` | `OrmOptions \| undefined` | ORM options |
361
+ | `connect(callback, isolationLevel?)` | `Promise<R>` | Execute callback within a transaction |
362
+ | `connectWithoutTransaction(callback)` | `Promise<R>` | Execute callback without a transaction |
363
+
364
+ ---
365
+
366
+ ### `OrmOptions`
367
+
368
+ Options passed to `createOrm` to override connection-level database/schema settings.
369
+
370
+ ```typescript
371
+ import type { OrmOptions } from "@simplysm/orm-node";
372
+ ```
373
+
374
+ | Property | Type | Description |
375
+ |----------|------|-------------|
376
+ | `database` | `string` (optional) | Override database name from `DbConnConfig` |
377
+ | `schema` | `string` (optional) | Override schema name (MSSQL: `dbo`, PostgreSQL: `public`) |
378
+
379
+ ---
380
+
381
+ ### `DbConnConfig`
382
+
383
+ Union type that selects the correct configuration shape based on `dialect`.
384
+
385
+ ```typescript
386
+ import type { DbConnConfig } from "@simplysm/orm-node";
387
+
388
+ type DbConnConfig = MysqlDbConnConfig | MssqlDbConnConfig | PostgresqlDbConnConfig;
389
+ ```
390
+
391
+ ---
392
+
393
+ ### `MysqlDbConnConfig`
394
+
395
+ ```typescript
396
+ import type { MysqlDbConnConfig } from "@simplysm/orm-node";
397
+ ```
398
+
399
+ | Property | Type | Description |
400
+ |----------|------|-------------|
401
+ | `dialect` | `"mysql"` | Dialect discriminant |
402
+ | `host` | `string` | Hostname |
403
+ | `port` | `number` (optional) | Port (default: 3306) |
404
+ | `username` | `string` | Username |
405
+ | `password` | `string` | Password |
406
+ | `database` | `string` (optional) | Database name |
407
+ | `defaultIsolationLevel` | `IsolationLevel` (optional) | Default isolation level |
408
+ | `pool` | `DbPoolConfig` (optional) | Connection pool settings |
409
+
410
+ ---
411
+
412
+ ### `MssqlDbConnConfig`
413
+
414
+ ```typescript
415
+ import type { MssqlDbConnConfig } from "@simplysm/orm-node";
416
+ ```
417
+
418
+ | Property | Type | Description |
419
+ |----------|------|-------------|
420
+ | `dialect` | `"mssql" \| "mssql-azure"` | Dialect discriminant (`mssql-azure` enables TLS encryption) |
421
+ | `host` | `string` | Hostname |
422
+ | `port` | `number` (optional) | Port (default: 1433) |
423
+ | `username` | `string` | Username |
424
+ | `password` | `string` | Password |
425
+ | `database` | `string` (optional) | Database name |
426
+ | `schema` | `string` (optional) | Schema name (default: `dbo`) |
427
+ | `defaultIsolationLevel` | `IsolationLevel` (optional) | Default isolation level |
428
+ | `pool` | `DbPoolConfig` (optional) | Connection pool settings |
429
+
430
+ ---
431
+
432
+ ### `PostgresqlDbConnConfig`
433
+
434
+ ```typescript
435
+ import type { PostgresqlDbConnConfig } from "@simplysm/orm-node";
436
+ ```
437
+
438
+ | Property | Type | Description |
439
+ |----------|------|-------------|
440
+ | `dialect` | `"postgresql"` | Dialect discriminant |
441
+ | `host` | `string` | Hostname |
442
+ | `port` | `number` (optional) | Port (default: 5432) |
443
+ | `username` | `string` | Username |
444
+ | `password` | `string` | Password |
445
+ | `database` | `string` (optional) | Database name |
446
+ | `schema` | `string` (optional) | Schema name (default: `public`) |
447
+ | `defaultIsolationLevel` | `IsolationLevel` (optional) | Default isolation level |
448
+ | `pool` | `DbPoolConfig` (optional) | Connection pool settings |
449
+
450
+ ---
451
+
452
+ ### `DbPoolConfig`
453
+
454
+ Connection pool settings shared by all configuration types.
455
+
456
+ ```typescript
457
+ import type { DbPoolConfig } from "@simplysm/orm-node";
458
+ ```
459
+
460
+ | Property | Type | Description |
461
+ |----------|------|-------------|
462
+ | `min` | `number` (optional) | Minimum connections in pool (default: `1`) |
463
+ | `max` | `number` (optional) | Maximum connections in pool (default: `10`) |
464
+ | `acquireTimeoutMillis` | `number` (optional) | Timeout to acquire a connection in ms (default: `30000`) |
465
+ | `idleTimeoutMillis` | `number` (optional) | Timeout before an idle connection is destroyed in ms (default: `30000`) |
466
+
467
+ ---
468
+
469
+ ### `getDialectFromConfig`
470
+
471
+ Utility function that normalizes `"mssql-azure"` to `"mssql"` and returns the `Dialect` value from a `DbConnConfig`.
472
+
473
+ ```typescript
474
+ import { getDialectFromConfig } from "@simplysm/orm-node";
475
+
476
+ const dialect = getDialectFromConfig({ dialect: "mssql-azure", ... }); // "mssql"
477
+ ```
478
+
479
+ **Signature**
480
+
481
+ ```typescript
482
+ function getDialectFromConfig(config: DbConnConfig): Dialect
483
+ ```
484
+
485
+ ---
28
486
 
29
- | Source | Exports | Description | Test |
30
- |--------|---------|-------------|------|
31
- | `src/create-db-conn.ts` | `createDbConn` | Factory function to create a dialect-specific database connection | `-` |
32
- | `src/node-db-context-executor.ts` | `NodeDbContextExecutor` | DbContextExecutor that runs queries directly on a local database connection | `-` |
33
- | `src/pooled-db-conn.ts` | `PooledDbConn` | Connection pool wrapper with configurable pool size and auto-release | `-` |
34
- | `src/create-orm.ts` | `OrmOptions`, `Orm`, `createOrm` | High-level ORM factory with connection pooling and migration support | `-` |
487
+ ### Constants
35
488
 
36
- ## License
489
+ | Name | Value | Description |
490
+ |------|-------|-------------|
491
+ | `DB_CONN_CONNECT_TIMEOUT` | `10000` | Connection establishment timeout in ms (10 seconds) |
492
+ | `DB_CONN_DEFAULT_TIMEOUT` | `600000` | Query default timeout in ms (10 minutes) |
493
+ | `DB_CONN_ERRORS` | `{ NOT_CONNECTED, ALREADY_CONNECTED }` | Standard connection error message strings |
37
494
 
38
- Apache-2.0
495
+ ```typescript
496
+ import { DB_CONN_CONNECT_TIMEOUT, DB_CONN_DEFAULT_TIMEOUT, DB_CONN_ERRORS } from "@simplysm/orm-node";
497
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simplysm/orm-node",
3
- "version": "13.0.72",
3
+ "version": "13.0.74",
4
4
  "description": "Simplysm package - ORM module (node)",
5
5
  "author": "simplysm",
6
6
  "license": "Apache-2.0",
@@ -20,11 +20,11 @@
20
20
  "dependencies": {
21
21
  "consola": "^3.4.2",
22
22
  "generic-pool": "^3.9.0",
23
- "@simplysm/core-common": "13.0.72",
24
- "@simplysm/orm-common": "13.0.72"
23
+ "@simplysm/orm-common": "13.0.74",
24
+ "@simplysm/core-common": "13.0.74"
25
25
  },
26
26
  "devDependencies": {
27
- "@types/pg": "^8.16.0",
27
+ "@types/pg": "^8.18.0",
28
28
  "@types/pg-copy-streams": "^1.2.5",
29
29
  "mysql2": "^3.18.2",
30
30
  "pg": "^8.19.0",