@simplysm/orm-common 13.0.99 → 14.0.1
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/dist/create-db-context.d.ts +10 -10
- package/dist/create-db-context.js +312 -276
- package/dist/create-db-context.js.map +1 -6
- package/dist/ddl/column-ddl.d.ts +4 -4
- package/dist/ddl/column-ddl.js +41 -35
- package/dist/ddl/column-ddl.js.map +1 -6
- package/dist/ddl/initialize.d.ts +17 -17
- package/dist/ddl/initialize.js +200 -142
- package/dist/ddl/initialize.js.map +1 -6
- package/dist/ddl/relation-ddl.d.ts +6 -6
- package/dist/ddl/relation-ddl.js +55 -48
- package/dist/ddl/relation-ddl.js.map +1 -6
- package/dist/ddl/schema-ddl.d.ts +4 -4
- package/dist/ddl/schema-ddl.js +21 -15
- package/dist/ddl/schema-ddl.js.map +1 -6
- package/dist/ddl/table-ddl.d.ts +20 -20
- package/dist/ddl/table-ddl.js +139 -93
- package/dist/ddl/table-ddl.js.map +1 -6
- package/dist/define-db-context.js +10 -13
- package/dist/define-db-context.js.map +1 -6
- package/dist/errors/db-transaction-error.d.ts +15 -15
- package/dist/errors/db-transaction-error.d.ts.map +1 -1
- package/dist/errors/db-transaction-error.js +53 -19
- package/dist/errors/db-transaction-error.js.map +1 -6
- package/dist/exec/executable.d.ts +23 -23
- package/dist/exec/executable.js +94 -40
- package/dist/exec/executable.js.map +1 -6
- package/dist/exec/queryable.d.ts +97 -97
- package/dist/exec/queryable.js +1310 -1204
- package/dist/exec/queryable.js.map +1 -6
- package/dist/exec/search-parser.d.ts +31 -31
- package/dist/exec/search-parser.d.ts.map +1 -1
- package/dist/exec/search-parser.js +158 -59
- package/dist/exec/search-parser.js.map +1 -6
- package/dist/expr/expr-unit.d.ts +4 -4
- package/dist/expr/expr-unit.js +24 -18
- package/dist/expr/expr-unit.js.map +1 -6
- package/dist/expr/expr.d.ts +6 -6
- package/dist/expr/expr.js +1872 -1844
- package/dist/expr/expr.js.map +1 -6
- package/dist/index.js +23 -1
- package/dist/index.js.map +1 -6
- package/dist/models/system-migration.js +7 -7
- package/dist/models/system-migration.js.map +1 -6
- package/dist/query-builder/base/expr-renderer-base.d.ts +10 -10
- package/dist/query-builder/base/expr-renderer-base.js +27 -21
- package/dist/query-builder/base/expr-renderer-base.js.map +1 -6
- package/dist/query-builder/base/query-builder-base.d.ts +21 -21
- package/dist/query-builder/base/query-builder-base.d.ts.map +1 -1
- package/dist/query-builder/base/query-builder-base.js +90 -80
- package/dist/query-builder/base/query-builder-base.js.map +1 -6
- package/dist/query-builder/mssql/mssql-expr-renderer.d.ts +4 -4
- package/dist/query-builder/mssql/mssql-expr-renderer.d.ts.map +1 -1
- package/dist/query-builder/mssql/mssql-expr-renderer.js +447 -420
- package/dist/query-builder/mssql/mssql-expr-renderer.js.map +1 -6
- package/dist/query-builder/mssql/mssql-query-builder.js +483 -443
- package/dist/query-builder/mssql/mssql-query-builder.js.map +1 -6
- package/dist/query-builder/mysql/mysql-expr-renderer.d.ts +4 -4
- package/dist/query-builder/mysql/mysql-expr-renderer.d.ts.map +1 -1
- package/dist/query-builder/mysql/mysql-expr-renderer.js +451 -419
- package/dist/query-builder/mysql/mysql-expr-renderer.js.map +1 -6
- package/dist/query-builder/mysql/mysql-query-builder.js +570 -479
- package/dist/query-builder/mysql/mysql-query-builder.js.map +1 -6
- package/dist/query-builder/postgresql/postgresql-expr-renderer.d.ts +4 -4
- package/dist/query-builder/postgresql/postgresql-expr-renderer.d.ts.map +1 -1
- package/dist/query-builder/postgresql/postgresql-expr-renderer.js +449 -422
- package/dist/query-builder/postgresql/postgresql-expr-renderer.js.map +1 -6
- package/dist/query-builder/postgresql/postgresql-query-builder.js +511 -460
- package/dist/query-builder/postgresql/postgresql-query-builder.js.map +1 -6
- package/dist/query-builder/query-builder.d.ts +1 -1
- package/dist/query-builder/query-builder.js +13 -13
- package/dist/query-builder/query-builder.js.map +1 -6
- package/dist/schema/factory/column-builder.d.ts +84 -84
- package/dist/schema/factory/column-builder.js +248 -185
- package/dist/schema/factory/column-builder.js.map +1 -6
- package/dist/schema/factory/index-builder.d.ts +38 -38
- package/dist/schema/factory/index-builder.js +144 -85
- package/dist/schema/factory/index-builder.js.map +1 -6
- package/dist/schema/factory/relation-builder.d.ts +91 -91
- package/dist/schema/factory/relation-builder.d.ts.map +1 -1
- package/dist/schema/factory/relation-builder.js +274 -136
- package/dist/schema/factory/relation-builder.js.map +1 -6
- package/dist/schema/procedure-builder.d.ts +51 -51
- package/dist/schema/procedure-builder.d.ts.map +1 -1
- package/dist/schema/procedure-builder.js +205 -131
- package/dist/schema/procedure-builder.js.map +1 -6
- package/dist/schema/table-builder.d.ts +55 -55
- package/dist/schema/table-builder.d.ts.map +1 -1
- package/dist/schema/table-builder.js +274 -205
- package/dist/schema/table-builder.js.map +1 -6
- package/dist/schema/view-builder.d.ts +44 -44
- package/dist/schema/view-builder.d.ts.map +1 -1
- package/dist/schema/view-builder.js +189 -116
- package/dist/schema/view-builder.js.map +1 -6
- package/dist/types/column.js +60 -30
- package/dist/types/column.js.map +1 -6
- package/dist/types/db-context-def.d.ts +9 -9
- package/dist/types/db-context-def.js +2 -1
- package/dist/types/db-context-def.js.map +1 -6
- package/dist/types/db.d.ts +47 -47
- package/dist/types/db.js +15 -5
- package/dist/types/db.js.map +1 -6
- package/dist/types/expr.d.ts +81 -81
- package/dist/types/expr.d.ts.map +1 -1
- package/dist/types/expr.js +3 -1
- package/dist/types/expr.js.map +1 -6
- package/dist/types/query-def.d.ts +46 -46
- package/dist/types/query-def.d.ts.map +1 -1
- package/dist/types/query-def.js +31 -24
- package/dist/types/query-def.js.map +1 -6
- package/dist/utils/result-parser.js +362 -221
- package/dist/utils/result-parser.js.map +1 -6
- package/package.json +5 -7
- package/src/create-db-context.ts +31 -31
- package/src/ddl/column-ddl.ts +4 -4
- package/src/ddl/initialize.ts +38 -38
- package/src/ddl/relation-ddl.ts +6 -6
- package/src/ddl/schema-ddl.ts +4 -4
- package/src/ddl/table-ddl.ts +24 -24
- package/src/errors/db-transaction-error.ts +13 -13
- package/src/exec/executable.ts +25 -25
- package/src/exec/queryable.ts +134 -134
- package/src/exec/search-parser.ts +50 -50
- package/src/expr/expr-unit.ts +4 -4
- package/src/expr/expr.ts +13 -13
- package/src/index.ts +8 -8
- package/src/models/system-migration.ts +1 -1
- package/src/query-builder/base/expr-renderer-base.ts +21 -21
- package/src/query-builder/base/query-builder-base.ts +33 -33
- package/src/query-builder/mssql/mssql-expr-renderer.ts +11 -11
- package/src/query-builder/mssql/mssql-query-builder.ts +11 -11
- package/src/query-builder/mysql/mysql-expr-renderer.ts +15 -15
- package/src/query-builder/mysql/mysql-query-builder.ts +3 -3
- package/src/query-builder/postgresql/postgresql-expr-renderer.ts +9 -9
- package/src/query-builder/postgresql/postgresql-query-builder.ts +7 -7
- package/src/query-builder/query-builder.ts +1 -1
- package/src/schema/factory/column-builder.ts +86 -86
- package/src/schema/factory/index-builder.ts +38 -38
- package/src/schema/factory/relation-builder.ts +93 -93
- package/src/schema/procedure-builder.ts +52 -52
- package/src/schema/table-builder.ts +56 -56
- package/src/schema/view-builder.ts +45 -45
- package/src/types/column.ts +1 -1
- package/src/types/db-context-def.ts +15 -15
- package/src/types/db.ts +50 -50
- package/src/types/expr.ts +103 -103
- package/src/types/query-def.ts +50 -50
- package/src/utils/result-parser.ts +39 -39
- package/README.md +0 -192
- package/docs/core.md +0 -234
- package/docs/expression.md +0 -234
- package/docs/query-builder.md +0 -93
- package/docs/queryable.md +0 -198
- package/docs/schema-builders.md +0 -463
- package/docs/types.md +0 -445
- package/docs/utilities.md +0 -27
- package/tests/db-context/create-db-context.spec.ts +0 -193
- package/tests/db-context/define-db-context.spec.ts +0 -17
- package/tests/ddl/basic.expected.ts +0 -341
- package/tests/ddl/basic.spec.ts +0 -557
- package/tests/ddl/column-builder.expected.ts +0 -310
- package/tests/ddl/column-builder.spec.ts +0 -525
- package/tests/ddl/index-builder.expected.ts +0 -38
- package/tests/ddl/index-builder.spec.ts +0 -148
- package/tests/ddl/procedure-builder.expected.ts +0 -52
- package/tests/ddl/procedure-builder.spec.ts +0 -128
- package/tests/ddl/relation-builder.expected.ts +0 -36
- package/tests/ddl/relation-builder.spec.ts +0 -171
- package/tests/ddl/table-builder.expected.ts +0 -113
- package/tests/ddl/table-builder.spec.ts +0 -399
- package/tests/ddl/view-builder.expected.ts +0 -38
- package/tests/ddl/view-builder.spec.ts +0 -116
- package/tests/dml/delete.expected.ts +0 -96
- package/tests/dml/delete.spec.ts +0 -127
- package/tests/dml/insert.expected.ts +0 -192
- package/tests/dml/insert.spec.ts +0 -210
- package/tests/dml/update.expected.ts +0 -176
- package/tests/dml/update.spec.ts +0 -222
- package/tests/dml/upsert.expected.ts +0 -215
- package/tests/dml/upsert.spec.ts +0 -190
- package/tests/errors/queryable-errors.spec.ts +0 -126
- package/tests/escape.spec.ts +0 -59
- package/tests/examples/pivot.expected.ts +0 -211
- package/tests/examples/pivot.spec.ts +0 -200
- package/tests/examples/sampling.expected.ts +0 -69
- package/tests/examples/sampling.spec.ts +0 -42
- package/tests/examples/unpivot.expected.ts +0 -120
- package/tests/examples/unpivot.spec.ts +0 -161
- package/tests/exec/search-parser.spec.ts +0 -267
- package/tests/executable/basic.expected.ts +0 -18
- package/tests/executable/basic.spec.ts +0 -54
- package/tests/expr/comparison.expected.ts +0 -282
- package/tests/expr/comparison.spec.ts +0 -334
- package/tests/expr/conditional.expected.ts +0 -134
- package/tests/expr/conditional.spec.ts +0 -249
- package/tests/expr/date.expected.ts +0 -332
- package/tests/expr/date.spec.ts +0 -459
- package/tests/expr/math.expected.ts +0 -62
- package/tests/expr/math.spec.ts +0 -59
- package/tests/expr/string.expected.ts +0 -218
- package/tests/expr/string.spec.ts +0 -300
- package/tests/expr/utility.expected.ts +0 -147
- package/tests/expr/utility.spec.ts +0 -155
- package/tests/select/basic.expected.ts +0 -322
- package/tests/select/basic.spec.ts +0 -433
- package/tests/select/filter.expected.ts +0 -357
- package/tests/select/filter.spec.ts +0 -954
- package/tests/select/group.expected.ts +0 -169
- package/tests/select/group.spec.ts +0 -159
- package/tests/select/join.expected.ts +0 -582
- package/tests/select/join.spec.ts +0 -692
- package/tests/select/order.expected.ts +0 -150
- package/tests/select/order.spec.ts +0 -140
- package/tests/select/recursive-cte.expected.ts +0 -244
- package/tests/select/recursive-cte.spec.ts +0 -514
- package/tests/select/result-meta.spec.ts +0 -270
- package/tests/select/subquery.expected.ts +0 -363
- package/tests/select/subquery.spec.ts +0 -441
- package/tests/select/view.expected.ts +0 -155
- package/tests/select/view.spec.ts +0 -235
- package/tests/select/window.expected.ts +0 -345
- package/tests/select/window.spec.ts +0 -433
- package/tests/setup/MockExecutor.ts +0 -18
- package/tests/setup/TestDbContext.ts +0 -59
- package/tests/setup/models/Company.ts +0 -13
- package/tests/setup/models/Employee.ts +0 -10
- package/tests/setup/models/MonthlySales.ts +0 -11
- package/tests/setup/models/Post.ts +0 -16
- package/tests/setup/models/Sales.ts +0 -10
- package/tests/setup/models/User.ts +0 -19
- package/tests/setup/procedure/GetAllUsers.ts +0 -9
- package/tests/setup/procedure/GetUserById.ts +0 -12
- package/tests/setup/test-utils.ts +0 -72
- package/tests/setup/views/ActiveUsers.ts +0 -8
- package/tests/setup/views/UserSummary.ts +0 -11
- package/tests/types/nullable-queryable-record.spec.ts +0 -97
- package/tests/utils/result-parser-perf.spec.ts +0 -143
- package/tests/utils/result-parser.spec.ts +0 -667
package/docs/types.md
DELETED
|
@@ -1,445 +0,0 @@
|
|
|
1
|
-
# Types
|
|
2
|
-
|
|
3
|
-
## `Dialect`
|
|
4
|
-
|
|
5
|
-
Supported database dialects.
|
|
6
|
-
|
|
7
|
-
```typescript
|
|
8
|
-
type Dialect = "mysql" | "mssql" | "postgresql";
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## `dialects`
|
|
12
|
-
|
|
13
|
-
List of all supported database dialects.
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
const dialects: Dialect[] = ["mysql", "mssql", "postgresql"];
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## `IsolationLevel`
|
|
20
|
-
|
|
21
|
-
Transaction isolation level.
|
|
22
|
-
|
|
23
|
-
```typescript
|
|
24
|
-
type IsolationLevel =
|
|
25
|
-
| "READ_UNCOMMITTED"
|
|
26
|
-
| "READ_COMMITTED"
|
|
27
|
-
| "REPEATABLE_READ"
|
|
28
|
-
| "SERIALIZABLE";
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## `DataRecord`
|
|
32
|
-
|
|
33
|
-
Query result data record type. Supports nested relation (include) results with recursive structure.
|
|
34
|
-
|
|
35
|
-
```typescript
|
|
36
|
-
type DataRecord = {
|
|
37
|
-
[key: string]: ColumnPrimitive | DataRecord | DataRecord[];
|
|
38
|
-
};
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
## `DbContextExecutor`
|
|
42
|
-
|
|
43
|
-
DbContext executor interface. Responsible for actual DB connection and query execution.
|
|
44
|
-
|
|
45
|
-
```typescript
|
|
46
|
-
interface DbContextExecutor {
|
|
47
|
-
connect(): Promise<void>;
|
|
48
|
-
close(): Promise<void>;
|
|
49
|
-
beginTransaction(isolationLevel?: IsolationLevel): Promise<void>;
|
|
50
|
-
commitTransaction(): Promise<void>;
|
|
51
|
-
rollbackTransaction(): Promise<void>;
|
|
52
|
-
executeDefs<T = DataRecord>(
|
|
53
|
-
defs: QueryDef[],
|
|
54
|
-
resultMetas?: (ResultMeta | undefined)[],
|
|
55
|
-
): Promise<T[][]>;
|
|
56
|
-
}
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
| Method | Description |
|
|
60
|
-
|--------|-------------|
|
|
61
|
-
| `connect()` | Establish DB connection |
|
|
62
|
-
| `close()` | Close DB connection |
|
|
63
|
-
| `beginTransaction()` | Begin transaction with optional isolation level |
|
|
64
|
-
| `commitTransaction()` | Commit transaction |
|
|
65
|
-
| `rollbackTransaction()` | Rollback transaction |
|
|
66
|
-
| `executeDefs()` | Execute QueryDef array and return results |
|
|
67
|
-
|
|
68
|
-
## `QueryBuildResult`
|
|
69
|
-
|
|
70
|
-
`QueryBuilder.build()` return type.
|
|
71
|
-
|
|
72
|
-
```typescript
|
|
73
|
-
interface QueryBuildResult {
|
|
74
|
-
sql: string;
|
|
75
|
-
resultSetIndex?: number;
|
|
76
|
-
resultSetStride?: number;
|
|
77
|
-
}
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
| Field | Type | Description |
|
|
81
|
-
|-------|------|-------------|
|
|
82
|
-
| `sql` | `string` | Built SQL string |
|
|
83
|
-
| `resultSetIndex` | `number` | Result set index to fetch results from |
|
|
84
|
-
| `resultSetStride` | `number` | Extract every Nth result set from multiple results |
|
|
85
|
-
|
|
86
|
-
## `ResultMeta`
|
|
87
|
-
|
|
88
|
-
Metadata for query result transformation.
|
|
89
|
-
|
|
90
|
-
```typescript
|
|
91
|
-
interface ResultMeta {
|
|
92
|
-
columns: Record<string, ColumnPrimitiveStr>;
|
|
93
|
-
joins: Record<string, { isSingle: boolean }>;
|
|
94
|
-
}
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
| Field | Type | Description |
|
|
98
|
-
|-------|------|-------------|
|
|
99
|
-
| `columns` | `Record<string, ColumnPrimitiveStr>` | Column name to type mapping |
|
|
100
|
-
| `joins` | `Record<string, { isSingle: boolean }>` | JOIN alias to single/array indicator |
|
|
101
|
-
|
|
102
|
-
## `Migration`
|
|
103
|
-
|
|
104
|
-
Database migration definition.
|
|
105
|
-
|
|
106
|
-
```typescript
|
|
107
|
-
interface Migration {
|
|
108
|
-
name: string;
|
|
109
|
-
up: (db: DbContextBase & DbContextDdlMethods) => Promise<void>;
|
|
110
|
-
}
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
| Field | Type | Description |
|
|
114
|
-
|-------|------|-------------|
|
|
115
|
-
| `name` | `string` | Unique migration name (timestamp recommended) |
|
|
116
|
-
| `up` | `(db) => Promise<void>` | Migration execution function |
|
|
117
|
-
|
|
118
|
-
## `DataType`
|
|
119
|
-
|
|
120
|
-
SQL data type definition.
|
|
121
|
-
|
|
122
|
-
```typescript
|
|
123
|
-
type DataType =
|
|
124
|
-
| { type: "int" }
|
|
125
|
-
| { type: "bigint" }
|
|
126
|
-
| { type: "float" }
|
|
127
|
-
| { type: "double" }
|
|
128
|
-
| { type: "decimal"; precision: number; scale?: number }
|
|
129
|
-
| { type: "varchar"; length: number }
|
|
130
|
-
| { type: "char"; length: number }
|
|
131
|
-
| { type: "text" }
|
|
132
|
-
| { type: "binary" }
|
|
133
|
-
| { type: "boolean" }
|
|
134
|
-
| { type: "datetime" }
|
|
135
|
-
| { type: "date" }
|
|
136
|
-
| { type: "time" }
|
|
137
|
-
| { type: "uuid" };
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
## `ColumnPrimitiveMap`
|
|
141
|
-
|
|
142
|
-
Column primitive type mapping. TypeScript type name (string) to actual type.
|
|
143
|
-
|
|
144
|
-
```typescript
|
|
145
|
-
type ColumnPrimitiveMap = {
|
|
146
|
-
string: string;
|
|
147
|
-
number: number;
|
|
148
|
-
boolean: boolean;
|
|
149
|
-
DateTime: DateTime;
|
|
150
|
-
DateOnly: DateOnly;
|
|
151
|
-
Time: Time;
|
|
152
|
-
Uuid: Uuid;
|
|
153
|
-
Bytes: Bytes;
|
|
154
|
-
};
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
## `ColumnPrimitiveStr`
|
|
158
|
-
|
|
159
|
-
Column primitive type name.
|
|
160
|
-
|
|
161
|
-
```typescript
|
|
162
|
-
type ColumnPrimitiveStr = keyof ColumnPrimitiveMap;
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
## `ColumnPrimitive`
|
|
166
|
-
|
|
167
|
-
All primitive types that can be stored in columns. `undefined` represents NULL.
|
|
168
|
-
|
|
169
|
-
```typescript
|
|
170
|
-
type ColumnPrimitive = ColumnPrimitiveMap[ColumnPrimitiveStr] | undefined;
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
## `dataTypeStrToColumnPrimitiveStr`
|
|
174
|
-
|
|
175
|
-
SQL DataType to TypeScript type name mapping.
|
|
176
|
-
|
|
177
|
-
```typescript
|
|
178
|
-
const dataTypeStrToColumnPrimitiveStr: {
|
|
179
|
-
int: "number"; bigint: "number"; float: "number"; double: "number"; decimal: "number";
|
|
180
|
-
varchar: "string"; char: "string"; text: "string";
|
|
181
|
-
binary: "Bytes"; boolean: "boolean";
|
|
182
|
-
datetime: "DateTime"; date: "DateOnly"; time: "Time"; uuid: "Uuid";
|
|
183
|
-
};
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
## `InferColumnPrimitiveFromDataType`
|
|
187
|
-
|
|
188
|
-
TypeScript type inference from DataType.
|
|
189
|
-
|
|
190
|
-
```typescript
|
|
191
|
-
type InferColumnPrimitiveFromDataType<TDataType extends DataType> =
|
|
192
|
-
ColumnPrimitiveMap[(typeof dataTypeStrToColumnPrimitiveStr)[TDataType["type"]]];
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
## `inferColumnPrimitiveStr`
|
|
196
|
-
|
|
197
|
-
Infer `ColumnPrimitiveStr` from a runtime value.
|
|
198
|
-
|
|
199
|
-
```typescript
|
|
200
|
-
function inferColumnPrimitiveStr(value: ColumnPrimitive): ColumnPrimitiveStr;
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
## `ColumnMeta`
|
|
204
|
-
|
|
205
|
-
Column metadata. Generated by `ColumnBuilder` and passed to `TableBuilder`.
|
|
206
|
-
|
|
207
|
-
```typescript
|
|
208
|
-
interface ColumnMeta {
|
|
209
|
-
type: ColumnPrimitiveStr;
|
|
210
|
-
dataType: DataType;
|
|
211
|
-
autoIncrement?: boolean;
|
|
212
|
-
nullable?: boolean;
|
|
213
|
-
default?: ColumnPrimitive;
|
|
214
|
-
description?: string;
|
|
215
|
-
}
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
| Field | Type | Description |
|
|
219
|
-
|-------|------|-------------|
|
|
220
|
-
| `type` | `ColumnPrimitiveStr` | TypeScript type name |
|
|
221
|
-
| `dataType` | `DataType` | SQL data type |
|
|
222
|
-
| `autoIncrement` | `boolean` | Whether to auto-increment |
|
|
223
|
-
| `nullable` | `boolean` | Whether to allow NULL |
|
|
224
|
-
| `default` | `ColumnPrimitive` | Default value |
|
|
225
|
-
| `description` | `string` | Column description (DDL comment) |
|
|
226
|
-
|
|
227
|
-
## `DateUnit`
|
|
228
|
-
|
|
229
|
-
Date operation unit.
|
|
230
|
-
|
|
231
|
-
```typescript
|
|
232
|
-
type DateUnit = "year" | "month" | "day" | "hour" | "minute" | "second";
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
## `QueryDefObjectName`
|
|
236
|
-
|
|
237
|
-
DB object name (table, view, procedure, etc.).
|
|
238
|
-
|
|
239
|
-
```typescript
|
|
240
|
-
interface QueryDefObjectName {
|
|
241
|
-
database?: string;
|
|
242
|
-
schema?: string;
|
|
243
|
-
name: string;
|
|
244
|
-
}
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
| Field | Type | Description |
|
|
248
|
-
|-------|------|-------------|
|
|
249
|
-
| `database` | `string` | Database name |
|
|
250
|
-
| `schema` | `string` | Schema name |
|
|
251
|
-
| `name` | `string` | Object name |
|
|
252
|
-
|
|
253
|
-
## `QueryDef`
|
|
254
|
-
|
|
255
|
-
All query definition union type. DML + DDL + Utils + Meta.
|
|
256
|
-
|
|
257
|
-
```typescript
|
|
258
|
-
type QueryDef =
|
|
259
|
-
| SelectQueryDef | InsertQueryDef | InsertIfNotExistsQueryDef | InsertIntoQueryDef
|
|
260
|
-
| UpdateQueryDef | DeleteQueryDef | UpsertQueryDef
|
|
261
|
-
| ClearSchemaQueryDef | CreateTableQueryDef | DropTableQueryDef | RenameTableQueryDef
|
|
262
|
-
| TruncateQueryDef | AddColumnQueryDef | DropColumnQueryDef | ModifyColumnQueryDef
|
|
263
|
-
| RenameColumnQueryDef | DropPrimaryKeyQueryDef | AddPrimaryKeyQueryDef
|
|
264
|
-
| AddForeignKeyQueryDef | DropForeignKeyQueryDef | AddIndexQueryDef | DropIndexQueryDef
|
|
265
|
-
| CreateViewQueryDef | DropViewQueryDef | CreateProcQueryDef | DropProcQueryDef
|
|
266
|
-
| ExecProcQueryDef | SwitchFkQueryDef | SchemaExistsQueryDef;
|
|
267
|
-
```
|
|
268
|
-
|
|
269
|
-
## `SelectQueryDef`
|
|
270
|
-
|
|
271
|
-
SELECT query definition.
|
|
272
|
-
|
|
273
|
-
```typescript
|
|
274
|
-
interface SelectQueryDef {
|
|
275
|
-
type: "select";
|
|
276
|
-
from?: QueryDefObjectName | SelectQueryDef | SelectQueryDef[] | string;
|
|
277
|
-
as: string;
|
|
278
|
-
select?: Record<string, Expr>;
|
|
279
|
-
distinct?: boolean;
|
|
280
|
-
top?: number;
|
|
281
|
-
lock?: boolean;
|
|
282
|
-
where?: WhereExpr[];
|
|
283
|
-
joins?: SelectQueryDefJoin[];
|
|
284
|
-
orderBy?: [Expr, ("ASC" | "DESC")?][];
|
|
285
|
-
limit?: [number, number];
|
|
286
|
-
groupBy?: Expr[];
|
|
287
|
-
having?: WhereExpr[];
|
|
288
|
-
with?: { name: string; base: SelectQueryDef; recursive: SelectQueryDef };
|
|
289
|
-
}
|
|
290
|
-
```
|
|
291
|
-
|
|
292
|
-
## `SelectQueryDefJoin`
|
|
293
|
-
|
|
294
|
-
JOIN query definition. Extends `SelectQueryDef` with `isSingle` flag.
|
|
295
|
-
|
|
296
|
-
```typescript
|
|
297
|
-
interface SelectQueryDefJoin extends SelectQueryDef {
|
|
298
|
-
isSingle?: boolean;
|
|
299
|
-
}
|
|
300
|
-
```
|
|
301
|
-
|
|
302
|
-
## `InsertQueryDef`
|
|
303
|
-
|
|
304
|
-
INSERT query definition.
|
|
305
|
-
|
|
306
|
-
```typescript
|
|
307
|
-
interface InsertQueryDef {
|
|
308
|
-
type: "insert";
|
|
309
|
-
table: QueryDefObjectName;
|
|
310
|
-
records: Record<string, ColumnPrimitive>[];
|
|
311
|
-
overrideIdentity?: boolean;
|
|
312
|
-
output?: CudOutputDef;
|
|
313
|
-
}
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
## `InsertIfNotExistsQueryDef`
|
|
317
|
-
|
|
318
|
-
Conditional INSERT query definition. Insert only if not exists.
|
|
319
|
-
|
|
320
|
-
```typescript
|
|
321
|
-
interface InsertIfNotExistsQueryDef {
|
|
322
|
-
type: "insertIfNotExists";
|
|
323
|
-
table: QueryDefObjectName;
|
|
324
|
-
record: Record<string, ColumnPrimitive>;
|
|
325
|
-
existsSelectQuery: SelectQueryDef;
|
|
326
|
-
overrideIdentity?: boolean;
|
|
327
|
-
output?: CudOutputDef;
|
|
328
|
-
}
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
## `InsertIntoQueryDef`
|
|
332
|
-
|
|
333
|
-
INSERT INTO SELECT query definition. Insert subquery results.
|
|
334
|
-
|
|
335
|
-
```typescript
|
|
336
|
-
interface InsertIntoQueryDef {
|
|
337
|
-
type: "insertInto";
|
|
338
|
-
table: QueryDefObjectName;
|
|
339
|
-
recordsSelectQuery: SelectQueryDef;
|
|
340
|
-
overrideIdentity?: boolean;
|
|
341
|
-
output?: CudOutputDef;
|
|
342
|
-
}
|
|
343
|
-
```
|
|
344
|
-
|
|
345
|
-
## `UpdateQueryDef`
|
|
346
|
-
|
|
347
|
-
UPDATE query definition.
|
|
348
|
-
|
|
349
|
-
```typescript
|
|
350
|
-
interface UpdateQueryDef {
|
|
351
|
-
type: "update";
|
|
352
|
-
table: QueryDefObjectName;
|
|
353
|
-
as: string;
|
|
354
|
-
record: Record<string, Expr>;
|
|
355
|
-
top?: number;
|
|
356
|
-
where?: WhereExpr[];
|
|
357
|
-
joins?: SelectQueryDefJoin[];
|
|
358
|
-
limit?: [number, number];
|
|
359
|
-
output?: CudOutputDef;
|
|
360
|
-
}
|
|
361
|
-
```
|
|
362
|
-
|
|
363
|
-
## `DeleteQueryDef`
|
|
364
|
-
|
|
365
|
-
DELETE query definition.
|
|
366
|
-
|
|
367
|
-
```typescript
|
|
368
|
-
interface DeleteQueryDef {
|
|
369
|
-
type: "delete";
|
|
370
|
-
table: QueryDefObjectName;
|
|
371
|
-
as: string;
|
|
372
|
-
top?: number;
|
|
373
|
-
where?: WhereExpr[];
|
|
374
|
-
joins?: SelectQueryDefJoin[];
|
|
375
|
-
limit?: [number, number];
|
|
376
|
-
output?: CudOutputDef;
|
|
377
|
-
}
|
|
378
|
-
```
|
|
379
|
-
|
|
380
|
-
## `UpsertQueryDef`
|
|
381
|
-
|
|
382
|
-
UPSERT query definition. INSERT or UPDATE (MERGE pattern).
|
|
383
|
-
|
|
384
|
-
```typescript
|
|
385
|
-
interface UpsertQueryDef {
|
|
386
|
-
type: "upsert";
|
|
387
|
-
table: QueryDefObjectName;
|
|
388
|
-
existsSelectQuery: SelectQueryDef;
|
|
389
|
-
insertRecord: Record<string, Expr>;
|
|
390
|
-
updateRecord: Record<string, Expr>;
|
|
391
|
-
overrideIdentity?: boolean;
|
|
392
|
-
output?: CudOutputDef;
|
|
393
|
-
}
|
|
394
|
-
```
|
|
395
|
-
|
|
396
|
-
## `CudOutputDef`
|
|
397
|
-
|
|
398
|
-
CUD query OUTPUT clause definition.
|
|
399
|
-
|
|
400
|
-
```typescript
|
|
401
|
-
interface CudOutputDef {
|
|
402
|
-
columns: string[];
|
|
403
|
-
pkColNames: string[];
|
|
404
|
-
aiColName?: string;
|
|
405
|
-
}
|
|
406
|
-
```
|
|
407
|
-
|
|
408
|
-
## `DDL_TYPES`
|
|
409
|
-
|
|
410
|
-
DDL type constants. Used for blocking DDL within transactions.
|
|
411
|
-
|
|
412
|
-
```typescript
|
|
413
|
-
const DDL_TYPES: readonly string[];
|
|
414
|
-
```
|
|
415
|
-
|
|
416
|
-
## `DdlType`
|
|
417
|
-
|
|
418
|
-
DDL type union.
|
|
419
|
-
|
|
420
|
-
```typescript
|
|
421
|
-
type DdlType = (typeof DDL_TYPES)[number];
|
|
422
|
-
```
|
|
423
|
-
|
|
424
|
-
## Expression Types (Expr)
|
|
425
|
-
|
|
426
|
-
Union type `Expr` covers all expression types: value (`ExprColumn`, `ExprValue`, `ExprRaw`), string, numeric, date, conditional, aggregate, window, and subquery expressions.
|
|
427
|
-
|
|
428
|
-
Union type `WhereExpr` covers comparison (`ExprEq`, `ExprGt`, `ExprLt`, `ExprGte`, `ExprLte`, `ExprBetween`, `ExprIsNull`, `ExprLike`, `ExprRegexp`, `ExprIn`, `ExprInQuery`, `ExprExists`) and logical (`ExprNot`, `ExprAnd`, `ExprOr`) expressions.
|
|
429
|
-
|
|
430
|
-
## Window Types
|
|
431
|
-
|
|
432
|
-
`WinFn` is a union of all window function types: `WinFnRowNumber`, `WinFnRank`, `WinFnDenseRank`, `WinFnNtile`, `WinFnLag`, `WinFnLead`, `WinFnFirstValue`, `WinFnLastValue`, `WinFnSum`, `WinFnAvg`, `WinFnCount`, `WinFnMin`, `WinFnMax`.
|
|
433
|
-
|
|
434
|
-
```typescript
|
|
435
|
-
interface WinSpec {
|
|
436
|
-
partitionBy?: Expr[];
|
|
437
|
-
orderBy?: [Expr, ("ASC" | "DESC")?][];
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
interface ExprWindow {
|
|
441
|
-
type: "window";
|
|
442
|
-
fn: WinFn;
|
|
443
|
-
spec: WinSpec;
|
|
444
|
-
}
|
|
445
|
-
```
|
package/docs/utilities.md
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
# Utilities
|
|
2
|
-
|
|
3
|
-
## `parseQueryResult`
|
|
4
|
-
|
|
5
|
-
Transform raw DB query results to typed TypeScript objects via `ResultMeta`. Handles type parsing, nested object construction from flat JOIN results, and deduplication.
|
|
6
|
-
|
|
7
|
-
```typescript
|
|
8
|
-
async function parseQueryResult<TRecord>(
|
|
9
|
-
rawResults: Record<string, unknown>[],
|
|
10
|
-
meta: ResultMeta,
|
|
11
|
-
): Promise<TRecord[] | undefined>;
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
| Parameter | Type | Description |
|
|
15
|
-
|-----------|------|-------------|
|
|
16
|
-
| `rawResults` | `Record<string, unknown>[]` | Raw result array from database |
|
|
17
|
-
| `meta` | `ResultMeta` | Type transformation and JOIN structure information |
|
|
18
|
-
|
|
19
|
-
**Returns:** Type-transformed and nested result array. Returns `undefined` if input is empty or no valid results.
|
|
20
|
-
|
|
21
|
-
### Behavior
|
|
22
|
-
|
|
23
|
-
- **Type parsing**: Converts raw values to TypeScript types based on `meta.columns` mapping (e.g., string to `DateTime`, number to boolean).
|
|
24
|
-
- **JOIN nesting**: Converts flat `"posts.id"` keys into nested `{ posts: { id: ... } }` structures.
|
|
25
|
-
- **Deduplication**: Groups records by non-JOIN columns and collects JOIN data into arrays (or single objects for `isSingle: true`).
|
|
26
|
-
- **Async**: Yields to the event loop every 100 records to prevent blocking.
|
|
27
|
-
- **Empty handling**: Returns `undefined` for empty input or all-empty parsed records.
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { defineDbContext } from "../../src/define-db-context";
|
|
3
|
-
import { createDbContext } from "../../src/create-db-context";
|
|
4
|
-
import { User } from "../setup/models/User";
|
|
5
|
-
import { Post } from "../setup/models/Post";
|
|
6
|
-
import { MockExecutor } from "../setup/MockExecutor";
|
|
7
|
-
import "../setup/test-utils";
|
|
8
|
-
|
|
9
|
-
const TestDb = defineDbContext({
|
|
10
|
-
tables: { user: User, post: Post },
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
describe("createDbContext", () => {
|
|
14
|
-
it("creates instance with queryable accessors", () => {
|
|
15
|
-
const db = createDbContext(TestDb, new MockExecutor(), {
|
|
16
|
-
database: "TestDb",
|
|
17
|
-
schema: "TestSchema",
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
expect(db.database).toBe("TestDb");
|
|
21
|
-
expect(db.schema).toBe("TestSchema");
|
|
22
|
-
expect(db.status).toBe("ready");
|
|
23
|
-
expect(typeof db.user).toBe("function");
|
|
24
|
-
expect(typeof db.post).toBe("function");
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("queryable functions create correct QueryDef", () => {
|
|
28
|
-
const db = createDbContext(TestDb, new MockExecutor(), {
|
|
29
|
-
database: "TestDb",
|
|
30
|
-
schema: "TestSchema",
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
const def = db.user().getSelectQueryDef();
|
|
34
|
-
expect(def).toEqual({
|
|
35
|
-
type: "select",
|
|
36
|
-
as: "T1",
|
|
37
|
-
from: { database: "TestDb", schema: "TestSchema", name: "User" },
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("alias counter increments between queryable calls", () => {
|
|
42
|
-
const db = createDbContext(TestDb, new MockExecutor(), {
|
|
43
|
-
database: "TestDb",
|
|
44
|
-
schema: "TestSchema",
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const userDef = db.user().getSelectQueryDef();
|
|
48
|
-
const postDef = db.post().getSelectQueryDef();
|
|
49
|
-
expect(userDef.as).toBe("T1");
|
|
50
|
-
expect(postDef.as).toBe("T2");
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("DDL QueryDef generators produce correct output", () => {
|
|
54
|
-
const db = createDbContext(TestDb, new MockExecutor(), {
|
|
55
|
-
database: "TestDb",
|
|
56
|
-
schema: "TestSchema",
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
const clearDef = db.getClearSchemaQueryDef({ database: "TestDb", schema: "TestSchema" });
|
|
60
|
-
expect(clearDef).toEqual({
|
|
61
|
-
type: "clearSchema",
|
|
62
|
-
database: "TestDb",
|
|
63
|
-
schema: "TestSchema",
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("connect/transaction methods exist", () => {
|
|
68
|
-
const db = createDbContext(TestDb, new MockExecutor(), {
|
|
69
|
-
database: "TestDb",
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
expect(typeof db.connect).toBe("function");
|
|
73
|
-
expect(typeof db.connectWithoutTransaction).toBe("function");
|
|
74
|
-
expect(typeof db.transaction).toBe("function");
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("connect manages status lifecycle", async () => {
|
|
78
|
-
const db = createDbContext(TestDb, new MockExecutor(), {
|
|
79
|
-
database: "TestDb",
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
expect(db.status).toBe("ready");
|
|
83
|
-
await db.connect(async () => {
|
|
84
|
-
await Promise.resolve();
|
|
85
|
-
expect(db.status).toBe("transact");
|
|
86
|
-
});
|
|
87
|
-
expect(db.status).toBe("ready");
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it("connectWithoutTransaction manages status lifecycle", async () => {
|
|
91
|
-
const db = createDbContext(TestDb, new MockExecutor(), {
|
|
92
|
-
database: "TestDb",
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
expect(db.status).toBe("ready");
|
|
96
|
-
await db.connectWithoutTransaction(async () => {
|
|
97
|
-
await Promise.resolve();
|
|
98
|
-
expect(db.status).toBe("connect");
|
|
99
|
-
});
|
|
100
|
-
expect(db.status).toBe("ready");
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it("transaction manages status lifecycle within connectWithoutTransaction", async () => {
|
|
104
|
-
const db = createDbContext(TestDb, new MockExecutor(), {
|
|
105
|
-
database: "TestDb",
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
await db.connectWithoutTransaction(async () => {
|
|
109
|
-
await Promise.resolve();
|
|
110
|
-
expect(db.status).toBe("connect");
|
|
111
|
-
await db.transaction(async () => {
|
|
112
|
-
await Promise.resolve();
|
|
113
|
-
expect(db.status).toBe("transact");
|
|
114
|
-
});
|
|
115
|
-
expect(db.status).toBe("connect");
|
|
116
|
-
});
|
|
117
|
-
expect(db.status).toBe("ready");
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it("connect resets alias counter", async () => {
|
|
121
|
-
const db = createDbContext(TestDb, new MockExecutor(), {
|
|
122
|
-
database: "TestDb",
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
// Use some aliases
|
|
126
|
-
db.getNextAlias(); // T1
|
|
127
|
-
db.getNextAlias(); // T2
|
|
128
|
-
|
|
129
|
-
await db.connect(async () => {
|
|
130
|
-
await Promise.resolve();
|
|
131
|
-
// After connect, alias counter should be reset
|
|
132
|
-
const userDef = db.user().getSelectQueryDef();
|
|
133
|
-
expect(userDef.as).toBe("T1");
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it("connect rolls back and rethrows on callback error", async () => {
|
|
138
|
-
const db = createDbContext(TestDb, new MockExecutor(), {
|
|
139
|
-
database: "TestDb",
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
const testError = new Error("test error");
|
|
143
|
-
await expect(
|
|
144
|
-
db.connect(async () => {
|
|
145
|
-
await Promise.resolve();
|
|
146
|
-
throw testError;
|
|
147
|
-
}),
|
|
148
|
-
).rejects.toThrow("test error");
|
|
149
|
-
|
|
150
|
-
expect(db.status).toBe("ready");
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it("connectWithoutTransaction rethrows on callback error", async () => {
|
|
154
|
-
const db = createDbContext(TestDb, new MockExecutor(), {
|
|
155
|
-
database: "TestDb",
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
const testError = new Error("test error");
|
|
159
|
-
await expect(
|
|
160
|
-
db.connectWithoutTransaction(async () => {
|
|
161
|
-
await Promise.resolve();
|
|
162
|
-
throw testError;
|
|
163
|
-
}),
|
|
164
|
-
).rejects.toThrow("test error");
|
|
165
|
-
|
|
166
|
-
expect(db.status).toBe("ready");
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it("transaction throws when already in transaction state", async () => {
|
|
170
|
-
const db = createDbContext(TestDb, new MockExecutor(), {
|
|
171
|
-
database: "TestDb",
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
await db.connect(async () => {
|
|
175
|
-
// Already in transact state via connect
|
|
176
|
-
await expect(db.transaction(async () => {})).rejects.toThrow("Already in TRANSACTION state.");
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it("getQueryDefObjectName resolves table with defaults", () => {
|
|
181
|
-
const db = createDbContext(TestDb, new MockExecutor(), {
|
|
182
|
-
database: "TestDb",
|
|
183
|
-
schema: "TestSchema",
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
const objName = db.getQueryDefObjectName(User);
|
|
187
|
-
expect(objName).toEqual({
|
|
188
|
-
database: "TestDb",
|
|
189
|
-
schema: "TestSchema",
|
|
190
|
-
name: "User",
|
|
191
|
-
});
|
|
192
|
-
});
|
|
193
|
-
});
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { defineDbContext } from "../../src/define-db-context";
|
|
3
|
-
import { User } from "../setup/models/User";
|
|
4
|
-
import { Post } from "../setup/models/Post";
|
|
5
|
-
|
|
6
|
-
describe("defineDbContext", () => {
|
|
7
|
-
it("creates a DbContextDef with tables", () => {
|
|
8
|
-
const MyDb = defineDbContext({
|
|
9
|
-
tables: { user: User, post: Post },
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
expect(MyDb.meta.tables.user).toBe(User);
|
|
13
|
-
expect(MyDb.meta.tables.post).toBe(Post);
|
|
14
|
-
expect(MyDb.meta.migrations).toEqual([]);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
});
|