metal-orm 1.0.34 → 1.0.36
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 +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +15 -10
- package/dist/index.d.ts +15 -10
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/decorators/bootstrap.ts +3 -3
- package/src/decorators/column.ts +3 -1
- package/src/orm/entity-metadata.ts +16 -9
- package/src/orm/entity.ts +7 -4
- package/src/schema/column.ts +216 -214
- package/src/schema/types.ts +10 -8
package/package.json
CHANGED
|
@@ -119,16 +119,16 @@ export const bootstrapEntities = (): TableDef[] => {
|
|
|
119
119
|
return metas.map(meta => meta.table!) as TableDef[];
|
|
120
120
|
};
|
|
121
121
|
|
|
122
|
-
export const getTableDefFromEntity = (ctor: EntityConstructor):
|
|
122
|
+
export const getTableDefFromEntity = <TTable extends TableDef = TableDef>(ctor: EntityConstructor): TTable | undefined => {
|
|
123
123
|
const meta = getEntityMetadata(ctor);
|
|
124
124
|
if (!meta) return undefined;
|
|
125
125
|
if (!meta.table) {
|
|
126
126
|
bootstrapEntities();
|
|
127
127
|
}
|
|
128
|
-
return meta.table;
|
|
128
|
+
return meta.table as TTable;
|
|
129
129
|
};
|
|
130
130
|
|
|
131
|
-
export const selectFromEntity = <TTable extends TableDef>(
|
|
131
|
+
export const selectFromEntity = <TTable extends TableDef = TableDef>(
|
|
132
132
|
ctor: EntityConstructor
|
|
133
133
|
): SelectQueryBuilder<any, TTable> => {
|
|
134
134
|
const table = getTableDefFromEntity(ctor);
|
package/src/decorators/column.ts
CHANGED
|
@@ -18,9 +18,10 @@ export interface ColumnOptions {
|
|
|
18
18
|
args?: ColumnDef['args'];
|
|
19
19
|
notNull?: boolean;
|
|
20
20
|
primary?: boolean;
|
|
21
|
+
tsType?: ColumnDef['tsType'];
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
export type ColumnInput = ColumnOptions | ColumnDef
|
|
24
|
+
export type ColumnInput = ColumnOptions | ColumnDef<any, any>;
|
|
24
25
|
|
|
25
26
|
const normalizeColumnInput = (input: ColumnInput): ColumnDefLike => {
|
|
26
27
|
const asOptions = input as ColumnOptions;
|
|
@@ -30,6 +31,7 @@ const normalizeColumnInput = (input: ColumnInput): ColumnDefLike => {
|
|
|
30
31
|
args: asOptions.args ?? asDefinition.args,
|
|
31
32
|
notNull: asOptions.notNull ?? asDefinition.notNull,
|
|
32
33
|
primary: asOptions.primary ?? asDefinition.primary,
|
|
34
|
+
tsType: asDefinition.tsType ?? asOptions.tsType,
|
|
33
35
|
unique: asDefinition.unique,
|
|
34
36
|
default: asDefinition.default,
|
|
35
37
|
autoIncrement: asDefinition.autoIncrement,
|
|
@@ -6,7 +6,14 @@ export type EntityConstructor = new (...args: any[]) => any;
|
|
|
6
6
|
export type EntityOrTableTarget = EntityConstructor | TableDef;
|
|
7
7
|
export type EntityOrTableTargetResolver = EntityOrTableTarget | (() => EntityOrTableTarget);
|
|
8
8
|
|
|
9
|
-
export type ColumnDefLike = Omit<
|
|
9
|
+
export type ColumnDefLike<T extends ColumnDef = ColumnDef> = Omit<T, 'name' | 'table'>;
|
|
10
|
+
|
|
11
|
+
type MaterializeColumns<TColumns extends Record<string, ColumnDefLike>> = {
|
|
12
|
+
[K in keyof TColumns]: ColumnDef<TColumns[K]['type'], TColumns[K]['tsType']> & Omit<
|
|
13
|
+
TColumns[K],
|
|
14
|
+
'name' | 'table' | 'type' | 'tsType'
|
|
15
|
+
> & { name: string; table: string };
|
|
16
|
+
};
|
|
10
17
|
|
|
11
18
|
interface BaseRelationMetadata {
|
|
12
19
|
propertyKey: string;
|
|
@@ -49,13 +56,13 @@ export type RelationMetadata =
|
|
|
49
56
|
| BelongsToRelationMetadata
|
|
50
57
|
| BelongsToManyRelationMetadata;
|
|
51
58
|
|
|
52
|
-
export interface EntityMetadata {
|
|
59
|
+
export interface EntityMetadata<TColumns extends Record<string, ColumnDefLike> = Record<string, ColumnDefLike>> {
|
|
53
60
|
target: EntityConstructor;
|
|
54
61
|
tableName: string;
|
|
55
|
-
columns:
|
|
62
|
+
columns: TColumns;
|
|
56
63
|
relations: Record<string, RelationMetadata>;
|
|
57
64
|
hooks?: TableHooks;
|
|
58
|
-
table?: TableDef
|
|
65
|
+
table?: TableDef<MaterializeColumns<TColumns>>;
|
|
59
66
|
}
|
|
60
67
|
|
|
61
68
|
const metadataMap = new Map<EntityConstructor, EntityMetadata>();
|
|
@@ -96,7 +103,7 @@ export const addColumnMetadata = (
|
|
|
96
103
|
column: ColumnDefLike
|
|
97
104
|
): void => {
|
|
98
105
|
const meta = ensureEntityMetadata(target);
|
|
99
|
-
meta.columns[propertyKey] = { ...column };
|
|
106
|
+
(meta.columns as Record<string, ColumnDefLike>)[propertyKey] = { ...column };
|
|
100
107
|
};
|
|
101
108
|
|
|
102
109
|
export const addRelationMetadata = (
|
|
@@ -122,19 +129,19 @@ export const setEntityTableName = (
|
|
|
122
129
|
}
|
|
123
130
|
};
|
|
124
131
|
|
|
125
|
-
export const buildTableDef = (meta: EntityMetadata): TableDef => {
|
|
132
|
+
export const buildTableDef = <TColumns extends Record<string, ColumnDefLike>>(meta: EntityMetadata<TColumns>): TableDef<MaterializeColumns<TColumns>> => {
|
|
126
133
|
if (meta.table) {
|
|
127
134
|
return meta.table;
|
|
128
135
|
}
|
|
129
136
|
|
|
130
|
-
const columns = Object.entries(meta.columns).reduce<
|
|
131
|
-
acc[key] = {
|
|
137
|
+
const columns = Object.entries(meta.columns).reduce<MaterializeColumns<TColumns>>((acc, [key, def]) => {
|
|
138
|
+
(acc as any)[key] = {
|
|
132
139
|
...def,
|
|
133
140
|
name: key,
|
|
134
141
|
table: meta.tableName
|
|
135
142
|
};
|
|
136
143
|
return acc;
|
|
137
|
-
}, {});
|
|
144
|
+
}, {} as MaterializeColumns<TColumns>);
|
|
138
145
|
|
|
139
146
|
const table = defineTable(meta.tableName, columns, {}, meta.hooks);
|
|
140
147
|
meta.table = table;
|
package/src/orm/entity.ts
CHANGED
|
@@ -104,17 +104,20 @@ export const createEntityProxy = <
|
|
|
104
104
|
return proxy;
|
|
105
105
|
};
|
|
106
106
|
|
|
107
|
-
export const createEntityFromRow = <
|
|
107
|
+
export const createEntityFromRow = <
|
|
108
|
+
TTable extends TableDef,
|
|
109
|
+
TResult extends EntityInstance<TTable> = EntityInstance<TTable>
|
|
110
|
+
>(
|
|
108
111
|
ctx: EntityContext,
|
|
109
112
|
table: TTable,
|
|
110
113
|
row: Record<string, any>,
|
|
111
114
|
lazyRelations: (keyof RelationMap<TTable>)[] = []
|
|
112
|
-
):
|
|
115
|
+
): TResult => {
|
|
113
116
|
const pkName = findPrimaryKey(table);
|
|
114
117
|
const pkValue = row[pkName];
|
|
115
118
|
if (pkValue !== undefined && pkValue !== null) {
|
|
116
119
|
const tracked = ctx.getEntity(table, pkValue);
|
|
117
|
-
if (tracked) return tracked;
|
|
120
|
+
if (tracked) return tracked as TResult;
|
|
118
121
|
}
|
|
119
122
|
|
|
120
123
|
const entity = createEntityProxy(ctx, table, row, lazyRelations);
|
|
@@ -124,7 +127,7 @@ export const createEntityFromRow = <TTable extends TableDef>(
|
|
|
124
127
|
ctx.trackNew(table, entity);
|
|
125
128
|
}
|
|
126
129
|
|
|
127
|
-
return entity;
|
|
130
|
+
return entity as TResult;
|
|
128
131
|
};
|
|
129
132
|
|
|
130
133
|
const toKey = (value: unknown): string => (value === null || value === undefined ? '' : String(value));
|
package/src/schema/column.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Supported column data types for database schema definitions
|
|
3
|
-
*/
|
|
1
|
+
/**
|
|
2
|
+
* Supported column data types for database schema definitions
|
|
3
|
+
*/
|
|
4
4
|
export type ColumnType =
|
|
5
5
|
| 'INT'
|
|
6
6
|
| 'INTEGER'
|
|
@@ -42,110 +42,112 @@ export type ColumnType =
|
|
|
42
42
|
| 'timestamp'
|
|
43
43
|
| 'timestamptz'
|
|
44
44
|
| 'boolean';
|
|
45
|
-
|
|
46
|
-
export type ReferentialAction =
|
|
47
|
-
| 'NO ACTION'
|
|
48
|
-
| 'RESTRICT'
|
|
49
|
-
| 'CASCADE'
|
|
50
|
-
| 'SET NULL'
|
|
51
|
-
| 'SET DEFAULT';
|
|
52
|
-
|
|
53
|
-
export interface RawDefaultValue {
|
|
54
|
-
raw: string;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export type DefaultValue = unknown | RawDefaultValue;
|
|
58
|
-
|
|
59
|
-
export interface ForeignKeyReference {
|
|
60
|
-
/** Target table name */
|
|
61
|
-
table: string;
|
|
62
|
-
/** Target column name */
|
|
63
|
-
column: string;
|
|
64
|
-
/** Optional constraint name */
|
|
65
|
-
name?: string;
|
|
66
|
-
/** ON DELETE action */
|
|
67
|
-
onDelete?: ReferentialAction;
|
|
68
|
-
/** ON UPDATE action */
|
|
69
|
-
onUpdate?: ReferentialAction;
|
|
70
|
-
/** Whether the constraint is deferrable (Postgres) */
|
|
71
|
-
deferrable?: boolean;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Definition of a database column
|
|
76
|
-
*/
|
|
77
|
-
export interface ColumnDef<T extends ColumnType = ColumnType> {
|
|
78
|
-
/** Column name (filled at runtime by defineTable) */
|
|
79
|
-
name: string;
|
|
80
|
-
/** Data type of the column */
|
|
81
|
-
type: T;
|
|
82
|
-
/**
|
|
83
|
-
|
|
84
|
-
/** Whether this column
|
|
85
|
-
|
|
86
|
-
/** Whether this column
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
*
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
45
|
+
|
|
46
|
+
export type ReferentialAction =
|
|
47
|
+
| 'NO ACTION'
|
|
48
|
+
| 'RESTRICT'
|
|
49
|
+
| 'CASCADE'
|
|
50
|
+
| 'SET NULL'
|
|
51
|
+
| 'SET DEFAULT';
|
|
52
|
+
|
|
53
|
+
export interface RawDefaultValue {
|
|
54
|
+
raw: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type DefaultValue = unknown | RawDefaultValue;
|
|
58
|
+
|
|
59
|
+
export interface ForeignKeyReference {
|
|
60
|
+
/** Target table name */
|
|
61
|
+
table: string;
|
|
62
|
+
/** Target column name */
|
|
63
|
+
column: string;
|
|
64
|
+
/** Optional constraint name */
|
|
65
|
+
name?: string;
|
|
66
|
+
/** ON DELETE action */
|
|
67
|
+
onDelete?: ReferentialAction;
|
|
68
|
+
/** ON UPDATE action */
|
|
69
|
+
onUpdate?: ReferentialAction;
|
|
70
|
+
/** Whether the constraint is deferrable (Postgres) */
|
|
71
|
+
deferrable?: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Definition of a database column
|
|
76
|
+
*/
|
|
77
|
+
export interface ColumnDef<T extends ColumnType = ColumnType, TRuntime = unknown> {
|
|
78
|
+
/** Column name (filled at runtime by defineTable) */
|
|
79
|
+
name: string;
|
|
80
|
+
/** Data type of the column */
|
|
81
|
+
type: T;
|
|
82
|
+
/** Optional override for the inferred TypeScript type */
|
|
83
|
+
tsType?: TRuntime;
|
|
84
|
+
/** Whether this column is a primary key */
|
|
85
|
+
primary?: boolean;
|
|
86
|
+
/** Whether this column cannot be null */
|
|
87
|
+
notNull?: boolean;
|
|
88
|
+
/** Whether this column must be unique (or name of the unique constraint) */
|
|
89
|
+
unique?: boolean | string;
|
|
90
|
+
/** Default value for the column */
|
|
91
|
+
default?: DefaultValue;
|
|
92
|
+
/** Whether the column auto-increments / identity */
|
|
93
|
+
autoIncrement?: boolean;
|
|
94
|
+
/** Identity strategy where supported */
|
|
95
|
+
generated?: 'always' | 'byDefault';
|
|
96
|
+
/** Inline check constraint expression */
|
|
97
|
+
check?: string;
|
|
98
|
+
/** Foreign key reference */
|
|
99
|
+
references?: ForeignKeyReference;
|
|
100
|
+
/** Column comment/description */
|
|
101
|
+
comment?: string;
|
|
102
|
+
/** Additional arguments for the column type (e.g., VARCHAR length) */
|
|
103
|
+
args?: any[];
|
|
104
|
+
/** Table name this column belongs to (filled at runtime by defineTable) */
|
|
105
|
+
table?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Factory for creating column definitions with common data types
|
|
110
|
+
*/
|
|
111
|
+
export const col = {
|
|
112
|
+
/**
|
|
113
|
+
* Creates an integer column definition
|
|
114
|
+
* @returns ColumnDef with INT type
|
|
115
|
+
*/
|
|
116
|
+
int: (): ColumnDef<'INT'> => ({ name: '', type: 'INT' }),
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Creates a big integer column definition
|
|
120
|
+
*/
|
|
121
|
+
bigint: (): ColumnDef<'BIGINT'> => ({ name: '', type: 'BIGINT' }),
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Creates a variable character column definition
|
|
125
|
+
* @param length - Maximum length of the string
|
|
126
|
+
* @returns ColumnDef with VARCHAR type
|
|
127
|
+
*/
|
|
128
|
+
varchar: (length: number): ColumnDef<'VARCHAR'> => ({ name: '', type: 'VARCHAR', args: [length] }),
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Creates a fixed precision decimal column definition
|
|
132
|
+
*/
|
|
133
|
+
decimal: (precision: number, scale = 0): ColumnDef<'DECIMAL'> => ({
|
|
134
|
+
name: '',
|
|
135
|
+
type: 'DECIMAL',
|
|
136
|
+
args: [precision, scale]
|
|
137
|
+
}),
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Creates a floating point column definition
|
|
141
|
+
*/
|
|
142
|
+
float: (precision?: number): ColumnDef<'FLOAT'> => ({
|
|
143
|
+
name: '',
|
|
144
|
+
type: 'FLOAT',
|
|
145
|
+
args: precision !== undefined ? [precision] : undefined
|
|
146
|
+
}),
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Creates a UUID column definition
|
|
150
|
+
*/
|
|
149
151
|
uuid: (): ColumnDef<'UUID'> => ({ name: '', type: 'UUID' }),
|
|
150
152
|
|
|
151
153
|
/**
|
|
@@ -179,110 +181,110 @@ export const col = {
|
|
|
179
181
|
/**
|
|
180
182
|
* Creates a timestamp column definition
|
|
181
183
|
*/
|
|
182
|
-
timestamp: (): ColumnDef<'TIMESTAMP'> => ({ name: '', type: 'TIMESTAMP' }),
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Creates a timestamptz column definition
|
|
186
|
-
*/
|
|
187
|
-
timestamptz: (): ColumnDef<'TIMESTAMPTZ'> => ({ name: '', type: 'TIMESTAMPTZ' }),
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Creates a date column definition
|
|
191
|
-
*/
|
|
192
|
-
date: (): ColumnDef<'DATE'> => ({ name: '', type: 'DATE' }),
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Creates a datetime column definition
|
|
196
|
-
*/
|
|
197
|
-
datetime: (): ColumnDef<'DATETIME'> => ({ name: '', type: 'DATETIME' }),
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Creates a JSON column definition
|
|
201
|
-
* @returns ColumnDef with JSON type
|
|
202
|
-
*/
|
|
203
|
-
json: (): ColumnDef<'JSON'> => ({ name: '', type: 'JSON' }),
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Creates a boolean column definition
|
|
207
|
-
* @returns ColumnDef with BOOLEAN type
|
|
208
|
-
*/
|
|
209
|
-
boolean: (): ColumnDef<'BOOLEAN'> => ({ name: '', type: 'BOOLEAN' }),
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Creates an enum column definition
|
|
213
|
-
* @param values - Enum values
|
|
214
|
-
*/
|
|
215
|
-
enum: (values: string[]): ColumnDef<'ENUM'> => ({ name: '', type: 'ENUM', args: values }),
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Marks a column definition as a primary key
|
|
219
|
-
* @param def - Column definition to modify
|
|
220
|
-
* @returns Modified ColumnDef with primary: true
|
|
221
|
-
*/
|
|
222
|
-
primaryKey: <T extends ColumnType>(def: ColumnDef<T>): ColumnDef<T> =>
|
|
223
|
-
({ ...def, primary: true }),
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Marks a column as NOT NULL
|
|
227
|
-
*/
|
|
228
|
-
notNull: <T extends ColumnType>(def: ColumnDef<T>): ColumnDef<T> =>
|
|
229
|
-
({ ...def, notNull: true }),
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Marks a column as UNIQUE
|
|
233
|
-
*/
|
|
234
|
-
unique: <T extends ColumnType>(def: ColumnDef<T>, name?: string): ColumnDef<T> =>
|
|
235
|
-
({
|
|
236
|
-
...def,
|
|
237
|
-
unique: name ?? true
|
|
238
|
-
}),
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Sets a default value for the column
|
|
242
|
-
*/
|
|
243
|
-
default: <T extends ColumnType>(def: ColumnDef<T>, value: unknown): ColumnDef<T> =>
|
|
244
|
-
({
|
|
245
|
-
...def,
|
|
246
|
-
default: value
|
|
247
|
-
}),
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Sets a raw SQL default value for the column
|
|
251
|
-
*/
|
|
252
|
-
defaultRaw: <T extends ColumnType>(def: ColumnDef<T>, expression: string): ColumnDef<T> =>
|
|
253
|
-
({
|
|
254
|
-
...def,
|
|
255
|
-
default: { raw: expression }
|
|
256
|
-
}),
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Marks a column as auto-increment / identity
|
|
260
|
-
*/
|
|
261
|
-
autoIncrement: <T extends ColumnType>(
|
|
262
|
-
def: ColumnDef<T>,
|
|
263
|
-
strategy: ColumnDef['generated'] = 'byDefault'
|
|
264
|
-
): ColumnDef<T> =>
|
|
265
|
-
({
|
|
266
|
-
...def,
|
|
267
|
-
autoIncrement: true,
|
|
268
|
-
generated: strategy
|
|
269
|
-
}),
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Adds a foreign key reference
|
|
273
|
-
*/
|
|
274
|
-
references: <T extends ColumnType>(def: ColumnDef<T>, ref: ForeignKeyReference): ColumnDef<T> =>
|
|
275
|
-
({
|
|
276
|
-
...def,
|
|
277
|
-
references: ref
|
|
278
|
-
}),
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Adds a check constraint to the column
|
|
282
|
-
*/
|
|
283
|
-
check: <T extends ColumnType>(def: ColumnDef<T>, expression: string): ColumnDef<T> =>
|
|
284
|
-
({
|
|
285
|
-
...def,
|
|
286
|
-
check: expression
|
|
287
|
-
})
|
|
288
|
-
};
|
|
184
|
+
timestamp: <TRuntime = string>(): ColumnDef<'TIMESTAMP', TRuntime> => ({ name: '', type: 'TIMESTAMP' }),
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Creates a timestamptz column definition
|
|
188
|
+
*/
|
|
189
|
+
timestamptz: <TRuntime = string>(): ColumnDef<'TIMESTAMPTZ', TRuntime> => ({ name: '', type: 'TIMESTAMPTZ' }),
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Creates a date column definition
|
|
193
|
+
*/
|
|
194
|
+
date: <TRuntime = string>(): ColumnDef<'DATE', TRuntime> => ({ name: '', type: 'DATE' }),
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Creates a datetime column definition
|
|
198
|
+
*/
|
|
199
|
+
datetime: <TRuntime = string>(): ColumnDef<'DATETIME', TRuntime> => ({ name: '', type: 'DATETIME' }),
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Creates a JSON column definition
|
|
203
|
+
* @returns ColumnDef with JSON type
|
|
204
|
+
*/
|
|
205
|
+
json: (): ColumnDef<'JSON'> => ({ name: '', type: 'JSON' }),
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Creates a boolean column definition
|
|
209
|
+
* @returns ColumnDef with BOOLEAN type
|
|
210
|
+
*/
|
|
211
|
+
boolean: (): ColumnDef<'BOOLEAN'> => ({ name: '', type: 'BOOLEAN' }),
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Creates an enum column definition
|
|
215
|
+
* @param values - Enum values
|
|
216
|
+
*/
|
|
217
|
+
enum: (values: string[]): ColumnDef<'ENUM'> => ({ name: '', type: 'ENUM', args: values }),
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Marks a column definition as a primary key
|
|
221
|
+
* @param def - Column definition to modify
|
|
222
|
+
* @returns Modified ColumnDef with primary: true
|
|
223
|
+
*/
|
|
224
|
+
primaryKey: <T extends ColumnType>(def: ColumnDef<T>): ColumnDef<T> =>
|
|
225
|
+
({ ...def, primary: true }),
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Marks a column as NOT NULL
|
|
229
|
+
*/
|
|
230
|
+
notNull: <T extends ColumnType>(def: ColumnDef<T>): ColumnDef<T> =>
|
|
231
|
+
({ ...def, notNull: true }),
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Marks a column as UNIQUE
|
|
235
|
+
*/
|
|
236
|
+
unique: <T extends ColumnType>(def: ColumnDef<T>, name?: string): ColumnDef<T> =>
|
|
237
|
+
({
|
|
238
|
+
...def,
|
|
239
|
+
unique: name ?? true
|
|
240
|
+
}),
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Sets a default value for the column
|
|
244
|
+
*/
|
|
245
|
+
default: <T extends ColumnType>(def: ColumnDef<T>, value: unknown): ColumnDef<T> =>
|
|
246
|
+
({
|
|
247
|
+
...def,
|
|
248
|
+
default: value
|
|
249
|
+
}),
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Sets a raw SQL default value for the column
|
|
253
|
+
*/
|
|
254
|
+
defaultRaw: <T extends ColumnType>(def: ColumnDef<T>, expression: string): ColumnDef<T> =>
|
|
255
|
+
({
|
|
256
|
+
...def,
|
|
257
|
+
default: { raw: expression }
|
|
258
|
+
}),
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Marks a column as auto-increment / identity
|
|
262
|
+
*/
|
|
263
|
+
autoIncrement: <T extends ColumnType>(
|
|
264
|
+
def: ColumnDef<T>,
|
|
265
|
+
strategy: ColumnDef['generated'] = 'byDefault'
|
|
266
|
+
): ColumnDef<T> =>
|
|
267
|
+
({
|
|
268
|
+
...def,
|
|
269
|
+
autoIncrement: true,
|
|
270
|
+
generated: strategy
|
|
271
|
+
}),
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Adds a foreign key reference
|
|
275
|
+
*/
|
|
276
|
+
references: <T extends ColumnType>(def: ColumnDef<T>, ref: ForeignKeyReference): ColumnDef<T> =>
|
|
277
|
+
({
|
|
278
|
+
...def,
|
|
279
|
+
references: ref
|
|
280
|
+
}),
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Adds a check constraint to the column
|
|
284
|
+
*/
|
|
285
|
+
check: <T extends ColumnType>(def: ColumnDef<T>, expression: string): ColumnDef<T> =>
|
|
286
|
+
({
|
|
287
|
+
...def,
|
|
288
|
+
check: expression
|
|
289
|
+
})
|
|
290
|
+
};
|
package/src/schema/types.ts
CHANGED
|
@@ -22,14 +22,16 @@ export type RelationTargetTable<TRel extends RelationDef> =
|
|
|
22
22
|
* Maps a ColumnDef to its TypeScript type representation
|
|
23
23
|
*/
|
|
24
24
|
export type ColumnToTs<T extends ColumnDef> =
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
25
|
+
[unknown] extends [T['tsType']]
|
|
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>;
|
|
33
35
|
|
|
34
36
|
/**
|
|
35
37
|
* Infers a row shape from a table definition
|