metal-orm 1.0.41 → 1.0.42
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 +53 -14
- package/dist/index.cjs +54 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +22 -1
- package/dist/index.d.ts +22 -1
- package/dist/index.js +53 -4
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/core/ast/expression.ts +2 -2
- package/src/schema/table.ts +210 -115
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from './expression-nodes.js';
|
|
1
|
+
export * from './expression-nodes.js';
|
|
2
2
|
export * from './expression-builders.js';
|
|
3
3
|
export * from './window-functions.js';
|
|
4
4
|
export * from './aggregate-functions.js';
|
|
5
5
|
export * from './expression-visitor.js';
|
|
6
|
-
export
|
|
6
|
+
export type { ColumnRef, TableRef as AstTableRef } from './types.js';
|
|
7
7
|
export * from './adapters.js';
|
package/src/schema/table.ts
CHANGED
|
@@ -1,115 +1,210 @@
|
|
|
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: any): Promise<void> | void;
|
|
35
|
-
afterInsert?(ctx: unknown, entity: any): Promise<void> | void;
|
|
36
|
-
beforeUpdate?(ctx: unknown, entity: any): Promise<void> | void;
|
|
37
|
-
afterUpdate?(ctx: unknown, entity: any): Promise<void> | void;
|
|
38
|
-
beforeDelete?(ctx: unknown, entity: any): Promise<void> | void;
|
|
39
|
-
afterDelete?(ctx: unknown, entity: any): 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
|
-
|
|
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
|
-
(acc as any)[key] = { ...def, name: key, table: name };
|
|
98
|
-
return acc;
|
|
99
|
-
}, {} as T);
|
|
100
|
-
|
|
101
|
-
return {
|
|
102
|
-
name,
|
|
103
|
-
schema: options.schema,
|
|
104
|
-
columns: colsWithNames,
|
|
105
|
-
relations,
|
|
106
|
-
hooks,
|
|
107
|
-
primaryKey: options.primaryKey,
|
|
108
|
-
indexes: options.indexes,
|
|
109
|
-
checks: options.checks,
|
|
110
|
-
comment: options.comment,
|
|
111
|
-
engine: options.engine,
|
|
112
|
-
charset: options.charset,
|
|
113
|
-
collation: options.collation
|
|
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: any): Promise<void> | void;
|
|
35
|
+
afterInsert?(ctx: unknown, entity: any): Promise<void> | void;
|
|
36
|
+
beforeUpdate?(ctx: unknown, entity: any): Promise<void> | void;
|
|
37
|
+
afterUpdate?(ctx: unknown, entity: any): Promise<void> | void;
|
|
38
|
+
beforeDelete?(ctx: unknown, entity: any): Promise<void> | void;
|
|
39
|
+
afterDelete?(ctx: unknown, entity: any): 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
|
+
(acc as any)[key] = { ...def, name: key, table: name };
|
|
98
|
+
return acc;
|
|
99
|
+
}, {} as T);
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
name,
|
|
103
|
+
schema: options.schema,
|
|
104
|
+
columns: colsWithNames,
|
|
105
|
+
relations,
|
|
106
|
+
hooks,
|
|
107
|
+
primaryKey: options.primaryKey,
|
|
108
|
+
indexes: options.indexes,
|
|
109
|
+
checks: options.checks,
|
|
110
|
+
comment: options.comment,
|
|
111
|
+
engine: options.engine,
|
|
112
|
+
charset: options.charset,
|
|
113
|
+
collation: options.collation
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
type DirectColumnKeys<T extends TableDef> =
|
|
118
|
+
Exclude<keyof T["columns"] & string, keyof T | "$">;
|
|
119
|
+
|
|
120
|
+
export type TableRef<T extends TableDef> =
|
|
121
|
+
T &
|
|
122
|
+
{ [K in DirectColumnKeys<T>]: T["columns"][K] } & {
|
|
123
|
+
/**
|
|
124
|
+
* Escape hatch for collisions:
|
|
125
|
+
* - tref.name => table name (string)
|
|
126
|
+
* - tref.$.name => column def for "name"
|
|
127
|
+
*/
|
|
128
|
+
$: T["columns"];
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const TABLE_REF_CACHE: WeakMap<object, any> = new WeakMap();
|
|
132
|
+
|
|
133
|
+
const withColumnProps = <T extends TableDef>(table: T): TableRef<T> => {
|
|
134
|
+
const cached = TABLE_REF_CACHE.get(table as any);
|
|
135
|
+
if (cached) return cached as TableRef<T>;
|
|
136
|
+
|
|
137
|
+
const proxy = new Proxy(table as any, {
|
|
138
|
+
get(target, prop, receiver) {
|
|
139
|
+
if (prop === "$") return target.columns;
|
|
140
|
+
|
|
141
|
+
// Prefer real table fields first (prevents collision surprises)
|
|
142
|
+
if (Reflect.has(target, prop)) return Reflect.get(target, prop, receiver);
|
|
143
|
+
|
|
144
|
+
// Fall back to columns bag
|
|
145
|
+
if (typeof prop === "string" && prop in target.columns) return target.columns[prop];
|
|
146
|
+
|
|
147
|
+
return undefined;
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
has(target, prop) {
|
|
151
|
+
return (
|
|
152
|
+
prop === "$" ||
|
|
153
|
+
Reflect.has(target, prop) ||
|
|
154
|
+
(typeof prop === "string" && prop in target.columns)
|
|
155
|
+
);
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
ownKeys(target) {
|
|
159
|
+
const base = Reflect.ownKeys(target);
|
|
160
|
+
const cols = Object.keys(target.columns);
|
|
161
|
+
|
|
162
|
+
for (const k of cols) {
|
|
163
|
+
if (!base.includes(k)) base.push(k);
|
|
164
|
+
}
|
|
165
|
+
if (!base.includes("$")) base.push("$");
|
|
166
|
+
return base;
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
170
|
+
if (prop === "$") {
|
|
171
|
+
return {
|
|
172
|
+
configurable: true,
|
|
173
|
+
enumerable: false,
|
|
174
|
+
get() {
|
|
175
|
+
return target.columns;
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (
|
|
181
|
+
typeof prop === "string" &&
|
|
182
|
+
prop in target.columns &&
|
|
183
|
+
!Reflect.has(target, prop)
|
|
184
|
+
) {
|
|
185
|
+
return {
|
|
186
|
+
configurable: true,
|
|
187
|
+
enumerable: true,
|
|
188
|
+
value: target.columns[prop],
|
|
189
|
+
writable: false,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return Reflect.getOwnPropertyDescriptor(target, prop);
|
|
194
|
+
},
|
|
195
|
+
}) as TableRef<T>;
|
|
196
|
+
|
|
197
|
+
TABLE_REF_CACHE.set(table as any, proxy);
|
|
198
|
+
return proxy;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Public API: opt-in ergonomic table reference.
|
|
203
|
+
* Usage:
|
|
204
|
+
* const t = tableRef(todos);
|
|
205
|
+
* qb.where(eq(t.done, false)).orderBy(t.id, "ASC");
|
|
206
|
+
* Collisions:
|
|
207
|
+
* t.name is the table name (real field)
|
|
208
|
+
* t.$.name is the "name" column (escape hatch)
|
|
209
|
+
*/
|
|
210
|
+
export const tableRef = <T extends TableDef>(table: T): TableRef<T> => withColumnProps(table);
|