@neupgroup/mapper 1.4.0 → 1.4.1
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/dist/cli/create-migration.js +30 -0
- package/dist/cli/migrate.d.ts +2 -0
- package/dist/cli/migrate.js +57 -0
- package/dist/migrator.d.ts +2 -0
- package/dist/migrator.js +41 -0
- package/package.json +5 -3
|
@@ -61,3 +61,33 @@ export const ${tableName} = {
|
|
|
61
61
|
else {
|
|
62
62
|
console.log(`Schema file already exists: ${schemaFilePath}`);
|
|
63
63
|
}
|
|
64
|
+
// Update migrations index
|
|
65
|
+
const indexFilePath = path.join(migrationDir, 'index.ts');
|
|
66
|
+
let migrations = [];
|
|
67
|
+
let completed = [];
|
|
68
|
+
if (fs.existsSync(indexFilePath)) {
|
|
69
|
+
const content = fs.readFileSync(indexFilePath, 'utf-8');
|
|
70
|
+
const matchMigrations = content.match(/migrations = \[(.*?)\]/s);
|
|
71
|
+
if (matchMigrations) {
|
|
72
|
+
migrations = matchMigrations[1].split(',').map(s => s.trim().replace(/['"]/g, '')).filter(Boolean);
|
|
73
|
+
}
|
|
74
|
+
const matchCompleted = content.match(/completed = \[(.*?)\]/s);
|
|
75
|
+
if (matchCompleted) {
|
|
76
|
+
completed = matchCompleted[1].split(',').map(s => s.trim().replace(/['"]/g, '')).filter(Boolean);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const migrationName = fileName.replace('.ts', '');
|
|
80
|
+
if (!migrations.includes(migrationName)) {
|
|
81
|
+
migrations.push(migrationName);
|
|
82
|
+
}
|
|
83
|
+
const indexContent = `
|
|
84
|
+
export const migrations = [
|
|
85
|
+
${migrations.map(m => ` '${m}'`).join(',\n')}
|
|
86
|
+
];
|
|
87
|
+
|
|
88
|
+
export const completed = [
|
|
89
|
+
${completed.map(m => ` '${m}'`).join(',\n')}
|
|
90
|
+
];
|
|
91
|
+
`;
|
|
92
|
+
fs.writeFileSync(indexFilePath, indexContent.trim() + '\n');
|
|
93
|
+
console.log(`Updated migration index: ${indexFilePath}`);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
const migrationDir = path.resolve(process.cwd(), 'src/migration');
|
|
5
|
+
const indexFilePath = path.join(migrationDir, 'index.ts');
|
|
6
|
+
if (!fs.existsSync(indexFilePath)) {
|
|
7
|
+
console.log('No migrations found.');
|
|
8
|
+
process.exit(0);
|
|
9
|
+
}
|
|
10
|
+
async function run() {
|
|
11
|
+
const content = fs.readFileSync(indexFilePath, 'utf-8');
|
|
12
|
+
const matchMigrations = content.match(/migrations = \[(.*?)\]/s);
|
|
13
|
+
const matchCompleted = content.match(/completed = \[(.*?)\]/s);
|
|
14
|
+
if (!matchMigrations) {
|
|
15
|
+
console.log('No migrations list found in index.ts');
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const migrations = matchMigrations[1].split(',').map(s => s.trim().replace(/['"]/g, '')).filter(Boolean);
|
|
19
|
+
const completed = matchCompleted ? matchCompleted[1].split(',').map(s => s.trim().replace(/['"]/g, '')).filter(Boolean) : [];
|
|
20
|
+
const pending = migrations.filter(m => !completed.includes(m));
|
|
21
|
+
if (pending.length === 0) {
|
|
22
|
+
console.log('No pending migrations.');
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
console.log(`Found ${pending.length} pending migrations.`);
|
|
26
|
+
for (const m of pending) {
|
|
27
|
+
console.log(`Running migration: ${m}...`);
|
|
28
|
+
const filePath = path.join(migrationDir, `${m}.ts`);
|
|
29
|
+
// Use path.relative to current working directory or absolute file:// URL
|
|
30
|
+
const absolutePath = path.isAbsolute(filePath) ? filePath : path.resolve(filePath);
|
|
31
|
+
const mod = await import('file://' + absolutePath);
|
|
32
|
+
if (mod.up) {
|
|
33
|
+
await mod.up();
|
|
34
|
+
completed.push(m);
|
|
35
|
+
console.log(`Completed: ${m}`);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
console.error(`Migration ${m} does not have an up() function.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Update index.ts
|
|
42
|
+
const indexContent = `
|
|
43
|
+
export const migrations = [
|
|
44
|
+
${migrations.map(m => ` '${m}'`).join(',\n')}
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
export const completed = [
|
|
48
|
+
${completed.map(m => ` '${m}'`).join(',\n')}
|
|
49
|
+
];
|
|
50
|
+
`;
|
|
51
|
+
fs.writeFileSync(indexFilePath, indexContent.trim() + '\n');
|
|
52
|
+
console.log('Migration runner finished.');
|
|
53
|
+
}
|
|
54
|
+
run().catch(err => {
|
|
55
|
+
console.error('Migration failed:', err);
|
|
56
|
+
process.exit(1);
|
|
57
|
+
});
|
package/dist/migrator.d.ts
CHANGED
package/dist/migrator.js
CHANGED
|
@@ -62,4 +62,45 @@ export class TableMigrator {
|
|
|
62
62
|
this.columns.push(col);
|
|
63
63
|
return col;
|
|
64
64
|
}
|
|
65
|
+
getColumns() {
|
|
66
|
+
return this.columns.map(c => c.getDefinition());
|
|
67
|
+
}
|
|
68
|
+
async exec() {
|
|
69
|
+
// This is where we trigger schema file update
|
|
70
|
+
const fs = await import('fs');
|
|
71
|
+
const path = await import('path');
|
|
72
|
+
const schemasDir = path.resolve(process.cwd(), 'src/schemas');
|
|
73
|
+
const schemaFilePath = path.join(schemasDir, `${this.name}.ts`);
|
|
74
|
+
const columns = this.getColumns();
|
|
75
|
+
let existingSchema = {
|
|
76
|
+
fields: [],
|
|
77
|
+
insertableFields: [],
|
|
78
|
+
updatableFields: [],
|
|
79
|
+
massUpdateable: false,
|
|
80
|
+
massDeletable: false,
|
|
81
|
+
usesConnection: 'default'
|
|
82
|
+
};
|
|
83
|
+
if (fs.existsSync(schemaFilePath)) {
|
|
84
|
+
// Very basic parser/updater.
|
|
85
|
+
// Real implementation would use AST, but for now we'll do simple string replacement or overwrite if it's the first time.
|
|
86
|
+
// For the sake of this demo, we will generate the fields array.
|
|
87
|
+
}
|
|
88
|
+
const fieldsContent = columns.map(col => {
|
|
89
|
+
return ` { name: '${col.name}', type: '${col.type}'${col.isPrimary ? ', isPrimary: true' : ''}${col.autoIncrement ? ', autoIncrement: true' : ''}${col.notNull ? ', notNull: true' : ''}${col.isUnique ? ', isUnique: true' : ''} }`;
|
|
90
|
+
}).join(',\n');
|
|
91
|
+
const schemaContent = `
|
|
92
|
+
export const ${this.name} = {
|
|
93
|
+
fields: [
|
|
94
|
+
${fieldsContent}
|
|
95
|
+
],
|
|
96
|
+
insertableFields: [${columns.filter(c => !c.autoIncrement).map(c => `'${c.name}'`).join(', ')}],
|
|
97
|
+
updatableFields: [${columns.filter(c => !c.autoIncrement && !c.isPrimary).map(c => `'${c.name}'`).join(', ')}],
|
|
98
|
+
massUpdateable: false,
|
|
99
|
+
massDeletable: false,
|
|
100
|
+
usesConnection: 'default'
|
|
101
|
+
};
|
|
102
|
+
`;
|
|
103
|
+
fs.writeFileSync(schemaFilePath, schemaContent.trim() + '\n');
|
|
104
|
+
console.log(`Updated schema: ${schemaFilePath}`);
|
|
105
|
+
}
|
|
65
106
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neupgroup/mapper",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "Neup.Mapper core library for schema and mapping utilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
"types": "dist/index.d.ts",
|
|
9
9
|
"bin": {
|
|
10
10
|
"create-migration": "dist/cli/create-migration.js",
|
|
11
|
-
"create-connection": "dist/cli/create-connection.js"
|
|
11
|
+
"create-connection": "dist/cli/create-connection.js",
|
|
12
|
+
"migrate": "dist/cli/migrate.js"
|
|
12
13
|
},
|
|
13
14
|
"files": [
|
|
14
15
|
"dist"
|
|
@@ -17,7 +18,8 @@
|
|
|
17
18
|
"scripts": {
|
|
18
19
|
"build": "tsc -p ./tsconfig.json",
|
|
19
20
|
"create-migration": "npx tsx src/cli/create-migration.ts",
|
|
20
|
-
"create-connection": "npx tsx src/cli/create-connection.ts"
|
|
21
|
+
"create-connection": "npx tsx src/cli/create-connection.ts",
|
|
22
|
+
"migrate": "npx tsx src/cli/migrate.ts"
|
|
21
23
|
},
|
|
22
24
|
"publishConfig": {
|
|
23
25
|
"access": "public"
|