sqlmorpheus 0.1.0 → 0.1.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/index.d.mts +6 -6
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5 -0
- package/dist/index.mjs +41 -3
- package/dist/lib/tableInfo/TableInfo.js +7 -3
- package/dist/lib/tableInfo/decorators/DbTable.d.ts +1 -1
- package/dist/lib/tableInfo/decorators/DbTable.js +1 -1
- package/dist/lib/tableInfo/decorators/ForeignKey.d.ts +1 -1
- package/dist/lib/tableInfo/decorators/ForeignKey.js +3 -3
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -98,13 +98,13 @@ interface DatabaseInstructions {
|
|
|
98
98
|
postMigration?(fromVersion: number, toVersion: number): SqlChanges;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
declare function DbTable<T extends BackendTable>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context
|
|
101
|
+
declare function DbTable<T extends BackendTable>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context?: any) => void;
|
|
102
102
|
|
|
103
|
-
declare function ForeignKey<TOther extends BackendTable>(toTable: Class<TOther>, toColumn: keyof TOther, onDelete?: ForeignKeyActions, onUpdate?: ForeignKeyActions): (
|
|
103
|
+
declare function ForeignKey<TOther extends BackendTable>(toTable: Class<TOther>, toColumn: keyof TOther, onDelete?: ForeignKeyActions, onUpdate?: ForeignKeyActions): (table: any, context: any) => void;
|
|
104
104
|
|
|
105
|
-
declare
|
|
106
|
-
declare
|
|
107
|
-
declare
|
|
108
|
-
declare
|
|
105
|
+
declare function prepareMigration(db: DatabaseAccess, instructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
|
|
106
|
+
declare function runPreparedMigrations(db: DatabaseAccess, instructions: DatabaseInstructions): Promise<void>;
|
|
107
|
+
declare function prepareAndRunMigration(db: DatabaseAccess, instructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
|
|
108
|
+
declare function rollback(db: DatabaseAccess, instructions: DatabaseInstructions, toVersion: number): Promise<void>;
|
|
109
109
|
|
|
110
110
|
export { DbTable, ForeignKey, prepareAndRunMigration, prepareMigration, rollback, runPreparedMigrations };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { DatabaseAccess } from "./lib/typings/DatabaseAccess";
|
|
2
2
|
import DatabaseInstructions from "./lib/typings/DatabaseInstructions";
|
|
3
|
+
import DbTable from "./lib/tableInfo/decorators/DbTable";
|
|
4
|
+
import ForeignKey from "./lib/tableInfo/decorators/ForeignKey";
|
|
3
5
|
export declare function prepareMigration(db: DatabaseAccess, instructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
|
|
4
6
|
export declare function runPreparedMigrations(db: DatabaseAccess, instructions: DatabaseInstructions): Promise<void>;
|
|
5
7
|
export declare function prepareAndRunMigration(db: DatabaseAccess, instructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
|
|
6
8
|
export declare function rollback(db: DatabaseAccess, instructions: DatabaseInstructions, toVersion: number): Promise<void>;
|
|
9
|
+
export { DbTable, ForeignKey };
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ForeignKey = exports.DbTable = void 0;
|
|
6
7
|
exports.prepareMigration = prepareMigration;
|
|
7
8
|
exports.runPreparedMigrations = runPreparedMigrations;
|
|
8
9
|
exports.prepareAndRunMigration = prepareAndRunMigration;
|
|
@@ -10,6 +11,10 @@ exports.rollback = rollback;
|
|
|
10
11
|
const MigrationManager_1 = require("./lib/MigrationManager");
|
|
11
12
|
const MigrationHistoryManager_1 = __importDefault(require("./lib/MigrationHistoryManager"));
|
|
12
13
|
const Logger_1 = require("./lib/Logger");
|
|
14
|
+
const DbTable_1 = __importDefault(require("./lib/tableInfo/decorators/DbTable"));
|
|
15
|
+
exports.DbTable = DbTable_1.default;
|
|
16
|
+
const ForeignKey_1 = __importDefault(require("./lib/tableInfo/decorators/ForeignKey"));
|
|
17
|
+
exports.ForeignKey = ForeignKey_1.default;
|
|
13
18
|
async function prepareMigration(db, instructions, overwriteExisting) {
|
|
14
19
|
Logger_1.Logger.setMode(instructions.loggerMode);
|
|
15
20
|
const mm = new MigrationManager_1.MigrationManager(db, instructions);
|
package/dist/index.mjs
CHANGED
|
@@ -24,9 +24,12 @@ var Logger = new LoggerClass();
|
|
|
24
24
|
// src/lib/tableInfo/TableInfo.ts
|
|
25
25
|
var TABLE_INFO_PROPERTY_NAME = Symbol("tableInfo");
|
|
26
26
|
function getTableInfo(table) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
let metadata = table[Symbol.metadata] ?? table.prototype?.[Symbol.metadata];
|
|
28
|
+
if (!metadata) {
|
|
29
|
+
metadata = {};
|
|
30
|
+
table[Symbol.metadata] = metadata;
|
|
31
|
+
}
|
|
32
|
+
return getTableInfoFromMetadata(metadata);
|
|
30
33
|
}
|
|
31
34
|
function getTableInfoFromMetadata(metadata) {
|
|
32
35
|
if (!metadata[TABLE_INFO_PROPERTY_NAME])
|
|
@@ -836,6 +839,39 @@ ${foreignKeyOnQuery}`;
|
|
|
836
839
|
}
|
|
837
840
|
};
|
|
838
841
|
|
|
842
|
+
// src/lib/tableInfo/decorators/DbTable.ts
|
|
843
|
+
function DbTable(tableName, primaryKey) {
|
|
844
|
+
return (table, context) => {
|
|
845
|
+
Object.defineProperty(table, "name", { value: tableName, writable: false });
|
|
846
|
+
const tableInfo = context?.metadata ? getTableInfoFromMetadata(context.metadata) : getTableInfo(table);
|
|
847
|
+
tableInfo.primaryKey = primaryKey?.toString();
|
|
848
|
+
if (tableInfo.foreignKeys) {
|
|
849
|
+
for (const foreignKey of tableInfo.foreignKeys) {
|
|
850
|
+
foreignKey.fromTable = tableName;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
// src/lib/tableInfo/decorators/ForeignKey.ts
|
|
857
|
+
import "polyfill-symbol-metadata";
|
|
858
|
+
function ForeignKey(toTable, toColumn, onDelete, onUpdate) {
|
|
859
|
+
return (table, context) => {
|
|
860
|
+
const metadata = context?.metadata ? getTableInfoFromMetadata(context.metadata) : getTableInfo(table);
|
|
861
|
+
if (!metadata.foreignKeys)
|
|
862
|
+
metadata.foreignKeys = [];
|
|
863
|
+
metadata.foreignKeys.push({
|
|
864
|
+
fromTable: "",
|
|
865
|
+
fromColumn: context.name ?? context,
|
|
866
|
+
//to stay compatible with old decorator structure (see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#decorator-metadata)
|
|
867
|
+
toTable: toTable.name,
|
|
868
|
+
toColumn: toColumn.toString(),
|
|
869
|
+
onDelete,
|
|
870
|
+
onUpdate
|
|
871
|
+
});
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
|
|
839
875
|
// src/index.ts
|
|
840
876
|
async function prepareMigration(db, instructions, overwriteExisting) {
|
|
841
877
|
Logger.setMode(instructions.loggerMode);
|
|
@@ -877,6 +913,8 @@ async function rollback(db, instructions, toVersion) {
|
|
|
877
913
|
migrationHistoryManager.setLastHistoryVersion(toVersion);
|
|
878
914
|
}
|
|
879
915
|
export {
|
|
916
|
+
DbTable,
|
|
917
|
+
ForeignKey,
|
|
880
918
|
prepareAndRunMigration,
|
|
881
919
|
prepareMigration,
|
|
882
920
|
rollback,
|
|
@@ -5,9 +5,13 @@ exports.getTableInfo = getTableInfo;
|
|
|
5
5
|
exports.getTableInfoFromMetadata = getTableInfoFromMetadata;
|
|
6
6
|
exports.TABLE_INFO_PROPERTY_NAME = Symbol("tableInfo");
|
|
7
7
|
function getTableInfo(table) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
//to stay compatible with old decorator structure (see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#decorator-metadata):
|
|
9
|
+
let metadata = table[Symbol.metadata] ?? table.prototype?.[Symbol.metadata];
|
|
10
|
+
if (!metadata) {
|
|
11
|
+
metadata = {};
|
|
12
|
+
table[Symbol.metadata] = metadata;
|
|
13
|
+
}
|
|
14
|
+
return getTableInfoFromMetadata(metadata);
|
|
11
15
|
}
|
|
12
16
|
function getTableInfoFromMetadata(metadata) {
|
|
13
17
|
if (!metadata[exports.TABLE_INFO_PROPERTY_NAME])
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { BackendTable, Class } from "../../typings/BackendTable";
|
|
2
|
-
export default function DbTable<T extends BackendTable>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context
|
|
2
|
+
export default function DbTable<T extends BackendTable>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context?: any) => void;
|
|
@@ -5,7 +5,7 @@ const TableInfo_1 = require("../TableInfo");
|
|
|
5
5
|
function DbTable(tableName, primaryKey) {
|
|
6
6
|
return (table, context) => {
|
|
7
7
|
Object.defineProperty(table, "name", { value: tableName, writable: false });
|
|
8
|
-
const tableInfo = (0, TableInfo_1.getTableInfoFromMetadata)(context.metadata);
|
|
8
|
+
const tableInfo = context?.metadata ? (0, TableInfo_1.getTableInfoFromMetadata)(context.metadata) : (0, TableInfo_1.getTableInfo)(table);
|
|
9
9
|
tableInfo.primaryKey = primaryKey?.toString();
|
|
10
10
|
if (tableInfo.foreignKeys) {
|
|
11
11
|
for (const foreignKey of tableInfo.foreignKeys) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { BackendTable, Class } from "../../typings/BackendTable";
|
|
2
2
|
import "polyfill-symbol-metadata";
|
|
3
3
|
import { ForeignKeyActions } from "../../typings/ForeignKeyInfo";
|
|
4
|
-
export default function ForeignKey<TOther extends BackendTable>(toTable: Class<TOther>, toColumn: keyof TOther, onDelete?: ForeignKeyActions, onUpdate?: ForeignKeyActions): (
|
|
4
|
+
export default function ForeignKey<TOther extends BackendTable>(toTable: Class<TOther>, toColumn: keyof TOther, onDelete?: ForeignKeyActions, onUpdate?: ForeignKeyActions): (table: any, context: any) => void;
|
|
@@ -4,13 +4,13 @@ exports.default = ForeignKey;
|
|
|
4
4
|
const TableInfo_1 = require("../TableInfo");
|
|
5
5
|
require("polyfill-symbol-metadata");
|
|
6
6
|
function ForeignKey(toTable, toColumn, onDelete, onUpdate) {
|
|
7
|
-
return (
|
|
8
|
-
const metadata = (0, TableInfo_1.getTableInfoFromMetadata)(context.metadata);
|
|
7
|
+
return (table, context) => {
|
|
8
|
+
const metadata = context?.metadata ? (0, TableInfo_1.getTableInfoFromMetadata)(context.metadata) : (0, TableInfo_1.getTableInfo)(table);
|
|
9
9
|
if (!metadata.foreignKeys)
|
|
10
10
|
metadata.foreignKeys = [];
|
|
11
11
|
metadata.foreignKeys.push({
|
|
12
12
|
fromTable: "",
|
|
13
|
-
fromColumn: context.name,
|
|
13
|
+
fromColumn: context.name ?? context, //to stay compatible with old decorator structure (see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#decorator-metadata)
|
|
14
14
|
toTable: toTable.name,
|
|
15
15
|
toColumn: toColumn.toString(),
|
|
16
16
|
onDelete: onDelete,
|