@simplysm/orm-common 14.0.1 → 14.0.5

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 (59) hide show
  1. package/README.md +163 -0
  2. package/dist/exec/queryable.js +18 -18
  3. package/dist/exec/queryable.js.map +1 -1
  4. package/dist/expr/expr.d.ts +102 -102
  5. package/dist/expr/expr.js +105 -105
  6. package/dist/expr/expr.js.map +1 -1
  7. package/dist/query-builder/mssql/mssql-expr-renderer.d.ts +1 -1
  8. package/dist/query-builder/mssql/mssql-expr-renderer.d.ts.map +1 -1
  9. package/dist/query-builder/mssql/mssql-expr-renderer.js +17 -17
  10. package/dist/query-builder/mssql/mssql-expr-renderer.js.map +1 -1
  11. package/dist/query-builder/mssql/mssql-query-builder.d.ts +2 -2
  12. package/dist/query-builder/mssql/mssql-query-builder.d.ts.map +1 -1
  13. package/dist/query-builder/mssql/mssql-query-builder.js +26 -26
  14. package/dist/query-builder/mssql/mssql-query-builder.js.map +1 -1
  15. package/dist/query-builder/mysql/mysql-expr-renderer.d.ts +1 -1
  16. package/dist/query-builder/mysql/mysql-expr-renderer.d.ts.map +1 -1
  17. package/dist/query-builder/mysql/mysql-expr-renderer.js +14 -14
  18. package/dist/query-builder/mysql/mysql-expr-renderer.js.map +1 -1
  19. package/dist/query-builder/mysql/mysql-query-builder.d.ts +10 -10
  20. package/dist/query-builder/mysql/mysql-query-builder.d.ts.map +1 -1
  21. package/dist/query-builder/mysql/mysql-query-builder.js +67 -67
  22. package/dist/query-builder/mysql/mysql-query-builder.js.map +1 -1
  23. package/dist/query-builder/postgresql/postgresql-expr-renderer.d.ts +1 -1
  24. package/dist/query-builder/postgresql/postgresql-expr-renderer.d.ts.map +1 -1
  25. package/dist/query-builder/postgresql/postgresql-expr-renderer.js +13 -13
  26. package/dist/query-builder/postgresql/postgresql-expr-renderer.js.map +1 -1
  27. package/dist/query-builder/postgresql/postgresql-query-builder.d.ts +8 -8
  28. package/dist/query-builder/postgresql/postgresql-query-builder.d.ts.map +1 -1
  29. package/dist/query-builder/postgresql/postgresql-query-builder.js +47 -47
  30. package/dist/query-builder/postgresql/postgresql-query-builder.js.map +1 -1
  31. package/dist/schema/factory/relation-builder.d.ts +8 -8
  32. package/dist/schema/factory/relation-builder.js +9 -9
  33. package/dist/schema/factory/relation-builder.js.map +1 -1
  34. package/dist/schema/view-builder.js +2 -2
  35. package/dist/schema/view-builder.js.map +1 -1
  36. package/dist/types/column.d.ts +21 -21
  37. package/dist/types/column.js +5 -5
  38. package/dist/utils/result-parser.d.ts +11 -11
  39. package/dist/utils/result-parser.js +48 -48
  40. package/dist/utils/result-parser.js.map +1 -1
  41. package/docs/core.md +157 -0
  42. package/docs/expression.md +220 -0
  43. package/docs/query-builder.md +150 -0
  44. package/docs/queryable.md +261 -0
  45. package/docs/schema-builders.md +294 -0
  46. package/docs/types.md +520 -0
  47. package/package.json +7 -3
  48. package/src/exec/queryable.ts +18 -18
  49. package/src/expr/expr.ts +105 -105
  50. package/src/query-builder/mssql/mssql-expr-renderer.ts +17 -17
  51. package/src/query-builder/mssql/mssql-query-builder.ts +26 -26
  52. package/src/query-builder/mysql/mysql-expr-renderer.ts +14 -14
  53. package/src/query-builder/mysql/mysql-query-builder.ts +67 -67
  54. package/src/query-builder/postgresql/postgresql-expr-renderer.ts +13 -13
  55. package/src/query-builder/postgresql/postgresql-query-builder.ts +47 -47
  56. package/src/schema/factory/relation-builder.ts +9 -9
  57. package/src/schema/view-builder.ts +2 -2
  58. package/src/types/column.ts +23 -23
  59. package/src/utils/result-parser.ts +49 -49
@@ -0,0 +1,261 @@
1
+ # Queryable / Executable
2
+
3
+ Type-safe query builder and stored procedure executor.
4
+
5
+ ## Queryable
6
+
7
+ ```typescript
8
+ class Queryable<TData extends DataRecord, TFrom extends TableBuilder<any, any> | never> {
9
+ constructor(readonly meta: QueryableMeta<TData>);
10
+ }
11
+ ```
12
+
13
+ Fluent query builder for composing SELECT, INSERT, UPDATE, DELETE, and UPSERT queries. All methods return new Queryable instances (immutable chaining).
14
+
15
+ - `TData` - Query result data type
16
+ - `TFrom` - Source table (required for CUD operations, `never` for read-only)
17
+
18
+ ### Option Methods (SELECT / DISTINCT / LOCK)
19
+
20
+ | Method | Signature | Description |
21
+ |--------|-----------|-------------|
22
+ | `select` | `<R>(fn: (cols: QueryableRecord<TData>) => R) => Queryable<UnwrapQueryableRecord<R>, never>` | Specify columns to SELECT |
23
+ | `distinct` | `() => Queryable<TData, never>` | Apply DISTINCT |
24
+ | `lock` | `() => Queryable<TData, TFrom>` | Apply FOR UPDATE row lock |
25
+
26
+ ### Restrict Methods (TOP / LIMIT)
27
+
28
+ | Method | Signature | Description |
29
+ |--------|-----------|-------------|
30
+ | `top` | `(count: number) => Queryable<TData, TFrom>` | Select top N rows |
31
+ | `limit` | `(skip: number, take: number) => Queryable<TData, TFrom>` | Pagination (requires orderBy) |
32
+
33
+ ### Sorting (ORDER BY)
34
+
35
+ | Method | Signature | Description |
36
+ |--------|-----------|-------------|
37
+ | `orderBy` | `(fn: (cols) => ExprUnit, orderBy?: "ASC" \| "DESC") => Queryable<TData, TFrom>` | Add sort condition (stackable) |
38
+
39
+ ### Filtering (WHERE)
40
+
41
+ | Method | Signature | Description |
42
+ |--------|-----------|-------------|
43
+ | `where` | `(predicate: (cols) => WhereExprUnit[]) => Queryable<TData, TFrom>` | Add WHERE conditions (stackable, AND) |
44
+ | `search` | `(fn: (cols) => ExprUnit<string\|undefined>[], searchText: string) => Queryable<TData, TFrom>` | Full-text search across columns |
45
+
46
+ ### Grouping (GROUP BY / HAVING)
47
+
48
+ | Method | Signature | Description |
49
+ |--------|-----------|-------------|
50
+ | `groupBy` | `(fn: (cols) => ExprUnit[]) => Queryable<TData, never>` | Group by columns |
51
+ | `having` | `(predicate: (cols) => WhereExprUnit[]) => Queryable<TData, never>` | Filter groups |
52
+
53
+ ### JOIN
54
+
55
+ | Method | Signature | Description |
56
+ |--------|-----------|-------------|
57
+ | `join` | `<A, R>(as: A, fn: (qr: JoinQueryable, cols) => Queryable<R, any>) => Queryable<TData & { [K in A]?: R[] }, TFrom>` | LEFT OUTER JOIN (1:N, result as array) |
58
+ | `joinSingle` | `<A, R>(as: A, fn: (qr: JoinQueryable, cols) => Queryable<R, any>) => Queryable<TData & { [K in A]?: R }, TFrom>` | LEFT OUTER JOIN (N:1 or 1:1, result as single object) |
59
+ | `include` | `(fn: (item: PathProxy<TData>) => PathProxy) => Queryable<TData, TFrom>` | Auto-JOIN based on TableBuilder FK/FKT relations |
60
+
61
+ ### Subquery / Union
62
+
63
+ | Method | Signature | Description |
64
+ |--------|-----------|-------------|
65
+ | `wrap` | `() => Queryable<TData, never>` | Wrap as subquery (needed for count after distinct/groupBy) |
66
+ | `Queryable.union` (static) | `(...queries: Queryable<TData, any>[]) => Queryable<TData, never>` | Combine queries with UNION (min 2) |
67
+
68
+ ### Recursive CTE
69
+
70
+ | Method | Signature | Description |
71
+ |--------|-----------|-------------|
72
+ | `recursive` | `(fn: (cte: RecursiveQueryable<TData>) => Queryable<TData, any>) => Queryable<TData, never>` | Generate recursive CTE (WITH RECURSIVE) |
73
+
74
+ ### Execution (SELECT)
75
+
76
+ | Method | Signature | Description |
77
+ |--------|-----------|-------------|
78
+ | `execute` | `() => Promise<TData[]>` | Execute SELECT and return results |
79
+ | `single` | `() => Promise<TData \| undefined>` | Return single result (throws if > 1) |
80
+ | `first` | `() => Promise<TData \| undefined>` | Return first result |
81
+ | `count` | `(fn?: (cols) => ExprUnit) => Promise<number>` | Count rows (throws after distinct/groupBy without wrap) |
82
+ | `exists` | `() => Promise<boolean>` | Check if any matching data exists |
83
+
84
+ ### QueryDef Getters (SELECT)
85
+
86
+ | Method | Signature | Description |
87
+ |--------|-----------|-------------|
88
+ | `getSelectQueryDef` | `() => SelectQueryDef` | Get the SELECT QueryDef AST |
89
+ | `getResultMeta` | `(outputColumns?: string[]) => ResultMeta` | Get result parsing metadata |
90
+
91
+ ### INSERT
92
+
93
+ | Method | Signature | Description |
94
+ |--------|-----------|-------------|
95
+ | `insert` | `(records: TFrom["$inferInsert"][]) => Promise<void>` | Insert records (auto-chunks per 1000) |
96
+ | `insert` | `(records, outputColumns: K[]) => Promise<Pick<...>[]>` | Insert with output columns |
97
+ | `insertIfNotExists` | `(record: TFrom["$inferInsert"]) => Promise<void>` | Insert if WHERE condition has no match |
98
+ | `insertIfNotExists` | `(record, outputColumns: K[]) => Promise<Pick<...>>` | Insert if not exists with output |
99
+ | `insertInto` | `(targetTable: TableBuilder) => Promise<void>` | INSERT INTO ... SELECT |
100
+ | `insertInto` | `(targetTable, outputColumns: K[]) => Promise<Pick<...>[]>` | INSERT INTO ... SELECT with output |
101
+
102
+ ### UPDATE / DELETE
103
+
104
+ | Method | Signature | Description |
105
+ |--------|-----------|-------------|
106
+ | `update` | `(recordFwd: (cols) => QueryableWriteRecord) => Promise<void>` | Update matching rows |
107
+ | `update` | `(recordFwd, outputColumns: K[]) => Promise<Pick<...>[]>` | Update with output |
108
+ | `delete` | `() => Promise<void>` | Delete matching rows |
109
+ | `delete` | `(outputColumns: K[]) => Promise<Pick<...>[]>` | Delete with output |
110
+
111
+ ### UPSERT
112
+
113
+ | Method | Signature | Description |
114
+ |--------|-----------|-------------|
115
+ | `upsert` | `(updateFn: (cols) => WriteRecord) => Promise<void>` | Update or insert (same data) |
116
+ | `upsert` | `(updateFn, insertFn) => Promise<void>` | Update or insert (different data) |
117
+ | `upsert` | `(updateFn, insertFn?, outputColumns?) => Promise<Pick<...>[] \| void>` | With output columns |
118
+
119
+ ### DDL Helper
120
+
121
+ | Method | Signature | Description |
122
+ |--------|-----------|-------------|
123
+ | `switchFk` | `(enabled: boolean) => Promise<void>` | Enable/disable FK constraints on this table |
124
+
125
+ ## queryable (factory)
126
+
127
+ ```typescript
128
+ function queryable<TBuilder extends TableBuilder<any, any> | ViewBuilder<any, any, any>>(
129
+ db: DbContextBase,
130
+ tableOrView: TBuilder,
131
+ as?: string,
132
+ ): () => Queryable<TBuilder["$inferSelect"], TBuilder extends TableBuilder ? TBuilder : never>
133
+ ```
134
+
135
+ Creates a factory function that returns a new Queryable instance each time it is called. Each call allocates a fresh alias via `db.getNextAlias()`.
136
+
137
+ ## Executable
138
+
139
+ ```typescript
140
+ class Executable<TParams extends ColumnBuilderRecord, TReturns extends ColumnBuilderRecord> {
141
+ constructor(db: DbContextBase, builder: ProcedureBuilder<TParams, TReturns>);
142
+ getExecProcQueryDef(params?: InferColumnExprs<TParams>): { type: "execProc"; ... };
143
+ async execute(params: InferColumnExprs<TParams>): Promise<InferColumnExprs<TReturns>[][]>;
144
+ }
145
+ ```
146
+
147
+ Stored procedure execution wrapper.
148
+
149
+ | Method | Signature | Description |
150
+ |--------|-----------|-------------|
151
+ | `getExecProcQueryDef` | `(params?) => ExecProcQueryDef` | Build procedure execution QueryDef |
152
+ | `execute` | `(params) => Promise<T[][]>` | Execute the procedure |
153
+
154
+ ## executable (factory)
155
+
156
+ ```typescript
157
+ function executable<TParams, TReturns>(
158
+ db: DbContextBase,
159
+ builder: ProcedureBuilder<TParams, TReturns>,
160
+ ): () => Executable<TParams, TReturns>
161
+ ```
162
+
163
+ Creates a factory function that returns a new Executable instance.
164
+
165
+ ## parseSearchQuery
166
+
167
+ ```typescript
168
+ function parseSearchQuery(searchText: string): ParsedSearchQuery
169
+ ```
170
+
171
+ Parses a search query string into SQL LIKE patterns.
172
+
173
+ | Syntax | Meaning | Example |
174
+ |--------|---------|---------|
175
+ | `term1 term2` | OR (match any) | `apple banana` |
176
+ | `+term` | Required (AND) | `+apple +banana` |
177
+ | `-term` | Exclude (NOT) | `apple -banana` |
178
+ | `"exact phrase"` | Exact match (required) | `"delicious fruit"` |
179
+ | `*` | Wildcard | `app*` becomes `app%` |
180
+
181
+ Escape sequences: `\\` (literal `\`), `\*`, `\%`, `\"`, `\+`, `\-`.
182
+
183
+ ## ParsedSearchQuery
184
+
185
+ ```typescript
186
+ interface ParsedSearchQuery {
187
+ or: string[]; // OR conditions - LIKE patterns
188
+ must: string[]; // AND conditions (+ prefix or quotes) - LIKE patterns
189
+ not: string[]; // NOT conditions (- prefix) - LIKE patterns
190
+ }
191
+ ```
192
+
193
+ ## getMatchedPrimaryKeys
194
+
195
+ ```typescript
196
+ function getMatchedPrimaryKeys(
197
+ fkCols: string[],
198
+ targetTable: TableBuilder<any, any>,
199
+ ): string[]
200
+ ```
201
+
202
+ Match FK column array with the target table's PK. Returns PK column name array. Throws if column counts do not match.
203
+
204
+ ## QueryableRecord
205
+
206
+ ```typescript
207
+ type QueryableRecord<TData extends DataRecord> = {
208
+ [K in keyof TData]: TData[K] extends ColumnPrimitive
209
+ ? ExprUnit<TData[K]>
210
+ : TData[K] extends (infer U)[]
211
+ ? U extends DataRecord ? QueryableRecord<U>[] : never
212
+ : TData[K] extends DataRecord ? QueryableRecord<TData[K]> : ...
213
+ }
214
+ ```
215
+
216
+ Maps each field of a DataRecord to its corresponding ExprUnit proxy. Used as the column accessor type in Queryable callbacks.
217
+
218
+ ## QueryableWriteRecord
219
+
220
+ ```typescript
221
+ type QueryableWriteRecord<TData> = {
222
+ [K in keyof TData]: TData[K] extends ColumnPrimitive ? ExprInput<TData[K]> : never;
223
+ }
224
+ ```
225
+
226
+ Maps each field to ExprInput for write operations (UPDATE, UPSERT).
227
+
228
+ ## NullableQueryableRecord
229
+
230
+ ```typescript
231
+ type NullableQueryableRecord<TData extends DataRecord> = { ... }
232
+ ```
233
+
234
+ Like QueryableRecord but all primitive fields become `ExprUnit<T | undefined>`. Used for LEFT JOIN results where all columns may be NULL.
235
+
236
+ ## UnwrapQueryableRecord
237
+
238
+ ```typescript
239
+ type UnwrapQueryableRecord<R> = {
240
+ [K in keyof R]: R[K] extends ExprUnit<infer T> ? T : ...
241
+ }
242
+ ```
243
+
244
+ Reverse-maps QueryableRecord back to a plain DataRecord. Used internally by `select()` to infer the resulting data type.
245
+
246
+ ## PathProxy
247
+
248
+ ```typescript
249
+ type PathProxy<TObject> = {
250
+ [K in keyof TObject as TObject[K] extends ColumnPrimitive ? never : K]-?: PathProxy<UnwrapArray<TObject[K]>>;
251
+ } & { readonly [PATH_SYMBOL]: string[] }
252
+ ```
253
+
254
+ Type-safe proxy for `include()` that only exposes non-primitive (relation) fields. Property access builds a path array internally.
255
+
256
+ ```typescript
257
+ // Only relation fields are accessible:
258
+ db.post().include((p) => p.author) // OK - author is a relation
259
+ db.post().include((p) => p.author.company) // OK - nested relation
260
+ // db.post().include((p) => p.title) // Compile error - title is string
261
+ ```
@@ -0,0 +1,294 @@
1
+ # Schema Builders
2
+
3
+ Fluent API builders for defining tables, views, procedures, columns, indexes, and relations.
4
+
5
+ ## Table (function)
6
+
7
+ ```typescript
8
+ function Table(name: string): TableBuilder<{}, {}>
9
+ ```
10
+
11
+ Creates a new TableBuilder with the given table name.
12
+
13
+ ## TableBuilder
14
+
15
+ ```typescript
16
+ class TableBuilder<TColumns extends ColumnBuilderRecord, TRelations extends RelationBuilderRecord>
17
+ ```
18
+
19
+ Fluent API for defining database table schema including columns, primary key, indexes, and relations.
20
+
21
+ ### Phantom Type Fields
22
+
23
+ | Field | Type | Description |
24
+ |-------|------|-------------|
25
+ | `$columns` | `TColumns` | Column definitions (type inference) |
26
+ | `$relations` | `TRelations` | Relation definitions (type inference) |
27
+ | `$inferSelect` | `InferColumns<TColumns> & InferDeepRelations<TRelations>` | Full SELECT type |
28
+ | `$inferColumns` | `InferColumns<TColumns>` | Column-only type |
29
+ | `$inferInsert` | `InferInsertColumns<TColumns>` | INSERT type (AI/nullable/default optional) |
30
+ | `$inferUpdate` | `InferUpdateColumns<TColumns>` | UPDATE type (all optional) |
31
+
32
+ ### Constructor
33
+
34
+ ```typescript
35
+ constructor(readonly meta: {
36
+ name: string;
37
+ description?: string;
38
+ database?: string;
39
+ schema?: string;
40
+ columns?: TColumns;
41
+ primaryKey?: (keyof TColumns & string)[];
42
+ relations?: TRelations;
43
+ indexes?: IndexBuilder<(keyof TColumns & string)[]>[];
44
+ })
45
+ ```
46
+
47
+ ### Methods
48
+
49
+ | Method | Signature | Description |
50
+ |--------|-----------|-------------|
51
+ | `description` | `(desc: string) => TableBuilder` | Set table description (DDL comment) |
52
+ | `database` | `(db: string) => TableBuilder` | Set database name |
53
+ | `schema` | `(schema: string) => TableBuilder` | Set schema name (MSSQL/PostgreSQL) |
54
+ | `columns` | `<T>(fn: (c: ColumnFactory) => T) => TableBuilder<T, TRelations>` | Define columns via column factory |
55
+ | `primaryKey` | `(...columns: (keyof TColumns & string)[]) => TableBuilder` | Set primary key (single or composite) |
56
+ | `indexes` | `(fn: (i: IndexFactory) => IndexBuilder[]) => TableBuilder` | Define indexes |
57
+ | `relations` | `<T>(fn: (r: RelationFactory) => T) => TableBuilder<TColumns, T>` | Define FK and relation mappings |
58
+
59
+ ## View (function)
60
+
61
+ ```typescript
62
+ function View(name: string): ViewBuilder<any, any, {}>
63
+ ```
64
+
65
+ Creates a new ViewBuilder with the given view name.
66
+
67
+ ## ViewBuilder
68
+
69
+ ```typescript
70
+ class ViewBuilder<TDbContext extends DbContextBase, TData extends DataRecord, TRelations extends RelationBuilderRecord>
71
+ ```
72
+
73
+ Fluent API for defining database view schema.
74
+
75
+ ### Phantom Type Fields
76
+
77
+ | Field | Type | Description |
78
+ |-------|------|-------------|
79
+ | `$relations` | `TRelations` | Relation definitions |
80
+ | `$inferSelect` | `TData` | Full SELECT type |
81
+
82
+ ### Methods
83
+
84
+ | Method | Signature | Description |
85
+ |--------|-----------|-------------|
86
+ | `description` | `(desc: string) => ViewBuilder` | Set view description |
87
+ | `database` | `(db: string) => ViewBuilder` | Set database name |
88
+ | `schema` | `(schema: string) => ViewBuilder` | Set schema name |
89
+ | `query` | `<TViewData, TDb>(viewFn: (db: TDb) => Queryable<TViewData, any>) => ViewBuilder<TDb, TViewData, TRelations>` | Define the view's SELECT query |
90
+ | `relations` | `<T>(fn: (r: RelationFactory) => T) => ViewBuilder` | Define relations (RelationKey only, no FK) |
91
+
92
+ ## Procedure (function)
93
+
94
+ ```typescript
95
+ function Procedure(name: string): ProcedureBuilder<never, never>
96
+ ```
97
+
98
+ Creates a new ProcedureBuilder with the given procedure name.
99
+
100
+ ## ProcedureBuilder
101
+
102
+ ```typescript
103
+ class ProcedureBuilder<TParams extends ColumnBuilderRecord, TReturns extends ColumnBuilderRecord>
104
+ ```
105
+
106
+ Fluent API for defining stored procedure schema.
107
+
108
+ ### Phantom Type Fields
109
+
110
+ | Field | Type | Description |
111
+ |-------|------|-------------|
112
+ | `$params` | `TParams` | Parameter definitions |
113
+ | `$returns` | `TReturns` | Return type definitions |
114
+
115
+ ### Methods
116
+
117
+ | Method | Signature | Description |
118
+ |--------|-----------|-------------|
119
+ | `description` | `(desc: string) => ProcedureBuilder` | Set procedure description |
120
+ | `database` | `(db: string) => ProcedureBuilder` | Set database name |
121
+ | `schema` | `(schema: string) => ProcedureBuilder` | Set schema name |
122
+ | `params` | `<T>(fn: (c: ColumnFactory) => T) => ProcedureBuilder<T, TReturns>` | Define input parameters |
123
+ | `returns` | `<T>(fn: (c: ColumnFactory) => T) => ProcedureBuilder<TParams, T>` | Define return columns |
124
+ | `body` | `(sql: string) => ProcedureBuilder` | Set procedure body SQL |
125
+
126
+ ## ColumnBuilder
127
+
128
+ ```typescript
129
+ class ColumnBuilder<TValue extends ColumnPrimitive, TMeta extends ColumnMeta> {
130
+ constructor(readonly meta: TMeta);
131
+ }
132
+ ```
133
+
134
+ Column definition builder with fluent API for type modifiers.
135
+
136
+ ### Methods
137
+
138
+ | Method | Signature | Description |
139
+ |--------|-----------|-------------|
140
+ | `autoIncrement` | `() => ColumnBuilder<TValue, TMeta & { autoIncrement: true }>` | Set auto-increment (INSERT optional) |
141
+ | `nullable` | `() => ColumnBuilder<TValue \| undefined, TMeta & { nullable: true }>` | Allow NULL values |
142
+ | `default` | `(value: TValue) => ColumnBuilder<TValue, TMeta & { default: TValue }>` | Set default value (INSERT optional) |
143
+ | `description` | `(desc: string) => ColumnBuilder<TValue, TMeta & { description: string }>` | Set column description (DDL comment) |
144
+
145
+ ## createColumnFactory
146
+
147
+ ```typescript
148
+ function createColumnFactory(): ColumnFactory
149
+ ```
150
+
151
+ Returns a column type factory object. Used inside `TableBuilder.columns()` and `ProcedureBuilder.params()/returns()`.
152
+
153
+ ### Factory Methods
154
+
155
+ | Method | Signature | SQL Type | TypeScript Type |
156
+ |--------|-----------|----------|-----------------|
157
+ | `int` | `() => ColumnBuilder<number, ...>` | `INT` | `number` |
158
+ | `bigint` | `() => ColumnBuilder<number, ...>` | `BIGINT` | `number` |
159
+ | `float` | `() => ColumnBuilder<number, ...>` | `FLOAT` | `number` |
160
+ | `double` | `() => ColumnBuilder<number, ...>` | `DOUBLE` | `number` |
161
+ | `decimal` | `(precision, scale?) => ColumnBuilder<number, ...>` | `DECIMAL(p,s)` | `number` |
162
+ | `varchar` | `(length) => ColumnBuilder<string, ...>` | `VARCHAR(n)` | `string` |
163
+ | `char` | `(length) => ColumnBuilder<string, ...>` | `CHAR(n)` | `string` |
164
+ | `text` | `() => ColumnBuilder<string, ...>` | `TEXT` | `string` |
165
+ | `binary` | `() => ColumnBuilder<Bytes, ...>` | `LONGBLOB`/`BYTEA` | `Bytes` |
166
+ | `boolean` | `() => ColumnBuilder<boolean, ...>` | `TINYINT(1)`/`BIT`/`BOOLEAN` | `boolean` |
167
+ | `datetime` | `() => ColumnBuilder<DateTime, ...>` | `DATETIME` | `DateTime` |
168
+ | `date` | `() => ColumnBuilder<DateOnly, ...>` | `DATE` | `DateOnly` |
169
+ | `time` | `() => ColumnBuilder<Time, ...>` | `TIME` | `Time` |
170
+ | `uuid` | `() => ColumnBuilder<Uuid, ...>` | `BINARY(16)`/`UNIQUEIDENTIFIER`/`UUID` | `Uuid` |
171
+
172
+ ## IndexBuilder
173
+
174
+ ```typescript
175
+ class IndexBuilder<TKeys extends string[]> {
176
+ constructor(readonly meta: {
177
+ columns: TKeys;
178
+ name?: string;
179
+ unique?: boolean;
180
+ orderBy?: { [K in keyof TKeys]: "ASC" | "DESC" };
181
+ description?: string;
182
+ });
183
+ }
184
+ ```
185
+
186
+ ### Methods
187
+
188
+ | Method | Signature | Description |
189
+ |--------|-----------|-------------|
190
+ | `name` | `(name: string) => IndexBuilder<TKeys>` | Set custom index name |
191
+ | `unique` | `() => IndexBuilder<TKeys>` | Mark as unique index |
192
+ | `orderBy` | `(...orderBy: ("ASC" \| "DESC")[]) => IndexBuilder<TKeys>` | Set sort order per column |
193
+ | `description` | `(desc: string) => IndexBuilder<TKeys>` | Set index description |
194
+
195
+ ## createIndexFactory
196
+
197
+ ```typescript
198
+ function createIndexFactory<TColumnKey extends string>(): { index: (...columns: TColumnKey[]) => IndexBuilder }
199
+ ```
200
+
201
+ Returns an index factory. Used inside `TableBuilder.indexes()`.
202
+
203
+ ## ForeignKeyBuilder
204
+
205
+ ```typescript
206
+ class ForeignKeyBuilder<TOwner extends TableBuilder<any, any>, TTargetFn extends () => TableBuilder<any, any>> {
207
+ constructor(readonly meta: {
208
+ ownerFn: () => TOwner;
209
+ columns: string[];
210
+ targetFn: TTargetFn;
211
+ description?: string;
212
+ });
213
+ }
214
+ ```
215
+
216
+ N:1 FK relation builder. Creates an actual DB foreign key constraint.
217
+
218
+ | Method | Signature | Description |
219
+ |--------|-----------|-------------|
220
+ | `description` | `(desc: string) => ForeignKeyBuilder` | Set relation description |
221
+
222
+ ## ForeignKeyTargetBuilder
223
+
224
+ ```typescript
225
+ class ForeignKeyTargetBuilder<TTargetTableFn extends () => TableBuilder<any, any>, TIsSingle extends boolean> {
226
+ constructor(readonly meta: {
227
+ targetTableFn: TTargetTableFn;
228
+ relationName: string;
229
+ description?: string;
230
+ isSingle?: TIsSingle;
231
+ });
232
+ }
233
+ ```
234
+
235
+ 1:N FK reverse-reference builder. Loaded as array by default, single object with `.single()`.
236
+
237
+ | Method | Signature | Description |
238
+ |--------|-----------|-------------|
239
+ | `description` | `(desc: string) => ForeignKeyTargetBuilder` | Set relation description |
240
+ | `single` | `() => ForeignKeyTargetBuilder<..., true>` | Change to 1:1 (single object) |
241
+
242
+ ## RelationKeyBuilder
243
+
244
+ ```typescript
245
+ class RelationKeyBuilder<
246
+ TOwner extends TableBuilder<any, any> | ViewBuilder<any, any, any>,
247
+ TTargetFn extends () => TableBuilder<any, any> | ViewBuilder<any, any, any>,
248
+ >
249
+ ```
250
+
251
+ Logical N:1 relation builder (no DB FK constraint). Works with both Tables and Views.
252
+
253
+ | Method | Signature | Description |
254
+ |--------|-----------|-------------|
255
+ | `description` | `(desc: string) => RelationKeyBuilder` | Set relation description |
256
+
257
+ ## RelationKeyTargetBuilder
258
+
259
+ ```typescript
260
+ class RelationKeyTargetBuilder<
261
+ TTargetTableFn extends () => TableBuilder<any, any> | ViewBuilder<any, any, any>,
262
+ TIsSingle extends boolean,
263
+ >
264
+ ```
265
+
266
+ Logical 1:N reverse-reference builder (no DB FK constraint). Works with both Tables and Views.
267
+
268
+ | Method | Signature | Description |
269
+ |--------|-----------|-------------|
270
+ | `description` | `(desc: string) => RelationKeyTargetBuilder` | Set relation description |
271
+ | `single` | `() => RelationKeyTargetBuilder<..., true>` | Change to 1:1 (single object) |
272
+
273
+ ## createRelationFactory
274
+
275
+ ```typescript
276
+ function createRelationFactory<TOwner, TColumnKey extends string>(
277
+ ownerFn: () => TOwner,
278
+ ): RelationFactory
279
+ ```
280
+
281
+ Creates a relation factory. For `TableBuilder`: provides `foreignKey`, `foreignKeyTarget`, `relationKey`, `relationKeyTarget`. For `ViewBuilder`: provides only `relationKey`, `relationKeyTarget`.
282
+
283
+ ### Factory Methods (Table)
284
+
285
+ | Method | Signature | Description |
286
+ |--------|-----------|-------------|
287
+ | `foreignKey` | `(columns: TColumnKey[], targetFn: () => TableBuilder) => ForeignKeyBuilder` | N:1 FK (creates DB constraint) |
288
+ | `foreignKeyTarget` | `(targetTableFn: () => TableBuilder, relationName: string) => ForeignKeyTargetBuilder` | 1:N FK reverse-reference |
289
+ | `relationKey` | `(columns: TColumnKey[], targetFn: () => TableBuilder \| ViewBuilder) => RelationKeyBuilder` | N:1 logical relation (no DB FK) |
290
+ | `relationKeyTarget` | `(targetTableFn: () => TableBuilder \| ViewBuilder, relationName: string) => RelationKeyTargetBuilder` | 1:N logical reverse-reference |
291
+
292
+ ### Factory Methods (View)
293
+
294
+ Only `relationKey` and `relationKeyTarget` are available for ViewBuilder.