sqlmorpheus 0.2.0 → 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
 
@@ -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;
@@ -63,29 +62,32 @@ interface TableStructure {
63
62
  }
64
63
 
65
64
  declare class Migrations {
66
- private toVersion;
67
- private alwaysAllowed;
68
65
  private fromVersion;
66
+ private toVersion;
67
+ private lastUsedVersion;
69
68
  private migrationData;
70
69
  private migrationDataForOldTableNames;
70
+ private alwaysAllowed;
71
71
  private notAllowedChanges;
72
72
  reset(dbInstructions: DatabaseInstructions, fromVersion: number): void;
73
73
  private getEntry;
74
+ private getEntryFromTableName;
74
75
  private getTableName;
76
+ private versionIsRelevant;
75
77
  verifyRenamingTasks(newTables: Record<string, TableStructure>): Error | void;
76
78
  verifyAllowedMigrations(): Error | void;
77
79
  compareWithAllowedMigration(tableName: string, type: keyof AllowedMigrations): void;
78
- allowMigration(version: number, table: string | Class<BackendTable>, ...allowedMigrations: (keyof AllowedMigrations)[]): void;
79
- renameTable(version: number, oldTableName: string, newTableName: string | Class<BackendTable>): void;
80
- renameColumn(version: number, table: string | Class<BackendTable>, oldColumn: string, newColumn: string): void;
81
- recreateTableImp(table: Class<BackendTable> | string): void;
82
- recreateTable(version: number, table: Class<BackendTable> | string): void;
83
80
  getMigrationData(): Record<string, MigrationInstructions>;
84
81
  loopRenamedColumns(tableName: string, callback: (oldColumnName: string, newColumnName: string) => void): void;
85
82
  getNewestTableName(tableName: string): string;
86
83
  getOldTableName(tableName: string): string;
87
84
  getNewestColumnName(tableName: string, oldColumnName: string): string;
88
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;
89
91
  }
90
92
  type PublicMigrations = Pick<Migrations, "recreateTable" | "renameTable" | "renameColumn" | "allowMigration">;
91
93
 
@@ -94,19 +96,45 @@ interface SqlChanges {
94
96
  down: string;
95
97
  }
96
98
 
99
+ type NativeDataTypeOptions = "boolean" | "number" | "bigint" | "string";
100
+ type AddedDataTypeOptions = "date" | "time" | "dateTime";
101
+ type DataTypeOptions = NativeDataTypeOptions | AddedDataTypeOptions;
102
+
97
103
  interface TableInfo {
98
104
  primaryKey?: string;
99
105
  foreignKeys?: ForeignKeyInfo[];
106
+ dataTypes?: Record<string, DataTypeOptions>;
100
107
  }
101
108
 
102
- type TableObjects = Record<string, {
103
- columns: BackendTable;
104
- tableInfo?: TableInfo;
105
- }>;
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
+ }
106
133
 
134
+ type TableInput = TableObjInput | Class<TableClassInterface>;
107
135
  interface DatabaseInstructions {
108
136
  dialect: "Sqlite" | "MsSql" | "MySql" | "Postgres";
109
- tables: Class<BackendTable>[] | TableObjects;
137
+ tables: TableInput[];
110
138
  version: number;
111
139
  configPath: string;
112
140
  throwIfNotAllowed: boolean;
@@ -130,24 +158,33 @@ interface DatabaseInstructions {
130
158
  }
131
159
 
132
160
  /**
133
- * Decorator function to configure a database table with its name and primary key
134
- * and to add missing "fromTable" properties to foreign keys.
161
+ * Decorator function to configure a database table with its name and primary key.
135
162
  *
136
163
  * @param tableName - The name of the database table used in SQL.
137
164
  * @param primaryKey - The property name representing the primary property of the table.
138
165
  */
139
- 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;
140
167
 
141
168
  /**
142
- * Decorator function to define a foreign key constraint for a database table. It defines a relationship
143
- * between a column on the current table and a column on another table.
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;
176
+
177
+ /**
178
+ * Decorator function to define a foreign key relationship for a database table.
144
179
  *
145
180
  * @param toTable - The target table class to which the foreign key points.
146
181
  * @param toColumn - The column name in the target table to which the foreign key points.
147
- * @param [onDelete] - Optional. The action to perform when a row in the target table is deleted.
148
- * @param [onUpdate] - Optional. The action to perform when a related row in the target table is updated.
182
+ * @param options - Additional options.
149
183
  */
150
- 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;
151
188
 
152
189
  declare function prepareMigration(db: DatabaseAccess, dbInstructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
153
190
  declare function runPreparedMigrations(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
@@ -155,4 +192,4 @@ declare function prepareAndRunMigration(db: DatabaseAccess, instructions: Databa
155
192
  declare function runMigrationWithoutHistory(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
156
193
  declare function rollback(db: DatabaseAccess, dbInstructions: DatabaseInstructions, toVersion: number): Promise<void>;
157
194
 
158
- 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
 
@@ -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;
@@ -63,29 +62,32 @@ interface TableStructure {
63
62
  }
64
63
 
65
64
  declare class Migrations {
66
- private toVersion;
67
- private alwaysAllowed;
68
65
  private fromVersion;
66
+ private toVersion;
67
+ private lastUsedVersion;
69
68
  private migrationData;
70
69
  private migrationDataForOldTableNames;
70
+ private alwaysAllowed;
71
71
  private notAllowedChanges;
72
72
  reset(dbInstructions: DatabaseInstructions, fromVersion: number): void;
73
73
  private getEntry;
74
+ private getEntryFromTableName;
74
75
  private getTableName;
76
+ private versionIsRelevant;
75
77
  verifyRenamingTasks(newTables: Record<string, TableStructure>): Error | void;
76
78
  verifyAllowedMigrations(): Error | void;
77
79
  compareWithAllowedMigration(tableName: string, type: keyof AllowedMigrations): void;
78
- allowMigration(version: number, table: string | Class<BackendTable>, ...allowedMigrations: (keyof AllowedMigrations)[]): void;
79
- renameTable(version: number, oldTableName: string, newTableName: string | Class<BackendTable>): void;
80
- renameColumn(version: number, table: string | Class<BackendTable>, oldColumn: string, newColumn: string): void;
81
- recreateTableImp(table: Class<BackendTable> | string): void;
82
- recreateTable(version: number, table: Class<BackendTable> | string): void;
83
80
  getMigrationData(): Record<string, MigrationInstructions>;
84
81
  loopRenamedColumns(tableName: string, callback: (oldColumnName: string, newColumnName: string) => void): void;
85
82
  getNewestTableName(tableName: string): string;
86
83
  getOldTableName(tableName: string): string;
87
84
  getNewestColumnName(tableName: string, oldColumnName: string): string;
88
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;
89
91
  }
90
92
  type PublicMigrations = Pick<Migrations, "recreateTable" | "renameTable" | "renameColumn" | "allowMigration">;
91
93
 
@@ -94,19 +96,45 @@ interface SqlChanges {
94
96
  down: string;
95
97
  }
96
98
 
99
+ type NativeDataTypeOptions = "boolean" | "number" | "bigint" | "string";
100
+ type AddedDataTypeOptions = "date" | "time" | "dateTime";
101
+ type DataTypeOptions = NativeDataTypeOptions | AddedDataTypeOptions;
102
+
97
103
  interface TableInfo {
98
104
  primaryKey?: string;
99
105
  foreignKeys?: ForeignKeyInfo[];
106
+ dataTypes?: Record<string, DataTypeOptions>;
100
107
  }
101
108
 
102
- type TableObjects = Record<string, {
103
- columns: BackendTable;
104
- tableInfo?: TableInfo;
105
- }>;
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
+ }
106
133
 
134
+ type TableInput = TableObjInput | Class<TableClassInterface>;
107
135
  interface DatabaseInstructions {
108
136
  dialect: "Sqlite" | "MsSql" | "MySql" | "Postgres";
109
- tables: Class<BackendTable>[] | TableObjects;
137
+ tables: TableInput[];
110
138
  version: number;
111
139
  configPath: string;
112
140
  throwIfNotAllowed: boolean;
@@ -130,24 +158,33 @@ interface DatabaseInstructions {
130
158
  }
131
159
 
132
160
  /**
133
- * Decorator function to configure a database table with its name and primary key
134
- * and to add missing "fromTable" properties to foreign keys.
161
+ * Decorator function to configure a database table with its name and primary key.
135
162
  *
136
163
  * @param tableName - The name of the database table used in SQL.
137
164
  * @param primaryKey - The property name representing the primary property of the table.
138
165
  */
139
- 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;
140
167
 
141
168
  /**
142
- * Decorator function to define a foreign key constraint for a database table. It defines a relationship
143
- * between a column on the current table and a column on another table.
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;
176
+
177
+ /**
178
+ * Decorator function to define a foreign key relationship for a database table.
144
179
  *
145
180
  * @param toTable - The target table class to which the foreign key points.
146
181
  * @param toColumn - The column name in the target table to which the foreign key points.
147
- * @param [onDelete] - Optional. The action to perform when a row in the target table is deleted.
148
- * @param [onUpdate] - Optional. The action to perform when a related row in the target table is updated.
182
+ * @param options - Additional options.
149
183
  */
150
- 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;
151
188
 
152
189
  declare function prepareMigration(db: DatabaseAccess, dbInstructions: DatabaseInstructions, overwriteExisting?: boolean): Promise<void>;
153
190
  declare function runPreparedMigrations(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
@@ -155,4 +192,4 @@ declare function prepareAndRunMigration(db: DatabaseAccess, instructions: Databa
155
192
  declare function runMigrationWithoutHistory(db: DatabaseAccess, dbInstructions: DatabaseInstructions): Promise<void>;
156
193
  declare function rollback(db: DatabaseAccess, dbInstructions: DatabaseInstructions, toVersion: number): Promise<void>;
157
194
 
158
- 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 };