sqlmorpheus 0.2.0 → 0.2.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/README.md +3 -0
- package/dist/index.d.mts +226 -28
- package/dist/index.d.ts +226 -28
- package/dist/index.js +665 -194
- package/dist/index.mjs +661 -192
- package/package.json +1 -1
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
|
|
11
|
+
type TableClassInterface = {
|
|
12
12
|
[key: string]: any;
|
|
13
13
|
};
|
|
14
14
|
|
|
@@ -31,7 +31,6 @@ interface MigrationInstructions {
|
|
|
31
31
|
recreate: boolean;
|
|
32
32
|
/**
|
|
33
33
|
* Stores changes to column. One entry per column.
|
|
34
|
-
* Each entry is an array (0: name in the database, last: current name in code)
|
|
35
34
|
*/
|
|
36
35
|
renamedColumns: RenameData[];
|
|
37
36
|
allowedMigrations: AllowedMigrations;
|
|
@@ -41,7 +40,8 @@ interface MigrationInstructions {
|
|
|
41
40
|
interface ColumnInfo {
|
|
42
41
|
name: string;
|
|
43
42
|
type: string;
|
|
44
|
-
|
|
43
|
+
maxLength?: number;
|
|
44
|
+
defaultValue?: string;
|
|
45
45
|
isPrimaryKey: boolean;
|
|
46
46
|
}
|
|
47
47
|
|
|
@@ -58,34 +58,190 @@ interface ForeignKeyInfo {
|
|
|
58
58
|
interface TableStructure {
|
|
59
59
|
table: string;
|
|
60
60
|
primaryKey?: string;
|
|
61
|
-
columns: ColumnInfo
|
|
61
|
+
columns: Record<string, ColumnInfo>;
|
|
62
62
|
foreignKeys?: ForeignKeyInfo[];
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
+
/**
|
|
66
|
+
* The Migrations class is responsible for managing manual database schema migrations.
|
|
67
|
+
*/
|
|
65
68
|
declare class Migrations {
|
|
66
|
-
private toVersion;
|
|
67
|
-
private alwaysAllowed;
|
|
68
69
|
private fromVersion;
|
|
70
|
+
private toVersion;
|
|
71
|
+
private lastUsedVersion;
|
|
69
72
|
private migrationData;
|
|
70
73
|
private migrationDataForOldTableNames;
|
|
74
|
+
private alwaysAllowed;
|
|
71
75
|
private notAllowedChanges;
|
|
76
|
+
/**
|
|
77
|
+
* Resets the migration state to its initial values.
|
|
78
|
+
* Only used internally.
|
|
79
|
+
*
|
|
80
|
+
* @param dbInstructions - The instructions containing database metadata and migration details.
|
|
81
|
+
* @param fromVersion - The version of the actual database.
|
|
82
|
+
*/
|
|
72
83
|
reset(dbInstructions: DatabaseInstructions, fromVersion: number): void;
|
|
84
|
+
/**
|
|
85
|
+
* Retrieves the migration instructions for the specified table.
|
|
86
|
+
* @see getEntryFromTableName
|
|
87
|
+
*
|
|
88
|
+
* @param table - The {@link TableInput} object from which the table name will be retrieved.
|
|
89
|
+
* @return The migration instructions corresponding to the given table input.
|
|
90
|
+
*/
|
|
73
91
|
private getEntry;
|
|
92
|
+
/**
|
|
93
|
+
* Retrieves the migration instructions for the specified table.
|
|
94
|
+
*
|
|
95
|
+
* @param tableName - The name of the table for which the entry is to be retrieved or created.
|
|
96
|
+
* @return The migration instructions corresponding to the given table input.
|
|
97
|
+
*/
|
|
98
|
+
private getEntryFromTableName;
|
|
99
|
+
/**
|
|
100
|
+
* Retrieves the table name from the given table input.
|
|
101
|
+
*
|
|
102
|
+
* @param table - The {@link TableInput} object.
|
|
103
|
+
* @return The name of the table derived from either {@link TableObj} or a class.
|
|
104
|
+
*/
|
|
74
105
|
private getTableName;
|
|
106
|
+
/**
|
|
107
|
+
* Checks whether a provided version is relevant based on database version ({@link fromVersion}) and target version ({@link toVersion}).
|
|
108
|
+
* Also makes sure that version checks happen in order.
|
|
109
|
+
*
|
|
110
|
+
* @param version - The version to check for relevance.
|
|
111
|
+
* @return Returns true if the version is relevant, otherwise false.
|
|
112
|
+
* @throws Error if versions are not properly ordered
|
|
113
|
+
*/
|
|
114
|
+
private versionIsRelevant;
|
|
115
|
+
/**
|
|
116
|
+
* Verifies the renaming tasks specified in {@link DatabaseInstructions.preMigration} that were considered are valid.
|
|
117
|
+
* Called after all SQL queries have been generated.
|
|
118
|
+
* Only used internally.
|
|
119
|
+
*
|
|
120
|
+
* @param newTables - The table structures extracted from {@link DatabaseInstructions.tables}.
|
|
121
|
+
* @throws Error if a renaming task is invalid.
|
|
122
|
+
*/
|
|
75
123
|
verifyRenamingTasks(newTables: Record<string, TableStructure>): Error | void;
|
|
124
|
+
/**
|
|
125
|
+
* Making sure all considered calls to {@link allowMigration()} where necessary to prevent input mistakes.
|
|
126
|
+
* Called after all SQL queries have been generated.
|
|
127
|
+
* Only used internally.
|
|
128
|
+
*
|
|
129
|
+
* @throws Error if unnecessary calls were discovered. All discovered calls are collected and thrown as one Error.
|
|
130
|
+
*/
|
|
76
131
|
verifyAllowedMigrations(): Error | void;
|
|
132
|
+
/**
|
|
133
|
+
* Called by {@link MigrationManager} to check if a specific destructive query is allowed.
|
|
134
|
+
* Only used internally.
|
|
135
|
+
*
|
|
136
|
+
* @see allowMigration()
|
|
137
|
+
* @param tableName - The name of the table to check the migration against.
|
|
138
|
+
* @param type - The type of destructive migration being checked.
|
|
139
|
+
*/
|
|
77
140
|
compareWithAllowedMigration(tableName: string, type: keyof AllowedMigrations): void;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Retrieves the migration data containing instructions for data migration.
|
|
143
|
+
* Only used internally.
|
|
144
|
+
*
|
|
145
|
+
* @return An object where each key corresponds to the table name,
|
|
146
|
+
* and the value is the associated migration instructions.
|
|
147
|
+
*/
|
|
83
148
|
getMigrationData(): Record<string, MigrationInstructions>;
|
|
149
|
+
/**
|
|
150
|
+
* Iterates over renamed columns for the specified table and executes a callback for each pair of old and new column names.
|
|
151
|
+
* Only used internally.
|
|
152
|
+
*
|
|
153
|
+
* @param tableName - Name of the table whose renamed columns should be processed.
|
|
154
|
+
* @param callback - A function to execute for each renamed column, receiving the old and new column names as arguments.
|
|
155
|
+
*/
|
|
84
156
|
loopRenamedColumns(tableName: string, callback: (oldColumnName: string, newColumnName: string) => void): void;
|
|
157
|
+
/**
|
|
158
|
+
* Considers the renaming of a table and returns its (newest) name after the migration or
|
|
159
|
+
* the original name if no renaming is queued.
|
|
160
|
+
* Only used internally.
|
|
161
|
+
*
|
|
162
|
+
* @see getOldTableName()
|
|
163
|
+
* @param tableName - The current table name in the database.
|
|
164
|
+
* @return The newest table name if available, otherwise the current table name.
|
|
165
|
+
*/
|
|
85
166
|
getNewestTableName(tableName: string): string;
|
|
167
|
+
/**
|
|
168
|
+
* Retrieves the current table name in the database.
|
|
169
|
+
* Only used internally.
|
|
170
|
+
*
|
|
171
|
+
* @see getNewestTableName()
|
|
172
|
+
* @param tableName - The name of the newest table name.
|
|
173
|
+
* @return The current table name in the database.
|
|
174
|
+
*/
|
|
86
175
|
getOldTableName(tableName: string): string;
|
|
176
|
+
/**
|
|
177
|
+
* Considers the renaming of a column in a table and returns its (newest) name after the migration or
|
|
178
|
+
* the original name if no renaming is queued.
|
|
179
|
+
* Only used internally.
|
|
180
|
+
*
|
|
181
|
+
* @param tableName - The name of the table containing the column.
|
|
182
|
+
* @param oldColumnName - The current name of the column in the database.
|
|
183
|
+
* @return The newest column name if available, otherwise the original column name.
|
|
184
|
+
*/
|
|
87
185
|
getNewestColumnName(tableName: string, oldColumnName: string): string;
|
|
186
|
+
/**
|
|
187
|
+
* Determines if the specified table is marked to be recreated.
|
|
188
|
+
* Only used internally.
|
|
189
|
+
*
|
|
190
|
+
* @param tableName The name of the table to check.
|
|
191
|
+
* @return Returns true if the table will be recreated, false otherwise.
|
|
192
|
+
*/
|
|
88
193
|
willBeRecreated(tableName: string): boolean;
|
|
194
|
+
/**
|
|
195
|
+
* Marks a table to be recreated and logs the recreation reason for the user.
|
|
196
|
+
* Only used internally.
|
|
197
|
+
*
|
|
198
|
+
* @param tableName - The name of the table to be marked for recreation.
|
|
199
|
+
* @param reason - The reason why the table is being marked for recreation.
|
|
200
|
+
*/
|
|
201
|
+
internalRecreate(tableName: string, reason: string): void;
|
|
202
|
+
/**
|
|
203
|
+
* SqlMorpheus only allows destructive statements if it was specifically allowed in {@link DatabaseInstructions.preMigration()}.
|
|
204
|
+
* Use this method to allow a destructive statement for a specific version.
|
|
205
|
+
* This method will be ignored if {@link version} is smaller than the current database version or higher than the target version of {@link DatabaseInstructions}.
|
|
206
|
+
*
|
|
207
|
+
* @param version - The target version for which the specific migration is allowed. Must be provided to make sure this method will only have an effect when specified.
|
|
208
|
+
* @param table - Either a {@link TableObj} or a class decorated with @TableClass.
|
|
209
|
+
* @param allowedMigrations - The keys representing the migrations to be allowed. Multiple migrations can be provided
|
|
210
|
+
*/
|
|
211
|
+
allowMigration(version: number, table: TableInput, ...allowedMigrations: (keyof AllowedMigrations)[]): void;
|
|
212
|
+
/**
|
|
213
|
+
* When you change the name of a table, SqlMorpheus will assume the original table was meant to be dropped
|
|
214
|
+
* and the new table was meant to be added, which would lead to data loss.
|
|
215
|
+
* Use this method to mark a table to be renamed instead.
|
|
216
|
+
* This method will be ignored if {@link version} is smaller than the current database version or higher than the target version of {@link DatabaseInstructions}.
|
|
217
|
+
*
|
|
218
|
+
* @param version - The target version for which the table should be renamed. Must be provided to make sure this method will only have an effect when specified.
|
|
219
|
+
* @param oldTableName - The current name in the database at the time of the migration.
|
|
220
|
+
* @param newTableName - The new table name or its equivalent input format.
|
|
221
|
+
*/
|
|
222
|
+
renameTable(version: number, oldTableName: string, newTableName: TableInput): void;
|
|
223
|
+
/**
|
|
224
|
+
* When you change the name of a column, SqlMorpheus will assume the original column was meant to be removed
|
|
225
|
+
* and the new column was meant to be added, which would lead to data loss.
|
|
226
|
+
* Use this method to mark a column to be renamed instead.
|
|
227
|
+
* This method will be ignored if {@link version} is smaller than the current database version or higher than the target version of {@link DatabaseInstructions}.
|
|
228
|
+
*
|
|
229
|
+
* @param version - The target version for which the table should be renamed. Must be provided to make sure this method will only have an effect when specified.
|
|
230
|
+
* @param table Either a {@link TableObj} or a class decorated with @TableClass containing the column to be renamed.
|
|
231
|
+
* @param oldColumn The current name in the database at the time of the migration.
|
|
232
|
+
* @param newColumn The new name to assign to the column.
|
|
233
|
+
*/
|
|
234
|
+
renameColumn(version: number, table: TableInput, oldColumn: string, newColumn: string): void;
|
|
235
|
+
/**
|
|
236
|
+
* Should not be needed most of the time.
|
|
237
|
+
* Marks a table to be recreated
|
|
238
|
+
* (a copy of the table will be created, all data will be moved over, the original table will be dropped and the new table will be renamed to the original name).
|
|
239
|
+
* This method will be ignored if {@link version} is smaller than the current database version or higher than the target version of {@link DatabaseInstructions}.
|
|
240
|
+
*
|
|
241
|
+
* @param version - The target version for which the table should be renamed. Must be provided to make sure this method will only have an effect when specified.
|
|
242
|
+
* @param table Either a {@link TableObj} or a class decorated with @TableClass.
|
|
243
|
+
*/
|
|
244
|
+
recreateTable(version: number, table: TableInput): void;
|
|
89
245
|
}
|
|
90
246
|
type PublicMigrations = Pick<Migrations, "recreateTable" | "renameTable" | "renameColumn" | "allowMigration">;
|
|
91
247
|
|
|
@@ -94,19 +250,52 @@ interface SqlChanges {
|
|
|
94
250
|
down: string;
|
|
95
251
|
}
|
|
96
252
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
253
|
+
type NativeDataTypeOptions = "boolean" | "number" | "bigint" | "string";
|
|
254
|
+
type AddedDataTypeOptions = "date" | "time" | "dateTime";
|
|
255
|
+
type DataTypeOptions = NativeDataTypeOptions | AddedDataTypeOptions;
|
|
256
|
+
|
|
257
|
+
interface InputColumnInfo extends ColumnInfo {
|
|
258
|
+
inputType?: DataTypeOptions;
|
|
259
|
+
inputDefaultValue?: unknown;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
interface InputTableStructure extends TableStructure {
|
|
263
|
+
columns: Record<string, InputColumnInfo>;
|
|
100
264
|
}
|
|
101
265
|
|
|
102
|
-
type
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
266
|
+
type DataFormat = Record<string, unknown>;
|
|
267
|
+
type GetColumns<T> = T extends TableObjBuilderDefinition<infer Item, any> ? Item : never;
|
|
268
|
+
type TableObjBuilderDefinition<T extends DataFormat, TPick extends keyof TableObj<T>> = Pick<TableObj<T>, TPick>;
|
|
269
|
+
type NewTableObjBuilder<T extends DataFormat> = TableObjBuilderDefinition<T, "primaryKey" | "foreignKey" | "dataType" | "maxCharacterLength">;
|
|
270
|
+
type TableObjBuilder<T extends DataFormat> = TableObjBuilderDefinition<T, "foreignKey" | "dataType" | "maxCharacterLength">;
|
|
271
|
+
type TableObjInput = TableObjBuilderDefinition<any, any>;
|
|
272
|
+
/**
|
|
273
|
+
* Use this class to define tables for DatabaseInstructions
|
|
274
|
+
* (as an alternative for Class decorators)
|
|
275
|
+
*
|
|
276
|
+
*/
|
|
277
|
+
declare class TableObj<T extends DataFormat> {
|
|
278
|
+
readonly IS_TABLE_OBJ = true;
|
|
279
|
+
readonly tableName: string;
|
|
280
|
+
tableStructure: InputTableStructure;
|
|
281
|
+
static getEmptyColumnEntry(key: string): InputColumnInfo;
|
|
282
|
+
static getColumnEntry(key: string, value: unknown): InputColumnInfo | null;
|
|
283
|
+
private constructor();
|
|
284
|
+
primaryKey(key: keyof T): TableObjBuilder<T>;
|
|
285
|
+
foreignKey<TOther extends TableObjBuilderDefinition<DataFormat, any>>(fromColumn: keyof T, toTable: TOther, toColumn: keyof GetColumns<TOther>, options?: {
|
|
286
|
+
onUpdate?: ForeignKeyActions;
|
|
287
|
+
onDelete?: ForeignKeyActions;
|
|
288
|
+
}): TableObjBuilder<T>;
|
|
289
|
+
dataType(column: keyof T, type: DataTypeOptions): TableObjBuilder<T>;
|
|
290
|
+
maxCharacterLength(column: keyof T, max: number): TableObjBuilder<T>;
|
|
291
|
+
static create<T extends Record<string, unknown>>(tableName: string, columns: T): NewTableObjBuilder<T>;
|
|
292
|
+
static isTableObj(obj: unknown): obj is TableObj<any>;
|
|
293
|
+
}
|
|
106
294
|
|
|
295
|
+
type TableInput = TableObjInput | Class<TableClassInterface>;
|
|
107
296
|
interface DatabaseInstructions {
|
|
108
297
|
dialect: "Sqlite" | "MsSql" | "MySql" | "Postgres";
|
|
109
|
-
tables:
|
|
298
|
+
tables: TableInput[];
|
|
110
299
|
version: number;
|
|
111
300
|
configPath: string;
|
|
112
301
|
throwIfNotAllowed: boolean;
|
|
@@ -130,24 +319,33 @@ interface DatabaseInstructions {
|
|
|
130
319
|
}
|
|
131
320
|
|
|
132
321
|
/**
|
|
133
|
-
* Decorator function to configure a database table with its name and primary key
|
|
134
|
-
* and to add missing "fromTable" properties to foreign keys.
|
|
322
|
+
* Decorator function to configure a database table with its name and primary key.
|
|
135
323
|
*
|
|
136
324
|
* @param tableName - The name of the database table used in SQL.
|
|
137
325
|
* @param primaryKey - The property name representing the primary property of the table.
|
|
138
326
|
*/
|
|
139
|
-
declare function
|
|
327
|
+
declare function TableClass<T extends TableClassInterface>(tableName: string, primaryKey?: keyof T): (table: Class<T>, context?: any) => void;
|
|
140
328
|
|
|
141
329
|
/**
|
|
142
|
-
* Decorator function to
|
|
143
|
-
*
|
|
330
|
+
* Decorator function to assign a specific data type to a column.
|
|
331
|
+
* Should be used if you want to use a data type that does not exist in JavaScript or
|
|
332
|
+
* if the default value should be NULL
|
|
333
|
+
*
|
|
334
|
+
* @param type - The data type to be assigned.
|
|
335
|
+
*/
|
|
336
|
+
declare function DataType(type: DataTypeOptions): (table: any, context: any) => void;
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Decorator function to define a foreign key relationship for a database table.
|
|
144
340
|
*
|
|
145
341
|
* @param toTable - The target table class to which the foreign key points.
|
|
146
342
|
* @param toColumn - The column name in the target table to which the foreign key points.
|
|
147
|
-
* @param
|
|
148
|
-
* @param [onUpdate] - Optional. The action to perform when a related row in the target table is updated.
|
|
343
|
+
* @param options - Additional options.
|
|
149
344
|
*/
|
|
150
|
-
declare function ForeignKey<TOther extends
|
|
345
|
+
declare function ForeignKey<TOther extends TableClassInterface>(toTable: Class<TOther>, toColumn: keyof TOther, options?: {
|
|
346
|
+
onDelete?: ForeignKeyActions;
|
|
347
|
+
onUpdate?: ForeignKeyActions;
|
|
348
|
+
}): (table: any, context: any) => void;
|
|
151
349
|
|
|
152
350
|
declare function prepareMigration(db: DatabaseAccess, dbInstructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
|
|
153
351
|
declare function runPreparedMigrations(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
|
|
@@ -155,4 +353,4 @@ declare function prepareAndRunMigration(db: DatabaseAccess, instructions: Databa
|
|
|
155
353
|
declare function runMigrationWithoutHistory(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
|
|
156
354
|
declare function rollback(db: DatabaseAccess, dbInstructions: DatabaseInstructions, toVersion: number): Promise<void>;
|
|
157
355
|
|
|
158
|
-
export { type DatabaseAccess, type DatabaseInstructions,
|
|
356
|
+
export { DataType, type DatabaseAccess, type DatabaseInstructions, ForeignKey, type PublicMigrations, type SqlChanges, TableClass, TableObj, prepareAndRunMigration, prepareMigration, rollback, runMigrationWithoutHistory, runPreparedMigrations };
|