metal-orm 1.0.41 → 1.0.43
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/README.md +74 -20
- package/dist/index.cjs +180 -74
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +142 -96
- package/dist/index.d.ts +142 -96
- package/dist/index.js +177 -74
- package/dist/index.js.map +1 -1
- package/package.json +8 -2
- package/scripts/run-eslint.mjs +34 -0
- package/src/codegen/typescript.ts +32 -15
- package/src/core/ast/builders.ts +7 -2
- package/src/core/ast/expression-builders.ts +0 -2
- package/src/core/ast/expression-nodes.ts +14 -5
- package/src/core/ast/expression-visitor.ts +11 -8
- package/src/core/ast/expression.ts +2 -2
- package/src/core/ast/join-node.ts +1 -1
- package/src/core/ast/query.ts +6 -6
- package/src/core/ast/window-functions.ts +10 -2
- package/src/core/ddl/dialects/base-schema-dialect.ts +30 -3
- package/src/core/ddl/dialects/mssql-schema-dialect.ts +4 -0
- package/src/core/ddl/dialects/mysql-schema-dialect.ts +2 -0
- package/src/core/ddl/dialects/postgres-schema-dialect.ts +13 -1
- package/src/core/ddl/dialects/render-reference.test.ts +69 -0
- package/src/core/ddl/dialects/sqlite-schema-dialect.ts +9 -0
- package/src/core/ddl/introspect/mssql.ts +42 -8
- package/src/core/ddl/introspect/mysql.ts +30 -6
- package/src/core/ddl/introspect/postgres.ts +88 -34
- package/src/core/ddl/introspect/run-select.ts +6 -4
- package/src/core/ddl/introspect/sqlite.ts +56 -11
- package/src/core/ddl/introspect/types.ts +0 -1
- package/src/core/ddl/introspect/utils.ts +3 -3
- package/src/core/ddl/schema-dialect.ts +1 -0
- package/src/core/ddl/schema-generator.ts +4 -12
- package/src/core/ddl/sql-writing.ts +4 -4
- package/src/core/dialect/abstract.ts +18 -6
- package/src/core/dialect/base/function-table-formatter.ts +3 -2
- package/src/core/dialect/base/join-compiler.ts +5 -3
- package/src/core/dialect/base/returning-strategy.ts +1 -0
- package/src/core/dialect/base/sql-dialect.ts +3 -3
- package/src/core/dialect/mssql/functions.ts +24 -25
- package/src/core/dialect/mssql/index.ts +1 -4
- package/src/core/dialect/mysql/functions.ts +0 -1
- package/src/core/dialect/postgres/functions.ts +33 -34
- package/src/core/dialect/postgres/index.ts +1 -0
- package/src/core/dialect/sqlite/functions.ts +18 -19
- package/src/core/dialect/sqlite/index.ts +2 -0
- package/src/core/execution/db-executor.ts +1 -1
- package/src/core/execution/executors/mysql-executor.ts +2 -2
- package/src/core/execution/executors/postgres-executor.ts +1 -1
- package/src/core/execution/pooling/pool.ts +2 -0
- package/src/core/functions/datetime.ts +1 -1
- package/src/core/functions/numeric.ts +1 -1
- package/src/core/functions/text.ts +1 -1
- package/src/decorators/bootstrap.ts +27 -8
- package/src/decorators/column.ts +3 -11
- package/src/decorators/decorator-metadata.ts +3 -9
- package/src/decorators/entity.ts +21 -5
- package/src/decorators/relations.ts +2 -11
- package/src/orm/entity-context.ts +8 -8
- package/src/orm/entity-meta.ts +8 -8
- package/src/orm/entity-metadata.ts +11 -9
- package/src/orm/entity.ts +28 -29
- package/src/orm/execute.ts +4 -4
- package/src/orm/hydration.ts +42 -39
- package/src/orm/identity-map.ts +1 -1
- package/src/orm/lazy-batch.ts +9 -9
- package/src/orm/orm-session.ts +24 -23
- package/src/orm/orm.ts +2 -5
- package/src/orm/relation-change-processor.ts +12 -11
- package/src/orm/relations/belongs-to.ts +11 -11
- package/src/orm/relations/has-many.ts +10 -10
- package/src/orm/relations/has-one.ts +8 -7
- package/src/orm/relations/many-to-many.ts +13 -13
- package/src/orm/runtime-types.ts +4 -4
- package/src/orm/save-graph.ts +31 -25
- package/src/orm/unit-of-work.ts +17 -17
- package/src/query-builder/delete.ts +4 -3
- package/src/query-builder/hydration-manager.ts +6 -5
- package/src/query-builder/insert.ts +12 -8
- package/src/query-builder/query-ast-service.ts +2 -2
- package/src/query-builder/raw-column-parser.ts +2 -1
- package/src/query-builder/select-helpers.ts +2 -2
- package/src/query-builder/select.ts +31 -31
- package/src/query-builder/update.ts +4 -3
- package/src/schema/column.ts +26 -26
- package/src/schema/table.ts +239 -115
- package/src/schema/types.ts +22 -22
|
@@ -81,9 +81,10 @@ export class UpdateQueryBuilder<T> {
|
|
|
81
81
|
compile(dialect: UpdateDialectInput): CompiledQuery;
|
|
82
82
|
|
|
83
83
|
compile(arg: UpdateCompiler | UpdateDialectInput): CompiledQuery {
|
|
84
|
-
|
|
84
|
+
const candidate = arg as { compileUpdate?: (ast: UpdateQueryNode) => CompiledQuery };
|
|
85
|
+
if (typeof candidate.compileUpdate === 'function') {
|
|
85
86
|
// UpdateCompiler path – old behavior
|
|
86
|
-
return
|
|
87
|
+
return candidate.compileUpdate(this.state.ast);
|
|
87
88
|
}
|
|
88
89
|
|
|
89
90
|
// Dialect | string path – new behavior
|
|
@@ -92,7 +93,7 @@ export class UpdateQueryBuilder<T> {
|
|
|
92
93
|
}
|
|
93
94
|
|
|
94
95
|
toSql(arg: UpdateCompiler | UpdateDialectInput): string {
|
|
95
|
-
return this.compile(arg as
|
|
96
|
+
return this.compile(arg as UpdateCompiler).sql;
|
|
96
97
|
}
|
|
97
98
|
|
|
98
99
|
getAST(): UpdateQueryNode {
|
package/src/schema/column.ts
CHANGED
|
@@ -100,7 +100,7 @@ export interface ColumnDef<T extends ColumnType = ColumnType, TRuntime = unknown
|
|
|
100
100
|
/** Column comment/description */
|
|
101
101
|
comment?: string;
|
|
102
102
|
/** Additional arguments for the column type (e.g., VARCHAR length) */
|
|
103
|
-
args?:
|
|
103
|
+
args?: unknown[];
|
|
104
104
|
/** Table name this column belongs to (filled at runtime by defineTable) */
|
|
105
105
|
table?: string;
|
|
106
106
|
}
|
|
@@ -234,28 +234,28 @@ export const col = {
|
|
|
234
234
|
* Marks a column as UNIQUE
|
|
235
235
|
*/
|
|
236
236
|
unique: <T extends ColumnType>(def: ColumnDef<T>, name?: string): ColumnDef<T> =>
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
237
|
+
({
|
|
238
|
+
...def,
|
|
239
|
+
unique: name ?? true
|
|
240
|
+
}),
|
|
241
241
|
|
|
242
242
|
/**
|
|
243
243
|
* Sets a default value for the column
|
|
244
244
|
*/
|
|
245
245
|
default: <T extends ColumnType>(def: ColumnDef<T>, value: unknown): ColumnDef<T> =>
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
246
|
+
({
|
|
247
|
+
...def,
|
|
248
|
+
default: value
|
|
249
|
+
}),
|
|
250
250
|
|
|
251
251
|
/**
|
|
252
252
|
* Sets a raw SQL default value for the column
|
|
253
253
|
*/
|
|
254
254
|
defaultRaw: <T extends ColumnType>(def: ColumnDef<T>, expression: string): ColumnDef<T> =>
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
255
|
+
({
|
|
256
|
+
...def,
|
|
257
|
+
default: { raw: expression }
|
|
258
|
+
}),
|
|
259
259
|
|
|
260
260
|
/**
|
|
261
261
|
* Marks a column as auto-increment / identity
|
|
@@ -264,27 +264,27 @@ export const col = {
|
|
|
264
264
|
def: ColumnDef<T>,
|
|
265
265
|
strategy: ColumnDef['generated'] = 'byDefault'
|
|
266
266
|
): ColumnDef<T> =>
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
267
|
+
({
|
|
268
|
+
...def,
|
|
269
|
+
autoIncrement: true,
|
|
270
|
+
generated: strategy
|
|
271
|
+
}),
|
|
272
272
|
|
|
273
273
|
/**
|
|
274
274
|
* Adds a foreign key reference
|
|
275
275
|
*/
|
|
276
276
|
references: <T extends ColumnType>(def: ColumnDef<T>, ref: ForeignKeyReference): ColumnDef<T> =>
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
277
|
+
({
|
|
278
|
+
...def,
|
|
279
|
+
references: ref
|
|
280
|
+
}),
|
|
281
281
|
|
|
282
282
|
/**
|
|
283
283
|
* Adds a check constraint to the column
|
|
284
284
|
*/
|
|
285
285
|
check: <T extends ColumnType>(def: ColumnDef<T>, expression: string): ColumnDef<T> =>
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
286
|
+
({
|
|
287
|
+
...def,
|
|
288
|
+
check: expression
|
|
289
|
+
})
|
|
290
290
|
};
|
package/src/schema/table.ts
CHANGED
|
@@ -1,115 +1,239 @@
|
|
|
1
|
-
import type { ColumnDef } from './column.js';
|
|
2
|
-
import type { RelationDef } from './relation.js';
|
|
3
|
-
|
|
4
|
-
export interface IndexColumn {
|
|
5
|
-
column: string;
|
|
6
|
-
order?: 'ASC' | 'DESC';
|
|
7
|
-
nulls?: 'FIRST' | 'LAST';
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface IndexDef {
|
|
11
|
-
name?: string;
|
|
12
|
-
columns: (string | IndexColumn)[];
|
|
13
|
-
unique?: boolean;
|
|
14
|
-
where?: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface CheckConstraint {
|
|
18
|
-
name?: string;
|
|
19
|
-
expression: string;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface TableOptions {
|
|
23
|
-
schema?: string;
|
|
24
|
-
primaryKey?: string[];
|
|
25
|
-
indexes?: IndexDef[];
|
|
26
|
-
checks?: CheckConstraint[];
|
|
27
|
-
comment?: string;
|
|
28
|
-
engine?: string;
|
|
29
|
-
charset?: string;
|
|
30
|
-
collation?: string;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface TableHooks {
|
|
34
|
-
beforeInsert?(ctx: unknown, entity:
|
|
35
|
-
afterInsert?(ctx: unknown, entity:
|
|
36
|
-
beforeUpdate?(ctx: unknown, entity:
|
|
37
|
-
afterUpdate?(ctx: unknown, entity:
|
|
38
|
-
beforeDelete?(ctx: unknown, entity:
|
|
39
|
-
afterDelete?(ctx: unknown, entity:
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Definition of a database table with its columns and relationships
|
|
44
|
-
* @typeParam T - Type of the columns record
|
|
45
|
-
*/
|
|
46
|
-
export interface TableDef<T extends Record<string, ColumnDef> = Record<string, ColumnDef>> {
|
|
47
|
-
/** Name of the table */
|
|
48
|
-
name: string;
|
|
49
|
-
/** Optional schema/catalog name */
|
|
50
|
-
schema?: string;
|
|
51
|
-
/** Record of column definitions keyed by column name */
|
|
52
|
-
columns: T;
|
|
53
|
-
/** Record of relationship definitions keyed by relation name */
|
|
54
|
-
relations: Record<string, RelationDef>;
|
|
55
|
-
/** Optional lifecycle hooks */
|
|
56
|
-
hooks?: TableHooks;
|
|
57
|
-
/** Composite primary key definition (falls back to column.primary flags) */
|
|
58
|
-
primaryKey?: string[];
|
|
59
|
-
/** Secondary indexes */
|
|
60
|
-
indexes?: IndexDef[];
|
|
61
|
-
/** Table-level check constraints */
|
|
62
|
-
checks?: CheckConstraint[];
|
|
63
|
-
/** Table comment/description */
|
|
64
|
-
comment?: string;
|
|
65
|
-
/** Dialect-specific options */
|
|
66
|
-
engine?: string;
|
|
67
|
-
charset?: string;
|
|
68
|
-
collation?: string;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Creates a table definition with columns and relationships
|
|
73
|
-
* @typeParam T - Type of the columns record
|
|
74
|
-
* @param name - Name of the table
|
|
75
|
-
* @param columns - Record of column definitions
|
|
76
|
-
* @param relations - Record of relationship definitions (optional)
|
|
77
|
-
* @returns Complete table definition with runtime-filled column metadata
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```typescript
|
|
81
|
-
* const usersTable = defineTable('users', {
|
|
82
|
-
* id: col.primaryKey(col.int()),
|
|
83
|
-
* name: col.varchar(255),
|
|
84
|
-
* email: col.varchar(255)
|
|
85
|
-
* });
|
|
86
|
-
* ```
|
|
87
|
-
*/
|
|
88
|
-
export const defineTable = <T extends Record<string, ColumnDef>>(
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
): TableDef<T> => {
|
|
95
|
-
// Runtime mutability to assign names to column definitions for convenience
|
|
96
|
-
const colsWithNames = Object.entries(columns).reduce((acc, [key, def]) => {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
};
|
|
1
|
+
import type { ColumnDef } from './column.js';
|
|
2
|
+
import type { RelationDef } from './relation.js';
|
|
3
|
+
|
|
4
|
+
export interface IndexColumn {
|
|
5
|
+
column: string;
|
|
6
|
+
order?: 'ASC' | 'DESC';
|
|
7
|
+
nulls?: 'FIRST' | 'LAST';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface IndexDef {
|
|
11
|
+
name?: string;
|
|
12
|
+
columns: (string | IndexColumn)[];
|
|
13
|
+
unique?: boolean;
|
|
14
|
+
where?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface CheckConstraint {
|
|
18
|
+
name?: string;
|
|
19
|
+
expression: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface TableOptions {
|
|
23
|
+
schema?: string;
|
|
24
|
+
primaryKey?: string[];
|
|
25
|
+
indexes?: IndexDef[];
|
|
26
|
+
checks?: CheckConstraint[];
|
|
27
|
+
comment?: string;
|
|
28
|
+
engine?: string;
|
|
29
|
+
charset?: string;
|
|
30
|
+
collation?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface TableHooks {
|
|
34
|
+
beforeInsert?(ctx: unknown, entity: unknown): Promise<void> | void;
|
|
35
|
+
afterInsert?(ctx: unknown, entity: unknown): Promise<void> | void;
|
|
36
|
+
beforeUpdate?(ctx: unknown, entity: unknown): Promise<void> | void;
|
|
37
|
+
afterUpdate?(ctx: unknown, entity: unknown): Promise<void> | void;
|
|
38
|
+
beforeDelete?(ctx: unknown, entity: unknown): Promise<void> | void;
|
|
39
|
+
afterDelete?(ctx: unknown, entity: unknown): Promise<void> | void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Definition of a database table with its columns and relationships
|
|
44
|
+
* @typeParam T - Type of the columns record
|
|
45
|
+
*/
|
|
46
|
+
export interface TableDef<T extends Record<string, ColumnDef> = Record<string, ColumnDef>> {
|
|
47
|
+
/** Name of the table */
|
|
48
|
+
name: string;
|
|
49
|
+
/** Optional schema/catalog name */
|
|
50
|
+
schema?: string;
|
|
51
|
+
/** Record of column definitions keyed by column name */
|
|
52
|
+
columns: T;
|
|
53
|
+
/** Record of relationship definitions keyed by relation name */
|
|
54
|
+
relations: Record<string, RelationDef>;
|
|
55
|
+
/** Optional lifecycle hooks */
|
|
56
|
+
hooks?: TableHooks;
|
|
57
|
+
/** Composite primary key definition (falls back to column.primary flags) */
|
|
58
|
+
primaryKey?: string[];
|
|
59
|
+
/** Secondary indexes */
|
|
60
|
+
indexes?: IndexDef[];
|
|
61
|
+
/** Table-level check constraints */
|
|
62
|
+
checks?: CheckConstraint[];
|
|
63
|
+
/** Table comment/description */
|
|
64
|
+
comment?: string;
|
|
65
|
+
/** Dialect-specific options */
|
|
66
|
+
engine?: string;
|
|
67
|
+
charset?: string;
|
|
68
|
+
collation?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Creates a table definition with columns and relationships
|
|
73
|
+
* @typeParam T - Type of the columns record
|
|
74
|
+
* @param name - Name of the table
|
|
75
|
+
* @param columns - Record of column definitions
|
|
76
|
+
* @param relations - Record of relationship definitions (optional)
|
|
77
|
+
* @returns Complete table definition with runtime-filled column metadata
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const usersTable = defineTable('users', {
|
|
82
|
+
* id: col.primaryKey(col.int()),
|
|
83
|
+
* name: col.varchar(255),
|
|
84
|
+
* email: col.varchar(255)
|
|
85
|
+
* });
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export const defineTable = <T extends Record<string, ColumnDef>>(
|
|
89
|
+
name: string,
|
|
90
|
+
columns: T,
|
|
91
|
+
relations: Record<string, RelationDef> = {},
|
|
92
|
+
hooks?: TableHooks,
|
|
93
|
+
options: TableOptions = {}
|
|
94
|
+
): TableDef<T> => {
|
|
95
|
+
// Runtime mutability to assign names to column definitions for convenience
|
|
96
|
+
const colsWithNames = Object.entries(columns).reduce((acc, [key, def]) => {
|
|
97
|
+
const colDef = { ...def, name: key, table: name };
|
|
98
|
+
(acc as Record<string, unknown>)[key] = colDef;
|
|
99
|
+
return acc;
|
|
100
|
+
}, {} as T);
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
name,
|
|
104
|
+
schema: options.schema,
|
|
105
|
+
columns: colsWithNames,
|
|
106
|
+
relations,
|
|
107
|
+
hooks,
|
|
108
|
+
primaryKey: options.primaryKey,
|
|
109
|
+
indexes: options.indexes,
|
|
110
|
+
checks: options.checks,
|
|
111
|
+
comment: options.comment,
|
|
112
|
+
engine: options.engine,
|
|
113
|
+
charset: options.charset,
|
|
114
|
+
collation: options.collation
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
type DirectColumnKeys<T extends TableDef> =
|
|
119
|
+
Exclude<keyof T["columns"] & string, keyof T | "$">;
|
|
120
|
+
|
|
121
|
+
export type TableRef<T extends TableDef> =
|
|
122
|
+
T &
|
|
123
|
+
{ [K in DirectColumnKeys<T>]: T["columns"][K] } & {
|
|
124
|
+
/**
|
|
125
|
+
* Escape hatch for collisions:
|
|
126
|
+
* - tref.name => table name (string)
|
|
127
|
+
* - tref.$.name => column def for "name"
|
|
128
|
+
*/
|
|
129
|
+
$: T["columns"];
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const TABLE_REF_CACHE: WeakMap<object, unknown> = new WeakMap();
|
|
133
|
+
|
|
134
|
+
const withColumnProps = <T extends TableDef>(table: T): TableRef<T> => {
|
|
135
|
+
const cached = TABLE_REF_CACHE.get(table);
|
|
136
|
+
if (cached) return cached as TableRef<T>;
|
|
137
|
+
|
|
138
|
+
const proxy = new Proxy(table as object, {
|
|
139
|
+
get(target, prop, receiver) {
|
|
140
|
+
const t = target as TableDef;
|
|
141
|
+
if (prop === "$") return t.columns;
|
|
142
|
+
|
|
143
|
+
// Prefer real table fields first (prevents collision surprises)
|
|
144
|
+
if (Reflect.has(target, prop)) return Reflect.get(target, prop, receiver);
|
|
145
|
+
|
|
146
|
+
// Fall back to columns bag
|
|
147
|
+
if (typeof prop === "string" && prop in t.columns) return t.columns[prop];
|
|
148
|
+
|
|
149
|
+
return undefined;
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
has(target, prop) {
|
|
153
|
+
const t = target as TableDef;
|
|
154
|
+
return (
|
|
155
|
+
prop === "$" ||
|
|
156
|
+
Reflect.has(target, prop) ||
|
|
157
|
+
(typeof prop === "string" && prop in t.columns)
|
|
158
|
+
);
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
ownKeys(target) {
|
|
162
|
+
const t = target as TableDef;
|
|
163
|
+
const base = Reflect.ownKeys(target);
|
|
164
|
+
const cols = Object.keys(t.columns);
|
|
165
|
+
|
|
166
|
+
for (const k of cols) {
|
|
167
|
+
if (!base.includes(k)) base.push(k);
|
|
168
|
+
}
|
|
169
|
+
if (!base.includes("$")) base.push("$");
|
|
170
|
+
return base;
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
174
|
+
if (prop === "$") {
|
|
175
|
+
return {
|
|
176
|
+
configurable: true,
|
|
177
|
+
enumerable: false,
|
|
178
|
+
get() {
|
|
179
|
+
return (target as TableDef).columns;
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (
|
|
185
|
+
typeof prop === "string" &&
|
|
186
|
+
prop in (target as TableDef).columns &&
|
|
187
|
+
!Reflect.has(target, prop)
|
|
188
|
+
) {
|
|
189
|
+
return {
|
|
190
|
+
configurable: true,
|
|
191
|
+
enumerable: true,
|
|
192
|
+
value: (target as TableDef).columns[prop],
|
|
193
|
+
writable: false,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return Reflect.getOwnPropertyDescriptor(target, prop);
|
|
198
|
+
},
|
|
199
|
+
}) as TableRef<T>;
|
|
200
|
+
|
|
201
|
+
TABLE_REF_CACHE.set(table, proxy);
|
|
202
|
+
return proxy;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Public API: opt-in ergonomic table reference.
|
|
207
|
+
* Usage:
|
|
208
|
+
* const t = tableRef(todos);
|
|
209
|
+
* qb.where(eq(t.done, false)).orderBy(t.id, "ASC");
|
|
210
|
+
* Collisions:
|
|
211
|
+
* t.name is the table name (real field)
|
|
212
|
+
* t.$.name is the "name" column (escape hatch)
|
|
213
|
+
*/
|
|
214
|
+
export const tableRef = <T extends TableDef>(table: T): TableRef<T> => withColumnProps(table);
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Public API: dynamic column lookup by string key.
|
|
218
|
+
*
|
|
219
|
+
* Useful when the column name is only known at runtime, or when a column name
|
|
220
|
+
* collides with a real table field (e.g. "name").
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* const t = tableRef(todos);
|
|
225
|
+
* const key = runtimeKey();
|
|
226
|
+
* where(eq(getColumn(t, key), 123));
|
|
227
|
+
* // or: t.$.name (escape hatch)
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
export function getColumn<T extends TableDef, K extends keyof T['columns'] & string>(table: T, key: K): T['columns'][K];
|
|
231
|
+
export function getColumn<T extends TableDef>(table: T, key: string): ColumnDef;
|
|
232
|
+
export function getColumn<T extends TableDef>(table: T, key: string): ColumnDef {
|
|
233
|
+
const col = table.columns[key] as ColumnDef | undefined;
|
|
234
|
+
if (!col) {
|
|
235
|
+
const tableName = table.name || '<unknown>';
|
|
236
|
+
throw new Error(`Column '${key}' does not exist on table '${tableName}'`);
|
|
237
|
+
}
|
|
238
|
+
return col;
|
|
239
|
+
}
|
package/src/schema/types.ts
CHANGED
|
@@ -23,15 +23,15 @@ export type RelationTargetTable<TRel extends RelationDef> =
|
|
|
23
23
|
*/
|
|
24
24
|
export type ColumnToTs<T extends ColumnDef> =
|
|
25
25
|
[unknown] extends [T['tsType']]
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
26
|
+
? T['type'] extends 'INT' | 'INTEGER' | 'int' | 'integer' ? number :
|
|
27
|
+
T['type'] extends 'BIGINT' | 'bigint' ? number | bigint :
|
|
28
|
+
T['type'] extends 'DECIMAL' | 'decimal' | 'FLOAT' | 'float' | 'DOUBLE' | 'double' ? number :
|
|
29
|
+
T['type'] extends 'BOOLEAN' | 'boolean' ? boolean :
|
|
30
|
+
T['type'] extends 'JSON' | 'json' ? unknown :
|
|
31
|
+
T['type'] extends 'BLOB' | 'blob' | 'BINARY' | 'binary' | 'VARBINARY' | 'varbinary' | 'BYTEA' | 'bytea' ? Buffer :
|
|
32
|
+
T['type'] extends 'DATE' | 'date' | 'DATETIME' | 'datetime' | 'TIMESTAMP' | 'timestamp' | 'TIMESTAMPTZ' | 'timestamptz' ? string :
|
|
33
|
+
string
|
|
34
|
+
: Exclude<T['tsType'], undefined>;
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
37
|
* Infers a row shape from a table definition
|
|
@@ -41,10 +41,10 @@ export type InferRow<TTable extends TableDef> = {
|
|
|
41
41
|
};
|
|
42
42
|
|
|
43
43
|
type RelationResult<T extends RelationDef> =
|
|
44
|
-
T extends HasManyRelation<infer TTarget>
|
|
45
|
-
T extends HasOneRelation<infer TTarget>
|
|
46
|
-
T extends BelongsToRelation<infer TTarget>
|
|
47
|
-
T extends BelongsToManyRelation<infer TTarget>
|
|
44
|
+
T extends HasManyRelation<infer TTarget> ? InferRow<TTarget>[] :
|
|
45
|
+
T extends HasOneRelation<infer TTarget> ? InferRow<TTarget> | null :
|
|
46
|
+
T extends BelongsToRelation<infer TTarget> ? InferRow<TTarget> | null :
|
|
47
|
+
T extends BelongsToManyRelation<infer TTarget> ? (InferRow<TTarget> & { _pivot?: unknown })[] :
|
|
48
48
|
never;
|
|
49
49
|
|
|
50
50
|
/**
|
|
@@ -88,15 +88,15 @@ export type EntityInstance<
|
|
|
88
88
|
TRow = InferRow<TTable>
|
|
89
89
|
> = TRow & {
|
|
90
90
|
[K in keyof RelationMap<TTable>]:
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
91
|
+
TTable['relations'][K] extends HasManyRelation<infer TTarget>
|
|
92
|
+
? HasManyCollection<EntityInstance<TTarget>>
|
|
93
|
+
: TTable['relations'][K] extends HasOneRelation<infer TTarget>
|
|
94
|
+
? HasOneReference<EntityInstance<TTarget>>
|
|
95
|
+
: TTable['relations'][K] extends BelongsToManyRelation<infer TTarget>
|
|
96
|
+
? ManyToManyCollection<EntityInstance<TTarget>>
|
|
97
|
+
: TTable['relations'][K] extends BelongsToRelation<infer TTarget>
|
|
98
|
+
? BelongsToReference<EntityInstance<TTarget>>
|
|
99
|
+
: never;
|
|
100
100
|
} & {
|
|
101
101
|
$load<K extends keyof RelationMap<TTable>>(relation: K): Promise<RelationMap<TTable>[K]>;
|
|
102
102
|
};
|