@neupgroup/mapper 1.4.1 → 1.4.2
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 +7 -0
- package/dist/cli/migrate.js +46 -17
- package/dist/migrator.d.ts +4 -0
- package/dist/migrator.js +51 -0
- package/package.json +7 -3
|
@@ -65,6 +65,7 @@ else {
|
|
|
65
65
|
const indexFilePath = path.join(migrationDir, 'index.ts');
|
|
66
66
|
let migrations = [];
|
|
67
67
|
let completed = [];
|
|
68
|
+
let currentVersion = -1;
|
|
68
69
|
if (fs.existsSync(indexFilePath)) {
|
|
69
70
|
const content = fs.readFileSync(indexFilePath, 'utf-8');
|
|
70
71
|
const matchMigrations = content.match(/migrations = \[(.*?)\]/s);
|
|
@@ -75,6 +76,10 @@ if (fs.existsSync(indexFilePath)) {
|
|
|
75
76
|
if (matchCompleted) {
|
|
76
77
|
completed = matchCompleted[1].split(',').map(s => s.trim().replace(/['"]/g, '')).filter(Boolean);
|
|
77
78
|
}
|
|
79
|
+
const matchVersion = content.match(/currentVersion = (.*?);/);
|
|
80
|
+
if (matchVersion) {
|
|
81
|
+
currentVersion = parseInt(matchVersion[1]);
|
|
82
|
+
}
|
|
78
83
|
}
|
|
79
84
|
const migrationName = fileName.replace('.ts', '');
|
|
80
85
|
if (!migrations.includes(migrationName)) {
|
|
@@ -88,6 +93,8 @@ ${migrations.map(m => ` '${m}'`).join(',\n')}
|
|
|
88
93
|
export const completed = [
|
|
89
94
|
${completed.map(m => ` '${m}'`).join(',\n')}
|
|
90
95
|
];
|
|
96
|
+
|
|
97
|
+
export const currentVersion = ${currentVersion};
|
|
91
98
|
`;
|
|
92
99
|
fs.writeFileSync(indexFilePath, indexContent.trim() + '\n');
|
|
93
100
|
console.log(`Updated migration index: ${indexFilePath}`);
|
package/dist/cli/migrate.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import * as fs from 'fs';
|
|
3
3
|
import * as path from 'path';
|
|
4
|
+
const args = process.argv.slice(2);
|
|
5
|
+
const command = args[0] || 'up'; // 'up' or 'down'
|
|
4
6
|
const migrationDir = path.resolve(process.cwd(), 'src/migration');
|
|
5
7
|
const indexFilePath = path.join(migrationDir, 'index.ts');
|
|
6
8
|
if (!fs.existsSync(indexFilePath)) {
|
|
@@ -11,31 +13,56 @@ async function run() {
|
|
|
11
13
|
const content = fs.readFileSync(indexFilePath, 'utf-8');
|
|
12
14
|
const matchMigrations = content.match(/migrations = \[(.*?)\]/s);
|
|
13
15
|
const matchCompleted = content.match(/completed = \[(.*?)\]/s);
|
|
16
|
+
const matchVersion = content.match(/currentVersion = (.*?);/);
|
|
14
17
|
if (!matchMigrations) {
|
|
15
18
|
console.log('No migrations list found in index.ts');
|
|
16
19
|
return;
|
|
17
20
|
}
|
|
18
21
|
const migrations = matchMigrations[1].split(',').map(s => s.trim().replace(/['"]/g, '')).filter(Boolean);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
let completed = matchCompleted ? matchCompleted[1].split(',').map(s => s.trim().replace(/['"]/g, '')).filter(Boolean) : [];
|
|
23
|
+
let currentVersion = matchVersion ? parseInt(matchVersion[1]) : -1;
|
|
24
|
+
if (command === 'up') {
|
|
25
|
+
const pending = migrations.filter(m => !completed.includes(m));
|
|
26
|
+
if (pending.length === 0) {
|
|
27
|
+
console.log('No pending migrations.');
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
console.log(`Found ${pending.length} pending migrations.`);
|
|
31
|
+
for (const m of pending) {
|
|
32
|
+
console.log(`Running migration UP: ${m}...`);
|
|
33
|
+
const filePath = path.join(migrationDir, `${m}.ts`);
|
|
34
|
+
const absolutePath = path.resolve(filePath);
|
|
35
|
+
const mod = await import('file://' + absolutePath);
|
|
36
|
+
if (mod.up) {
|
|
37
|
+
await mod.up();
|
|
38
|
+
completed.push(m);
|
|
39
|
+
currentVersion = migrations.indexOf(m);
|
|
40
|
+
console.log(`Completed UP: ${m}`);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
console.error(`Migration ${m} does not have an up() function.`);
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
24
47
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
48
|
+
else if (command === 'down') {
|
|
49
|
+
if (completed.length === 0) {
|
|
50
|
+
console.log('No migrations to roll back.');
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const lastMigrationName = completed[completed.length - 1];
|
|
54
|
+
console.log(`Rolling back migration: ${lastMigrationName}...`);
|
|
55
|
+
const filePath = path.join(migrationDir, `${lastMigrationName}.ts`);
|
|
56
|
+
const absolutePath = path.resolve(filePath);
|
|
31
57
|
const mod = await import('file://' + absolutePath);
|
|
32
|
-
if (mod.
|
|
33
|
-
await mod.
|
|
34
|
-
completed.
|
|
35
|
-
|
|
58
|
+
if (mod.down) {
|
|
59
|
+
await mod.down();
|
|
60
|
+
completed.pop();
|
|
61
|
+
currentVersion = completed.length > 0 ? migrations.indexOf(completed[completed.length - 1]) : -1;
|
|
62
|
+
console.log(`Completed DOWN: ${lastMigrationName}`);
|
|
36
63
|
}
|
|
37
64
|
else {
|
|
38
|
-
console.error(`Migration ${
|
|
65
|
+
console.error(`Migration ${lastMigrationName} does not have a down() function.`);
|
|
39
66
|
}
|
|
40
67
|
}
|
|
41
68
|
// Update index.ts
|
|
@@ -47,9 +74,11 @@ ${migrations.map(m => ` '${m}'`).join(',\n')}
|
|
|
47
74
|
export const completed = [
|
|
48
75
|
${completed.map(m => ` '${m}'`).join(',\n')}
|
|
49
76
|
];
|
|
77
|
+
|
|
78
|
+
export const currentVersion = ${currentVersion};
|
|
50
79
|
`;
|
|
51
80
|
fs.writeFileSync(indexFilePath, indexContent.trim() + '\n');
|
|
52
|
-
console.log(
|
|
81
|
+
console.log(`Migration runner finished. Current version: ${currentVersion}`);
|
|
53
82
|
}
|
|
54
83
|
run().catch(err => {
|
|
55
84
|
console.error('Migration failed:', err);
|
package/dist/migrator.d.ts
CHANGED
|
@@ -21,4 +21,8 @@ export declare class TableMigrator {
|
|
|
21
21
|
addColumn(name: string): ColumnBuilder;
|
|
22
22
|
getColumns(): any[];
|
|
23
23
|
exec(): Promise<void>;
|
|
24
|
+
drop(): Promise<void>;
|
|
25
|
+
dropColumn(columnName: string): Promise<void>;
|
|
26
|
+
dropUnique(columnName: string): Promise<void>;
|
|
27
|
+
dropPrimaryKey(columnName: string): Promise<void>;
|
|
24
28
|
}
|
package/dist/migrator.js
CHANGED
|
@@ -103,4 +103,55 @@ ${fieldsContent}
|
|
|
103
103
|
fs.writeFileSync(schemaFilePath, schemaContent.trim() + '\n');
|
|
104
104
|
console.log(`Updated schema: ${schemaFilePath}`);
|
|
105
105
|
}
|
|
106
|
+
async drop() {
|
|
107
|
+
const fs = await import('fs');
|
|
108
|
+
const path = await import('path');
|
|
109
|
+
const schemasDir = path.resolve(process.cwd(), 'src/schemas');
|
|
110
|
+
const schemaFilePath = path.join(schemasDir, `${this.name}.ts`);
|
|
111
|
+
if (fs.existsSync(schemaFilePath)) {
|
|
112
|
+
fs.unlinkSync(schemaFilePath);
|
|
113
|
+
console.log(`Deleted schema: ${schemaFilePath}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async dropColumn(columnName) {
|
|
117
|
+
const fs = await import('fs');
|
|
118
|
+
const path = await import('path');
|
|
119
|
+
const schemasDir = path.resolve(process.cwd(), 'src/schemas');
|
|
120
|
+
const schemaFilePath = path.join(schemasDir, `${this.name}.ts`);
|
|
121
|
+
if (fs.existsSync(schemaFilePath)) {
|
|
122
|
+
// Very simple implementation: filter out the line with the column
|
|
123
|
+
const content = fs.readFileSync(schemaFilePath, 'utf-8');
|
|
124
|
+
const lines = content.split('\n');
|
|
125
|
+
const filteredLines = lines.filter(line => !line.includes(`name: '${columnName}'`));
|
|
126
|
+
fs.writeFileSync(schemaFilePath, filteredLines.join('\n'));
|
|
127
|
+
console.log(`Dropped column ${columnName} from schema ${this.name}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
async dropUnique(columnName) {
|
|
131
|
+
const fs = await import('fs');
|
|
132
|
+
const path = await import('path');
|
|
133
|
+
const schemasDir = path.resolve(process.cwd(), 'src/schemas');
|
|
134
|
+
const schemaFilePath = path.join(schemasDir, `${this.name}.ts`);
|
|
135
|
+
if (fs.existsSync(schemaFilePath)) {
|
|
136
|
+
let content = fs.readFileSync(schemaFilePath, 'utf-8');
|
|
137
|
+
// Remove ", isUnique: true" from the line containing the column
|
|
138
|
+
const regex = new RegExp(`({ name: '${columnName}', .*?), isUnique: true(.*})`);
|
|
139
|
+
content = content.replace(regex, '$1$2');
|
|
140
|
+
fs.writeFileSync(schemaFilePath, content);
|
|
141
|
+
console.log(`Dropped unique constraint from ${columnName} in schema ${this.name}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async dropPrimaryKey(columnName) {
|
|
145
|
+
const fs = await import('fs');
|
|
146
|
+
const path = await import('path');
|
|
147
|
+
const schemasDir = path.resolve(process.cwd(), 'src/schemas');
|
|
148
|
+
const schemaFilePath = path.join(schemasDir, `${this.name}.ts`);
|
|
149
|
+
if (fs.existsSync(schemaFilePath)) {
|
|
150
|
+
let content = fs.readFileSync(schemaFilePath, 'utf-8');
|
|
151
|
+
const regex = new RegExp(`({ name: '${columnName}', .*?), isPrimary: true(.*})`);
|
|
152
|
+
content = content.replace(regex, '$1$2');
|
|
153
|
+
fs.writeFileSync(schemaFilePath, content);
|
|
154
|
+
console.log(`Dropped primary key constraint from ${columnName} in schema ${this.name}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
106
157
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neupgroup/mapper",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
4
4
|
"description": "Neup.Mapper core library for schema and mapping utilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -9,7 +9,9 @@
|
|
|
9
9
|
"bin": {
|
|
10
10
|
"create-migration": "dist/cli/create-migration.js",
|
|
11
11
|
"create-connection": "dist/cli/create-connection.js",
|
|
12
|
-
"migrate": "dist/cli/migrate.js"
|
|
12
|
+
"migrate": "dist/cli/migrate.js",
|
|
13
|
+
"migrate-up": "dist/cli/migrate.js",
|
|
14
|
+
"migrate-down": "dist/cli/migrate.js"
|
|
13
15
|
},
|
|
14
16
|
"files": [
|
|
15
17
|
"dist"
|
|
@@ -19,7 +21,9 @@
|
|
|
19
21
|
"build": "tsc -p ./tsconfig.json",
|
|
20
22
|
"create-migration": "npx tsx src/cli/create-migration.ts",
|
|
21
23
|
"create-connection": "npx tsx src/cli/create-connection.ts",
|
|
22
|
-
"migrate": "npx tsx src/cli/migrate.ts"
|
|
24
|
+
"migrate": "npx tsx src/cli/migrate.ts",
|
|
25
|
+
"migrate-up": "npx tsx src/cli/migrate.ts up",
|
|
26
|
+
"migrate-down": "npx tsx src/cli/migrate.ts down"
|
|
23
27
|
},
|
|
24
28
|
"publishConfig": {
|
|
25
29
|
"access": "public"
|