@oliasoft-open-source/node-json-migrator 3.1.0-beta-4 → 3.1.0-beta-5
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/index.cjs +39 -25
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +39 -25
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2321,6 +2321,11 @@ const throwIfFilesNotFound = (migrationEntries, importModule) => {
|
|
|
2321
2321
|
}
|
|
2322
2322
|
};
|
|
2323
2323
|
|
|
2324
|
+
const capitalize = (word) => {
|
|
2325
|
+
if (!word) return "";
|
|
2326
|
+
return word[0].toUpperCase() + word.slice(1);
|
|
2327
|
+
};
|
|
2328
|
+
|
|
2324
2329
|
const loadModuleFromString = async (script) => {
|
|
2325
2330
|
let patchedScript = script;
|
|
2326
2331
|
try {
|
|
@@ -9036,7 +9041,7 @@ const getRecordList = async (db, entity, entityTableName, entityColumnNames, lat
|
|
|
9036
9041
|
version
|
|
9037
9042
|
FROM $(entityTable:name)
|
|
9038
9043
|
WHERE (
|
|
9039
|
-
version IS NULL
|
|
9044
|
+
version IS NULL OR version = ''
|
|
9040
9045
|
OR (
|
|
9041
9046
|
version <> $(latestVersion)
|
|
9042
9047
|
AND (
|
|
@@ -9324,47 +9329,47 @@ const getPlannedMigrations = async ({ config }) => {
|
|
|
9324
9329
|
};
|
|
9325
9330
|
const PlanModule = { getPlannedMigrations };
|
|
9326
9331
|
|
|
9327
|
-
const templateMigrationFileLegacy = `import produce from 'immer';
|
|
9332
|
+
const templateMigrationFileLegacy = (entity) => `import produce from 'immer';
|
|
9328
9333
|
//other imports not allowed
|
|
9329
9334
|
|
|
9330
|
-
export default (
|
|
9335
|
+
export default (${entity}) => produce(${entity}, (draft) => {
|
|
9331
9336
|
// https://immerjs.github.io/immer/produce#example
|
|
9332
9337
|
});
|
|
9333
9338
|
`;
|
|
9334
|
-
const templateMigrationFile = `import { produce } from 'immer';
|
|
9339
|
+
const templateMigrationFile = (entity) => `import { produce } from 'immer';
|
|
9335
9340
|
//other imports not allowed
|
|
9336
9341
|
|
|
9337
|
-
export default (
|
|
9342
|
+
export default (${entity}) => produce(${entity}, (draft) => {
|
|
9338
9343
|
// https://immerjs.github.io/immer/produce#example
|
|
9339
9344
|
});
|
|
9340
9345
|
`;
|
|
9341
|
-
const getTemplateMigrationFile = async () => {
|
|
9342
|
-
let template = templateMigrationFileLegacy;
|
|
9346
|
+
const getTemplateMigrationFile = async (entity) => {
|
|
9347
|
+
let template = templateMigrationFileLegacy(entity);
|
|
9343
9348
|
try {
|
|
9344
9349
|
await Promise.resolve().then(function () { return require('./immer-BsT8CIGL.cjs'); }).then(({ produce }) => {
|
|
9345
9350
|
if (produce) {
|
|
9346
|
-
template = templateMigrationFile;
|
|
9351
|
+
template = templateMigrationFile(entity);
|
|
9347
9352
|
}
|
|
9348
9353
|
});
|
|
9349
9354
|
} catch (_error) {
|
|
9350
9355
|
}
|
|
9351
9356
|
return template;
|
|
9352
9357
|
};
|
|
9353
|
-
const templateTestFile = (fileName) => `import migrate from './${fileName}';
|
|
9358
|
+
const templateTestFile = (fileName, entity) => `import migrate from './${fileName}';
|
|
9354
9359
|
|
|
9355
|
-
describe('describe
|
|
9360
|
+
describe('describe ${entity} change', () => {
|
|
9356
9361
|
test('describe test', () => {
|
|
9357
9362
|
//arrange
|
|
9358
|
-
const
|
|
9363
|
+
const ${entity} = {};
|
|
9359
9364
|
|
|
9360
9365
|
//act
|
|
9361
|
-
const
|
|
9366
|
+
const next${capitalize(entity)} = migrate(${entity});
|
|
9362
9367
|
|
|
9363
9368
|
//assert
|
|
9364
9369
|
});
|
|
9365
9370
|
});
|
|
9366
9371
|
`;
|
|
9367
|
-
const createMigration = async (directory, description) => {
|
|
9372
|
+
const createMigration = async (directory, description, entity) => {
|
|
9368
9373
|
if (!validateFileDescription(description)) {
|
|
9369
9374
|
throw new Error("Invalid migration description");
|
|
9370
9375
|
}
|
|
@@ -9375,11 +9380,11 @@ const createMigration = async (directory, description) => {
|
|
|
9375
9380
|
const filePath = path$1.resolve(directoryFullPath, fileName);
|
|
9376
9381
|
const testFilePath = path$1.resolve(directoryFullPath, testFileName);
|
|
9377
9382
|
await fs.promises.mkdir(directoryFullPath, { recursive: true });
|
|
9378
|
-
const templateMigrationScript = await getTemplateMigrationFile();
|
|
9383
|
+
const templateMigrationScript = await getTemplateMigrationFile(entity);
|
|
9379
9384
|
await fs.promises.writeFile(filePath, templateMigrationScript, {
|
|
9380
9385
|
encoding: "utf8"
|
|
9381
9386
|
});
|
|
9382
|
-
await fs.promises.writeFile(testFilePath, templateTestFile(fileName), {
|
|
9387
|
+
await fs.promises.writeFile(testFilePath, templateTestFile(fileName, entity), {
|
|
9383
9388
|
encoding: "utf8"
|
|
9384
9389
|
});
|
|
9385
9390
|
const rawPlan = await readPlan(directory);
|
|
@@ -9391,7 +9396,7 @@ const createMigration = async (directory, description) => {
|
|
|
9391
9396
|
});
|
|
9392
9397
|
const nextPlan = JSON.stringify(nextPlannedMigrations, null, 2);
|
|
9393
9398
|
await writePlan(directory, nextPlan);
|
|
9394
|
-
console.log(chalk.green(`Created new
|
|
9399
|
+
console.log(chalk.green(`Created new ${entity} migration ${fileName} \u2713`));
|
|
9395
9400
|
} catch (error) {
|
|
9396
9401
|
console.log(chalk.red(error));
|
|
9397
9402
|
throw new Error("Unable to create migration file");
|
|
@@ -9435,11 +9440,11 @@ const terminals = Object.freeze({
|
|
|
9435
9440
|
bash: "bash",
|
|
9436
9441
|
cmd: "cmd"
|
|
9437
9442
|
});
|
|
9438
|
-
const historyError = `Unable to fetch
|
|
9439
|
-
- Have you merged latest master branch into your environment (
|
|
9443
|
+
const historyError = `Unable to fetch migrations history from git. Possible reasons:
|
|
9444
|
+
- Have you merged latest master branch into your environment (fetch latest migrations)?
|
|
9440
9445
|
- Are you trying to downgrade data (not allowed), e.g. newer data into an older environment?
|
|
9441
|
-
- Not allowed to export from TEST and import to PROD
|
|
9442
|
-
- Not allowed to export
|
|
9446
|
+
- Not allowed to export data from TEST and import to PROD
|
|
9447
|
+
- Not allowed to export data from feature branches and import into TEST, PROD or master ENVs
|
|
9443
9448
|
- On Windows? Do you have git and a terminal (bash or cmd) in your PATH? Try cmder.app, gitforwindows.org, or WSL
|
|
9444
9449
|
`;
|
|
9445
9450
|
const commandAvailable = (command) => {
|
|
@@ -9725,7 +9730,7 @@ const migrateRecord = async ({
|
|
|
9725
9730
|
};
|
|
9726
9731
|
console.log(
|
|
9727
9732
|
chalk.red(
|
|
9728
|
-
`
|
|
9733
|
+
` ${capitalize(config.entity)} migration with id ${record.id} failed:
|
|
9729
9734
|
${error.stack}`
|
|
9730
9735
|
)
|
|
9731
9736
|
);
|
|
@@ -9756,15 +9761,24 @@ const migrateRecords = async ({
|
|
|
9756
9761
|
}
|
|
9757
9762
|
}
|
|
9758
9763
|
if (migrationErrors.length) {
|
|
9764
|
+
try {
|
|
9765
|
+
await onMigrationErrors({
|
|
9766
|
+
migrationErrors,
|
|
9767
|
+
dry
|
|
9768
|
+
});
|
|
9769
|
+
} catch (error) {
|
|
9770
|
+
console.log(
|
|
9771
|
+
chalk.red(
|
|
9772
|
+
`Handling errors for ${config.entity} migrations failed:
|
|
9773
|
+
${error.stack}`
|
|
9774
|
+
)
|
|
9775
|
+
);
|
|
9776
|
+
}
|
|
9759
9777
|
console.log(
|
|
9760
9778
|
chalk.yellow(
|
|
9761
9779
|
`Completed ${config?.entity ?? "entity"} migrations (some failed)${config?.dry ? " [dry-run, no output written]" : ""} \u2713`
|
|
9762
9780
|
)
|
|
9763
9781
|
);
|
|
9764
|
-
await onMigrationErrors({
|
|
9765
|
-
migrationErrors,
|
|
9766
|
-
dry
|
|
9767
|
-
});
|
|
9768
9782
|
} else {
|
|
9769
9783
|
console.log(
|
|
9770
9784
|
chalk.green(
|
package/dist/index.d.cts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { IDatabase, IHelpers, ITask } from 'pg-promise';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Generate a new
|
|
4
|
+
* Generate a new entity migration
|
|
5
5
|
*/
|
|
6
|
-
declare const createMigration: (directory: string, description: string) => Promise<void>;
|
|
6
|
+
declare const createMigration: (directory: string, description: string, entity: string) => Promise<void>;
|
|
7
7
|
|
|
8
8
|
type TMigration = {
|
|
9
9
|
fileName?: string;
|
package/dist/index.d.mts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { IDatabase, IHelpers, ITask } from 'pg-promise';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Generate a new
|
|
4
|
+
* Generate a new entity migration
|
|
5
5
|
*/
|
|
6
|
-
declare const createMigration: (directory: string, description: string) => Promise<void>;
|
|
6
|
+
declare const createMigration: (directory: string, description: string, entity: string) => Promise<void>;
|
|
7
7
|
|
|
8
8
|
type TMigration = {
|
|
9
9
|
fileName?: string;
|
package/dist/index.mjs
CHANGED
|
@@ -2300,6 +2300,11 @@ const throwIfFilesNotFound = (migrationEntries, importModule) => {
|
|
|
2300
2300
|
}
|
|
2301
2301
|
};
|
|
2302
2302
|
|
|
2303
|
+
const capitalize = (word) => {
|
|
2304
|
+
if (!word) return "";
|
|
2305
|
+
return word[0].toUpperCase() + word.slice(1);
|
|
2306
|
+
};
|
|
2307
|
+
|
|
2303
2308
|
const loadModuleFromString = async (script) => {
|
|
2304
2309
|
let patchedScript = script;
|
|
2305
2310
|
try {
|
|
@@ -9015,7 +9020,7 @@ const getRecordList = async (db, entity, entityTableName, entityColumnNames, lat
|
|
|
9015
9020
|
version
|
|
9016
9021
|
FROM $(entityTable:name)
|
|
9017
9022
|
WHERE (
|
|
9018
|
-
version IS NULL
|
|
9023
|
+
version IS NULL OR version = ''
|
|
9019
9024
|
OR (
|
|
9020
9025
|
version <> $(latestVersion)
|
|
9021
9026
|
AND (
|
|
@@ -9303,47 +9308,47 @@ const getPlannedMigrations = async ({ config }) => {
|
|
|
9303
9308
|
};
|
|
9304
9309
|
const PlanModule = { getPlannedMigrations };
|
|
9305
9310
|
|
|
9306
|
-
const templateMigrationFileLegacy = `import produce from 'immer';
|
|
9311
|
+
const templateMigrationFileLegacy = (entity) => `import produce from 'immer';
|
|
9307
9312
|
//other imports not allowed
|
|
9308
9313
|
|
|
9309
|
-
export default (
|
|
9314
|
+
export default (${entity}) => produce(${entity}, (draft) => {
|
|
9310
9315
|
// https://immerjs.github.io/immer/produce#example
|
|
9311
9316
|
});
|
|
9312
9317
|
`;
|
|
9313
|
-
const templateMigrationFile = `import { produce } from 'immer';
|
|
9318
|
+
const templateMigrationFile = (entity) => `import { produce } from 'immer';
|
|
9314
9319
|
//other imports not allowed
|
|
9315
9320
|
|
|
9316
|
-
export default (
|
|
9321
|
+
export default (${entity}) => produce(${entity}, (draft) => {
|
|
9317
9322
|
// https://immerjs.github.io/immer/produce#example
|
|
9318
9323
|
});
|
|
9319
9324
|
`;
|
|
9320
|
-
const getTemplateMigrationFile = async () => {
|
|
9321
|
-
let template = templateMigrationFileLegacy;
|
|
9325
|
+
const getTemplateMigrationFile = async (entity) => {
|
|
9326
|
+
let template = templateMigrationFileLegacy(entity);
|
|
9322
9327
|
try {
|
|
9323
9328
|
await import('./immer-C8oEWD0M.mjs').then(({ produce }) => {
|
|
9324
9329
|
if (produce) {
|
|
9325
|
-
template = templateMigrationFile;
|
|
9330
|
+
template = templateMigrationFile(entity);
|
|
9326
9331
|
}
|
|
9327
9332
|
});
|
|
9328
9333
|
} catch (_error) {
|
|
9329
9334
|
}
|
|
9330
9335
|
return template;
|
|
9331
9336
|
};
|
|
9332
|
-
const templateTestFile = (fileName) => `import migrate from './${fileName}';
|
|
9337
|
+
const templateTestFile = (fileName, entity) => `import migrate from './${fileName}';
|
|
9333
9338
|
|
|
9334
|
-
describe('describe
|
|
9339
|
+
describe('describe ${entity} change', () => {
|
|
9335
9340
|
test('describe test', () => {
|
|
9336
9341
|
//arrange
|
|
9337
|
-
const
|
|
9342
|
+
const ${entity} = {};
|
|
9338
9343
|
|
|
9339
9344
|
//act
|
|
9340
|
-
const
|
|
9345
|
+
const next${capitalize(entity)} = migrate(${entity});
|
|
9341
9346
|
|
|
9342
9347
|
//assert
|
|
9343
9348
|
});
|
|
9344
9349
|
});
|
|
9345
9350
|
`;
|
|
9346
|
-
const createMigration = async (directory, description) => {
|
|
9351
|
+
const createMigration = async (directory, description, entity) => {
|
|
9347
9352
|
if (!validateFileDescription(description)) {
|
|
9348
9353
|
throw new Error("Invalid migration description");
|
|
9349
9354
|
}
|
|
@@ -9354,11 +9359,11 @@ const createMigration = async (directory, description) => {
|
|
|
9354
9359
|
const filePath = path$1.resolve(directoryFullPath, fileName);
|
|
9355
9360
|
const testFilePath = path$1.resolve(directoryFullPath, testFileName);
|
|
9356
9361
|
await promises.mkdir(directoryFullPath, { recursive: true });
|
|
9357
|
-
const templateMigrationScript = await getTemplateMigrationFile();
|
|
9362
|
+
const templateMigrationScript = await getTemplateMigrationFile(entity);
|
|
9358
9363
|
await promises.writeFile(filePath, templateMigrationScript, {
|
|
9359
9364
|
encoding: "utf8"
|
|
9360
9365
|
});
|
|
9361
|
-
await promises.writeFile(testFilePath, templateTestFile(fileName), {
|
|
9366
|
+
await promises.writeFile(testFilePath, templateTestFile(fileName, entity), {
|
|
9362
9367
|
encoding: "utf8"
|
|
9363
9368
|
});
|
|
9364
9369
|
const rawPlan = await readPlan(directory);
|
|
@@ -9370,7 +9375,7 @@ const createMigration = async (directory, description) => {
|
|
|
9370
9375
|
});
|
|
9371
9376
|
const nextPlan = JSON.stringify(nextPlannedMigrations, null, 2);
|
|
9372
9377
|
await writePlan(directory, nextPlan);
|
|
9373
|
-
console.log(chalk.green(`Created new
|
|
9378
|
+
console.log(chalk.green(`Created new ${entity} migration ${fileName} \u2713`));
|
|
9374
9379
|
} catch (error) {
|
|
9375
9380
|
console.log(chalk.red(error));
|
|
9376
9381
|
throw new Error("Unable to create migration file");
|
|
@@ -9414,11 +9419,11 @@ const terminals = Object.freeze({
|
|
|
9414
9419
|
bash: "bash",
|
|
9415
9420
|
cmd: "cmd"
|
|
9416
9421
|
});
|
|
9417
|
-
const historyError = `Unable to fetch
|
|
9418
|
-
- Have you merged latest master branch into your environment (
|
|
9422
|
+
const historyError = `Unable to fetch migrations history from git. Possible reasons:
|
|
9423
|
+
- Have you merged latest master branch into your environment (fetch latest migrations)?
|
|
9419
9424
|
- Are you trying to downgrade data (not allowed), e.g. newer data into an older environment?
|
|
9420
|
-
- Not allowed to export from TEST and import to PROD
|
|
9421
|
-
- Not allowed to export
|
|
9425
|
+
- Not allowed to export data from TEST and import to PROD
|
|
9426
|
+
- Not allowed to export data from feature branches and import into TEST, PROD or master ENVs
|
|
9422
9427
|
- On Windows? Do you have git and a terminal (bash or cmd) in your PATH? Try cmder.app, gitforwindows.org, or WSL
|
|
9423
9428
|
`;
|
|
9424
9429
|
const commandAvailable = (command) => {
|
|
@@ -9704,7 +9709,7 @@ const migrateRecord = async ({
|
|
|
9704
9709
|
};
|
|
9705
9710
|
console.log(
|
|
9706
9711
|
chalk.red(
|
|
9707
|
-
`
|
|
9712
|
+
` ${capitalize(config.entity)} migration with id ${record.id} failed:
|
|
9708
9713
|
${error.stack}`
|
|
9709
9714
|
)
|
|
9710
9715
|
);
|
|
@@ -9735,15 +9740,24 @@ const migrateRecords = async ({
|
|
|
9735
9740
|
}
|
|
9736
9741
|
}
|
|
9737
9742
|
if (migrationErrors.length) {
|
|
9743
|
+
try {
|
|
9744
|
+
await onMigrationErrors({
|
|
9745
|
+
migrationErrors,
|
|
9746
|
+
dry
|
|
9747
|
+
});
|
|
9748
|
+
} catch (error) {
|
|
9749
|
+
console.log(
|
|
9750
|
+
chalk.red(
|
|
9751
|
+
`Handling errors for ${config.entity} migrations failed:
|
|
9752
|
+
${error.stack}`
|
|
9753
|
+
)
|
|
9754
|
+
);
|
|
9755
|
+
}
|
|
9738
9756
|
console.log(
|
|
9739
9757
|
chalk.yellow(
|
|
9740
9758
|
`Completed ${config?.entity ?? "entity"} migrations (some failed)${config?.dry ? " [dry-run, no output written]" : ""} \u2713`
|
|
9741
9759
|
)
|
|
9742
9760
|
);
|
|
9743
|
-
await onMigrationErrors({
|
|
9744
|
-
migrationErrors,
|
|
9745
|
-
dry
|
|
9746
|
-
});
|
|
9747
9761
|
} else {
|
|
9748
9762
|
console.log(
|
|
9749
9763
|
chalk.green(
|
package/package.json
CHANGED