@stonyx/orm 0.2.1-beta.9 → 0.2.1-beta.90

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 (171) hide show
  1. package/README.md +64 -6
  2. package/config/environment.js +37 -1
  3. package/dist/aggregates.d.ts +21 -0
  4. package/dist/aggregates.js +93 -0
  5. package/dist/attr.d.ts +2 -0
  6. package/dist/attr.js +22 -0
  7. package/dist/belongs-to.d.ts +11 -0
  8. package/dist/belongs-to.js +59 -0
  9. package/dist/cli.d.ts +22 -0
  10. package/dist/cli.js +148 -0
  11. package/dist/commands.d.ts +7 -0
  12. package/dist/commands.js +146 -0
  13. package/dist/db.d.ts +21 -0
  14. package/dist/db.js +180 -0
  15. package/dist/exports/db.d.ts +7 -0
  16. package/{src → dist}/exports/db.js +2 -4
  17. package/dist/has-many.d.ts +11 -0
  18. package/dist/has-many.js +58 -0
  19. package/dist/hooks.d.ts +62 -0
  20. package/dist/hooks.js +110 -0
  21. package/dist/index.d.ts +14 -0
  22. package/dist/index.js +34 -0
  23. package/dist/main.d.ts +46 -0
  24. package/dist/main.js +181 -0
  25. package/dist/manage-record.d.ts +13 -0
  26. package/dist/manage-record.js +123 -0
  27. package/dist/meta-request.d.ts +6 -0
  28. package/dist/meta-request.js +52 -0
  29. package/dist/migrate.d.ts +2 -0
  30. package/dist/migrate.js +57 -0
  31. package/dist/model-property.d.ts +9 -0
  32. package/dist/model-property.js +29 -0
  33. package/dist/model.d.ts +15 -0
  34. package/dist/model.js +18 -0
  35. package/dist/mysql/connection.d.ts +14 -0
  36. package/dist/mysql/connection.js +24 -0
  37. package/dist/mysql/migration-generator.d.ts +45 -0
  38. package/dist/mysql/migration-generator.js +254 -0
  39. package/dist/mysql/migration-runner.d.ts +12 -0
  40. package/dist/mysql/migration-runner.js +88 -0
  41. package/dist/mysql/mysql-db.d.ts +100 -0
  42. package/dist/mysql/mysql-db.js +425 -0
  43. package/dist/mysql/query-builder.d.ts +10 -0
  44. package/dist/mysql/query-builder.js +44 -0
  45. package/dist/mysql/schema-introspector.d.ts +19 -0
  46. package/dist/mysql/schema-introspector.js +291 -0
  47. package/dist/mysql/type-map.d.ts +21 -0
  48. package/dist/mysql/type-map.js +36 -0
  49. package/dist/orm-request.d.ts +38 -0
  50. package/dist/orm-request.js +474 -0
  51. package/dist/plural-registry.d.ts +4 -0
  52. package/dist/plural-registry.js +9 -0
  53. package/dist/postgres/connection.d.ts +15 -0
  54. package/dist/postgres/connection.js +32 -0
  55. package/dist/postgres/migration-generator.d.ts +45 -0
  56. package/dist/postgres/migration-generator.js +261 -0
  57. package/dist/postgres/migration-runner.d.ts +10 -0
  58. package/dist/postgres/migration-runner.js +87 -0
  59. package/dist/postgres/postgres-db.d.ts +119 -0
  60. package/dist/postgres/postgres-db.js +477 -0
  61. package/dist/postgres/query-builder.d.ts +27 -0
  62. package/dist/postgres/query-builder.js +98 -0
  63. package/dist/postgres/schema-introspector.d.ts +29 -0
  64. package/dist/postgres/schema-introspector.js +314 -0
  65. package/dist/postgres/type-map.d.ts +23 -0
  66. package/dist/postgres/type-map.js +56 -0
  67. package/dist/record.d.ts +75 -0
  68. package/dist/record.js +129 -0
  69. package/dist/relationships.d.ts +10 -0
  70. package/dist/relationships.js +41 -0
  71. package/dist/serializer.d.ts +17 -0
  72. package/dist/serializer.js +136 -0
  73. package/dist/setup-rest-server.d.ts +1 -0
  74. package/dist/setup-rest-server.js +52 -0
  75. package/dist/standalone-db.d.ts +58 -0
  76. package/dist/standalone-db.js +142 -0
  77. package/dist/store.d.ts +62 -0
  78. package/dist/store.js +286 -0
  79. package/dist/timescale/query-builder.d.ts +43 -0
  80. package/dist/timescale/query-builder.js +115 -0
  81. package/dist/timescale/timescale-db.d.ts +45 -0
  82. package/dist/timescale/timescale-db.js +84 -0
  83. package/dist/transforms.d.ts +2 -0
  84. package/dist/transforms.js +17 -0
  85. package/dist/types/orm-types.d.ts +142 -0
  86. package/dist/types/orm-types.js +1 -0
  87. package/dist/utils.d.ts +7 -0
  88. package/dist/utils.js +17 -0
  89. package/dist/view-resolver.d.ts +8 -0
  90. package/dist/view-resolver.js +171 -0
  91. package/dist/view.d.ts +11 -0
  92. package/dist/view.js +18 -0
  93. package/package.json +57 -15
  94. package/src/aggregates.ts +109 -0
  95. package/src/{attr.js → attr.ts} +2 -2
  96. package/src/belongs-to.ts +90 -0
  97. package/src/cli.ts +183 -0
  98. package/src/{commands.js → commands.ts} +179 -170
  99. package/src/{db.js → db.ts} +55 -29
  100. package/src/exports/db.ts +7 -0
  101. package/src/has-many.ts +92 -0
  102. package/src/{hooks.js → hooks.ts} +41 -27
  103. package/src/{index.js → index.ts} +11 -2
  104. package/src/main.ts +229 -0
  105. package/src/manage-record.ts +161 -0
  106. package/src/{meta-request.js → meta-request.ts} +17 -14
  107. package/src/{migrate.js → migrate.ts} +9 -9
  108. package/src/model-property.ts +35 -0
  109. package/src/model.ts +21 -0
  110. package/src/mysql/{connection.js → connection.ts} +43 -28
  111. package/src/mysql/migration-generator.ts +337 -0
  112. package/src/mysql/{migration-runner.js → migration-runner.ts} +121 -110
  113. package/src/mysql/mysql-db.ts +543 -0
  114. package/src/mysql/{query-builder.js → query-builder.ts} +69 -64
  115. package/src/mysql/schema-introspector.ts +358 -0
  116. package/src/mysql/{type-map.js → type-map.ts} +42 -37
  117. package/src/{orm-request.js → orm-request.ts} +186 -108
  118. package/src/plural-registry.ts +12 -0
  119. package/src/postgres/connection.ts +48 -0
  120. package/src/postgres/migration-generator.ts +348 -0
  121. package/src/postgres/migration-runner.ts +115 -0
  122. package/src/postgres/postgres-db.ts +616 -0
  123. package/src/postgres/query-builder.ts +148 -0
  124. package/src/postgres/schema-introspector.ts +386 -0
  125. package/src/postgres/type-map.ts +61 -0
  126. package/src/record.ts +186 -0
  127. package/src/relationships.ts +54 -0
  128. package/src/serializer.ts +161 -0
  129. package/src/{setup-rest-server.js → setup-rest-server.ts} +18 -16
  130. package/src/standalone-db.ts +185 -0
  131. package/src/store.ts +373 -0
  132. package/src/timescale/query-builder.ts +174 -0
  133. package/src/timescale/timescale-db.ts +119 -0
  134. package/src/transforms.ts +20 -0
  135. package/src/types/mysql2.d.ts +49 -0
  136. package/src/types/orm-types.ts +146 -0
  137. package/src/types/pg.d.ts +32 -0
  138. package/src/types/stonyx-cron.d.ts +5 -0
  139. package/src/types/stonyx-events.d.ts +4 -0
  140. package/src/types/stonyx-rest-server.d.ts +16 -0
  141. package/src/types/stonyx-utils.d.ts +33 -0
  142. package/src/types/stonyx.d.ts +21 -0
  143. package/src/utils.ts +22 -0
  144. package/src/view-resolver.ts +211 -0
  145. package/src/view.ts +22 -0
  146. package/.claude/code-style-rules.md +0 -44
  147. package/.claude/hooks.md +0 -250
  148. package/.claude/index.md +0 -279
  149. package/.claude/usage-patterns.md +0 -217
  150. package/.github/workflows/ci.yml +0 -16
  151. package/.github/workflows/publish.yml +0 -51
  152. package/improvements.md +0 -139
  153. package/project-structure.md +0 -343
  154. package/src/belongs-to.js +0 -63
  155. package/src/has-many.js +0 -61
  156. package/src/main.js +0 -148
  157. package/src/manage-record.js +0 -118
  158. package/src/model-property.js +0 -29
  159. package/src/model.js +0 -9
  160. package/src/mysql/migration-generator.js +0 -188
  161. package/src/mysql/mysql-db.js +0 -320
  162. package/src/mysql/schema-introspector.js +0 -158
  163. package/src/record.js +0 -127
  164. package/src/relationships.js +0 -43
  165. package/src/serializer.js +0 -138
  166. package/src/store.js +0 -211
  167. package/src/transforms.js +0 -20
  168. package/src/utils.js +0 -12
  169. package/test-events-setup.js +0 -41
  170. package/test-hooks-manual.js +0 -54
  171. package/test-hooks-with-logging.js +0 -52
@@ -0,0 +1,358 @@
1
+ import Orm from '@stonyx/orm';
2
+ import { getMysqlType } from './type-map.js';
3
+ import { camelCaseToKebabCase } from '@stonyx/utils/string';
4
+ import { getPluralName } from '../plural-registry.js';
5
+ import { dbKey } from '../db.js';
6
+ import { AggregateProperty } from '../aggregates.js';
7
+ import type { ForeignKeyDef, ModelSchema, ViewSchema, SnapshotEntry } from '../types/orm-types.js';
8
+ import ModelProperty from '../model-property.js';
9
+
10
+ interface RelationshipInfo {
11
+ type: 'belongsTo' | 'hasMany';
12
+ modelName: string | null;
13
+ }
14
+
15
+ interface JoinClause {
16
+ table: string;
17
+ condition: string;
18
+ }
19
+
20
+ interface RelationshipProperty {
21
+ __relatedModelName?: string | null;
22
+ __relationshipType?: string;
23
+ }
24
+
25
+ function getRelationshipInfo(property: unknown): RelationshipInfo | null {
26
+ if (typeof property !== 'function') return null;
27
+ const relType = (property as RelationshipProperty).__relationshipType;
28
+ const modelName = (property as RelationshipProperty).__relatedModelName || null;
29
+
30
+ if (relType === 'belongsTo') return { type: 'belongsTo', modelName };
31
+ if (relType === 'hasMany') return { type: 'hasMany', modelName };
32
+
33
+ return null;
34
+ }
35
+
36
+ function sanitizeTableName(name: string): string {
37
+ return name.replace(/[-/]/g, '_');
38
+ }
39
+
40
+ export function introspectModels(): Record<string, ModelSchema> {
41
+ const { models } = (Orm as unknown as { instance: { models: Record<string, unknown>; transforms: Record<string, unknown> } }).instance;
42
+ const schemas: Record<string, ModelSchema> = {};
43
+
44
+ for (const [modelKey, modelClass] of Object.entries(models)) {
45
+ const name = camelCaseToKebabCase(modelKey.slice(0, -5));
46
+
47
+ if (name === dbKey) continue;
48
+
49
+ const model = new (modelClass as new (key: string) => Record<string, unknown>)(modelKey);
50
+ const columns: Record<string, string> = {};
51
+ const foreignKeys: Record<string, ForeignKeyDef> = {};
52
+ const relationships: { belongsTo: Record<string, string | null>; hasMany: Record<string, string | null> } = { belongsTo: {}, hasMany: {} };
53
+ let idType = 'number';
54
+
55
+ const transforms = (Orm as unknown as { instance: { transforms: Record<string, unknown> } }).instance.transforms;
56
+
57
+ for (const [key, property] of Object.entries(model)) {
58
+ if (key.startsWith('__')) continue;
59
+
60
+ const relInfo = getRelationshipInfo(property);
61
+
62
+ if (relInfo?.type === 'belongsTo') {
63
+ relationships.belongsTo[key] = relInfo.modelName;
64
+ } else if (relInfo?.type === 'hasMany') {
65
+ relationships.hasMany[key] = relInfo.modelName;
66
+ } else if (property instanceof ModelProperty) {
67
+ if (key === 'id') {
68
+ idType = (property as ModelProperty).type;
69
+ } else {
70
+ columns[key] = getMysqlType((property as ModelProperty).type, transforms[(property as ModelProperty).type] as ((...args: unknown[]) => unknown) & { mysqlType?: string });
71
+ }
72
+ }
73
+ }
74
+
75
+ // Build foreign keys from belongsTo relationships
76
+ for (const [relName, targetModelName] of Object.entries(relationships.belongsTo)) {
77
+ if (!targetModelName) continue;
78
+ const fkColumn = `${relName}_id`;
79
+ foreignKeys[fkColumn] = {
80
+ references: sanitizeTableName(getPluralName(targetModelName)),
81
+ column: 'id',
82
+ };
83
+ }
84
+
85
+ schemas[name] = {
86
+ table: sanitizeTableName(getPluralName(name)),
87
+ idType,
88
+ columns,
89
+ foreignKeys,
90
+ relationships,
91
+ memory: (modelClass as { memory?: boolean }).memory === true,
92
+ };
93
+ }
94
+
95
+ return schemas;
96
+ }
97
+
98
+ export function buildTableDDL(name: string, schema: ModelSchema, allSchemas: Record<string, ModelSchema> = {}): string {
99
+ const { idType, columns, foreignKeys } = schema;
100
+ const table = sanitizeTableName(schema.table);
101
+ const lines: string[] = [];
102
+
103
+ // Primary key
104
+ if (idType === 'string') {
105
+ lines.push(' `id` VARCHAR(255) PRIMARY KEY');
106
+ } else {
107
+ lines.push(' `id` INT AUTO_INCREMENT PRIMARY KEY');
108
+ }
109
+
110
+ // Attribute columns
111
+ for (const [col, mysqlType] of Object.entries(columns)) {
112
+ lines.push(` \`${col}\` ${mysqlType}`);
113
+ }
114
+
115
+ // Foreign key columns
116
+ for (const [fkCol, fkDef] of Object.entries(foreignKeys)) {
117
+ const refIdType = getReferencedIdType(fkDef.references, allSchemas);
118
+ lines.push(` \`${fkCol}\` ${refIdType}`);
119
+ }
120
+
121
+ // Timestamps
122
+ lines.push(' `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP');
123
+ lines.push(' `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP');
124
+
125
+ // Foreign key constraints
126
+ for (const [fkCol, fkDef] of Object.entries(foreignKeys)) {
127
+ const refTable = sanitizeTableName(fkDef.references);
128
+ lines.push(` FOREIGN KEY (\`${fkCol}\`) REFERENCES \`${refTable}\`(\`${fkDef.column}\`) ON DELETE SET NULL`);
129
+ }
130
+
131
+ return `CREATE TABLE IF NOT EXISTS \`${table}\` (\n${lines.join(',\n')}\n)`;
132
+ }
133
+
134
+ function getReferencedIdType(tableName: string, allSchemas: Record<string, ModelSchema>): string {
135
+ // Look up the referenced table's PK type from schemas
136
+ for (const schema of Object.values(allSchemas)) {
137
+ if (schema.table === tableName) {
138
+ return schema.idType === 'string' ? 'VARCHAR(255)' : 'INT';
139
+ }
140
+ }
141
+
142
+ // Default to INT if referenced table not found in schemas
143
+ return 'INT';
144
+ }
145
+
146
+ export function getTopologicalOrder(schemas: Record<string, ModelSchema>): string[] {
147
+ const visited = new Set<string>();
148
+ const order: string[] = [];
149
+
150
+ function visit(name: string): void {
151
+ if (visited.has(name)) return;
152
+ visited.add(name);
153
+
154
+ const schema = schemas[name];
155
+ if (!schema) return;
156
+
157
+ // Visit dependencies (belongsTo targets) first
158
+ for (const targetModelName of Object.values(schema.relationships.belongsTo)) {
159
+ if (targetModelName) visit(targetModelName);
160
+ }
161
+
162
+ order.push(name);
163
+ }
164
+
165
+ for (const name of Object.keys(schemas)) {
166
+ visit(name);
167
+ }
168
+
169
+ return order;
170
+ }
171
+
172
+ export function introspectViews(): Record<string, ViewSchema> {
173
+ const orm = (Orm as unknown as { instance: { views?: Record<string, unknown>; transforms: Record<string, unknown> } }).instance;
174
+ if (!orm.views) return {};
175
+
176
+ const schemas: Record<string, ViewSchema> = {};
177
+
178
+ for (const [viewKey, viewClass] of Object.entries(orm.views)) {
179
+ const name = camelCaseToKebabCase(viewKey.slice(0, -4)); // Remove 'View' suffix
180
+
181
+ const source = (viewClass as { source?: string }).source;
182
+ if (!source) continue;
183
+
184
+ const model = new (viewClass as new (name: string) => Record<string, unknown>)(name);
185
+ const columns: Record<string, string> = {};
186
+ const foreignKeys: Record<string, ForeignKeyDef> = {};
187
+ const aggregates: Record<string, AggregateProperty> = {};
188
+ const relationships: { belongsTo: Record<string, string | null>; hasMany: Record<string, string | null> } = { belongsTo: {}, hasMany: {} };
189
+
190
+ for (const [key, property] of Object.entries(model)) {
191
+ if (key.startsWith('__')) continue;
192
+ if (key === 'id') continue;
193
+
194
+ if (property instanceof AggregateProperty) {
195
+ aggregates[key] = property;
196
+ continue;
197
+ }
198
+
199
+ const relInfo = getRelationshipInfo(property);
200
+
201
+ if (relInfo?.type === 'belongsTo') {
202
+ relationships.belongsTo[key] = relInfo.modelName;
203
+ if (relInfo.modelName) {
204
+ const fkColumn = `${key}_id`;
205
+ foreignKeys[fkColumn] = {
206
+ references: sanitizeTableName(getPluralName(relInfo.modelName)),
207
+ column: 'id',
208
+ };
209
+ }
210
+ } else if (relInfo?.type === 'hasMany') {
211
+ relationships.hasMany[key] = relInfo.modelName;
212
+ } else if (property instanceof ModelProperty) {
213
+ const transforms = orm.transforms;
214
+ columns[key] = getMysqlType((property as ModelProperty).type, transforms[(property as ModelProperty).type] as ((...args: unknown[]) => unknown) & { mysqlType?: string });
215
+ }
216
+ }
217
+
218
+ schemas[name] = {
219
+ viewName: sanitizeTableName(getPluralName(name)),
220
+ source,
221
+ groupBy: (viewClass as { groupBy?: string }).groupBy || undefined,
222
+ columns,
223
+ foreignKeys,
224
+ aggregates,
225
+ relationships,
226
+ isView: true,
227
+ memory: false,
228
+ };
229
+ }
230
+
231
+ return schemas;
232
+ }
233
+
234
+ export function buildViewDDL(name: string, viewSchema: ViewSchema, modelSchemas: Record<string, ModelSchema> = {}): string {
235
+ if (!viewSchema.source) {
236
+ throw new Error(`View '${name}' must define a source model`);
237
+ }
238
+
239
+ const sourceModelName = viewSchema.source;
240
+ const sourceSchema = modelSchemas[sourceModelName];
241
+ const sourceTable = sanitizeTableName(sourceSchema
242
+ ? sourceSchema.table
243
+ : getPluralName(sourceModelName));
244
+
245
+ const selectColumns: string[] = [];
246
+ const joins: JoinClause[] = [];
247
+ const hasAggregates = Object.keys(viewSchema.aggregates || {}).length > 0;
248
+ const groupByField = viewSchema.groupBy;
249
+
250
+ // ID column: groupBy field or source table PK
251
+ if (groupByField) {
252
+ selectColumns.push(`\`${sourceTable}\`.\`${groupByField}\` AS \`id\``);
253
+ } else {
254
+ selectColumns.push(`\`${sourceTable}\`.\`id\` AS \`id\``);
255
+ }
256
+
257
+ // Aggregate columns
258
+ for (const [key, aggProp] of Object.entries(viewSchema.aggregates || {})) {
259
+ if (aggProp.relationship === undefined) {
260
+ // Field-level aggregate (groupBy views)
261
+ if (aggProp.aggregateType === 'count') {
262
+ selectColumns.push(`COUNT(*) AS \`${key}\``);
263
+ } else {
264
+ selectColumns.push(`${aggProp.mysqlFunction}(\`${sourceTable}\`.\`${aggProp.field}\`) AS \`${key}\``);
265
+ }
266
+ } else {
267
+ // Relationship aggregate
268
+ const relName = aggProp.relationship;
269
+ const relTable = sanitizeTableName(getPluralName(relName));
270
+
271
+ if (aggProp.aggregateType === 'count') {
272
+ selectColumns.push(`${aggProp.mysqlFunction}(\`${relTable}\`.\`id\`) AS \`${key}\``);
273
+ } else {
274
+ const field = aggProp.field;
275
+ selectColumns.push(`${aggProp.mysqlFunction}(\`${relTable}\`.\`${field}\`) AS \`${key}\``);
276
+ }
277
+
278
+ // Add LEFT JOIN for the relationship if not already added
279
+ const joinKey = `${relTable}`;
280
+ if (!joins.find(j => j.table === joinKey)) {
281
+ const fkColumn = `${sourceModelName}_id`;
282
+ joins.push({
283
+ table: relTable,
284
+ condition: `\`${relTable}\`.\`${fkColumn}\` = \`${sourceTable}\`.\`id\``
285
+ });
286
+ }
287
+ }
288
+ }
289
+
290
+ // Regular columns (from resolve map string paths or direct attr fields)
291
+ for (const [key] of Object.entries(viewSchema.columns || {})) {
292
+ selectColumns.push(`\`${sourceTable}\`.\`${key}\` AS \`${key}\``);
293
+ }
294
+
295
+ // Build JOIN clauses
296
+ const joinClauses = joins.map(j =>
297
+ `LEFT JOIN \`${j.table}\` ON ${j.condition}`
298
+ ).join('\n ');
299
+
300
+ // Build GROUP BY
301
+ let groupBy = '';
302
+ if (groupByField) {
303
+ groupBy = `\nGROUP BY \`${sourceTable}\`.\`${groupByField}\``;
304
+ } else if (hasAggregates) {
305
+ groupBy = `\nGROUP BY \`${sourceTable}\`.\`id\``;
306
+ }
307
+
308
+ const viewName = sanitizeTableName(viewSchema.viewName);
309
+ const sql = `CREATE OR REPLACE VIEW \`${viewName}\` AS\nSELECT\n ${selectColumns.join(',\n ')}\nFROM \`${sourceTable}\`${joinClauses ? '\n ' + joinClauses : ''}${groupBy}`;
310
+
311
+ return sql;
312
+ }
313
+
314
+ export function viewSchemasToSnapshot(viewSchemas: Record<string, ViewSchema>): Record<string, ViewSnapshotEntry> {
315
+ const snapshot: Record<string, ViewSnapshotEntry> = {};
316
+
317
+ for (const [name, schema] of Object.entries(viewSchemas)) {
318
+ snapshot[name] = {
319
+ viewName: schema.viewName,
320
+ source: schema.source,
321
+ ...(schema.groupBy ? { groupBy: schema.groupBy } : {}),
322
+ columns: { ...schema.columns },
323
+ foreignKeys: { ...schema.foreignKeys },
324
+ isView: true,
325
+ viewQuery: buildViewDDL(name, schema),
326
+ };
327
+ }
328
+
329
+ return snapshot;
330
+ }
331
+
332
+ interface ViewSnapshotEntry {
333
+ viewName: string;
334
+ source: string;
335
+ groupBy?: string;
336
+ columns: Record<string, string>;
337
+ foreignKeys: Record<string, ForeignKeyDef>;
338
+ isView: true;
339
+ viewQuery: string;
340
+ }
341
+
342
+ export function schemasToSnapshot(schemas: Record<string, ModelSchema>): Record<string, SnapshotEntry> {
343
+ const snapshot: Record<string, SnapshotEntry> = {};
344
+
345
+ for (const [name, schema] of Object.entries(schemas)) {
346
+ snapshot[name] = {
347
+ table: schema.table,
348
+ idType: schema.idType,
349
+ columns: { ...schema.columns },
350
+ foreignKeys: { ...schema.foreignKeys },
351
+ };
352
+ }
353
+
354
+ return snapshot;
355
+ }
356
+
357
+ export type { ModelSchema, ViewSchema, ForeignKeyDef, SnapshotEntry } from '../types/orm-types.js';
358
+ export type { ViewSnapshotEntry };
@@ -1,37 +1,42 @@
1
- const typeMap = {
2
- string: 'VARCHAR(255)',
3
- number: 'INT',
4
- float: 'FLOAT',
5
- boolean: 'TINYINT(1)',
6
- date: 'DATETIME',
7
- timestamp: 'BIGINT',
8
- passthrough: 'TEXT',
9
- trim: 'VARCHAR(255)',
10
- uppercase: 'VARCHAR(255)',
11
- ceil: 'INT',
12
- floor: 'INT',
13
- round: 'INT',
14
- };
15
-
16
- /**
17
- * Resolves a Stonyx ORM attribute type to a MySQL column type.
18
- *
19
- * For built-in types, returns the mapped MySQL type directly.
20
- *
21
- * For custom transforms (e.g. an `animal` transform that maps strings to ints):
22
- * - If the transform function exports a `mysqlType` property, that value is used.
23
- * Example: `const transform = (v) => codeMap[v]; transform.mysqlType = 'INT'; export default transform;`
24
- * - Otherwise, defaults to JSON. Values are JSON-stringified on write and
25
- * JSON-parsed on read. This handles primitives and plain objects correctly.
26
- * Class instances will be reduced to plain objects if a custom transform
27
- * produces class instances, it must declare a `mysqlType` and handle
28
- * serialization itself.
29
- */
30
- export function getMysqlType(attrType, transformFn) {
31
- if (typeMap[attrType]) return typeMap[attrType];
32
- if (transformFn?.mysqlType) return transformFn.mysqlType;
33
-
34
- return 'JSON';
35
- }
36
-
37
- export default typeMap;
1
+ interface TransformFn {
2
+ mysqlType?: string;
3
+ (...args: unknown[]): unknown;
4
+ }
5
+
6
+ const typeMap: Record<string, string> = {
7
+ string: 'VARCHAR(255)',
8
+ number: 'INT',
9
+ float: 'FLOAT',
10
+ boolean: 'TINYINT(1)',
11
+ date: 'DATETIME',
12
+ timestamp: 'BIGINT',
13
+ passthrough: 'TEXT',
14
+ trim: 'VARCHAR(255)',
15
+ uppercase: 'VARCHAR(255)',
16
+ ceil: 'INT',
17
+ floor: 'INT',
18
+ round: 'INT',
19
+ };
20
+
21
+ /**
22
+ * Resolves a Stonyx ORM attribute type to a MySQL column type.
23
+ *
24
+ * For built-in types, returns the mapped MySQL type directly.
25
+ *
26
+ * For custom transforms (e.g. an `animal` transform that maps strings to ints):
27
+ * - If the transform function exports a `mysqlType` property, that value is used.
28
+ * Example: `const transform = (v) => codeMap[v]; transform.mysqlType = 'INT'; export default transform;`
29
+ * - Otherwise, defaults to JSON. Values are JSON-stringified on write and
30
+ * JSON-parsed on read. This handles primitives and plain objects correctly.
31
+ * Class instances will be reduced to plain objects — if a custom transform
32
+ * produces class instances, it must declare a `mysqlType` and handle
33
+ * serialization itself.
34
+ */
35
+ export function getMysqlType(attrType: string, transformFn?: TransformFn): string {
36
+ if (typeMap[attrType]) return typeMap[attrType];
37
+ if (transformFn?.mysqlType) return transformFn.mysqlType;
38
+
39
+ return 'JSON';
40
+ }
41
+
42
+ export default typeMap;