@simplysm/orm-node 13.0.0-beta.6
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/.cache/typecheck-node.tsbuildinfo +1 -0
- package/README.md +418 -0
- package/dist/connections/mssql-db-conn.js +386 -0
- package/dist/connections/mssql-db-conn.js.map +7 -0
- package/dist/connections/mysql-db-conn.js +227 -0
- package/dist/connections/mysql-db-conn.js.map +7 -0
- package/dist/connections/postgresql-db-conn.js +191 -0
- package/dist/connections/postgresql-db-conn.js.map +7 -0
- package/dist/core-common/src/common.types.d.ts +74 -0
- package/dist/core-common/src/common.types.d.ts.map +1 -0
- package/dist/core-common/src/env.d.ts +6 -0
- package/dist/core-common/src/env.d.ts.map +1 -0
- package/dist/core-common/src/errors/argument-error.d.ts +25 -0
- package/dist/core-common/src/errors/argument-error.d.ts.map +1 -0
- package/dist/core-common/src/errors/not-implemented-error.d.ts +29 -0
- package/dist/core-common/src/errors/not-implemented-error.d.ts.map +1 -0
- package/dist/core-common/src/errors/sd-error.d.ts +27 -0
- package/dist/core-common/src/errors/sd-error.d.ts.map +1 -0
- package/dist/core-common/src/errors/timeout-error.d.ts +31 -0
- package/dist/core-common/src/errors/timeout-error.d.ts.map +1 -0
- package/dist/core-common/src/extensions/arr-ext.d.ts +15 -0
- package/dist/core-common/src/extensions/arr-ext.d.ts.map +1 -0
- package/dist/core-common/src/extensions/arr-ext.helpers.d.ts +19 -0
- package/dist/core-common/src/extensions/arr-ext.helpers.d.ts.map +1 -0
- package/dist/core-common/src/extensions/arr-ext.types.d.ts +215 -0
- package/dist/core-common/src/extensions/arr-ext.types.d.ts.map +1 -0
- package/dist/core-common/src/extensions/map-ext.d.ts +57 -0
- package/dist/core-common/src/extensions/map-ext.d.ts.map +1 -0
- package/dist/core-common/src/extensions/set-ext.d.ts +36 -0
- package/dist/core-common/src/extensions/set-ext.d.ts.map +1 -0
- package/dist/core-common/src/features/debounce-queue.d.ts +53 -0
- package/dist/core-common/src/features/debounce-queue.d.ts.map +1 -0
- package/dist/core-common/src/features/event-emitter.d.ts +66 -0
- package/dist/core-common/src/features/event-emitter.d.ts.map +1 -0
- package/dist/core-common/src/features/serial-queue.d.ts +47 -0
- package/dist/core-common/src/features/serial-queue.d.ts.map +1 -0
- package/dist/core-common/src/index.d.ts +32 -0
- package/dist/core-common/src/index.d.ts.map +1 -0
- package/dist/core-common/src/types/date-only.d.ts +152 -0
- package/dist/core-common/src/types/date-only.d.ts.map +1 -0
- package/dist/core-common/src/types/date-time.d.ts +96 -0
- package/dist/core-common/src/types/date-time.d.ts.map +1 -0
- package/dist/core-common/src/types/lazy-gc-map.d.ts +80 -0
- package/dist/core-common/src/types/lazy-gc-map.d.ts.map +1 -0
- package/dist/core-common/src/types/time.d.ts +68 -0
- package/dist/core-common/src/types/time.d.ts.map +1 -0
- package/dist/core-common/src/types/uuid.d.ts +35 -0
- package/dist/core-common/src/types/uuid.d.ts.map +1 -0
- package/dist/core-common/src/utils/bytes.d.ts +51 -0
- package/dist/core-common/src/utils/bytes.d.ts.map +1 -0
- package/dist/core-common/src/utils/date-format.d.ts +90 -0
- package/dist/core-common/src/utils/date-format.d.ts.map +1 -0
- package/dist/core-common/src/utils/json.d.ts +34 -0
- package/dist/core-common/src/utils/json.d.ts.map +1 -0
- package/dist/core-common/src/utils/num.d.ts +60 -0
- package/dist/core-common/src/utils/num.d.ts.map +1 -0
- package/dist/core-common/src/utils/obj.d.ts +258 -0
- package/dist/core-common/src/utils/obj.d.ts.map +1 -0
- package/dist/core-common/src/utils/path.d.ts +23 -0
- package/dist/core-common/src/utils/path.d.ts.map +1 -0
- package/dist/core-common/src/utils/primitive.d.ts +18 -0
- package/dist/core-common/src/utils/primitive.d.ts.map +1 -0
- package/dist/core-common/src/utils/str.d.ts +103 -0
- package/dist/core-common/src/utils/str.d.ts.map +1 -0
- package/dist/core-common/src/utils/template-strings.d.ts +84 -0
- package/dist/core-common/src/utils/template-strings.d.ts.map +1 -0
- package/dist/core-common/src/utils/transferable.d.ts +47 -0
- package/dist/core-common/src/utils/transferable.d.ts.map +1 -0
- package/dist/core-common/src/utils/wait.d.ts +19 -0
- package/dist/core-common/src/utils/wait.d.ts.map +1 -0
- package/dist/core-common/src/utils/xml.d.ts +36 -0
- package/dist/core-common/src/utils/xml.d.ts.map +1 -0
- package/dist/core-common/src/zip/sd-zip.d.ts +80 -0
- package/dist/core-common/src/zip/sd-zip.d.ts.map +1 -0
- package/dist/db-conn-factory.js +88 -0
- package/dist/db-conn-factory.js.map +7 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +7 -0
- package/dist/node-db-context-executor.js +129 -0
- package/dist/node-db-context-executor.js.map +7 -0
- package/dist/orm-common/src/db-context.d.ts +669 -0
- package/dist/orm-common/src/db-context.d.ts.map +1 -0
- package/dist/orm-common/src/errors/db-transaction-error.d.ts +51 -0
- package/dist/orm-common/src/errors/db-transaction-error.d.ts.map +1 -0
- package/dist/orm-common/src/exec/executable.d.ts +79 -0
- package/dist/orm-common/src/exec/executable.d.ts.map +1 -0
- package/dist/orm-common/src/exec/queryable.d.ts +708 -0
- package/dist/orm-common/src/exec/queryable.d.ts.map +1 -0
- package/dist/orm-common/src/exec/search-parser.d.ts +72 -0
- package/dist/orm-common/src/exec/search-parser.d.ts.map +1 -0
- package/dist/orm-common/src/expr/expr-unit.d.ts +25 -0
- package/dist/orm-common/src/expr/expr-unit.d.ts.map +1 -0
- package/dist/orm-common/src/expr/expr.d.ts +1369 -0
- package/dist/orm-common/src/expr/expr.d.ts.map +1 -0
- package/dist/orm-common/src/index.d.ts +32 -0
- package/dist/orm-common/src/index.d.ts.map +1 -0
- package/dist/orm-common/src/models/system-migration.d.ts +10 -0
- package/dist/orm-common/src/models/system-migration.d.ts.map +1 -0
- package/dist/orm-common/src/query-builder/base/expr-renderer-base.d.ts +95 -0
- package/dist/orm-common/src/query-builder/base/expr-renderer-base.d.ts.map +1 -0
- package/dist/orm-common/src/query-builder/base/query-builder-base.d.ts +66 -0
- package/dist/orm-common/src/query-builder/base/query-builder-base.d.ts.map +1 -0
- package/dist/orm-common/src/query-builder/mssql/mssql-expr-renderer.d.ts +84 -0
- package/dist/orm-common/src/query-builder/mssql/mssql-expr-renderer.d.ts.map +1 -0
- package/dist/orm-common/src/query-builder/mssql/mssql-query-builder.d.ts +45 -0
- package/dist/orm-common/src/query-builder/mssql/mssql-query-builder.d.ts.map +1 -0
- package/dist/orm-common/src/query-builder/mysql/mysql-expr-renderer.d.ts +84 -0
- package/dist/orm-common/src/query-builder/mysql/mysql-expr-renderer.d.ts.map +1 -0
- package/dist/orm-common/src/query-builder/mysql/mysql-query-builder.d.ts +54 -0
- package/dist/orm-common/src/query-builder/mysql/mysql-query-builder.d.ts.map +1 -0
- package/dist/orm-common/src/query-builder/postgresql/postgresql-expr-renderer.d.ts +84 -0
- package/dist/orm-common/src/query-builder/postgresql/postgresql-expr-renderer.d.ts.map +1 -0
- package/dist/orm-common/src/query-builder/postgresql/postgresql-query-builder.d.ts +52 -0
- package/dist/orm-common/src/query-builder/postgresql/postgresql-query-builder.d.ts.map +1 -0
- package/dist/orm-common/src/query-builder/query-builder.d.ts +7 -0
- package/dist/orm-common/src/query-builder/query-builder.d.ts.map +1 -0
- package/dist/orm-common/src/schema/factory/column-builder.d.ts +394 -0
- package/dist/orm-common/src/schema/factory/column-builder.d.ts.map +1 -0
- package/dist/orm-common/src/schema/factory/index-builder.d.ts +151 -0
- package/dist/orm-common/src/schema/factory/index-builder.d.ts.map +1 -0
- package/dist/orm-common/src/schema/factory/relation-builder.d.ts +337 -0
- package/dist/orm-common/src/schema/factory/relation-builder.d.ts.map +1 -0
- package/dist/orm-common/src/schema/procedure-builder.d.ts +202 -0
- package/dist/orm-common/src/schema/procedure-builder.d.ts.map +1 -0
- package/dist/orm-common/src/schema/table-builder.d.ts +259 -0
- package/dist/orm-common/src/schema/table-builder.d.ts.map +1 -0
- package/dist/orm-common/src/schema/view-builder.d.ts +183 -0
- package/dist/orm-common/src/schema/view-builder.d.ts.map +1 -0
- package/dist/orm-common/src/types/column.d.ts +172 -0
- package/dist/orm-common/src/types/column.d.ts.map +1 -0
- package/dist/orm-common/src/types/db.d.ts +175 -0
- package/dist/orm-common/src/types/db.d.ts.map +1 -0
- package/dist/orm-common/src/types/expr.d.ts +474 -0
- package/dist/orm-common/src/types/expr.d.ts.map +1 -0
- package/dist/orm-common/src/types/query-def.d.ts +351 -0
- package/dist/orm-common/src/types/query-def.d.ts.map +1 -0
- package/dist/orm-common/src/utils/result-parser.d.ts +38 -0
- package/dist/orm-common/src/utils/result-parser.d.ts.map +1 -0
- package/dist/orm-node/src/connections/mssql-db-conn.d.ts +44 -0
- package/dist/orm-node/src/connections/mssql-db-conn.d.ts.map +1 -0
- package/dist/orm-node/src/connections/mysql-db-conn.d.ts +38 -0
- package/dist/orm-node/src/connections/mysql-db-conn.d.ts.map +1 -0
- package/dist/orm-node/src/connections/postgresql-db-conn.d.ts +39 -0
- package/dist/orm-node/src/connections/postgresql-db-conn.d.ts.map +1 -0
- package/dist/orm-node/src/db-conn-factory.d.ts +25 -0
- package/dist/orm-node/src/db-conn-factory.d.ts.map +1 -0
- package/dist/orm-node/src/index.d.ts +9 -0
- package/dist/orm-node/src/index.d.ts.map +1 -0
- package/dist/orm-node/src/node-db-context-executor.d.ts +77 -0
- package/dist/orm-node/src/node-db-context-executor.d.ts.map +1 -0
- package/dist/orm-node/src/pooled-db-conn.d.ts +79 -0
- package/dist/orm-node/src/pooled-db-conn.d.ts.map +1 -0
- package/dist/orm-node/src/sd-orm.d.ts +78 -0
- package/dist/orm-node/src/sd-orm.d.ts.map +1 -0
- package/dist/orm-node/src/types/db-conn.d.ts +159 -0
- package/dist/orm-node/src/types/db-conn.d.ts.map +1 -0
- package/dist/pooled-db-conn.js +134 -0
- package/dist/pooled-db-conn.js.map +7 -0
- package/dist/sd-orm.js +44 -0
- package/dist/sd-orm.js.map +7 -0
- package/dist/types/db-conn.js +17 -0
- package/dist/types/db-conn.js.map +7 -0
- package/package.json +50 -0
- package/src/connections/mssql-db-conn.ts +483 -0
- package/src/connections/mysql-db-conn.ts +299 -0
- package/src/connections/postgresql-db-conn.ts +254 -0
- package/src/db-conn-factory.ts +114 -0
- package/src/index.ts +13 -0
- package/src/node-db-context-executor.ts +162 -0
- package/src/pooled-db-conn.ts +175 -0
- package/src/sd-orm.ts +102 -0
- package/src/types/db-conn.ts +196 -0
package/README.md
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
# @simplysm/orm-node
|
|
2
|
+
|
|
3
|
+
The Node.js implementation module of Simplysm ORM, responsible for actual database connections, query execution, transaction management, and connection pooling for MySQL, MSSQL (SQL Server), and PostgreSQL. This is the layer that directly communicates with databases in a Node.js environment, based on the schemas and query builders defined in `@simplysm/orm-common`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @simplysm/orm-node
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @simplysm/orm-node
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Database-Specific Driver Installation
|
|
14
|
+
|
|
15
|
+
You must additionally install the driver for your database. Drivers not in use don't need to be installed.
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# MySQL
|
|
19
|
+
npm install mysql2
|
|
20
|
+
|
|
21
|
+
# SQL Server (MSSQL)
|
|
22
|
+
npm install tedious
|
|
23
|
+
|
|
24
|
+
# PostgreSQL
|
|
25
|
+
npm install pg pg-copy-streams
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Architecture
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
SdOrm (top-level entry point)
|
|
32
|
+
└── NodeDbContextExecutor (executor between DbContext and actual DB)
|
|
33
|
+
└── DbConnFactory (connection creation and pool management)
|
|
34
|
+
└── PooledDbConn (connection pool wrapper)
|
|
35
|
+
└── MysqlDbConn / MssqlDbConn / PostgresqlDbConn (DBMS-specific low-level connections)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
- `SdOrm` is the top-level class that takes a `DbContext` type and connection settings to manage transactions.
|
|
39
|
+
- `NodeDbContextExecutor` is the executor used by `DbContext`, converting `QueryDef` to SQL and executing it.
|
|
40
|
+
- `DbConnFactory` is a factory that acquires connections from the connection pool.
|
|
41
|
+
- `PooledDbConn` is a connection pool wrapper based on `generic-pool`, returning connections to the pool instead of closing them after use.
|
|
42
|
+
- Each DBMS-specific connection class (`MysqlDbConn`, `MssqlDbConn`, `PostgresqlDbConn`) directly uses low-level DB drivers.
|
|
43
|
+
|
|
44
|
+
## Core Modules
|
|
45
|
+
|
|
46
|
+
### Classes
|
|
47
|
+
|
|
48
|
+
| Class | Description |
|
|
49
|
+
|--------|------|
|
|
50
|
+
| `SdOrm` | ORM top-level class. Takes `DbContext` type and connection settings to manage transaction-based connections. |
|
|
51
|
+
| `NodeDbContextExecutor` | `DbContextExecutor` implementation. Converts `QueryDef` to SQL, executes it, and parses results. |
|
|
52
|
+
| `DbConnFactory` | Connection factory. Caches connection pools by configuration and returns `PooledDbConn`. |
|
|
53
|
+
| `PooledDbConn` | Connection pool wrapper. Acquires/returns physical connections from `generic-pool`, implements `DbConn` interface. |
|
|
54
|
+
| `MysqlDbConn` | MySQL connection class. Uses `mysql2/promise` driver. |
|
|
55
|
+
| `MssqlDbConn` | MSSQL/Azure SQL connection class. Uses `tedious` driver. |
|
|
56
|
+
| `PostgresqlDbConn` | PostgreSQL connection class. Uses `pg` and `pg-copy-streams` drivers. |
|
|
57
|
+
|
|
58
|
+
### Interfaces and Types
|
|
59
|
+
|
|
60
|
+
| Type | Description |
|
|
61
|
+
|------|------|
|
|
62
|
+
| `DbConn` | Low-level DB connection interface. Implemented by all DBMS-specific connection classes. |
|
|
63
|
+
| `DbConnConfig` | DB connection config union type (`MysqlDbConnConfig \| MssqlDbConnConfig \| PostgresqlDbConnConfig`). |
|
|
64
|
+
| `MysqlDbConnConfig` | MySQL connection config. `dialect: "mysql"`. |
|
|
65
|
+
| `MssqlDbConnConfig` | MSSQL connection config. `dialect: "mssql" \| "mssql-azure"`. |
|
|
66
|
+
| `PostgresqlDbConnConfig` | PostgreSQL connection config. `dialect: "postgresql"`. |
|
|
67
|
+
| `DbPoolConfig` | Connection pool config (`min`, `max`, `acquireTimeoutMillis`, `idleTimeoutMillis`). |
|
|
68
|
+
| `SdOrmOptions` | `SdOrm` options. `database`, `schema` settings that override `DbConnConfig`. |
|
|
69
|
+
|
|
70
|
+
### Constants and Utility Functions
|
|
71
|
+
|
|
72
|
+
| Name | Description |
|
|
73
|
+
|------|------|
|
|
74
|
+
| `DB_CONN_DEFAULT_TIMEOUT` | DB connection default timeout (10 minutes, 600000ms). |
|
|
75
|
+
| `DB_CONN_ERRORS` | DB connection error message constants (`NOT_CONNECTED`, `ALREADY_CONNECTED`). |
|
|
76
|
+
| `getDialectFromConfig(config)` | Extract `Dialect` from `DbConnConfig`. `"mssql-azure"` is converted to `"mssql"`. |
|
|
77
|
+
|
|
78
|
+
## Usage
|
|
79
|
+
|
|
80
|
+
### Basic Usage with SdOrm
|
|
81
|
+
|
|
82
|
+
`SdOrm` is the top-level entry point used with `DbContext`. It automatically handles transaction management.
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { SdOrm } from "@simplysm/orm-node";
|
|
86
|
+
import { DbContext, queryable, Table } from "@simplysm/orm-common";
|
|
87
|
+
|
|
88
|
+
// 1. Define table
|
|
89
|
+
const User = Table("User")
|
|
90
|
+
.database("mydb")
|
|
91
|
+
.columns((c) => ({
|
|
92
|
+
id: c.bigint().autoIncrement(),
|
|
93
|
+
name: c.varchar(100),
|
|
94
|
+
email: c.varchar(200).nullable(),
|
|
95
|
+
}))
|
|
96
|
+
.primaryKey("id");
|
|
97
|
+
|
|
98
|
+
// 2. Define DbContext
|
|
99
|
+
class MyDb extends DbContext {
|
|
100
|
+
readonly user = queryable(this, User);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 3. Create SdOrm instance
|
|
104
|
+
const orm = new SdOrm(MyDb, {
|
|
105
|
+
dialect: "mysql",
|
|
106
|
+
host: "localhost",
|
|
107
|
+
port: 3306,
|
|
108
|
+
username: "root",
|
|
109
|
+
password: "password",
|
|
110
|
+
database: "mydb",
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// 4. Execute queries within transaction (commits on success, rolls back on failure)
|
|
114
|
+
const users = await orm.connect(async (db) => {
|
|
115
|
+
return await db.user().result();
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Transaction Management
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
// Execute within transaction (auto commit/rollback)
|
|
123
|
+
await orm.connect(async (db) => {
|
|
124
|
+
await db.user().insert([
|
|
125
|
+
{ name: "John Doe", email: "john@example.com" },
|
|
126
|
+
{ name: "Jane Smith", email: "jane@example.com" },
|
|
127
|
+
]);
|
|
128
|
+
// Commits if callback completes successfully
|
|
129
|
+
// Auto rollback if exception occurs
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Specify isolation level
|
|
133
|
+
await orm.connect(async (db) => {
|
|
134
|
+
const users = await db.user().result();
|
|
135
|
+
return users;
|
|
136
|
+
}, "SERIALIZABLE");
|
|
137
|
+
|
|
138
|
+
// Execute without transaction (for DDL operations, etc.)
|
|
139
|
+
await orm.connectWithoutTransaction(async (db) => {
|
|
140
|
+
const users = await db.user().result();
|
|
141
|
+
return users;
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Supported isolation levels (`IsolationLevel`):
|
|
146
|
+
- `"READ_UNCOMMITTED"`
|
|
147
|
+
- `"READ_COMMITTED"`
|
|
148
|
+
- `"REPEATABLE_READ"`
|
|
149
|
+
- `"SERIALIZABLE"`
|
|
150
|
+
|
|
151
|
+
### Overriding database/schema via SdOrmOptions
|
|
152
|
+
|
|
153
|
+
Using `SdOrmOptions`, you can use different values instead of the `database`/`schema` set in `DbConnConfig`.
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
const orm = new SdOrm(MyDb, {
|
|
157
|
+
dialect: "postgresql",
|
|
158
|
+
host: "localhost",
|
|
159
|
+
port: 5432,
|
|
160
|
+
username: "postgres",
|
|
161
|
+
password: "password",
|
|
162
|
+
database: "default_db", // Default DB used for connection
|
|
163
|
+
schema: "public",
|
|
164
|
+
}, {
|
|
165
|
+
database: "app_db", // DB to use in DbContext (takes precedence)
|
|
166
|
+
schema: "app_schema", // Schema to use in DbContext (takes precedence)
|
|
167
|
+
});
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Connection Pool Configuration
|
|
171
|
+
|
|
172
|
+
Configure connection pool via the `pool` field in `DbConnConfig`. The pool is based on the `generic-pool` library, and pools are automatically cached for identical configurations.
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
const orm = new SdOrm(MyDb, {
|
|
176
|
+
dialect: "mssql",
|
|
177
|
+
host: "localhost",
|
|
178
|
+
port: 1433,
|
|
179
|
+
username: "sa",
|
|
180
|
+
password: "password",
|
|
181
|
+
database: "mydb",
|
|
182
|
+
pool: {
|
|
183
|
+
min: 2, // Minimum connections (default: 1)
|
|
184
|
+
max: 20, // Maximum connections (default: 10)
|
|
185
|
+
acquireTimeoutMillis: 60000, // Connection acquisition timeout (default: 30000ms)
|
|
186
|
+
idleTimeoutMillis: 60000, // Idle connection timeout (default: 30000ms)
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Low-Level Connection with DbConnFactory
|
|
192
|
+
|
|
193
|
+
You can connect directly to the DB and execute SQL without `SdOrm`/`DbContext`. `DbConnFactory.create()` returns `PooledDbConn` from the connection pool.
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
import { DbConnFactory } from "@simplysm/orm-node";
|
|
197
|
+
|
|
198
|
+
// Create connection (acquire from pool)
|
|
199
|
+
const conn = await DbConnFactory.create({
|
|
200
|
+
dialect: "mysql",
|
|
201
|
+
host: "localhost",
|
|
202
|
+
port: 3306,
|
|
203
|
+
username: "root",
|
|
204
|
+
password: "password",
|
|
205
|
+
database: "mydb",
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
// Connect
|
|
209
|
+
await conn.connect();
|
|
210
|
+
|
|
211
|
+
try {
|
|
212
|
+
// Execute SQL
|
|
213
|
+
const results = await conn.execute(["SELECT * FROM User WHERE id = 1"]);
|
|
214
|
+
console.log(results); // [[{ id: 1, name: "John Doe", ... }]]
|
|
215
|
+
|
|
216
|
+
// Manual transaction management
|
|
217
|
+
await conn.beginTransaction("READ_COMMITTED");
|
|
218
|
+
await conn.execute(["INSERT INTO User (name) VALUES ('Jane Smith')"]);
|
|
219
|
+
await conn.commitTransaction();
|
|
220
|
+
} catch (err) {
|
|
221
|
+
if (conn.isOnTransaction) {
|
|
222
|
+
await conn.rollbackTransaction();
|
|
223
|
+
}
|
|
224
|
+
throw err;
|
|
225
|
+
} finally {
|
|
226
|
+
// Return connection (returns to pool, not actual close)
|
|
227
|
+
await conn.close();
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Parameterized Query Execution
|
|
232
|
+
|
|
233
|
+
Each connection class supports parameter binding via the `executeParametrized()` method.
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
const conn = await DbConnFactory.create({
|
|
237
|
+
dialect: "postgresql",
|
|
238
|
+
host: "localhost",
|
|
239
|
+
port: 5432,
|
|
240
|
+
username: "postgres",
|
|
241
|
+
password: "password",
|
|
242
|
+
database: "mydb",
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
await conn.connect();
|
|
246
|
+
|
|
247
|
+
// Parameterized query (uses DBMS-specific placeholders like $1, $2)
|
|
248
|
+
const results = await conn.executeParametrized(
|
|
249
|
+
"SELECT * FROM \"User\" WHERE name = $1",
|
|
250
|
+
["John Doe"],
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
await conn.close();
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Bulk INSERT
|
|
257
|
+
|
|
258
|
+
Supports bulk data insertion using native bulk APIs for each DBMS.
|
|
259
|
+
|
|
260
|
+
| DBMS | Bulk Method |
|
|
261
|
+
|------|----------|
|
|
262
|
+
| MySQL | `LOAD DATA LOCAL INFILE` (temporary CSV file) |
|
|
263
|
+
| MSSQL | tedious `BulkLoad` API |
|
|
264
|
+
| PostgreSQL | `COPY FROM STDIN` (pg-copy-streams) |
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
import type { ColumnMeta } from "@simplysm/orm-common";
|
|
268
|
+
|
|
269
|
+
const conn = await DbConnFactory.create({
|
|
270
|
+
dialect: "mysql",
|
|
271
|
+
host: "localhost",
|
|
272
|
+
port: 3306,
|
|
273
|
+
username: "root",
|
|
274
|
+
password: "password",
|
|
275
|
+
database: "mydb",
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
await conn.connect();
|
|
279
|
+
|
|
280
|
+
// Define column metadata
|
|
281
|
+
const columnMetas: Record<string, ColumnMeta> = {
|
|
282
|
+
name: { dataType: { type: "varchar", length: 100 } },
|
|
283
|
+
email: { dataType: { type: "varchar", length: 200 }, nullable: true },
|
|
284
|
+
age: { dataType: { type: "int" } },
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
// Insert bulk records
|
|
288
|
+
const records = [
|
|
289
|
+
{ name: "John Doe", email: "john@example.com", age: 30 },
|
|
290
|
+
{ name: "Jane Smith", email: "jane@example.com", age: 25 },
|
|
291
|
+
// ... thousands of records
|
|
292
|
+
];
|
|
293
|
+
|
|
294
|
+
await conn.bulkInsert("mydb.User", columnMetas, records);
|
|
295
|
+
await conn.close();
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### DBMS-Specific Connection Configuration
|
|
299
|
+
|
|
300
|
+
#### MySQL
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
const mysqlConfig: MysqlDbConnConfig = {
|
|
304
|
+
dialect: "mysql",
|
|
305
|
+
host: "localhost",
|
|
306
|
+
port: 3306, // Optional (default: 3306)
|
|
307
|
+
username: "root",
|
|
308
|
+
password: "password",
|
|
309
|
+
database: "mydb", // Optional
|
|
310
|
+
defaultIsolationLevel: "READ_UNCOMMITTED", // Optional (default isolation level)
|
|
311
|
+
pool: { min: 1, max: 10 }, // Optional (connection pool)
|
|
312
|
+
};
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
MySQL connection characteristics:
|
|
316
|
+
- `multipleStatements: true` -- Can execute multiple SQL statements in one request
|
|
317
|
+
- `charset: "utf8mb4"` -- Supports 4-byte characters like emojis
|
|
318
|
+
- `LOAD DATA LOCAL INFILE` support (for bulk INSERT)
|
|
319
|
+
- `root` user can connect without binding to a specific database and access all DBs
|
|
320
|
+
|
|
321
|
+
#### MSSQL / Azure SQL
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
const mssqlConfig: MssqlDbConnConfig = {
|
|
325
|
+
dialect: "mssql", // Or "mssql-azure" (for Azure SQL Database)
|
|
326
|
+
host: "localhost",
|
|
327
|
+
port: 1433, // Optional
|
|
328
|
+
username: "sa",
|
|
329
|
+
password: "password",
|
|
330
|
+
database: "mydb", // Optional
|
|
331
|
+
schema: "dbo", // Optional (MSSQL schema)
|
|
332
|
+
defaultIsolationLevel: "READ_UNCOMMITTED", // Optional
|
|
333
|
+
pool: { min: 1, max: 10 }, // Optional
|
|
334
|
+
};
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
MSSQL connection characteristics:
|
|
338
|
+
- `encrypt: true` automatically set when using `"mssql-azure"` dialect
|
|
339
|
+
- `trustServerCertificate: true` default setting
|
|
340
|
+
- `useUTC: false` -- Uses local timezone
|
|
341
|
+
|
|
342
|
+
#### PostgreSQL
|
|
343
|
+
|
|
344
|
+
```typescript
|
|
345
|
+
const pgConfig: PostgresqlDbConnConfig = {
|
|
346
|
+
dialect: "postgresql",
|
|
347
|
+
host: "localhost",
|
|
348
|
+
port: 5432, // Optional (default: 5432)
|
|
349
|
+
username: "postgres",
|
|
350
|
+
password: "password",
|
|
351
|
+
database: "mydb", // Optional
|
|
352
|
+
schema: "public", // Optional (PostgreSQL schema)
|
|
353
|
+
defaultIsolationLevel: "READ_UNCOMMITTED", // Optional
|
|
354
|
+
pool: { min: 1, max: 10 }, // Optional
|
|
355
|
+
};
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
## DbConn Interface
|
|
359
|
+
|
|
360
|
+
The common interface implemented by all DBMS-specific connection classes (`MysqlDbConn`, `MssqlDbConn`, `PostgresqlDbConn`) and `PooledDbConn`.
|
|
361
|
+
|
|
362
|
+
| Method/Property | Signature | Description |
|
|
363
|
+
|------------|----------|------|
|
|
364
|
+
| `config` | `DbConnConfig` | Connection config (read-only) |
|
|
365
|
+
| `isConnected` | `boolean` | Connection status |
|
|
366
|
+
| `isOnTransaction` | `boolean` | Transaction in progress |
|
|
367
|
+
| `connect()` | `() => Promise<void>` | Establish DB connection |
|
|
368
|
+
| `close()` | `() => Promise<void>` | Close DB connection (PooledDbConn returns to pool) |
|
|
369
|
+
| `beginTransaction()` | `(isolationLevel?: IsolationLevel) => Promise<void>` | Start transaction |
|
|
370
|
+
| `commitTransaction()` | `() => Promise<void>` | Commit transaction |
|
|
371
|
+
| `rollbackTransaction()` | `() => Promise<void>` | Rollback transaction |
|
|
372
|
+
| `execute()` | `(queries: string[]) => Promise<unknown[][]>` | Execute SQL query array |
|
|
373
|
+
| `executeParametrized()` | `(query: string, params?: unknown[]) => Promise<unknown[][]>` | Execute parameterized query |
|
|
374
|
+
| `bulkInsert()` | `(tableName: string, columnMetas: Record<string, ColumnMeta>, records: Record<string, unknown>[]) => Promise<void>` | Native bulk INSERT |
|
|
375
|
+
|
|
376
|
+
`DbConn` extends `EventEmitter<{ close: void }>`, so you can listen for connection close events with `on("close", handler)` / `off("close", handler)`.
|
|
377
|
+
|
|
378
|
+
## Supported Databases
|
|
379
|
+
|
|
380
|
+
| Database | Driver Package | dialect Value | Minimum Version |
|
|
381
|
+
|-------------|----------------|------------|----------|
|
|
382
|
+
| MySQL | `mysql2` | `"mysql"` | 8.0.14+ |
|
|
383
|
+
| SQL Server | `tedious` | `"mssql"` | 2012+ |
|
|
384
|
+
| Azure SQL Database | `tedious` | `"mssql-azure"` | - |
|
|
385
|
+
| PostgreSQL | `pg`, `pg-copy-streams` | `"postgresql"` | 9.0+ |
|
|
386
|
+
|
|
387
|
+
## Notes
|
|
388
|
+
|
|
389
|
+
### Timeouts
|
|
390
|
+
|
|
391
|
+
- Default connection timeout is 10 minutes (`DB_CONN_DEFAULT_TIMEOUT = 600000ms`).
|
|
392
|
+
- Connections are automatically closed if idle for more than twice the timeout (20 minutes).
|
|
393
|
+
- Connection pool's `acquireTimeoutMillis` (default 30s) and `idleTimeoutMillis` (default 30s) operate separately.
|
|
394
|
+
|
|
395
|
+
### SQL Injection Security
|
|
396
|
+
|
|
397
|
+
`@simplysm/orm-common` uses string escaping instead of parameter binding due to its dynamic query nature. Therefore, when passing user input to ORM queries, you must perform input validation at the application level. Refer to the "ORM Security Guide" in `CLAUDE.md` at the project root for details.
|
|
398
|
+
|
|
399
|
+
### Driver Lazy Loading
|
|
400
|
+
|
|
401
|
+
DBMS-specific drivers (`mysql2`, `tedious`, `pg`) are lazy-loaded within `DbConnFactory`. Therefore, import errors won't occur even if unused drivers are not installed.
|
|
402
|
+
|
|
403
|
+
### PooledDbConn close Behavior
|
|
404
|
+
|
|
405
|
+
`PooledDbConn.close()` returns the connection to the pool instead of closing the actual physical connection. If `close()` is called while a transaction is in progress, it automatically attempts to rollback before returning to the pool.
|
|
406
|
+
|
|
407
|
+
## Optional Peer Dependencies
|
|
408
|
+
|
|
409
|
+
| Package | Purpose |
|
|
410
|
+
|--------|------|
|
|
411
|
+
| `mysql2` | MySQL driver |
|
|
412
|
+
| `tedious` | MSSQL driver |
|
|
413
|
+
| `pg` | PostgreSQL driver |
|
|
414
|
+
| `pg-copy-streams` | PostgreSQL bulk COPY support |
|
|
415
|
+
|
|
416
|
+
## License
|
|
417
|
+
|
|
418
|
+
Apache-2.0
|