sqlmorpheus 0.1.0
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.d.mts +110 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +51 -0
- package/dist/index.mjs +884 -0
- package/dist/lib/Logger.d.ts +10 -0
- package/dist/lib/Logger.js +24 -0
- package/dist/lib/MigrationHistoryManager.d.ts +12 -0
- package/dist/lib/MigrationHistoryManager.js +47 -0
- package/dist/lib/MigrationManager.d.ts +27 -0
- package/dist/lib/MigrationManager.js +331 -0
- package/dist/lib/TableStructureGenerator.d.ts +10 -0
- package/dist/lib/TableStructureGenerator.js +69 -0
- package/dist/lib/dialects/DefaultSql.d.ts +34 -0
- package/dist/lib/dialects/DefaultSql.js +79 -0
- package/dist/lib/dialects/MsSqlDialect.d.ts +18 -0
- package/dist/lib/dialects/MsSqlDialect.js +53 -0
- package/dist/lib/dialects/MySqlDialect.d.ts +18 -0
- package/dist/lib/dialects/MySqlDialect.js +53 -0
- package/dist/lib/dialects/PostgresDialect.d.ts +16 -0
- package/dist/lib/dialects/PostgresDialect.js +42 -0
- package/dist/lib/dialects/SqliteDialect.d.ts +16 -0
- package/dist/lib/dialects/SqliteDialect.js +53 -0
- package/dist/lib/exceptions/NotAllowedException.d.ts +4 -0
- package/dist/lib/exceptions/NotAllowedException.js +31 -0
- package/dist/lib/tableInfo/TableInfo.d.ts +9 -0
- package/dist/lib/tableInfo/TableInfo.js +16 -0
- package/dist/lib/tableInfo/TableObjects.d.ts +6 -0
- package/dist/lib/tableInfo/TableObjects.js +2 -0
- package/dist/lib/tableInfo/decorators/DbTable.d.ts +2 -0
- package/dist/lib/tableInfo/decorators/DbTable.js +16 -0
- package/dist/lib/tableInfo/decorators/ForeignKey.d.ts +4 -0
- package/dist/lib/tableInfo/decorators/ForeignKey.js +20 -0
- package/dist/lib/typings/AllowedMigrations.d.ts +9 -0
- package/dist/lib/typings/AllowedMigrations.js +2 -0
- package/dist/lib/typings/BackendTable.d.ts +7 -0
- package/dist/lib/typings/BackendTable.js +2 -0
- package/dist/lib/typings/ColumnInfo.d.ts +6 -0
- package/dist/lib/typings/ColumnInfo.js +2 -0
- package/dist/lib/typings/DatabaseAccess.d.ts +5 -0
- package/dist/lib/typings/DatabaseAccess.js +2 -0
- package/dist/lib/typings/DatabaseInstructions.d.ts +26 -0
- package/dist/lib/typings/DatabaseInstructions.js +2 -0
- package/dist/lib/typings/ForeignKeyInfo.d.ts +9 -0
- package/dist/lib/typings/ForeignKeyInfo.js +2 -0
- package/dist/lib/typings/MigrationHistory.d.ts +7 -0
- package/dist/lib/typings/MigrationHistory.js +15 -0
- package/dist/lib/typings/MigrationInstructions.d.ts +10 -0
- package/dist/lib/typings/MigrationInstructions.js +2 -0
- package/dist/lib/typings/Migrations.d.ts +20 -0
- package/dist/lib/typings/Migrations.js +83 -0
- package/dist/lib/typings/SqlChanges.d.ts +4 -0
- package/dist/lib/typings/SqlChanges.js +2 -0
- package/dist/lib/typings/TableStructure.d.ts +8 -0
- package/dist/lib/typings/TableStructure.js +2 -0
- package/package.json +39 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
interface DatabaseAccess {
|
|
2
|
+
createBackup?(name: string): Promise<void>;
|
|
3
|
+
runGetStatement(query: string): Promise<unknown>;
|
|
4
|
+
runMultipleWriteStatements(query: string): Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
type Class<T> = new (...args: never[]) => T;
|
|
8
|
+
/**
|
|
9
|
+
* This does nothing. It mainly exists in case we want to filter class types in the future
|
|
10
|
+
*/
|
|
11
|
+
type BackendTable = {
|
|
12
|
+
[key: string]: any;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
interface AllowedMigrations {
|
|
16
|
+
recreateTable?: boolean;
|
|
17
|
+
dropTable?: boolean;
|
|
18
|
+
dropColumn?: boolean;
|
|
19
|
+
alterPrimaryKey?: boolean;
|
|
20
|
+
removeForeignKey?: boolean;
|
|
21
|
+
alterForeignKey?: boolean;
|
|
22
|
+
continueWithoutRollback?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface MigrationInstructions {
|
|
26
|
+
oldTableName?: string;
|
|
27
|
+
recreate: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Stores changes to column (0: name in the database, last: current name in code)
|
|
30
|
+
*/
|
|
31
|
+
renamedColumns: string[][];
|
|
32
|
+
allowedMigrations: Record<number, AllowedMigrations>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
declare class Migrations {
|
|
36
|
+
private readonly migrationData;
|
|
37
|
+
private alwaysAllowed;
|
|
38
|
+
private getEntry;
|
|
39
|
+
private getTableName;
|
|
40
|
+
alwaysAllow(...allowedMigrations: (keyof AllowedMigrations)[]): void;
|
|
41
|
+
allowMigration(version: number, table: string | Class<BackendTable>, ...allowedMigrations: (keyof AllowedMigrations)[]): void;
|
|
42
|
+
throwIfNotAllowed(version: number, tableName: string, type: keyof AllowedMigrations): void;
|
|
43
|
+
renameTable(oldTableName: string, newTable: string | Class<BackendTable>): void;
|
|
44
|
+
renameColumn(table: string | Class<BackendTable>, oldColumn: string, newColumn: string): void;
|
|
45
|
+
recreateTable(table: Class<BackendTable> | string): void;
|
|
46
|
+
getMigrationData(): Record<string, MigrationInstructions>;
|
|
47
|
+
loopRenamedColumns(tableName: string, callback: (oldColumnName: string, newColumnName: string) => void): void;
|
|
48
|
+
getUpdatedColumnName(tableName: string, oldColumnName: string): string;
|
|
49
|
+
willBeRecreated(tableName: string): boolean;
|
|
50
|
+
}
|
|
51
|
+
type PublicMigrations = Pick<Migrations, "recreateTable" | "renameTable" | "renameColumn" | "allowMigration" | "alwaysAllow">;
|
|
52
|
+
|
|
53
|
+
interface SqlChanges {
|
|
54
|
+
up: string;
|
|
55
|
+
down: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type ForeignKeyActions = "SET NULL" | "SET DEFAULT" | "RESTRICT" | "NO ACTION" | "CASCADE";
|
|
59
|
+
interface ForeignKeyInfo {
|
|
60
|
+
fromTable: string;
|
|
61
|
+
fromColumn: string;
|
|
62
|
+
toTable: string;
|
|
63
|
+
toColumn: string;
|
|
64
|
+
onUpdate?: ForeignKeyActions;
|
|
65
|
+
onDelete?: ForeignKeyActions;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface TableInfo {
|
|
69
|
+
primaryKey?: string;
|
|
70
|
+
foreignKeys?: ForeignKeyInfo[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
type TableObjects = Record<string, {
|
|
74
|
+
columns: BackendTable;
|
|
75
|
+
tableInfo?: TableInfo;
|
|
76
|
+
}>;
|
|
77
|
+
|
|
78
|
+
interface DatabaseInstructions {
|
|
79
|
+
dialect: "Sqlite" | "MsSql" | "MySql" | "Postgres";
|
|
80
|
+
tables: Class<BackendTable>[] | TableObjects;
|
|
81
|
+
version: number;
|
|
82
|
+
configPath: string;
|
|
83
|
+
loggerMode?: "silent" | "noLog" | "normal" | "debug";
|
|
84
|
+
/**
|
|
85
|
+
* Custom migrations that will run before any database changes are applied.
|
|
86
|
+
* @param migrations The migration object that will be validated after this.
|
|
87
|
+
* @param fromVersion Version of the current database.
|
|
88
|
+
* @param toVersion Version of the database after update.
|
|
89
|
+
* @return custom SQL that should be executed before migrations are executed.
|
|
90
|
+
*/
|
|
91
|
+
preMigration?(migrations: PublicMigrations, fromVersion: number, toVersion: number): SqlChanges | void;
|
|
92
|
+
/**
|
|
93
|
+
* Custom migrations that will run after database changes have been applied (but before the transaction is finished)
|
|
94
|
+
* @param fromVersion Version of the current database.
|
|
95
|
+
* @param toVersion Version of the database after update.
|
|
96
|
+
* @return custom SQL that should be executed before migrations are executed.
|
|
97
|
+
*/
|
|
98
|
+
postMigration?(fromVersion: number, toVersion: number): SqlChanges;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
declare function DbTable<T extends BackendTable>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context: any) => void;
|
|
102
|
+
|
|
103
|
+
declare function ForeignKey<TOther extends BackendTable>(toTable: Class<TOther>, toColumn: keyof TOther, onDelete?: ForeignKeyActions, onUpdate?: ForeignKeyActions): (_: undefined, context: any) => void;
|
|
104
|
+
|
|
105
|
+
declare async function prepareMigration(db: DatabaseAccess, dbInstructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
|
|
106
|
+
declare async function runPreparedMigrations(db: DatabaseAccess, dbInstructions: DatabaseInstructions);
|
|
107
|
+
declare async function prepareAndRunMigration(db: DatabaseAccess, dbInstructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
|
|
108
|
+
declare async function rollback(db: DatabaseAccess, dbInstructions: DatabaseInstructions, toVersion: number);
|
|
109
|
+
|
|
110
|
+
export { DbTable, ForeignKey, prepareAndRunMigration, prepareMigration, rollback, runPreparedMigrations };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DatabaseAccess } from "./lib/typings/DatabaseAccess";
|
|
2
|
+
import DatabaseInstructions from "./lib/typings/DatabaseInstructions";
|
|
3
|
+
export declare function prepareMigration(db: DatabaseAccess, instructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
|
|
4
|
+
export declare function runPreparedMigrations(db: DatabaseAccess, instructions: DatabaseInstructions): Promise<void>;
|
|
5
|
+
export declare function prepareAndRunMigration(db: DatabaseAccess, instructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
|
|
6
|
+
export declare function rollback(db: DatabaseAccess, instructions: DatabaseInstructions, toVersion: number): Promise<void>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.prepareMigration = prepareMigration;
|
|
7
|
+
exports.runPreparedMigrations = runPreparedMigrations;
|
|
8
|
+
exports.prepareAndRunMigration = prepareAndRunMigration;
|
|
9
|
+
exports.rollback = rollback;
|
|
10
|
+
const MigrationManager_1 = require("./lib/MigrationManager");
|
|
11
|
+
const MigrationHistoryManager_1 = __importDefault(require("./lib/MigrationHistoryManager"));
|
|
12
|
+
const Logger_1 = require("./lib/Logger");
|
|
13
|
+
async function prepareMigration(db, instructions, overwriteExisting) {
|
|
14
|
+
Logger_1.Logger.setMode(instructions.loggerMode);
|
|
15
|
+
const mm = new MigrationManager_1.MigrationManager(db, instructions);
|
|
16
|
+
await mm.prepareMigration(overwriteExisting);
|
|
17
|
+
}
|
|
18
|
+
async function runPreparedMigrations(db, instructions) {
|
|
19
|
+
Logger_1.Logger.setMode(instructions.loggerMode);
|
|
20
|
+
const migrationHistoryManager = new MigrationHistoryManager_1.default(instructions.configPath);
|
|
21
|
+
const fromVersion = migrationHistoryManager.getLastHistoryVersion();
|
|
22
|
+
const toVersion = instructions.version;
|
|
23
|
+
if (fromVersion == toVersion)
|
|
24
|
+
return;
|
|
25
|
+
Logger_1.Logger.log(`Running migrations from ${fromVersion} to ${toVersion}`);
|
|
26
|
+
for (let i = fromVersion ? fromVersion + 1 : toVersion; i <= toVersion; ++i) {
|
|
27
|
+
const upChanges = migrationHistoryManager.getUpMigration(i);
|
|
28
|
+
Logger_1.Logger.debug(upChanges);
|
|
29
|
+
await db.runMultipleWriteStatements(upChanges);
|
|
30
|
+
instructions.version = i;
|
|
31
|
+
}
|
|
32
|
+
migrationHistoryManager.setLastHistoryVersion(toVersion);
|
|
33
|
+
}
|
|
34
|
+
async function prepareAndRunMigration(db, instructions, overwriteExisting) {
|
|
35
|
+
Logger_1.Logger.setMode(instructions.loggerMode);
|
|
36
|
+
await prepareMigration(db, instructions, overwriteExisting);
|
|
37
|
+
await runPreparedMigrations(db, instructions);
|
|
38
|
+
}
|
|
39
|
+
async function rollback(db, instructions, toVersion) {
|
|
40
|
+
Logger_1.Logger.setMode(instructions.loggerMode);
|
|
41
|
+
const migrationHistoryManager = new MigrationHistoryManager_1.default(instructions.configPath);
|
|
42
|
+
const fromVersion = migrationHistoryManager.getLastHistoryVersion();
|
|
43
|
+
Logger_1.Logger.log(`Rolling back from ${fromVersion} to ${toVersion}`);
|
|
44
|
+
for (let i = fromVersion - 1; i >= toVersion; --i) {
|
|
45
|
+
const upChanges = migrationHistoryManager.getDownMigration(i);
|
|
46
|
+
Logger_1.Logger.debug(upChanges);
|
|
47
|
+
await db.runMultipleWriteStatements(upChanges);
|
|
48
|
+
instructions.version = i;
|
|
49
|
+
}
|
|
50
|
+
migrationHistoryManager.setLastHistoryVersion(toVersion);
|
|
51
|
+
}
|