@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,90 @@
1
+ import { createRecord, store } from '@stonyx/orm';
2
+ import { getRelationships, getHasManyRegistry, getPendingRegistry, getPendingBelongsToRegistry } from './relationships.js';
3
+ import type { SourceRecord } from './types/orm-types.js';
4
+
5
+ function getOrSet<K, V>(map: Map<K, V>, key: K, defaultValue: V): V {
6
+ if (!map.has(key)) map.set(key, defaultValue);
7
+ return map.get(key) as V;
8
+ }
9
+
10
+ interface BelongsToOptions {
11
+ _relationshipKey?: string;
12
+ [key: string]: unknown;
13
+ }
14
+
15
+ interface PendingBelongsToEntry {
16
+ sourceRecord: SourceRecord;
17
+ sourceModelName: string;
18
+ relationshipKey: string | undefined;
19
+ relationshipId: unknown;
20
+ }
21
+
22
+ type RelationshipHandler = ((sourceRecord: SourceRecord, rawData: unknown, options: BelongsToOptions) => unknown) & {
23
+ __relatedModelName: string;
24
+ __relationshipType: 'belongsTo';
25
+ };
26
+
27
+ export default function belongsTo(modelName: string): RelationshipHandler {
28
+ const hasManyRelationships = getHasManyRegistry();
29
+ const pendingHasManyQueue = getPendingRegistry();
30
+ const pendingBelongsToQueue = getPendingBelongsToRegistry();
31
+
32
+ const fn = (sourceRecord: SourceRecord, rawData: unknown, options: BelongsToOptions): unknown => {
33
+ if (!rawData) return null;
34
+
35
+ const { __name: sourceModelName } = sourceRecord.__model;
36
+ const relationshipId = sourceRecord.id;
37
+ const relationshipKey = options._relationshipKey;
38
+ const relationship = getRelationships('belongsTo', sourceModelName, modelName, relationshipId as string) as Map<unknown, unknown>;
39
+ const modelStore = store.get(modelName);
40
+
41
+ // Try to get existing record
42
+ let output: unknown;
43
+
44
+ if (typeof rawData === 'object') {
45
+ output = createRecord(modelName, rawData as Record<string, unknown>, options);
46
+ } else if (modelStore) {
47
+ output = modelStore.get(rawData as number | string);
48
+ }
49
+
50
+ // If not found and is a string ID, register as pending
51
+ if (!output && typeof rawData !== 'object') {
52
+ const targetId = rawData;
53
+
54
+ // Register pending belongsTo
55
+ const modelPendingMap = getOrSet(pendingBelongsToQueue, modelName, new Map());
56
+ const targetPendingArray = getOrSet(modelPendingMap, targetId, []);
57
+
58
+ targetPendingArray.push({
59
+ sourceRecord,
60
+ sourceModelName,
61
+ relationshipKey,
62
+ relationshipId
63
+ });
64
+
65
+ relationship.set(relationshipId, null);
66
+ return null;
67
+ }
68
+
69
+ relationship.set(relationshipId, output || {});
70
+
71
+ // Populate hasMany side if the relationship is defined
72
+ const outputRecord = typeof output === 'object' && output !== null && 'id' in output ? output as SourceRecord : undefined;
73
+ const otherSide = outputRecord ? hasManyRelationships.get(modelName)?.get(sourceModelName)?.get(outputRecord.id) as unknown[] | undefined : undefined;
74
+
75
+ if (otherSide) {
76
+ otherSide.push(sourceRecord);
77
+
78
+ // Remove pending queue if it was just fulfilled
79
+ const pendingModelRelationships = pendingHasManyQueue.get(sourceModelName);
80
+
81
+ if (pendingModelRelationships) pendingModelRelationships.delete(relationshipId);
82
+ }
83
+
84
+ return output;
85
+ };
86
+
87
+ Object.defineProperty(fn, '__relatedModelName', { value: modelName });
88
+ Object.defineProperty(fn, '__relationshipType', { value: 'belongsTo' as const });
89
+ return fn as RelationshipHandler;
90
+ }
package/src/cli.ts ADDED
@@ -0,0 +1,183 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Standalone CLI for ORM database operations.
5
+ *
6
+ * Performs CRUD operations on the JSON database without requiring
7
+ * the full Stonyx bootstrap. Supports both file and directory modes.
8
+ *
9
+ * Usage:
10
+ * stonyx-orm create <collection> <json-data>
11
+ * stonyx-orm list <collection>
12
+ * stonyx-orm get <collection> <id>
13
+ * stonyx-orm delete <collection> <id>
14
+ *
15
+ * Configuration (environment variables):
16
+ * DB_MODE -- 'file' or 'directory' (default: 'directory')
17
+ * DB_PATH -- Path to db.json (default: 'db.json')
18
+ * DB_DIRECTORY -- Directory name for collection files (default: 'db')
19
+ *
20
+ * Configuration (CLI flag):
21
+ * --config <path> -- Path to a JSON config file with { mode, dbPath, directory }
22
+ */
23
+
24
+ import StandaloneDB from './standalone-db.js';
25
+ import fs from 'fs/promises';
26
+
27
+ interface CLIConfig {
28
+ mode: 'file' | 'directory';
29
+ dbPath: string;
30
+ directory: string;
31
+ }
32
+
33
+ const USAGE = `Usage: stonyx-orm <command> [options]
34
+
35
+ Commands:
36
+ create <collection> <json-data> Create a record
37
+ list <collection> List all records
38
+ get <collection> <id> Get a record by ID
39
+ delete <collection> <id> Delete a record by ID
40
+
41
+ Options:
42
+ --config <path> Path to JSON config file
43
+ --help Show this help message
44
+
45
+ Environment variables:
46
+ DB_MODE 'file' or 'directory' (default: 'directory')
47
+ DB_PATH Path to db.json (default: 'db.json')
48
+ DB_DIRECTORY Directory name for collection files (default: 'db')`;
49
+
50
+ async function loadConfig(args: string[]): Promise<CLIConfig> {
51
+ const config: Record<string, string> = {};
52
+
53
+ // Check for --config flag
54
+ const configIndex = args.indexOf('--config');
55
+
56
+ if (configIndex !== -1 && args[configIndex + 1]) {
57
+ const configPath = args[configIndex + 1];
58
+
59
+ try {
60
+ const content = await fs.readFile(configPath, 'utf-8');
61
+ Object.assign(config, JSON.parse(content));
62
+ } catch (err) {
63
+ console.error(`Error reading config file '${configPath}': ${err instanceof Error ? err.message : String(err)}`);
64
+ process.exit(1);
65
+ }
66
+
67
+ // Remove --config and its value from args
68
+ args.splice(configIndex, 2);
69
+ }
70
+
71
+ // Environment variables override config file, config file overrides defaults
72
+ return {
73
+ mode: (process.env.DB_MODE || config.mode || 'directory') as 'file' | 'directory',
74
+ dbPath: process.env.DB_PATH || config.dbPath || 'db.json',
75
+ directory: process.env.DB_DIRECTORY || config.directory || 'db',
76
+ };
77
+ }
78
+
79
+ function parseArgs(argv: string[]): string[] {
80
+ // Strip node binary and script path
81
+ const args = argv.slice(2);
82
+
83
+ if (args.includes('--help') || args.includes('-h') || args.length === 0) {
84
+ console.log(USAGE);
85
+ process.exit(0);
86
+ }
87
+
88
+ return args;
89
+ }
90
+
91
+ async function run(): Promise<void> {
92
+ const args = parseArgs(process.argv);
93
+ const config = await loadConfig(args);
94
+ const db = new StandaloneDB(config);
95
+
96
+ const [command, collection, ...rest] = args;
97
+
98
+ if (!command) {
99
+ console.error('Error: No command specified.\n');
100
+ console.log(USAGE);
101
+ process.exit(1);
102
+ }
103
+
104
+ if (!collection && command !== '--help') {
105
+ console.error(`Error: No collection specified for '${command}' command.\n`);
106
+ console.log(USAGE);
107
+ process.exit(1);
108
+ }
109
+
110
+ try {
111
+ switch (command) {
112
+ case 'list': {
113
+ const records = await db.list(collection);
114
+ console.log(JSON.stringify(records, null, 2));
115
+ break;
116
+ }
117
+
118
+ case 'get': {
119
+ const id = rest[0];
120
+
121
+ if (!id) {
122
+ console.error("Error: 'get' command requires an <id> argument.");
123
+ process.exit(1);
124
+ }
125
+
126
+ const record = await db.get(collection, id);
127
+
128
+ if (!record) {
129
+ console.error(`Record with id '${id}' not found in '${collection}'.`);
130
+ process.exit(1);
131
+ }
132
+
133
+ console.log(JSON.stringify(record, null, 2));
134
+ break;
135
+ }
136
+
137
+ case 'create': {
138
+ const jsonStr = rest.join(' ');
139
+
140
+ if (!jsonStr) {
141
+ console.error("Error: 'create' command requires <json-data> argument.");
142
+ process.exit(1);
143
+ }
144
+
145
+ let data;
146
+
147
+ try {
148
+ data = JSON.parse(jsonStr);
149
+ } catch {
150
+ console.error(`Error: Invalid JSON data: ${jsonStr}`);
151
+ process.exit(1);
152
+ }
153
+
154
+ const created = await db.create(collection, data);
155
+ console.log(JSON.stringify(created, null, 2));
156
+ break;
157
+ }
158
+
159
+ case 'delete': {
160
+ const deleteId = rest[0];
161
+
162
+ if (!deleteId) {
163
+ console.error("Error: 'delete' command requires an <id> argument.");
164
+ process.exit(1);
165
+ }
166
+
167
+ const removed = await db.delete(collection, deleteId);
168
+ console.log(JSON.stringify(removed, null, 2));
169
+ break;
170
+ }
171
+
172
+ default:
173
+ console.error(`Error: Unknown command '${command}'.\n`);
174
+ console.log(USAGE);
175
+ process.exit(1);
176
+ }
177
+ } catch (err) {
178
+ console.error(`Error: ${err instanceof Error ? err.message : String(err)}`);
179
+ process.exit(1);
180
+ }
181
+ }
182
+
183
+ run();
@@ -1,170 +1,179 @@
1
- import { fileToDirectory, directoryToFile } from './migrate.js';
2
-
3
- export default {
4
- 'db:migrate-to-directory': {
5
- description: 'Migrate DB from single file to directory mode',
6
- bootstrap: true,
7
- run: async () => {
8
- await fileToDirectory();
9
- console.log('DB migration to directory mode complete.');
10
- }
11
- },
12
- 'db:migrate-to-file': {
13
- description: 'Migrate DB from directory mode to single file',
14
- bootstrap: true,
15
- run: async () => {
16
- await directoryToFile();
17
- console.log('DB migration to file mode complete.');
18
- }
19
- },
20
- 'db:generate-migration': {
21
- description: 'Generate a MySQL migration from current model schemas',
22
- bootstrap: true,
23
- run: async (args) => {
24
- const description = args.join(' ') || 'migration';
25
- const { generateMigration } = await import('./mysql/migration-generator.js');
26
- const result = await generateMigration(description);
27
-
28
- if (result) {
29
- console.log(`Migration created: ${result.filename}`);
30
- } else {
31
- console.log('No schema changes detected. No migration generated.');
32
- }
33
- }
34
- },
35
- 'db:migrate': {
36
- description: 'Apply pending MySQL migrations',
37
- bootstrap: true,
38
- run: async () => {
39
- const config = (await import('stonyx/config')).default;
40
- const mysqlConfig = config.orm.mysql;
41
-
42
- if (!mysqlConfig) {
43
- console.error('MySQL is not configured. Set MYSQL_HOST to enable MySQL mode.');
44
- process.exit(1);
45
- }
46
-
47
- const { getPool, closePool } = await import('./mysql/connection.js');
48
- const { ensureMigrationsTable, getAppliedMigrations, getMigrationFiles, applyMigration, parseMigrationFile } = await import('./mysql/migration-runner.js');
49
- const { readFile } = await import('@stonyx/utils/file');
50
- const path = await import('path');
51
-
52
- const pool = await getPool(mysqlConfig);
53
- const migrationsPath = path.resolve(config.rootPath, mysqlConfig.migrationsDir);
54
-
55
- try {
56
- await ensureMigrationsTable(pool, mysqlConfig.migrationsTable);
57
-
58
- const applied = await getAppliedMigrations(pool, mysqlConfig.migrationsTable);
59
- const files = await getMigrationFiles(migrationsPath);
60
- const pending = files.filter(f => !applied.includes(f));
61
-
62
- if (pending.length === 0) {
63
- console.log('No pending migrations.');
64
- return;
65
- }
66
-
67
- console.log(`Applying ${pending.length} migration(s)...`);
68
-
69
- for (const filename of pending) {
70
- const content = await readFile(path.join(migrationsPath, filename));
71
- const { up } = parseMigrationFile(content);
72
-
73
- await applyMigration(pool, filename, up, mysqlConfig.migrationsTable);
74
- console.log(` Applied: ${filename}`);
75
- }
76
-
77
- console.log('All migrations applied.');
78
- } finally {
79
- await closePool();
80
- }
81
- }
82
- },
83
- 'db:migrate:rollback': {
84
- description: 'Rollback the most recent MySQL migration',
85
- bootstrap: true,
86
- run: async () => {
87
- const config = (await import('stonyx/config')).default;
88
- const mysqlConfig = config.orm.mysql;
89
-
90
- if (!mysqlConfig) {
91
- console.error('MySQL is not configured. Set MYSQL_HOST to enable MySQL mode.');
92
- process.exit(1);
93
- }
94
-
95
- const { getPool, closePool } = await import('./mysql/connection.js');
96
- const { ensureMigrationsTable, getAppliedMigrations, rollbackMigration, parseMigrationFile } = await import('./mysql/migration-runner.js');
97
- const { readFile } = await import('@stonyx/utils/file');
98
- const path = await import('path');
99
-
100
- const pool = await getPool(mysqlConfig);
101
- const migrationsPath = path.resolve(config.rootPath, mysqlConfig.migrationsDir);
102
-
103
- try {
104
- await ensureMigrationsTable(pool, mysqlConfig.migrationsTable);
105
-
106
- const applied = await getAppliedMigrations(pool, mysqlConfig.migrationsTable);
107
-
108
- if (applied.length === 0) {
109
- console.log('No migrations to rollback.');
110
- return;
111
- }
112
-
113
- const lastFilename = applied[applied.length - 1];
114
- const content = await readFile(path.join(migrationsPath, lastFilename));
115
- const { down } = parseMigrationFile(content);
116
-
117
- if (!down) {
118
- console.error(`No DOWN section found in ${lastFilename}. Cannot rollback.`);
119
- process.exit(1);
120
- }
121
-
122
- await rollbackMigration(pool, lastFilename, down, mysqlConfig.migrationsTable);
123
- console.log(`Rolled back: ${lastFilename}`);
124
- } finally {
125
- await closePool();
126
- }
127
- }
128
- },
129
- 'db:migrate:status': {
130
- description: 'Show status of MySQL migrations',
131
- bootstrap: true,
132
- run: async () => {
133
- const config = (await import('stonyx/config')).default;
134
- const mysqlConfig = config.orm.mysql;
135
-
136
- if (!mysqlConfig) {
137
- console.error('MySQL is not configured. Set MYSQL_HOST to enable MySQL mode.');
138
- process.exit(1);
139
- }
140
-
141
- const { getPool, closePool } = await import('./mysql/connection.js');
142
- const { ensureMigrationsTable, getAppliedMigrations, getMigrationFiles } = await import('./mysql/migration-runner.js');
143
- const path = await import('path');
144
-
145
- const pool = await getPool(mysqlConfig);
146
- const migrationsPath = path.resolve(config.rootPath, mysqlConfig.migrationsDir);
147
-
148
- try {
149
- await ensureMigrationsTable(pool, mysqlConfig.migrationsTable);
150
-
151
- const applied = new Set(await getAppliedMigrations(pool, mysqlConfig.migrationsTable));
152
- const files = await getMigrationFiles(migrationsPath);
153
-
154
- if (files.length === 0) {
155
- console.log('No migration files found.');
156
- return;
157
- }
158
-
159
- console.log('Migration status:');
160
-
161
- for (const filename of files) {
162
- const status = applied.has(filename) ? 'applied' : 'pending';
163
- console.log(` [${status}] ${filename}`);
164
- }
165
- } finally {
166
- await closePool();
167
- }
168
- }
169
- },
170
- };
1
+ import { fileToDirectory, directoryToFile } from './migrate.js';
2
+ import type { MysqlConfig } from './mysql/connection.js';
3
+
4
+ interface Command {
5
+ description: string;
6
+ bootstrap: boolean;
7
+ run: (args?: string[]) => Promise<void>;
8
+ }
9
+
10
+ const commands: Record<string, Command> = {
11
+ 'db:migrate-to-directory': {
12
+ description: 'Migrate DB from single file to directory mode',
13
+ bootstrap: true,
14
+ run: async () => {
15
+ await fileToDirectory();
16
+ console.log('DB migration to directory mode complete.');
17
+ }
18
+ },
19
+ 'db:migrate-to-file': {
20
+ description: 'Migrate DB from directory mode to single file',
21
+ bootstrap: true,
22
+ run: async () => {
23
+ await directoryToFile();
24
+ console.log('DB migration to file mode complete.');
25
+ }
26
+ },
27
+ 'db:generate-migration': {
28
+ description: 'Generate a MySQL migration from current model schemas',
29
+ bootstrap: true,
30
+ run: async (args) => {
31
+ const description = args?.join(' ') || 'migration';
32
+ const { generateMigration } = await import('./mysql/migration-generator.js');
33
+ const result = await generateMigration(description);
34
+
35
+ if (result) {
36
+ console.log(`Migration created: ${result.filename}`);
37
+ } else {
38
+ console.log('No schema changes detected. No migration generated.');
39
+ }
40
+ }
41
+ },
42
+ 'db:migrate': {
43
+ description: 'Apply pending MySQL migrations',
44
+ bootstrap: true,
45
+ run: async () => {
46
+ const config = (await import('stonyx/config')).default;
47
+ const mysqlConfig = config.orm.mysql;
48
+
49
+ if (!mysqlConfig) {
50
+ console.error('MySQL is not configured. Set MYSQL_HOST to enable MySQL mode.');
51
+ process.exit(1);
52
+ }
53
+
54
+ const { getPool, closePool } = await import('./mysql/connection.js');
55
+ const { ensureMigrationsTable, getAppliedMigrations, getMigrationFiles, applyMigration, parseMigrationFile } = await import('./mysql/migration-runner.js');
56
+ const { readFile } = await import('@stonyx/utils/file');
57
+ const path = await import('path');
58
+
59
+ const pool = await getPool(mysqlConfig as MysqlConfig);
60
+ const migrationsPath = path.resolve(config.rootPath, mysqlConfig.migrationsDir as string);
61
+
62
+ try {
63
+ await ensureMigrationsTable(pool, mysqlConfig.migrationsTable as string);
64
+
65
+ const applied = await getAppliedMigrations(pool, mysqlConfig.migrationsTable as string);
66
+ const files = await getMigrationFiles(migrationsPath);
67
+ const pending = files.filter((f: string) => !applied.includes(f));
68
+
69
+ if (pending.length === 0) {
70
+ console.log('No pending migrations.');
71
+ return;
72
+ }
73
+
74
+ console.log(`Applying ${pending.length} migration(s)...`);
75
+
76
+ for (const filename of pending) {
77
+ const content = await readFile(path.join(migrationsPath, filename) as string) as string;
78
+ const { up } = parseMigrationFile(content);
79
+
80
+ await applyMigration(pool, filename, up, mysqlConfig.migrationsTable as string);
81
+ console.log(` Applied: ${filename}`);
82
+ }
83
+
84
+ console.log('All migrations applied.');
85
+ } finally {
86
+ await closePool();
87
+ }
88
+ }
89
+ },
90
+ 'db:migrate:rollback': {
91
+ description: 'Rollback the most recent MySQL migration',
92
+ bootstrap: true,
93
+ run: async () => {
94
+ const config = (await import('stonyx/config')).default;
95
+ const mysqlConfig = config.orm.mysql;
96
+
97
+ if (!mysqlConfig) {
98
+ console.error('MySQL is not configured. Set MYSQL_HOST to enable MySQL mode.');
99
+ process.exit(1);
100
+ }
101
+
102
+ const { getPool, closePool } = await import('./mysql/connection.js');
103
+ const { ensureMigrationsTable, getAppliedMigrations, rollbackMigration, parseMigrationFile } = await import('./mysql/migration-runner.js');
104
+ const { readFile } = await import('@stonyx/utils/file');
105
+ const path = await import('path');
106
+
107
+ const pool = await getPool(mysqlConfig as MysqlConfig);
108
+ const migrationsPath = path.resolve(config.rootPath, mysqlConfig.migrationsDir as string);
109
+
110
+ try {
111
+ await ensureMigrationsTable(pool, mysqlConfig.migrationsTable as string);
112
+
113
+ const applied = await getAppliedMigrations(pool, mysqlConfig.migrationsTable as string);
114
+
115
+ if (applied.length === 0) {
116
+ console.log('No migrations to rollback.');
117
+ return;
118
+ }
119
+
120
+ const lastFilename = applied[applied.length - 1];
121
+ const content = await readFile(path.join(migrationsPath, lastFilename) as string) as string;
122
+ const { down } = parseMigrationFile(content);
123
+
124
+ if (!down) {
125
+ console.error(`No DOWN section found in ${lastFilename}. Cannot rollback.`);
126
+ process.exit(1);
127
+ }
128
+
129
+ await rollbackMigration(pool, lastFilename, down, mysqlConfig.migrationsTable as string);
130
+ console.log(`Rolled back: ${lastFilename}`);
131
+ } finally {
132
+ await closePool();
133
+ }
134
+ }
135
+ },
136
+ 'db:migrate:status': {
137
+ description: 'Show status of MySQL migrations',
138
+ bootstrap: true,
139
+ run: async () => {
140
+ const config = (await import('stonyx/config')).default;
141
+ const mysqlConfig = config.orm.mysql;
142
+
143
+ if (!mysqlConfig) {
144
+ console.error('MySQL is not configured. Set MYSQL_HOST to enable MySQL mode.');
145
+ process.exit(1);
146
+ }
147
+
148
+ const { getPool, closePool } = await import('./mysql/connection.js');
149
+ const { ensureMigrationsTable, getAppliedMigrations, getMigrationFiles } = await import('./mysql/migration-runner.js');
150
+ const path = await import('path');
151
+
152
+ const pool = await getPool(mysqlConfig as MysqlConfig);
153
+ const migrationsPath = path.resolve(config.rootPath, mysqlConfig.migrationsDir as string);
154
+
155
+ try {
156
+ await ensureMigrationsTable(pool, mysqlConfig.migrationsTable as string);
157
+
158
+ const applied = new Set(await getAppliedMigrations(pool, mysqlConfig.migrationsTable as string));
159
+ const files = await getMigrationFiles(migrationsPath);
160
+
161
+ if (files.length === 0) {
162
+ console.log('No migration files found.');
163
+ return;
164
+ }
165
+
166
+ console.log('Migration status:');
167
+
168
+ for (const filename of files) {
169
+ const status = applied.has(filename) ? 'applied' : 'pending';
170
+ console.log(` [${status}] ${filename}`);
171
+ }
172
+ } finally {
173
+ await closePool();
174
+ }
175
+ }
176
+ },
177
+ };
178
+
179
+ export default commands;