metal-orm 1.0.14 → 1.0.15

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 (115) hide show
  1. package/README.md +40 -45
  2. package/dist/decorators/index.cjs +1600 -27
  3. package/dist/decorators/index.cjs.map +1 -1
  4. package/dist/decorators/index.d.cts +6 -2
  5. package/dist/decorators/index.d.ts +6 -2
  6. package/dist/decorators/index.js +1599 -27
  7. package/dist/decorators/index.js.map +1 -1
  8. package/dist/index.cjs +4608 -3429
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.cts +511 -159
  11. package/dist/index.d.ts +511 -159
  12. package/dist/index.js +4526 -3415
  13. package/dist/index.js.map +1 -1
  14. package/dist/{select-CCp1oz9p.d.cts → select-Bkv8g8u_.d.cts} +193 -67
  15. package/dist/{select-CCp1oz9p.d.ts → select-Bkv8g8u_.d.ts} +193 -67
  16. package/package.json +1 -1
  17. package/src/codegen/typescript.ts +38 -35
  18. package/src/core/ast/adapters.ts +21 -0
  19. package/src/core/ast/aggregate-functions.ts +13 -13
  20. package/src/core/ast/builders.ts +56 -43
  21. package/src/core/ast/expression-builders.ts +34 -34
  22. package/src/core/ast/expression-nodes.ts +18 -16
  23. package/src/core/ast/expression-visitor.ts +122 -69
  24. package/src/core/ast/expression.ts +6 -4
  25. package/src/core/ast/join-metadata.ts +15 -0
  26. package/src/core/ast/join-node.ts +22 -20
  27. package/src/core/ast/join.ts +5 -5
  28. package/src/core/ast/query.ts +52 -88
  29. package/src/core/ast/types.ts +20 -0
  30. package/src/core/ast/window-functions.ts +55 -55
  31. package/src/core/ddl/dialects/base-schema-dialect.ts +20 -6
  32. package/src/core/ddl/dialects/mssql-schema-dialect.ts +32 -8
  33. package/src/core/ddl/dialects/mysql-schema-dialect.ts +21 -10
  34. package/src/core/ddl/dialects/postgres-schema-dialect.ts +52 -7
  35. package/src/core/ddl/dialects/sqlite-schema-dialect.ts +23 -9
  36. package/src/core/ddl/introspect/catalogs/index.ts +1 -0
  37. package/src/core/ddl/introspect/catalogs/postgres.ts +143 -0
  38. package/src/core/ddl/introspect/context.ts +9 -0
  39. package/src/core/ddl/introspect/functions/postgres.ts +26 -0
  40. package/src/core/ddl/introspect/mssql.ts +149 -149
  41. package/src/core/ddl/introspect/mysql.ts +99 -99
  42. package/src/core/ddl/introspect/postgres.ts +245 -154
  43. package/src/core/ddl/introspect/registry.ts +26 -0
  44. package/src/core/ddl/introspect/run-select.ts +25 -0
  45. package/src/core/ddl/introspect/sqlite.ts +7 -7
  46. package/src/core/ddl/introspect/types.ts +23 -19
  47. package/src/core/ddl/introspect/utils.ts +1 -1
  48. package/src/core/ddl/naming-strategy.ts +10 -0
  49. package/src/core/ddl/schema-dialect.ts +41 -0
  50. package/src/core/ddl/schema-diff.ts +211 -179
  51. package/src/core/ddl/schema-generator.ts +16 -90
  52. package/src/core/ddl/schema-introspect.ts +25 -32
  53. package/src/core/ddl/schema-plan-executor.ts +17 -0
  54. package/src/core/ddl/schema-types.ts +46 -39
  55. package/src/core/ddl/sql-writing.ts +170 -0
  56. package/src/core/dialect/abstract.ts +144 -126
  57. package/src/core/dialect/base/cte-compiler.ts +33 -0
  58. package/src/core/dialect/base/function-table-formatter.ts +132 -0
  59. package/src/core/dialect/base/groupby-compiler.ts +21 -0
  60. package/src/core/dialect/base/join-compiler.ts +26 -0
  61. package/src/core/dialect/base/orderby-compiler.ts +21 -0
  62. package/src/core/dialect/base/pagination-strategy.ts +32 -0
  63. package/src/core/dialect/base/returning-strategy.ts +56 -0
  64. package/src/core/dialect/base/sql-dialect.ts +181 -204
  65. package/src/core/dialect/dialect-factory.ts +91 -0
  66. package/src/core/dialect/mssql/functions.ts +101 -0
  67. package/src/core/dialect/mssql/index.ts +128 -126
  68. package/src/core/dialect/mysql/functions.ts +101 -0
  69. package/src/core/dialect/mysql/index.ts +20 -18
  70. package/src/core/dialect/postgres/functions.ts +95 -0
  71. package/src/core/dialect/postgres/index.ts +30 -28
  72. package/src/core/dialect/sqlite/functions.ts +115 -0
  73. package/src/core/dialect/sqlite/index.ts +30 -28
  74. package/src/core/driver/database-driver.ts +11 -0
  75. package/src/core/driver/mssql-driver.ts +20 -0
  76. package/src/core/driver/mysql-driver.ts +20 -0
  77. package/src/core/driver/postgres-driver.ts +20 -0
  78. package/src/core/driver/sqlite-driver.ts +20 -0
  79. package/src/core/execution/db-executor.ts +63 -0
  80. package/src/core/execution/executors/mssql-executor.ts +39 -0
  81. package/src/core/execution/executors/mysql-executor.ts +47 -0
  82. package/src/core/execution/executors/postgres-executor.ts +32 -0
  83. package/src/core/execution/executors/sqlite-executor.ts +31 -0
  84. package/src/core/functions/datetime.ts +132 -0
  85. package/src/core/functions/numeric.ts +179 -0
  86. package/src/core/functions/standard-strategy.ts +47 -0
  87. package/src/core/functions/text.ts +147 -0
  88. package/src/core/functions/types.ts +18 -0
  89. package/src/core/hydration/types.ts +57 -0
  90. package/src/decorators/bootstrap.ts +10 -0
  91. package/src/decorators/relations.ts +15 -0
  92. package/src/index.ts +30 -19
  93. package/src/orm/entity-metadata.ts +7 -0
  94. package/src/orm/entity.ts +58 -27
  95. package/src/orm/hydration.ts +25 -17
  96. package/src/orm/lazy-batch.ts +46 -2
  97. package/src/orm/orm-context.ts +60 -60
  98. package/src/orm/query-logger.ts +1 -1
  99. package/src/orm/relation-change-processor.ts +43 -2
  100. package/src/orm/relations/has-one.ts +139 -0
  101. package/src/orm/transaction-runner.ts +1 -1
  102. package/src/orm/unit-of-work.ts +60 -60
  103. package/src/query-builder/delete.ts +22 -5
  104. package/src/query-builder/hydration-manager.ts +2 -1
  105. package/src/query-builder/hydration-planner.ts +8 -7
  106. package/src/query-builder/insert.ts +22 -5
  107. package/src/query-builder/relation-conditions.ts +9 -8
  108. package/src/query-builder/relation-service.ts +3 -2
  109. package/src/query-builder/select.ts +66 -61
  110. package/src/query-builder/update.ts +22 -5
  111. package/src/schema/column.ts +246 -246
  112. package/src/schema/relation.ts +35 -1
  113. package/src/schema/table.ts +28 -28
  114. package/src/schema/types.ts +41 -31
  115. package/src/orm/db-executor.ts +0 -11
@@ -1,252 +1,252 @@
1
1
  /**
2
2
  * Supported column data types for database schema definitions
3
3
  */
4
- export type ColumnType =
5
- | 'INT'
6
- | 'INTEGER'
7
- | 'BIGINT'
8
- | 'VARCHAR'
9
- | 'TEXT'
10
- | 'JSON'
11
- | 'ENUM'
12
- | 'DECIMAL'
13
- | 'FLOAT'
14
- | 'DOUBLE'
15
- | 'UUID'
16
- | 'DATE'
17
- | 'DATETIME'
18
- | 'TIMESTAMP'
19
- | 'TIMESTAMPTZ'
20
- | 'BOOLEAN'
21
- | 'int'
22
- | 'integer'
23
- | 'bigint'
24
- | 'varchar'
25
- | 'text'
26
- | 'json'
27
- | 'enum'
28
- | 'decimal'
29
- | 'float'
30
- | 'double'
31
- | 'uuid'
32
- | 'date'
33
- | 'datetime'
34
- | 'timestamp'
35
- | 'timestamptz'
36
- | 'boolean';
37
-
38
- export type ReferentialAction =
39
- | 'NO ACTION'
40
- | 'RESTRICT'
41
- | 'CASCADE'
42
- | 'SET NULL'
43
- | 'SET DEFAULT';
44
-
45
- export interface RawDefaultValue {
46
- raw: string;
47
- }
48
-
49
- export type DefaultValue = unknown | RawDefaultValue;
50
-
51
- export interface ForeignKeyReference {
52
- /** Target table name */
53
- table: string;
54
- /** Target column name */
55
- column: string;
56
- /** Optional constraint name */
57
- name?: string;
58
- /** ON DELETE action */
59
- onDelete?: ReferentialAction;
60
- /** ON UPDATE action */
61
- onUpdate?: ReferentialAction;
62
- /** Whether the constraint is deferrable (Postgres) */
63
- deferrable?: boolean;
64
- }
65
-
66
- /**
67
- * Definition of a database column
68
- */
69
- export interface ColumnDef<T extends ColumnType = ColumnType> {
70
- /** Column name (filled at runtime by defineTable) */
71
- name: string;
72
- /** Data type of the column */
73
- type: T;
74
- /** Whether this column is a primary key */
75
- primary?: boolean;
76
- /** Whether this column cannot be null */
77
- notNull?: boolean;
78
- /** Whether this column must be unique (or name of the unique constraint) */
79
- unique?: boolean | string;
80
- /** Default value for the column */
81
- default?: DefaultValue;
82
- /** Whether the column auto-increments / identity */
83
- autoIncrement?: boolean;
84
- /** Identity strategy where supported */
85
- generated?: 'always' | 'byDefault';
86
- /** Inline check constraint expression */
87
- check?: string;
88
- /** Foreign key reference */
89
- references?: ForeignKeyReference;
90
- /** Column comment/description */
91
- comment?: string;
92
- /** Additional arguments for the column type (e.g., VARCHAR length) */
93
- args?: any[];
94
- /** Table name this column belongs to (filled at runtime by defineTable) */
95
- table?: string;
96
- }
97
-
4
+ export type ColumnType =
5
+ | 'INT'
6
+ | 'INTEGER'
7
+ | 'BIGINT'
8
+ | 'VARCHAR'
9
+ | 'TEXT'
10
+ | 'JSON'
11
+ | 'ENUM'
12
+ | 'DECIMAL'
13
+ | 'FLOAT'
14
+ | 'DOUBLE'
15
+ | 'UUID'
16
+ | 'DATE'
17
+ | 'DATETIME'
18
+ | 'TIMESTAMP'
19
+ | 'TIMESTAMPTZ'
20
+ | 'BOOLEAN'
21
+ | 'int'
22
+ | 'integer'
23
+ | 'bigint'
24
+ | 'varchar'
25
+ | 'text'
26
+ | 'json'
27
+ | 'enum'
28
+ | 'decimal'
29
+ | 'float'
30
+ | 'double'
31
+ | 'uuid'
32
+ | 'date'
33
+ | 'datetime'
34
+ | 'timestamp'
35
+ | 'timestamptz'
36
+ | 'boolean';
37
+
38
+ export type ReferentialAction =
39
+ | 'NO ACTION'
40
+ | 'RESTRICT'
41
+ | 'CASCADE'
42
+ | 'SET NULL'
43
+ | 'SET DEFAULT';
44
+
45
+ export interface RawDefaultValue {
46
+ raw: string;
47
+ }
48
+
49
+ export type DefaultValue = unknown | RawDefaultValue;
50
+
51
+ export interface ForeignKeyReference {
52
+ /** Target table name */
53
+ table: string;
54
+ /** Target column name */
55
+ column: string;
56
+ /** Optional constraint name */
57
+ name?: string;
58
+ /** ON DELETE action */
59
+ onDelete?: ReferentialAction;
60
+ /** ON UPDATE action */
61
+ onUpdate?: ReferentialAction;
62
+ /** Whether the constraint is deferrable (Postgres) */
63
+ deferrable?: boolean;
64
+ }
65
+
66
+ /**
67
+ * Definition of a database column
68
+ */
69
+ export interface ColumnDef<T extends ColumnType = ColumnType> {
70
+ /** Column name (filled at runtime by defineTable) */
71
+ name: string;
72
+ /** Data type of the column */
73
+ type: T;
74
+ /** Whether this column is a primary key */
75
+ primary?: boolean;
76
+ /** Whether this column cannot be null */
77
+ notNull?: boolean;
78
+ /** Whether this column must be unique (or name of the unique constraint) */
79
+ unique?: boolean | string;
80
+ /** Default value for the column */
81
+ default?: DefaultValue;
82
+ /** Whether the column auto-increments / identity */
83
+ autoIncrement?: boolean;
84
+ /** Identity strategy where supported */
85
+ generated?: 'always' | 'byDefault';
86
+ /** Inline check constraint expression */
87
+ check?: string;
88
+ /** Foreign key reference */
89
+ references?: ForeignKeyReference;
90
+ /** Column comment/description */
91
+ comment?: string;
92
+ /** Additional arguments for the column type (e.g., VARCHAR length) */
93
+ args?: any[];
94
+ /** Table name this column belongs to (filled at runtime by defineTable) */
95
+ table?: string;
96
+ }
97
+
98
98
  /**
99
99
  * Factory for creating column definitions with common data types
100
100
  */
101
- export const col = {
102
- /**
103
- * Creates an integer column definition
104
- * @returns ColumnDef with INT type
105
- */
106
- int: (): ColumnDef<'INT'> => ({ name: '', type: 'INT' }),
107
-
108
- /**
109
- * Creates a big integer column definition
110
- */
111
- bigint: (): ColumnDef<'BIGINT'> => ({ name: '', type: 'BIGINT' }),
112
-
113
- /**
114
- * Creates a variable character column definition
115
- * @param length - Maximum length of the string
116
- * @returns ColumnDef with VARCHAR type
117
- */
118
- varchar: (length: number): ColumnDef<'VARCHAR'> => ({ name: '', type: 'VARCHAR', args: [length] }),
119
-
120
- /**
121
- * Creates a fixed precision decimal column definition
122
- */
123
- decimal: (precision: number, scale = 0): ColumnDef<'DECIMAL'> => ({
124
- name: '',
125
- type: 'DECIMAL',
126
- args: [precision, scale]
127
- }),
128
-
129
- /**
130
- * Creates a floating point column definition
131
- */
132
- float: (precision?: number): ColumnDef<'FLOAT'> => ({
133
- name: '',
134
- type: 'FLOAT',
135
- args: precision !== undefined ? [precision] : undefined
136
- }),
137
-
138
- /**
139
- * Creates a UUID column definition
140
- */
141
- uuid: (): ColumnDef<'UUID'> => ({ name: '', type: 'UUID' }),
142
-
143
- /**
144
- * Creates a timestamp column definition
145
- */
146
- timestamp: (): ColumnDef<'TIMESTAMP'> => ({ name: '', type: 'TIMESTAMP' }),
147
-
148
- /**
149
- * Creates a timestamptz column definition
150
- */
151
- timestamptz: (): ColumnDef<'TIMESTAMPTZ'> => ({ name: '', type: 'TIMESTAMPTZ' }),
152
-
153
- /**
154
- * Creates a date column definition
155
- */
156
- date: (): ColumnDef<'DATE'> => ({ name: '', type: 'DATE' }),
157
-
158
- /**
159
- * Creates a datetime column definition
160
- */
161
- datetime: (): ColumnDef<'DATETIME'> => ({ name: '', type: 'DATETIME' }),
162
-
163
- /**
164
- * Creates a JSON column definition
165
- * @returns ColumnDef with JSON type
166
- */
167
- json: (): ColumnDef<'JSON'> => ({ name: '', type: 'JSON' }),
168
-
169
- /**
170
- * Creates a boolean column definition
171
- * @returns ColumnDef with BOOLEAN type
172
- */
173
- boolean: (): ColumnDef<'BOOLEAN'> => ({ name: '', type: 'BOOLEAN' }),
174
-
175
- /**
176
- * Creates an enum column definition
177
- * @param values - Enum values
178
- */
179
- enum: (values: string[]): ColumnDef<'ENUM'> => ({ name: '', type: 'ENUM', args: values }),
180
-
181
- /**
182
- * Marks a column definition as a primary key
183
- * @param def - Column definition to modify
184
- * @returns Modified ColumnDef with primary: true
185
- */
186
- primaryKey: <T extends ColumnType>(def: ColumnDef<T>): ColumnDef<T> =>
187
- ({ ...def, primary: true }),
188
-
189
- /**
190
- * Marks a column as NOT NULL
191
- */
192
- notNull: <T extends ColumnType>(def: ColumnDef<T>): ColumnDef<T> =>
193
- ({ ...def, notNull: true }),
194
-
195
- /**
196
- * Marks a column as UNIQUE
197
- */
198
- unique: <T extends ColumnType>(def: ColumnDef<T>, name?: string): ColumnDef<T> =>
199
- ({
200
- ...def,
201
- unique: name ?? true
202
- }),
203
-
204
- /**
205
- * Sets a default value for the column
206
- */
207
- default: <T extends ColumnType>(def: ColumnDef<T>, value: unknown): ColumnDef<T> =>
208
- ({
209
- ...def,
210
- default: value
211
- }),
212
-
213
- /**
214
- * Sets a raw SQL default value for the column
215
- */
216
- defaultRaw: <T extends ColumnType>(def: ColumnDef<T>, expression: string): ColumnDef<T> =>
217
- ({
218
- ...def,
219
- default: { raw: expression }
220
- }),
221
-
222
- /**
223
- * Marks a column as auto-increment / identity
224
- */
225
- autoIncrement: <T extends ColumnType>(
226
- def: ColumnDef<T>,
227
- strategy: ColumnDef['generated'] = 'byDefault'
228
- ): ColumnDef<T> =>
229
- ({
230
- ...def,
231
- autoIncrement: true,
232
- generated: strategy
233
- }),
234
-
235
- /**
236
- * Adds a foreign key reference
237
- */
238
- references: <T extends ColumnType>(def: ColumnDef<T>, ref: ForeignKeyReference): ColumnDef<T> =>
239
- ({
240
- ...def,
241
- references: ref
242
- }),
243
-
244
- /**
245
- * Adds a check constraint to the column
246
- */
247
- check: <T extends ColumnType>(def: ColumnDef<T>, expression: string): ColumnDef<T> =>
248
- ({
249
- ...def,
250
- check: expression
251
- })
252
- };
101
+ export const col = {
102
+ /**
103
+ * Creates an integer column definition
104
+ * @returns ColumnDef with INT type
105
+ */
106
+ int: (): ColumnDef<'INT'> => ({ name: '', type: 'INT' }),
107
+
108
+ /**
109
+ * Creates a big integer column definition
110
+ */
111
+ bigint: (): ColumnDef<'BIGINT'> => ({ name: '', type: 'BIGINT' }),
112
+
113
+ /**
114
+ * Creates a variable character column definition
115
+ * @param length - Maximum length of the string
116
+ * @returns ColumnDef with VARCHAR type
117
+ */
118
+ varchar: (length: number): ColumnDef<'VARCHAR'> => ({ name: '', type: 'VARCHAR', args: [length] }),
119
+
120
+ /**
121
+ * Creates a fixed precision decimal column definition
122
+ */
123
+ decimal: (precision: number, scale = 0): ColumnDef<'DECIMAL'> => ({
124
+ name: '',
125
+ type: 'DECIMAL',
126
+ args: [precision, scale]
127
+ }),
128
+
129
+ /**
130
+ * Creates a floating point column definition
131
+ */
132
+ float: (precision?: number): ColumnDef<'FLOAT'> => ({
133
+ name: '',
134
+ type: 'FLOAT',
135
+ args: precision !== undefined ? [precision] : undefined
136
+ }),
137
+
138
+ /**
139
+ * Creates a UUID column definition
140
+ */
141
+ uuid: (): ColumnDef<'UUID'> => ({ name: '', type: 'UUID' }),
142
+
143
+ /**
144
+ * Creates a timestamp column definition
145
+ */
146
+ timestamp: (): ColumnDef<'TIMESTAMP'> => ({ name: '', type: 'TIMESTAMP' }),
147
+
148
+ /**
149
+ * Creates a timestamptz column definition
150
+ */
151
+ timestamptz: (): ColumnDef<'TIMESTAMPTZ'> => ({ name: '', type: 'TIMESTAMPTZ' }),
152
+
153
+ /**
154
+ * Creates a date column definition
155
+ */
156
+ date: (): ColumnDef<'DATE'> => ({ name: '', type: 'DATE' }),
157
+
158
+ /**
159
+ * Creates a datetime column definition
160
+ */
161
+ datetime: (): ColumnDef<'DATETIME'> => ({ name: '', type: 'DATETIME' }),
162
+
163
+ /**
164
+ * Creates a JSON column definition
165
+ * @returns ColumnDef with JSON type
166
+ */
167
+ json: (): ColumnDef<'JSON'> => ({ name: '', type: 'JSON' }),
168
+
169
+ /**
170
+ * Creates a boolean column definition
171
+ * @returns ColumnDef with BOOLEAN type
172
+ */
173
+ boolean: (): ColumnDef<'BOOLEAN'> => ({ name: '', type: 'BOOLEAN' }),
174
+
175
+ /**
176
+ * Creates an enum column definition
177
+ * @param values - Enum values
178
+ */
179
+ enum: (values: string[]): ColumnDef<'ENUM'> => ({ name: '', type: 'ENUM', args: values }),
180
+
181
+ /**
182
+ * Marks a column definition as a primary key
183
+ * @param def - Column definition to modify
184
+ * @returns Modified ColumnDef with primary: true
185
+ */
186
+ primaryKey: <T extends ColumnType>(def: ColumnDef<T>): ColumnDef<T> =>
187
+ ({ ...def, primary: true }),
188
+
189
+ /**
190
+ * Marks a column as NOT NULL
191
+ */
192
+ notNull: <T extends ColumnType>(def: ColumnDef<T>): ColumnDef<T> =>
193
+ ({ ...def, notNull: true }),
194
+
195
+ /**
196
+ * Marks a column as UNIQUE
197
+ */
198
+ unique: <T extends ColumnType>(def: ColumnDef<T>, name?: string): ColumnDef<T> =>
199
+ ({
200
+ ...def,
201
+ unique: name ?? true
202
+ }),
203
+
204
+ /**
205
+ * Sets a default value for the column
206
+ */
207
+ default: <T extends ColumnType>(def: ColumnDef<T>, value: unknown): ColumnDef<T> =>
208
+ ({
209
+ ...def,
210
+ default: value
211
+ }),
212
+
213
+ /**
214
+ * Sets a raw SQL default value for the column
215
+ */
216
+ defaultRaw: <T extends ColumnType>(def: ColumnDef<T>, expression: string): ColumnDef<T> =>
217
+ ({
218
+ ...def,
219
+ default: { raw: expression }
220
+ }),
221
+
222
+ /**
223
+ * Marks a column as auto-increment / identity
224
+ */
225
+ autoIncrement: <T extends ColumnType>(
226
+ def: ColumnDef<T>,
227
+ strategy: ColumnDef['generated'] = 'byDefault'
228
+ ): ColumnDef<T> =>
229
+ ({
230
+ ...def,
231
+ autoIncrement: true,
232
+ generated: strategy
233
+ }),
234
+
235
+ /**
236
+ * Adds a foreign key reference
237
+ */
238
+ references: <T extends ColumnType>(def: ColumnDef<T>, ref: ForeignKeyReference): ColumnDef<T> =>
239
+ ({
240
+ ...def,
241
+ references: ref
242
+ }),
243
+
244
+ /**
245
+ * Adds a check constraint to the column
246
+ */
247
+ check: <T extends ColumnType>(def: ColumnDef<T>, expression: string): ColumnDef<T> =>
248
+ ({
249
+ ...def,
250
+ check: expression
251
+ })
252
+ };
@@ -1,9 +1,11 @@
1
- import { TableDef } from './table.js';
1
+ import type { TableDef } from './table.js';
2
2
 
3
3
  /**
4
4
  * Types of relationships supported between tables
5
5
  */
6
6
  export const RelationKinds = {
7
+ /** One-to-one relationship */
8
+ HasOne: 'HAS_ONE',
7
9
  /** One-to-many relationship */
8
10
  HasMany: 'HAS_MANY',
9
11
  /** Many-to-one relationship */
@@ -30,6 +32,17 @@ export interface HasManyRelation<TTarget extends TableDef = TableDef> {
30
32
  cascade?: CascadeMode;
31
33
  }
32
34
 
35
+ /**
36
+ * One-to-one relationship definition
37
+ */
38
+ export interface HasOneRelation<TTarget extends TableDef = TableDef> {
39
+ type: typeof RelationKinds.HasOne;
40
+ target: TTarget;
41
+ foreignKey: string;
42
+ localKey?: string;
43
+ cascade?: CascadeMode;
44
+ }
45
+
33
46
  /**
34
47
  * Many-to-one relationship definition
35
48
  */
@@ -62,6 +75,7 @@ export interface BelongsToManyRelation<TTarget extends TableDef = TableDef> {
62
75
  */
63
76
  export type RelationDef =
64
77
  | HasManyRelation
78
+ | HasOneRelation
65
79
  | BelongsToRelation
66
80
  | BelongsToManyRelation;
67
81
 
@@ -90,6 +104,26 @@ export const hasMany = <TTarget extends TableDef>(
90
104
  cascade
91
105
  });
92
106
 
107
+ /**
108
+ * Creates a one-to-one relationship definition
109
+ * @param target - Target table of the relationship
110
+ * @param foreignKey - Foreign key column name on the child table
111
+ * @param localKey - Local key column name (optional)
112
+ * @returns HasOneRelation definition
113
+ */
114
+ export const hasOne = <TTarget extends TableDef>(
115
+ target: TTarget,
116
+ foreignKey: string,
117
+ localKey?: string,
118
+ cascade?: CascadeMode
119
+ ): HasOneRelation<TTarget> => ({
120
+ type: RelationKinds.HasOne,
121
+ target,
122
+ foreignKey,
123
+ localKey,
124
+ cascade
125
+ });
126
+
93
127
  /**
94
128
  * Creates a many-to-one relationship definition
95
129
  * @param target - Target table of the relationship
@@ -1,5 +1,5 @@
1
- import { ColumnDef } from './column.js';
2
- import { RelationDef } from './relation.js';
1
+ import type { ColumnDef } from './column.js';
2
+ import type { RelationDef } from './relation.js';
3
3
 
4
4
  export interface IndexColumn {
5
5
  column: string;
@@ -35,14 +35,14 @@ export interface TableHooks {
35
35
  afterInsert?(ctx: unknown, entity: any): Promise<void> | void;
36
36
  beforeUpdate?(ctx: unknown, entity: any): Promise<void> | void;
37
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
- */
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
46
  export interface TableDef<T extends Record<string, ColumnDef> = Record<string, ColumnDef>> {
47
47
  /** Name of the table */
48
48
  name: string;
@@ -67,24 +67,24 @@ export interface TableDef<T extends Record<string, ColumnDef> = Record<string, C
67
67
  charset?: string;
68
68
  collation?: string;
69
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
- */
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
88
  export const defineTable = <T extends Record<string, ColumnDef>>(
89
89
  name: string,
90
90
  columns: T,