@simplysm/orm-node 13.0.78 → 13.0.81

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