@simplysm/orm-node 13.0.97 → 13.0.98

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 +249 -0
  2. package/package.json +4 -4
package/README.md ADDED
@@ -0,0 +1,249 @@
1
+ # @simplysm/orm-node
2
+
3
+ ORM module (node) -- database connections and ORM integration for MySQL, PostgreSQL, and MSSQL on Node.js.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @simplysm/orm-node
9
+ ```
10
+
11
+ ## API Overview
12
+
13
+ ### Types
14
+
15
+ | API | Type | Description |
16
+ |-----|------|-------------|
17
+ | `DB_CONN_CONNECT_TIMEOUT` | const | Connection establishment timeout (10 seconds) |
18
+ | `DB_CONN_DEFAULT_TIMEOUT` | const | Query default timeout (10 minutes) |
19
+ | `DB_CONN_ERRORS` | const | Error message constants (`NOT_CONNECTED`, `ALREADY_CONNECTED`) |
20
+ | `DbConn` | interface | Low-level DB connection interface |
21
+ | `DbConnConfig` | type | Union of all DB connection configs |
22
+ | `MysqlDbConnConfig` | interface | MySQL connection configuration |
23
+ | `MssqlDbConnConfig` | interface | MSSQL/Azure SQL connection configuration |
24
+ | `PostgresqlDbConnConfig` | interface | PostgreSQL connection configuration |
25
+ | `getDialectFromConfig` | function | Extract `Dialect` from a `DbConnConfig` |
26
+
27
+ ### Connections
28
+
29
+ | API | Type | Description |
30
+ |-----|------|-------------|
31
+ | `createDbConn` | function | Factory function to create a DB connection from config |
32
+ | `MysqlDbConn` | class | MySQL connection implementation (mysql2) |
33
+ | `PostgresqlDbConn` | class | PostgreSQL connection implementation (pg) |
34
+ | `MssqlDbConn` | class | MSSQL connection implementation (tedious) |
35
+
36
+ ### Core
37
+
38
+ | API | Type | Description |
39
+ |-----|------|-------------|
40
+ | `OrmOptions` | interface | ORM options (database name, schema override) |
41
+ | `Orm` | interface | ORM instance with `connect` and `connectWithoutTransaction` |
42
+ | `createOrm` | function | Create an ORM instance from a DbContext definition and config |
43
+ | `NodeDbContextExecutor` | class | DbContextExecutor for Node.js environment |
44
+
45
+ ## `DbConnConfig`
46
+
47
+ ```typescript
48
+ type DbConnConfig = MysqlDbConnConfig | MssqlDbConnConfig | PostgresqlDbConnConfig;
49
+ ```
50
+
51
+ ## `MysqlDbConnConfig`
52
+
53
+ ```typescript
54
+ interface MysqlDbConnConfig {
55
+ dialect: "mysql";
56
+ host: string;
57
+ port?: number;
58
+ username: string;
59
+ password: string;
60
+ database?: string;
61
+ defaultIsolationLevel?: IsolationLevel;
62
+ }
63
+ ```
64
+
65
+ ## `MssqlDbConnConfig`
66
+
67
+ ```typescript
68
+ interface MssqlDbConnConfig {
69
+ dialect: "mssql" | "mssql-azure";
70
+ host: string;
71
+ port?: number;
72
+ username: string;
73
+ password: string;
74
+ database?: string;
75
+ schema?: string;
76
+ defaultIsolationLevel?: IsolationLevel;
77
+ }
78
+ ```
79
+
80
+ ## `PostgresqlDbConnConfig`
81
+
82
+ ```typescript
83
+ interface PostgresqlDbConnConfig {
84
+ dialect: "postgresql";
85
+ host: string;
86
+ port?: number;
87
+ username: string;
88
+ password: string;
89
+ database?: string;
90
+ schema?: string;
91
+ defaultIsolationLevel?: IsolationLevel;
92
+ }
93
+ ```
94
+
95
+ ## `DbConn`
96
+
97
+ ```typescript
98
+ interface DbConn extends EventEmitter<{ close: void }> {
99
+ config: DbConnConfig;
100
+ isConnected: boolean;
101
+ isInTransaction: boolean;
102
+ connect(): Promise<void>;
103
+ close(): Promise<void>;
104
+ beginTransaction(isolationLevel?: IsolationLevel): Promise<void>;
105
+ commitTransaction(): Promise<void>;
106
+ rollbackTransaction(): Promise<void>;
107
+ execute(queries: string[]): Promise<Record<string, unknown>[][]>;
108
+ executeParametrized(query: string, params?: unknown[]): Promise<Record<string, unknown>[][]>;
109
+ bulkInsert(
110
+ tableName: string,
111
+ columnMetas: Record<string, ColumnMeta>,
112
+ records: Record<string, unknown>[],
113
+ ): Promise<void>;
114
+ }
115
+ ```
116
+
117
+ ## `getDialectFromConfig`
118
+
119
+ ```typescript
120
+ function getDialectFromConfig(config: DbConnConfig): Dialect;
121
+ ```
122
+
123
+ Returns the `Dialect` for a given config. Maps `"mssql-azure"` to `"mssql"`.
124
+
125
+ ## `createDbConn`
126
+
127
+ ```typescript
128
+ async function createDbConn(config: DbConnConfig): Promise<DbConn>;
129
+ ```
130
+
131
+ Factory function that creates a DB connection. Driver modules (mysql2, pg, tedious) are lazy-loaded. Returns an unconnected instance -- call `connect()` separately.
132
+
133
+ ## `OrmOptions`
134
+
135
+ ```typescript
136
+ interface OrmOptions {
137
+ database?: string;
138
+ schema?: string;
139
+ }
140
+ ```
141
+
142
+ ## `Orm`
143
+
144
+ ```typescript
145
+ interface Orm<TDef extends DbContextDef<any, any, any>> {
146
+ readonly dbContextDef: TDef;
147
+ readonly config: DbConnConfig;
148
+ readonly options?: OrmOptions;
149
+ connect<R>(
150
+ callback: (conn: DbContextInstance<TDef>) => Promise<R>,
151
+ isolationLevel?: IsolationLevel,
152
+ ): Promise<R>;
153
+ connectWithoutTransaction<R>(
154
+ callback: (conn: DbContextInstance<TDef>) => Promise<R>,
155
+ ): Promise<R>;
156
+ }
157
+ ```
158
+
159
+ ## `createOrm`
160
+
161
+ ```typescript
162
+ function createOrm<TDef extends DbContextDef<any, any, any>>(
163
+ dbContextDef: TDef,
164
+ config: DbConnConfig,
165
+ options?: OrmOptions,
166
+ ): Orm<TDef>;
167
+ ```
168
+
169
+ Node.js ORM factory. Creates an instance that manages DbContext and DB connections with automatic transaction handling.
170
+
171
+ ## `NodeDbContextExecutor`
172
+
173
+ ```typescript
174
+ class NodeDbContextExecutor implements DbContextExecutor {
175
+ constructor(config: DbConnConfig);
176
+ async connect(): Promise<void>;
177
+ async close(): Promise<void>;
178
+ async beginTransaction(isolationLevel?: IsolationLevel): Promise<void>;
179
+ async commitTransaction(): Promise<void>;
180
+ async rollbackTransaction(): Promise<void>;
181
+ async executeParametrized(query: string, params?: unknown[]): Promise<Record<string, unknown>[][]>;
182
+ async bulkInsert(
183
+ tableName: string,
184
+ columnMetas: Record<string, ColumnMeta>,
185
+ records: DataRecord[],
186
+ ): Promise<void>;
187
+ async executeDefs<T = DataRecord>(
188
+ defs: QueryDef[],
189
+ resultMetas?: (ResultMeta | undefined)[],
190
+ ): Promise<T[][]>;
191
+ }
192
+ ```
193
+
194
+ DbContextExecutor for Node.js. Handles actual DB connections and query execution including QueryDef-to-SQL conversion.
195
+
196
+ ## Usage Examples
197
+
198
+ ### Create ORM and run queries in a transaction
199
+
200
+ ```typescript
201
+ import { createOrm } from "@simplysm/orm-node";
202
+ import { defineDbContext, queryable } from "@simplysm/orm-common";
203
+
204
+ const MyDb = defineDbContext({
205
+ user: (db) => queryable(db, User),
206
+ });
207
+
208
+ const orm = createOrm(MyDb, {
209
+ dialect: "mysql",
210
+ host: "localhost",
211
+ port: 3306,
212
+ username: "root",
213
+ password: "password",
214
+ database: "mydb",
215
+ });
216
+
217
+ const users = await orm.connect(async (db) => {
218
+ return await db.user().execute();
219
+ });
220
+ ```
221
+
222
+ ### Use low-level DB connection
223
+
224
+ ```typescript
225
+ import { createDbConn } from "@simplysm/orm-node";
226
+
227
+ const conn = await createDbConn({
228
+ dialect: "postgresql",
229
+ host: "localhost",
230
+ username: "admin",
231
+ password: "secret",
232
+ database: "testdb",
233
+ });
234
+
235
+ await conn.connect();
236
+ try {
237
+ const results = await conn.execute(["SELECT * FROM users"]);
238
+ } finally {
239
+ await conn.close();
240
+ }
241
+ ```
242
+
243
+ ### Run without transaction
244
+
245
+ ```typescript
246
+ const result = await orm.connectWithoutTransaction(async (db) => {
247
+ return await db.user().execute();
248
+ });
249
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simplysm/orm-node",
3
- "version": "13.0.97",
3
+ "version": "13.0.98",
4
4
  "description": "Simplysm package - ORM module (node)",
5
5
  "author": "simplysm",
6
6
  "license": "Apache-2.0",
@@ -20,11 +20,11 @@
20
20
  "sideEffects": false,
21
21
  "dependencies": {
22
22
  "consola": "^3.4.2",
23
- "@simplysm/core-common": "13.0.97",
24
- "@simplysm/orm-common": "13.0.97"
23
+ "@simplysm/core-common": "13.0.98",
24
+ "@simplysm/orm-common": "13.0.98"
25
25
  },
26
26
  "devDependencies": {
27
- "@types/pg": "^8.18.0",
27
+ "@types/pg": "^8.20.0",
28
28
  "@types/pg-copy-streams": "^1.2.5",
29
29
  "mysql2": "^3.20.0",
30
30
  "pg": "^8.20.0",