@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.
- package/README.md +64 -6
- package/config/environment.js +37 -1
- package/dist/aggregates.d.ts +21 -0
- package/dist/aggregates.js +93 -0
- package/dist/attr.d.ts +2 -0
- package/dist/attr.js +22 -0
- package/dist/belongs-to.d.ts +11 -0
- package/dist/belongs-to.js +59 -0
- package/dist/cli.d.ts +22 -0
- package/dist/cli.js +148 -0
- package/dist/commands.d.ts +7 -0
- package/dist/commands.js +146 -0
- package/dist/db.d.ts +21 -0
- package/dist/db.js +180 -0
- package/dist/exports/db.d.ts +7 -0
- package/{src → dist}/exports/db.js +2 -4
- package/dist/has-many.d.ts +11 -0
- package/dist/has-many.js +58 -0
- package/dist/hooks.d.ts +62 -0
- package/dist/hooks.js +110 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +34 -0
- package/dist/main.d.ts +46 -0
- package/dist/main.js +181 -0
- package/dist/manage-record.d.ts +13 -0
- package/dist/manage-record.js +123 -0
- package/dist/meta-request.d.ts +6 -0
- package/dist/meta-request.js +52 -0
- package/dist/migrate.d.ts +2 -0
- package/dist/migrate.js +57 -0
- package/dist/model-property.d.ts +9 -0
- package/dist/model-property.js +29 -0
- package/dist/model.d.ts +15 -0
- package/dist/model.js +18 -0
- package/dist/mysql/connection.d.ts +14 -0
- package/dist/mysql/connection.js +24 -0
- package/dist/mysql/migration-generator.d.ts +45 -0
- package/dist/mysql/migration-generator.js +254 -0
- package/dist/mysql/migration-runner.d.ts +12 -0
- package/dist/mysql/migration-runner.js +88 -0
- package/dist/mysql/mysql-db.d.ts +100 -0
- package/dist/mysql/mysql-db.js +425 -0
- package/dist/mysql/query-builder.d.ts +10 -0
- package/dist/mysql/query-builder.js +44 -0
- package/dist/mysql/schema-introspector.d.ts +19 -0
- package/dist/mysql/schema-introspector.js +291 -0
- package/dist/mysql/type-map.d.ts +21 -0
- package/dist/mysql/type-map.js +36 -0
- package/dist/orm-request.d.ts +38 -0
- package/dist/orm-request.js +474 -0
- package/dist/plural-registry.d.ts +4 -0
- package/dist/plural-registry.js +9 -0
- package/dist/postgres/connection.d.ts +15 -0
- package/dist/postgres/connection.js +32 -0
- package/dist/postgres/migration-generator.d.ts +45 -0
- package/dist/postgres/migration-generator.js +261 -0
- package/dist/postgres/migration-runner.d.ts +10 -0
- package/dist/postgres/migration-runner.js +87 -0
- package/dist/postgres/postgres-db.d.ts +119 -0
- package/dist/postgres/postgres-db.js +477 -0
- package/dist/postgres/query-builder.d.ts +27 -0
- package/dist/postgres/query-builder.js +98 -0
- package/dist/postgres/schema-introspector.d.ts +29 -0
- package/dist/postgres/schema-introspector.js +314 -0
- package/dist/postgres/type-map.d.ts +23 -0
- package/dist/postgres/type-map.js +56 -0
- package/dist/record.d.ts +75 -0
- package/dist/record.js +129 -0
- package/dist/relationships.d.ts +10 -0
- package/dist/relationships.js +41 -0
- package/dist/serializer.d.ts +17 -0
- package/dist/serializer.js +136 -0
- package/dist/setup-rest-server.d.ts +1 -0
- package/dist/setup-rest-server.js +52 -0
- package/dist/standalone-db.d.ts +58 -0
- package/dist/standalone-db.js +142 -0
- package/dist/store.d.ts +62 -0
- package/dist/store.js +286 -0
- package/dist/timescale/query-builder.d.ts +43 -0
- package/dist/timescale/query-builder.js +115 -0
- package/dist/timescale/timescale-db.d.ts +45 -0
- package/dist/timescale/timescale-db.js +84 -0
- package/dist/transforms.d.ts +2 -0
- package/dist/transforms.js +17 -0
- package/dist/types/orm-types.d.ts +142 -0
- package/dist/types/orm-types.js +1 -0
- package/dist/utils.d.ts +7 -0
- package/dist/utils.js +17 -0
- package/dist/view-resolver.d.ts +8 -0
- package/dist/view-resolver.js +171 -0
- package/dist/view.d.ts +11 -0
- package/dist/view.js +18 -0
- package/package.json +57 -15
- package/src/aggregates.ts +109 -0
- package/src/{attr.js → attr.ts} +2 -2
- package/src/belongs-to.ts +90 -0
- package/src/cli.ts +183 -0
- package/src/{commands.js → commands.ts} +179 -170
- package/src/{db.js → db.ts} +55 -29
- package/src/exports/db.ts +7 -0
- package/src/has-many.ts +92 -0
- package/src/{hooks.js → hooks.ts} +41 -27
- package/src/{index.js → index.ts} +11 -2
- package/src/main.ts +229 -0
- package/src/manage-record.ts +161 -0
- package/src/{meta-request.js → meta-request.ts} +17 -14
- package/src/{migrate.js → migrate.ts} +9 -9
- package/src/model-property.ts +35 -0
- package/src/model.ts +21 -0
- package/src/mysql/{connection.js → connection.ts} +43 -28
- package/src/mysql/migration-generator.ts +337 -0
- package/src/mysql/{migration-runner.js → migration-runner.ts} +121 -110
- package/src/mysql/mysql-db.ts +543 -0
- package/src/mysql/{query-builder.js → query-builder.ts} +69 -64
- package/src/mysql/schema-introspector.ts +358 -0
- package/src/mysql/{type-map.js → type-map.ts} +42 -37
- package/src/{orm-request.js → orm-request.ts} +186 -108
- package/src/plural-registry.ts +12 -0
- package/src/postgres/connection.ts +48 -0
- package/src/postgres/migration-generator.ts +348 -0
- package/src/postgres/migration-runner.ts +115 -0
- package/src/postgres/postgres-db.ts +616 -0
- package/src/postgres/query-builder.ts +148 -0
- package/src/postgres/schema-introspector.ts +386 -0
- package/src/postgres/type-map.ts +61 -0
- package/src/record.ts +186 -0
- package/src/relationships.ts +54 -0
- package/src/serializer.ts +161 -0
- package/src/{setup-rest-server.js → setup-rest-server.ts} +18 -16
- package/src/standalone-db.ts +185 -0
- package/src/store.ts +373 -0
- package/src/timescale/query-builder.ts +174 -0
- package/src/timescale/timescale-db.ts +119 -0
- package/src/transforms.ts +20 -0
- package/src/types/mysql2.d.ts +49 -0
- package/src/types/orm-types.ts +146 -0
- package/src/types/pg.d.ts +32 -0
- package/src/types/stonyx-cron.d.ts +5 -0
- package/src/types/stonyx-events.d.ts +4 -0
- package/src/types/stonyx-rest-server.d.ts +16 -0
- package/src/types/stonyx-utils.d.ts +33 -0
- package/src/types/stonyx.d.ts +21 -0
- package/src/utils.ts +22 -0
- package/src/view-resolver.ts +211 -0
- package/src/view.ts +22 -0
- package/.claude/code-style-rules.md +0 -44
- package/.claude/hooks.md +0 -250
- package/.claude/index.md +0 -279
- package/.claude/usage-patterns.md +0 -217
- package/.github/workflows/ci.yml +0 -16
- package/.github/workflows/publish.yml +0 -51
- package/improvements.md +0 -139
- package/project-structure.md +0 -343
- package/src/belongs-to.js +0 -63
- package/src/has-many.js +0 -61
- package/src/main.js +0 -148
- package/src/manage-record.js +0 -118
- package/src/model-property.js +0 -29
- package/src/model.js +0 -9
- package/src/mysql/migration-generator.js +0 -188
- package/src/mysql/mysql-db.js +0 -320
- package/src/mysql/schema-introspector.js +0 -158
- package/src/record.js +0 -127
- package/src/relationships.js +0 -43
- package/src/serializer.js +0 -138
- package/src/store.js +0 -211
- package/src/transforms.js +0 -20
- package/src/utils.js +0 -12
- package/test-events-setup.js +0 -41
- package/test-hooks-manual.js +0 -54
- 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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
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
|
-
const
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
|
|
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;
|