@simplysm/orm-common 13.0.98 → 13.0.100

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/docs/types.md CHANGED
@@ -1,45 +1,24 @@
1
1
  # Types
2
2
 
3
- TypeScript types for dialects, queries, expressions, columns, and results.
3
+ ## `Dialect`
4
4
 
5
- Source: `src/types/db.ts`, `src/types/column.ts`, `src/types/expr.ts`, `src/types/query-def.ts`
6
-
7
- ## Database Types
8
-
9
- Source: `src/types/db.ts`
10
-
11
- ### Dialect
5
+ Supported database dialects.
12
6
 
13
7
  ```typescript
14
8
  type Dialect = "mysql" | "mssql" | "postgresql";
15
9
  ```
16
10
 
17
- - `mysql`: MySQL 8.0.14+
18
- - `mssql`: Microsoft SQL Server 2012+
19
- - `postgresql`: PostgreSQL 9.0+
11
+ ## `dialects`
20
12
 
21
- ### dialects
13
+ List of all supported database dialects.
22
14
 
23
15
  ```typescript
24
16
  const dialects: Dialect[] = ["mysql", "mssql", "postgresql"];
25
17
  ```
26
18
 
27
- ### QueryBuildResult
19
+ ## `IsolationLevel`
28
20
 
29
- Return type of `QueryBuilderBase.build()`.
30
-
31
- ```typescript
32
- interface QueryBuildResult {
33
- /** Built SQL string */
34
- sql: string;
35
- /** Result set index to fetch results from (default: 0) */
36
- resultSetIndex?: number;
37
- /** Extract every Nth result set from multiple results */
38
- resultSetStride?: number;
39
- }
40
- ```
41
-
42
- ### IsolationLevel
21
+ Transaction isolation level.
43
22
 
44
23
  ```typescript
45
24
  type IsolationLevel =
@@ -49,9 +28,9 @@ type IsolationLevel =
49
28
  | "SERIALIZABLE";
50
29
  ```
51
30
 
52
- ### DataRecord
31
+ ## `DataRecord`
53
32
 
54
- Recursive record type for query results, supporting nested relations.
33
+ Query result data record type. Supports nested relation (include) results with recursive structure.
55
34
 
56
35
  ```typescript
57
36
  type DataRecord = {
@@ -59,9 +38,9 @@ type DataRecord = {
59
38
  };
60
39
  ```
61
40
 
62
- ### DbContextExecutor
41
+ ## `DbContextExecutor`
63
42
 
64
- Interface for actual DB connection and query execution. Implemented by `NodeDbContextExecutor` (server) or service client executors.
43
+ DbContext executor interface. Responsible for actual DB connection and query execution.
65
44
 
66
45
  ```typescript
67
46
  interface DbContextExecutor {
@@ -77,20 +56,50 @@ interface DbContextExecutor {
77
56
  }
78
57
  ```
79
58
 
80
- ### ResultMeta
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`
81
87
 
82
- Metadata for query result transformation (type parsing and JOIN nesting).
88
+ Metadata for query result transformation.
83
89
 
84
90
  ```typescript
85
91
  interface ResultMeta {
86
- /** Column name -> ColumnPrimitiveStr mapping */
87
92
  columns: Record<string, ColumnPrimitiveStr>;
88
- /** JOIN alias -> single/array indicator */
89
93
  joins: Record<string, { isSingle: boolean }>;
90
94
  }
91
95
  ```
92
96
 
93
- ### Migration
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`
94
103
 
95
104
  Database migration definition.
96
105
 
@@ -101,28 +110,14 @@ interface Migration {
101
110
  }
102
111
  ```
103
112
 
104
- **Example:**
113
+ | Field | Type | Description |
114
+ |-------|------|-------------|
115
+ | `name` | `string` | Unique migration name (timestamp recommended) |
116
+ | `up` | `(db) => Promise<void>` | Migration execution function |
105
117
 
106
- ```typescript
107
- const migrations: Migration[] = [
108
- {
109
- name: "20260105_001_create_user_table",
110
- up: async (db) => {
111
- await db.createTable(User);
112
- },
113
- },
114
- ];
115
- ```
116
-
117
- ---
118
-
119
- ## Column Types
120
-
121
- Source: `src/types/column.ts`
118
+ ## `DataType`
122
119
 
123
- ### DataType
124
-
125
- SQL data type definition (discriminated union).
120
+ SQL data type definition.
126
121
 
127
122
  ```typescript
128
123
  type DataType =
@@ -142,9 +137,9 @@ type DataType =
142
137
  | { type: "uuid" };
143
138
  ```
144
139
 
145
- ### ColumnPrimitiveMap
140
+ ## `ColumnPrimitiveMap`
146
141
 
147
- TypeScript type name to actual type mapping.
142
+ Column primitive type mapping. TypeScript type name (string) to actual type.
148
143
 
149
144
  ```typescript
150
145
  type ColumnPrimitiveMap = {
@@ -159,14 +154,15 @@ type ColumnPrimitiveMap = {
159
154
  };
160
155
  ```
161
156
 
162
- ### ColumnPrimitiveStr
157
+ ## `ColumnPrimitiveStr`
158
+
159
+ Column primitive type name.
163
160
 
164
161
  ```typescript
165
162
  type ColumnPrimitiveStr = keyof ColumnPrimitiveMap;
166
- // "string" | "number" | "boolean" | "DateTime" | "DateOnly" | "Time" | "Uuid" | "Bytes"
167
163
  ```
168
164
 
169
- ### ColumnPrimitive
165
+ ## `ColumnPrimitive`
170
166
 
171
167
  All primitive types that can be stored in columns. `undefined` represents NULL.
172
168
 
@@ -174,54 +170,29 @@ All primitive types that can be stored in columns. `undefined` represents NULL.
174
170
  type ColumnPrimitive = ColumnPrimitiveMap[ColumnPrimitiveStr] | undefined;
175
171
  ```
176
172
 
177
- ### ColumnMeta
178
-
179
- Column metadata generated by `ColumnBuilder`.
180
-
181
- ```typescript
182
- interface ColumnMeta {
183
- type: ColumnPrimitiveStr;
184
- dataType: DataType;
185
- autoIncrement?: boolean;
186
- nullable?: boolean;
187
- default?: ColumnPrimitive;
188
- description?: string;
189
- }
190
- ```
191
-
192
- ### dataTypeStrToColumnPrimitiveStr
173
+ ## `dataTypeStrToColumnPrimitiveStr`
193
174
 
194
- SQL DataType type string to TypeScript type name mapping.
175
+ SQL DataType to TypeScript type name mapping.
195
176
 
196
177
  ```typescript
197
178
  const dataTypeStrToColumnPrimitiveStr: {
198
- int: "number";
199
- bigint: "number";
200
- float: "number";
201
- double: "number";
202
- decimal: "number";
203
- varchar: "string";
204
- char: "string";
205
- text: "string";
206
- binary: "Bytes";
207
- boolean: "boolean";
208
- datetime: "DateTime";
209
- date: "DateOnly";
210
- time: "Time";
211
- uuid: "Uuid";
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";
212
183
  };
213
184
  ```
214
185
 
215
- ### InferColumnPrimitiveFromDataType
186
+ ## `InferColumnPrimitiveFromDataType`
216
187
 
217
- Infer TypeScript type from `DataType`.
188
+ TypeScript type inference from DataType.
218
189
 
219
190
  ```typescript
220
191
  type InferColumnPrimitiveFromDataType<TDataType extends DataType> =
221
192
  ColumnPrimitiveMap[(typeof dataTypeStrToColumnPrimitiveStr)[TDataType["type"]]];
222
193
  ```
223
194
 
224
- ### inferColumnPrimitiveStr (function)
195
+ ## `inferColumnPrimitiveStr`
225
196
 
226
197
  Infer `ColumnPrimitiveStr` from a runtime value.
227
198
 
@@ -229,83 +200,41 @@ Infer `ColumnPrimitiveStr` from a runtime value.
229
200
  function inferColumnPrimitiveStr(value: ColumnPrimitive): ColumnPrimitiveStr;
230
201
  ```
231
202
 
232
- ---
233
-
234
- ## Expression Types
235
-
236
- Source: `src/types/expr.ts`
203
+ ## `ColumnMeta`
237
204
 
238
- ### DateUnit
205
+ Column metadata. Generated by `ColumnBuilder` and passed to `TableBuilder`.
239
206
 
240
207
  ```typescript
241
- type DateUnit = "year" | "month" | "day" | "hour" | "minute" | "second";
208
+ interface ColumnMeta {
209
+ type: ColumnPrimitiveStr;
210
+ dataType: DataType;
211
+ autoIncrement?: boolean;
212
+ nullable?: boolean;
213
+ default?: ColumnPrimitive;
214
+ description?: string;
215
+ }
242
216
  ```
243
217
 
244
- ### Expr (union type)
245
-
246
- Discriminated union of all expression AST node types. Used in `select()`, `orderBy()`, etc.
247
-
248
- | Category | Types |
249
- |----------|-------|
250
- | Value | `ExprColumn`, `ExprValue`, `ExprRaw` |
251
- | String | `ExprConcat`, `ExprLeft`, `ExprRight`, `ExprTrim`, `ExprPadStart`, `ExprReplace`, `ExprUpper`, `ExprLower`, `ExprLength`, `ExprByteLength`, `ExprSubstring`, `ExprIndexOf` |
252
- | Numeric | `ExprAbs`, `ExprRound`, `ExprCeil`, `ExprFloor` |
253
- | Date | `ExprYear`, `ExprMonth`, `ExprDay`, `ExprHour`, `ExprMinute`, `ExprSecond`, `ExprIsoWeek`, `ExprIsoWeekStartDate`, `ExprIsoYearMonth`, `ExprDateDiff`, `ExprDateAdd`, `ExprFormatDate` |
254
- | Condition | `ExprCoalesce`, `ExprNullIf`, `ExprIs`, `ExprSwitch`, `ExprIf` |
255
- | Aggregate | `ExprCount`, `ExprSum`, `ExprAvg`, `ExprMax`, `ExprMin` |
256
- | Other | `ExprGreatest`, `ExprLeast`, `ExprRowNum`, `ExprRandom`, `ExprCast` |
257
- | Window | `ExprWindow` |
258
- | System | `ExprSubquery` |
259
-
260
- ### WhereExpr (union type)
261
-
262
- Subset of `Expr` for WHERE clauses (comparison + logical operators).
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) |
263
226
 
264
- | Category | Types |
265
- |----------|-------|
266
- | Comparison | `ExprEq`, `ExprGt`, `ExprLt`, `ExprGte`, `ExprLte`, `ExprBetween`, `ExprIsNull`, `ExprLike`, `ExprRegexp`, `ExprIn`, `ExprInQuery`, `ExprExists` |
267
- | Logical | `ExprNot`, `ExprAnd`, `ExprOr` |
227
+ ## `DateUnit`
268
228
 
269
- ### Individual Expr Interfaces
270
-
271
- Each expression type is a simple interface with a `type` discriminant:
272
-
273
- ```typescript
274
- interface ExprColumn { type: "column"; path: string[]; }
275
- interface ExprValue { type: "value"; value: ColumnPrimitive; }
276
- interface ExprRaw { type: "raw"; sql: string; params: Expr[]; }
277
- interface ExprEq { type: "eq"; source: Expr; target: Expr; }
278
- interface ExprGt { type: "gt"; source: Expr; target: Expr; }
279
- // ... etc.
280
- ```
281
-
282
- ### Window Function Types
229
+ Date operation unit.
283
230
 
284
231
  ```typescript
285
- type WinFn =
286
- | WinFnRowNumber | WinFnRank | WinFnDenseRank | WinFnNtile
287
- | WinFnLag | WinFnLead | WinFnFirstValue | WinFnLastValue
288
- | WinFnSum | WinFnAvg | WinFnCount | WinFnMin | WinFnMax;
289
-
290
- interface WinSpec {
291
- partitionBy?: Expr[];
292
- orderBy?: [Expr, ("ASC" | "DESC")?][];
293
- }
294
-
295
- interface ExprWindow {
296
- type: "window";
297
- fn: WinFn;
298
- spec: WinSpec;
299
- }
232
+ type DateUnit = "year" | "month" | "day" | "hour" | "minute" | "second";
300
233
  ```
301
234
 
302
- ---
235
+ ## `QueryDefObjectName`
303
236
 
304
- ## Query Definition Types
305
-
306
- Source: `src/types/query-def.ts`
307
-
308
- ### QueryDefObjectName
237
+ DB object name (table, view, procedure, etc.).
309
238
 
310
239
  ```typescript
311
240
  interface QueryDefObjectName {
@@ -315,32 +244,31 @@ interface QueryDefObjectName {
315
244
  }
316
245
  ```
317
246
 
318
- DBMS-specific namespaces:
319
- - MySQL: `database.name` (schema ignored)
320
- - MSSQL: `database.schema.name` (schema defaults to dbo)
321
- - PostgreSQL: `schema.name` (database is for connection only)
322
-
323
- ### QueryDef (union type)
324
-
325
- Union of all query definition types:
326
-
327
- **DML:** `SelectQueryDef`, `InsertQueryDef`, `InsertIfNotExistsQueryDef`, `InsertIntoQueryDef`, `UpdateQueryDef`, `DeleteQueryDef`, `UpsertQueryDef`
328
-
329
- **DDL - Schema:** `ClearSchemaQueryDef`
330
-
331
- **DDL - Table:** `CreateTableQueryDef`, `DropTableQueryDef`, `RenameTableQueryDef`, `TruncateQueryDef`
332
-
333
- **DDL - Column:** `AddColumnQueryDef`, `DropColumnQueryDef`, `ModifyColumnQueryDef`, `RenameColumnQueryDef`
247
+ | Field | Type | Description |
248
+ |-------|------|-------------|
249
+ | `database` | `string` | Database name |
250
+ | `schema` | `string` | Schema name |
251
+ | `name` | `string` | Object name |
334
252
 
335
- **DDL - Constraint:** `DropPrimaryKeyQueryDef`, `AddPrimaryKeyQueryDef`, `AddForeignKeyQueryDef`, `DropForeignKeyQueryDef`, `AddIndexQueryDef`, `DropIndexQueryDef`
253
+ ## `QueryDef`
336
254
 
337
- **DDL - View/Procedure:** `CreateViewQueryDef`, `DropViewQueryDef`, `CreateProcQueryDef`, `DropProcQueryDef`, `ExecProcQueryDef`
255
+ All query definition union type. DML + DDL + Utils + Meta.
338
256
 
339
- **Utils:** `SwitchFkQueryDef`
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
+ ```
340
268
 
341
- **Meta:** `SchemaExistsQueryDef`
269
+ ## `SelectQueryDef`
342
270
 
343
- ### SelectQueryDef
271
+ SELECT query definition.
344
272
 
345
273
  ```typescript
346
274
  interface SelectQueryDef {
@@ -361,7 +289,9 @@ interface SelectQueryDef {
361
289
  }
362
290
  ```
363
291
 
364
- ### SelectQueryDefJoin
292
+ ## `SelectQueryDefJoin`
293
+
294
+ JOIN query definition. Extends `SelectQueryDef` with `isSingle` flag.
365
295
 
366
296
  ```typescript
367
297
  interface SelectQueryDefJoin extends SelectQueryDef {
@@ -369,7 +299,9 @@ interface SelectQueryDefJoin extends SelectQueryDef {
369
299
  }
370
300
  ```
371
301
 
372
- ### InsertQueryDef
302
+ ## `InsertQueryDef`
303
+
304
+ INSERT query definition.
373
305
 
374
306
  ```typescript
375
307
  interface InsertQueryDef {
@@ -381,7 +313,38 @@ interface InsertQueryDef {
381
313
  }
382
314
  ```
383
315
 
384
- ### UpdateQueryDef
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.
385
348
 
386
349
  ```typescript
387
350
  interface UpdateQueryDef {
@@ -397,7 +360,9 @@ interface UpdateQueryDef {
397
360
  }
398
361
  ```
399
362
 
400
- ### DeleteQueryDef
363
+ ## `DeleteQueryDef`
364
+
365
+ DELETE query definition.
401
366
 
402
367
  ```typescript
403
368
  interface DeleteQueryDef {
@@ -412,7 +377,9 @@ interface DeleteQueryDef {
412
377
  }
413
378
  ```
414
379
 
415
- ### UpsertQueryDef
380
+ ## `UpsertQueryDef`
381
+
382
+ UPSERT query definition. INSERT or UPDATE (MERGE pattern).
416
383
 
417
384
  ```typescript
418
385
  interface UpsertQueryDef {
@@ -426,7 +393,9 @@ interface UpsertQueryDef {
426
393
  }
427
394
  ```
428
395
 
429
- ### CudOutputDef
396
+ ## `CudOutputDef`
397
+
398
+ CUD query OUTPUT clause definition.
430
399
 
431
400
  ```typescript
432
401
  interface CudOutputDef {
@@ -436,10 +405,41 @@ interface CudOutputDef {
436
405
  }
437
406
  ```
438
407
 
439
- ### DDL_TYPES
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`
440
417
 
441
- Constant array of all DDL type strings (for blocking DDL within transactions).
418
+ DDL type union.
442
419
 
443
420
  ```typescript
444
- const DDL_TYPES: readonly DdlType[];
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
445
  ```
package/docs/utilities.md CHANGED
@@ -1,12 +1,8 @@
1
1
  # Utilities
2
2
 
3
- Result parsing helpers for transforming raw database output to typed TypeScript objects.
3
+ ## `parseQueryResult`
4
4
 
5
- Source: `src/utils/result-parser.ts`
6
-
7
- ## parseQueryResult
8
-
9
- Transform raw DB query results to TypeScript objects via `ResultMeta`. Handles type parsing, flat-to-nested transformation, and JOIN grouping.
5
+ Transform raw DB query results to typed TypeScript objects via `ResultMeta`. Handles type parsing, nested object construction from flat JOIN results, and deduplication.
10
6
 
11
7
  ```typescript
12
8
  async function parseQueryResult<TRecord>(
@@ -15,108 +11,17 @@ async function parseQueryResult<TRecord>(
15
11
  ): Promise<TRecord[] | undefined>;
16
12
  ```
17
13
 
18
- **Parameters:**
19
-
20
- - `rawResults` -- Raw result array from the database driver (flat key-value records)
21
- - `meta` -- Type transformation and JOIN structure information (`ResultMeta`)
22
-
23
- **Returns:**
24
-
25
- - Type-transformed and nested result array, or `undefined` if input is empty or all records are empty after parsing
26
-
27
- **Behavior:**
28
-
29
- - **Type parsing** -- Converts raw values (strings from DB) to proper TypeScript types based on `meta.columns` mapping (e.g., `"1"` to `1` for number, `"2026-01-15T10:00:00Z"` to `DateTime`)
30
- - **Flat-to-nested** -- Transforms dot-notation keys to nested objects (e.g., `"posts.id"` becomes `{ posts: { id: ... } }`)
31
- - **JOIN grouping** -- Groups rows by non-JOIN columns, collecting JOIN data into arrays (1:N) or single objects (1:1)
32
- - **Async** -- Yields to the event loop every 100 records to prevent blocking
33
- - **Empty handling** -- Returns `undefined` for empty input or when all parsed records are empty objects
34
-
35
- ### Example: Simple Type Parsing
36
-
37
- ```typescript
38
- const raw = [
39
- { id: "1", name: "Alice", createdAt: "2026-01-07T10:00:00.000Z" },
40
- ];
41
-
42
- const meta: ResultMeta = {
43
- columns: { id: "number", name: "string", createdAt: "DateTime" },
44
- joins: {},
45
- };
46
-
47
- const result = await parseQueryResult(raw, meta);
48
- // [{ id: 1, name: "Alice", createdAt: DateTime("2026-01-07T10:00:00.000Z") }]
49
- ```
50
-
51
- ### Example: JOIN Result Nesting
52
-
53
- ```typescript
54
- const raw = [
55
- { id: 1, name: "User1", "posts.id": 10, "posts.title": "Post1" },
56
- { id: 1, name: "User1", "posts.id": 11, "posts.title": "Post2" },
57
- { id: 2, name: "User2", "posts.id": 20, "posts.title": "Post3" },
58
- ];
59
-
60
- const meta: ResultMeta = {
61
- columns: {
62
- id: "number",
63
- name: "string",
64
- "posts.id": "number",
65
- "posts.title": "string",
66
- },
67
- joins: {
68
- posts: { isSingle: false },
69
- },
70
- };
71
-
72
- const result = await parseQueryResult(raw, meta);
73
- // [
74
- // { id: 1, name: "User1", posts: [{ id: 10, title: "Post1" }, { id: 11, title: "Post2" }] },
75
- // { id: 2, name: "User2", posts: [{ id: 20, title: "Post3" }] },
76
- // ]
77
- ```
78
-
79
- ### Example: Single JOIN (1:1)
80
-
81
- ```typescript
82
- const raw = [
83
- { id: 1, title: "Post1", "author.id": 10, "author.name": "Alice" },
84
- { id: 2, title: "Post2", "author.id": 10, "author.name": "Alice" },
85
- ];
86
-
87
- const meta: ResultMeta = {
88
- columns: {
89
- id: "number",
90
- title: "string",
91
- "author.id": "number",
92
- "author.name": "string",
93
- },
94
- joins: {
95
- author: { isSingle: true },
96
- },
97
- };
98
-
99
- const result = await parseQueryResult(raw, meta);
100
- // [
101
- // { id: 1, title: "Post1", author: { id: 10, name: "Alice" } },
102
- // { id: 2, title: "Post2", author: { id: 10, name: "Alice" } },
103
- // ]
104
- ```
105
-
106
- ---
14
+ | Parameter | Type | Description |
15
+ |-----------|------|-------------|
16
+ | `rawResults` | `Record<string, unknown>[]` | Raw result array from database |
17
+ | `meta` | `ResultMeta` | Type transformation and JOIN structure information |
107
18
 
108
- ## Type Parsing Rules
19
+ **Returns:** Type-transformed and nested result array. Returns `undefined` if input is empty or no valid results.
109
20
 
110
- | ColumnPrimitiveStr | Input | Output |
111
- |--------------------|-------|--------|
112
- | `"number"` | `"123"` / `123` | `123` (throws if NaN) |
113
- | `"string"` | any | `String(value)` |
114
- | `"boolean"` | `0` / `"0"` / `false` | `false` |
115
- | `"boolean"` | `1` / `"1"` / `true` | `true` |
116
- | `"DateTime"` | ISO string | `DateTime.parse(value)` |
117
- | `"DateOnly"` | date string | `DateOnly.parse(value)` |
118
- | `"Time"` | time string | `Time.parse(value)` |
119
- | `"Uuid"` | `Uint8Array` / string | `Uuid.fromBytes(value)` / `new Uuid(value)` |
120
- | `"Bytes"` | `Uint8Array` / hex string | as-is / `bytes.fromHex(value)` |
21
+ ### Behavior
121
22
 
122
- `null` and `undefined` values are returned as `undefined` (keys are omitted from the result).
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.