sqlmorpheus 0.1.10 → 0.2.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/index.d.mts CHANGED
@@ -8,7 +8,7 @@ type Class<T> = new (...args: never[]) => T;
8
8
  /**
9
9
  * This does nothing. It mainly exists in case we want to filter class types in the future
10
10
  */
11
- type BackendTable = {
11
+ type TableClassInterface = {
12
12
  [key: string]: any;
13
13
  };
14
14
 
@@ -22,36 +22,72 @@ interface AllowedMigrations {
22
22
  continueWithoutRollback?: boolean;
23
23
  }
24
24
 
25
+ interface RenameData {
26
+ oldName: string;
27
+ newName: string;
28
+ }
25
29
  interface MigrationInstructions {
26
- oldTableName?: string;
30
+ tableRenaming?: RenameData;
27
31
  recreate: boolean;
28
32
  /**
29
- * Stores changes to column (0: name in the database, last: current name in code)
33
+ * Stores changes to column. One entry per column.
30
34
  */
31
- renamedColumns: string[][];
35
+ renamedColumns: RenameData[];
32
36
  allowedMigrations: AllowedMigrations;
37
+ usedMigrations: Record<string, boolean>;
38
+ }
39
+
40
+ interface ColumnInfo {
41
+ name: string;
42
+ type: string;
43
+ defaultValue: string;
44
+ isPrimaryKey: boolean;
45
+ }
46
+
47
+ type ForeignKeyActions = "SET NULL" | "SET DEFAULT" | "RESTRICT" | "NO ACTION" | "CASCADE";
48
+ interface ForeignKeyInfo {
49
+ fromTable: string;
50
+ fromColumn: string;
51
+ toTable: string;
52
+ toColumn: string;
53
+ onUpdate?: ForeignKeyActions;
54
+ onDelete?: ForeignKeyActions;
55
+ }
56
+
57
+ interface TableStructure {
58
+ table: string;
59
+ primaryKey?: string;
60
+ columns: ColumnInfo[];
61
+ foreignKeys?: ForeignKeyInfo[];
33
62
  }
34
63
 
35
64
  declare class Migrations {
36
- private toVersion;
37
- private alwaysAllowed;
38
65
  private fromVersion;
66
+ private toVersion;
67
+ private lastUsedVersion;
39
68
  private migrationData;
69
+ private migrationDataForOldTableNames;
70
+ private alwaysAllowed;
40
71
  private notAllowedChanges;
41
72
  reset(dbInstructions: DatabaseInstructions, fromVersion: number): void;
42
73
  private getEntry;
74
+ private getEntryFromTableName;
43
75
  private getTableName;
44
- verifyMigration(tableName: string, type: keyof AllowedMigrations): void;
45
- checkIfAllowed(throwIfNotAllowed: boolean): boolean;
46
- allowMigration(version: number, table: string | Class<BackendTable>, ...allowedMigrations: (keyof AllowedMigrations)[]): void;
47
- renameTable(version: number, oldTableName: string, newTable: string | Class<BackendTable>): void;
48
- renameColumn(version: number, table: string | Class<BackendTable>, oldColumn: string, newColumn: string): void;
49
- recreateTableImp(table: Class<BackendTable> | string): void;
50
- recreateTable(version: number, table: Class<BackendTable> | string): void;
76
+ private versionIsRelevant;
77
+ verifyRenamingTasks(newTables: Record<string, TableStructure>): Error | void;
78
+ verifyAllowedMigrations(): Error | void;
79
+ compareWithAllowedMigration(tableName: string, type: keyof AllowedMigrations): void;
51
80
  getMigrationData(): Record<string, MigrationInstructions>;
52
81
  loopRenamedColumns(tableName: string, callback: (oldColumnName: string, newColumnName: string) => void): void;
53
- getUpdatedColumnName(tableName: string, oldColumnName: string): string;
82
+ getNewestTableName(tableName: string): string;
83
+ getOldTableName(tableName: string): string;
84
+ getNewestColumnName(tableName: string, oldColumnName: string): string;
54
85
  willBeRecreated(tableName: string): boolean;
86
+ internalRecreate(tableName: string, reason: string): void;
87
+ allowMigration(version: number, table: TableInput, ...allowedMigrations: (keyof AllowedMigrations)[]): void;
88
+ renameTable(version: number, oldTableName: string, newTableName: TableInput): void;
89
+ renameColumn(version: number, table: TableInput, oldColumn: string, newColumn: string): void;
90
+ recreateTable(version: number, table: TableInput): void;
55
91
  }
56
92
  type PublicMigrations = Pick<Migrations, "recreateTable" | "renameTable" | "renameColumn" | "allowMigration">;
57
93
 
@@ -60,29 +96,45 @@ interface SqlChanges {
60
96
  down: string;
61
97
  }
62
98
 
63
- type ForeignKeyActions = "SET NULL" | "SET DEFAULT" | "RESTRICT" | "NO ACTION" | "CASCADE";
64
- interface ForeignKeyInfo {
65
- fromTable: string;
66
- fromColumn: string;
67
- toTable: string;
68
- toColumn: string;
69
- onUpdate?: ForeignKeyActions;
70
- onDelete?: ForeignKeyActions;
71
- }
99
+ type NativeDataTypeOptions = "boolean" | "number" | "bigint" | "string";
100
+ type AddedDataTypeOptions = "date" | "time" | "dateTime";
101
+ type DataTypeOptions = NativeDataTypeOptions | AddedDataTypeOptions;
72
102
 
73
103
  interface TableInfo {
74
104
  primaryKey?: string;
75
105
  foreignKeys?: ForeignKeyInfo[];
106
+ dataTypes?: Record<string, DataTypeOptions>;
76
107
  }
77
108
 
78
- type TableObjects = Record<string, {
79
- columns: BackendTable;
80
- tableInfo?: TableInfo;
81
- }>;
109
+ type DataFormat = Record<string, unknown>;
110
+ type GetColumns<T> = T extends TableObjBuilder<infer Item, any> ? Item : never;
111
+ type TableObjBuilder<T extends DataFormat, TPick extends keyof TableObj<T>> = Pick<TableObj<T>, TPick>;
112
+ type TableObjInput = TableObjBuilder<any, any>;
113
+ /**
114
+ * Use this class to define tables for DatabaseInstructions
115
+ * (as an alternative for Class decorators)
116
+ *
117
+ */
118
+ declare class TableObj<T extends DataFormat> {
119
+ readonly IS_TABLE_OBJ = true;
120
+ readonly tableName: string;
121
+ readonly columns: T;
122
+ tableInfo: TableInfo;
123
+ private constructor();
124
+ primaryKey(key: keyof T): TableObjBuilder<T, "foreignKey" | "dataType">;
125
+ foreignKey<TOther extends TableObjBuilder<DataFormat, any>>(fromColumn: keyof T, toTable: TOther, toColumn: keyof GetColumns<TOther>, options?: {
126
+ onUpdate?: ForeignKeyActions;
127
+ onDelete?: ForeignKeyActions;
128
+ }): TableObjBuilder<T, "foreignKey" | "dataType">;
129
+ dataType(column: keyof T, type: DataTypeOptions): TableObjBuilder<T, "foreignKey" | "dataType">;
130
+ static create<T extends Record<string, unknown>>(tableName: string, columns: T): TableObjBuilder<T, "primaryKey" | "foreignKey" | "dataType">;
131
+ static isDbTable(obj: unknown): obj is TableObj<any>;
132
+ }
82
133
 
134
+ type TableInput = TableObjInput | Class<TableClassInterface>;
83
135
  interface DatabaseInstructions {
84
136
  dialect: "Sqlite" | "MsSql" | "MySql" | "Postgres";
85
- tables: Class<BackendTable>[] | TableObjects;
137
+ tables: TableInput[];
86
138
  version: number;
87
139
  configPath: string;
88
140
  throwIfNotAllowed: boolean;
@@ -106,24 +158,33 @@ interface DatabaseInstructions {
106
158
  }
107
159
 
108
160
  /**
109
- * Decorator function to configure a database table with its name and primary key
110
- * and to add missing "fromTable" properties to foreign keys.
161
+ * Decorator function to configure a database table with its name and primary key.
111
162
  *
112
163
  * @param tableName - The name of the database table used in SQL.
113
164
  * @param primaryKey - The property name representing the primary property of the table.
114
165
  */
115
- declare function DbTable<T extends BackendTable>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context?: any) => void;
166
+ declare function TableClass<T extends TableClassInterface>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context?: any) => void;
167
+
168
+ /**
169
+ * Decorator function to assign a specific data type to a column.
170
+ * Should be used if you want to use a data type that does not exist in JavaScript or
171
+ * if the default value should be NULL
172
+ *
173
+ * @param type - The data type to be assigned.
174
+ */
175
+ declare function DataType(type: DataTypeOptions): (table: any, context: any) => void;
116
176
 
117
177
  /**
118
- * Decorator function to define a foreign key constraint for a database table. It defines a relationship
119
- * between a column on the current table and a column on another table.
178
+ * Decorator function to define a foreign key relationship for a database table.
120
179
  *
121
180
  * @param toTable - The target table class to which the foreign key points.
122
181
  * @param toColumn - The column name in the target table to which the foreign key points.
123
- * @param [onDelete] - Optional. The action to perform when a row in the target table is deleted.
124
- * @param [onUpdate] - Optional. The action to perform when a related row in the target table is updated.
182
+ * @param options - Additional options.
125
183
  */
126
- declare function ForeignKey<TOther extends BackendTable>(toTable: Class<TOther>, toColumn: keyof TOther, onDelete?: ForeignKeyActions, onUpdate?: ForeignKeyActions): (table: any, context: any) => void;
184
+ declare function ForeignKey<TOther extends TableClassInterface>(toTable: Class<TOther>, toColumn: keyof TOther, options?: {
185
+ onDelete?: ForeignKeyActions;
186
+ onUpdate?: ForeignKeyActions;
187
+ }): (table: any, context: any) => void;
127
188
 
128
189
  declare function prepareMigration(db: DatabaseAccess, dbInstructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
129
190
  declare function runPreparedMigrations(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
@@ -131,4 +192,4 @@ declare function prepareAndRunMigration(db: DatabaseAccess, instructions: Databa
131
192
  declare function runMigrationWithoutHistory(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
132
193
  declare function rollback(db: DatabaseAccess, dbInstructions: DatabaseInstructions, toVersion: number): Promise<void>;
133
194
 
134
- export { type DatabaseAccess, type DatabaseInstructions, DbTable, ForeignKey, type PublicMigrations, type SqlChanges, prepareAndRunMigration, prepareMigration, rollback, runMigrationWithoutHistory, runPreparedMigrations };
195
+ export { DataType, type DatabaseAccess, type DatabaseInstructions, ForeignKey, type PublicMigrations, type SqlChanges, TableClass, TableObj, prepareAndRunMigration, prepareMigration, rollback, runMigrationWithoutHistory, runPreparedMigrations };
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ type Class<T> = new (...args: never[]) => T;
8
8
  /**
9
9
  * This does nothing. It mainly exists in case we want to filter class types in the future
10
10
  */
11
- type BackendTable = {
11
+ type TableClassInterface = {
12
12
  [key: string]: any;
13
13
  };
14
14
 
@@ -22,36 +22,72 @@ interface AllowedMigrations {
22
22
  continueWithoutRollback?: boolean;
23
23
  }
24
24
 
25
+ interface RenameData {
26
+ oldName: string;
27
+ newName: string;
28
+ }
25
29
  interface MigrationInstructions {
26
- oldTableName?: string;
30
+ tableRenaming?: RenameData;
27
31
  recreate: boolean;
28
32
  /**
29
- * Stores changes to column (0: name in the database, last: current name in code)
33
+ * Stores changes to column. One entry per column.
30
34
  */
31
- renamedColumns: string[][];
35
+ renamedColumns: RenameData[];
32
36
  allowedMigrations: AllowedMigrations;
37
+ usedMigrations: Record<string, boolean>;
38
+ }
39
+
40
+ interface ColumnInfo {
41
+ name: string;
42
+ type: string;
43
+ defaultValue: string;
44
+ isPrimaryKey: boolean;
45
+ }
46
+
47
+ type ForeignKeyActions = "SET NULL" | "SET DEFAULT" | "RESTRICT" | "NO ACTION" | "CASCADE";
48
+ interface ForeignKeyInfo {
49
+ fromTable: string;
50
+ fromColumn: string;
51
+ toTable: string;
52
+ toColumn: string;
53
+ onUpdate?: ForeignKeyActions;
54
+ onDelete?: ForeignKeyActions;
55
+ }
56
+
57
+ interface TableStructure {
58
+ table: string;
59
+ primaryKey?: string;
60
+ columns: ColumnInfo[];
61
+ foreignKeys?: ForeignKeyInfo[];
33
62
  }
34
63
 
35
64
  declare class Migrations {
36
- private toVersion;
37
- private alwaysAllowed;
38
65
  private fromVersion;
66
+ private toVersion;
67
+ private lastUsedVersion;
39
68
  private migrationData;
69
+ private migrationDataForOldTableNames;
70
+ private alwaysAllowed;
40
71
  private notAllowedChanges;
41
72
  reset(dbInstructions: DatabaseInstructions, fromVersion: number): void;
42
73
  private getEntry;
74
+ private getEntryFromTableName;
43
75
  private getTableName;
44
- verifyMigration(tableName: string, type: keyof AllowedMigrations): void;
45
- checkIfAllowed(throwIfNotAllowed: boolean): boolean;
46
- allowMigration(version: number, table: string | Class<BackendTable>, ...allowedMigrations: (keyof AllowedMigrations)[]): void;
47
- renameTable(version: number, oldTableName: string, newTable: string | Class<BackendTable>): void;
48
- renameColumn(version: number, table: string | Class<BackendTable>, oldColumn: string, newColumn: string): void;
49
- recreateTableImp(table: Class<BackendTable> | string): void;
50
- recreateTable(version: number, table: Class<BackendTable> | string): void;
76
+ private versionIsRelevant;
77
+ verifyRenamingTasks(newTables: Record<string, TableStructure>): Error | void;
78
+ verifyAllowedMigrations(): Error | void;
79
+ compareWithAllowedMigration(tableName: string, type: keyof AllowedMigrations): void;
51
80
  getMigrationData(): Record<string, MigrationInstructions>;
52
81
  loopRenamedColumns(tableName: string, callback: (oldColumnName: string, newColumnName: string) => void): void;
53
- getUpdatedColumnName(tableName: string, oldColumnName: string): string;
82
+ getNewestTableName(tableName: string): string;
83
+ getOldTableName(tableName: string): string;
84
+ getNewestColumnName(tableName: string, oldColumnName: string): string;
54
85
  willBeRecreated(tableName: string): boolean;
86
+ internalRecreate(tableName: string, reason: string): void;
87
+ allowMigration(version: number, table: TableInput, ...allowedMigrations: (keyof AllowedMigrations)[]): void;
88
+ renameTable(version: number, oldTableName: string, newTableName: TableInput): void;
89
+ renameColumn(version: number, table: TableInput, oldColumn: string, newColumn: string): void;
90
+ recreateTable(version: number, table: TableInput): void;
55
91
  }
56
92
  type PublicMigrations = Pick<Migrations, "recreateTable" | "renameTable" | "renameColumn" | "allowMigration">;
57
93
 
@@ -60,29 +96,45 @@ interface SqlChanges {
60
96
  down: string;
61
97
  }
62
98
 
63
- type ForeignKeyActions = "SET NULL" | "SET DEFAULT" | "RESTRICT" | "NO ACTION" | "CASCADE";
64
- interface ForeignKeyInfo {
65
- fromTable: string;
66
- fromColumn: string;
67
- toTable: string;
68
- toColumn: string;
69
- onUpdate?: ForeignKeyActions;
70
- onDelete?: ForeignKeyActions;
71
- }
99
+ type NativeDataTypeOptions = "boolean" | "number" | "bigint" | "string";
100
+ type AddedDataTypeOptions = "date" | "time" | "dateTime";
101
+ type DataTypeOptions = NativeDataTypeOptions | AddedDataTypeOptions;
72
102
 
73
103
  interface TableInfo {
74
104
  primaryKey?: string;
75
105
  foreignKeys?: ForeignKeyInfo[];
106
+ dataTypes?: Record<string, DataTypeOptions>;
76
107
  }
77
108
 
78
- type TableObjects = Record<string, {
79
- columns: BackendTable;
80
- tableInfo?: TableInfo;
81
- }>;
109
+ type DataFormat = Record<string, unknown>;
110
+ type GetColumns<T> = T extends TableObjBuilder<infer Item, any> ? Item : never;
111
+ type TableObjBuilder<T extends DataFormat, TPick extends keyof TableObj<T>> = Pick<TableObj<T>, TPick>;
112
+ type TableObjInput = TableObjBuilder<any, any>;
113
+ /**
114
+ * Use this class to define tables for DatabaseInstructions
115
+ * (as an alternative for Class decorators)
116
+ *
117
+ */
118
+ declare class TableObj<T extends DataFormat> {
119
+ readonly IS_TABLE_OBJ = true;
120
+ readonly tableName: string;
121
+ readonly columns: T;
122
+ tableInfo: TableInfo;
123
+ private constructor();
124
+ primaryKey(key: keyof T): TableObjBuilder<T, "foreignKey" | "dataType">;
125
+ foreignKey<TOther extends TableObjBuilder<DataFormat, any>>(fromColumn: keyof T, toTable: TOther, toColumn: keyof GetColumns<TOther>, options?: {
126
+ onUpdate?: ForeignKeyActions;
127
+ onDelete?: ForeignKeyActions;
128
+ }): TableObjBuilder<T, "foreignKey" | "dataType">;
129
+ dataType(column: keyof T, type: DataTypeOptions): TableObjBuilder<T, "foreignKey" | "dataType">;
130
+ static create<T extends Record<string, unknown>>(tableName: string, columns: T): TableObjBuilder<T, "primaryKey" | "foreignKey" | "dataType">;
131
+ static isDbTable(obj: unknown): obj is TableObj<any>;
132
+ }
82
133
 
134
+ type TableInput = TableObjInput | Class<TableClassInterface>;
83
135
  interface DatabaseInstructions {
84
136
  dialect: "Sqlite" | "MsSql" | "MySql" | "Postgres";
85
- tables: Class<BackendTable>[] | TableObjects;
137
+ tables: TableInput[];
86
138
  version: number;
87
139
  configPath: string;
88
140
  throwIfNotAllowed: boolean;
@@ -106,24 +158,33 @@ interface DatabaseInstructions {
106
158
  }
107
159
 
108
160
  /**
109
- * Decorator function to configure a database table with its name and primary key
110
- * and to add missing "fromTable" properties to foreign keys.
161
+ * Decorator function to configure a database table with its name and primary key.
111
162
  *
112
163
  * @param tableName - The name of the database table used in SQL.
113
164
  * @param primaryKey - The property name representing the primary property of the table.
114
165
  */
115
- declare function DbTable<T extends BackendTable>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context?: any) => void;
166
+ declare function TableClass<T extends TableClassInterface>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context?: any) => void;
167
+
168
+ /**
169
+ * Decorator function to assign a specific data type to a column.
170
+ * Should be used if you want to use a data type that does not exist in JavaScript or
171
+ * if the default value should be NULL
172
+ *
173
+ * @param type - The data type to be assigned.
174
+ */
175
+ declare function DataType(type: DataTypeOptions): (table: any, context: any) => void;
116
176
 
117
177
  /**
118
- * Decorator function to define a foreign key constraint for a database table. It defines a relationship
119
- * between a column on the current table and a column on another table.
178
+ * Decorator function to define a foreign key relationship for a database table.
120
179
  *
121
180
  * @param toTable - The target table class to which the foreign key points.
122
181
  * @param toColumn - The column name in the target table to which the foreign key points.
123
- * @param [onDelete] - Optional. The action to perform when a row in the target table is deleted.
124
- * @param [onUpdate] - Optional. The action to perform when a related row in the target table is updated.
182
+ * @param options - Additional options.
125
183
  */
126
- declare function ForeignKey<TOther extends BackendTable>(toTable: Class<TOther>, toColumn: keyof TOther, onDelete?: ForeignKeyActions, onUpdate?: ForeignKeyActions): (table: any, context: any) => void;
184
+ declare function ForeignKey<TOther extends TableClassInterface>(toTable: Class<TOther>, toColumn: keyof TOther, options?: {
185
+ onDelete?: ForeignKeyActions;
186
+ onUpdate?: ForeignKeyActions;
187
+ }): (table: any, context: any) => void;
127
188
 
128
189
  declare function prepareMigration(db: DatabaseAccess, dbInstructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
129
190
  declare function runPreparedMigrations(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
@@ -131,4 +192,4 @@ declare function prepareAndRunMigration(db: DatabaseAccess, instructions: Databa
131
192
  declare function runMigrationWithoutHistory(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
132
193
  declare function rollback(db: DatabaseAccess, dbInstructions: DatabaseInstructions, toVersion: number): Promise<void>;
133
194
 
134
- export { type DatabaseAccess, type DatabaseInstructions, DbTable, ForeignKey, type PublicMigrations, type SqlChanges, prepareAndRunMigration, prepareMigration, rollback, runMigrationWithoutHistory, runPreparedMigrations };
195
+ export { DataType, type DatabaseAccess, type DatabaseInstructions, ForeignKey, type PublicMigrations, type SqlChanges, TableClass, TableObj, prepareAndRunMigration, prepareMigration, rollback, runMigrationWithoutHistory, runPreparedMigrations };