@objectql/types 1.8.2 → 1.8.3

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 CHANGED
@@ -66,6 +66,17 @@ npm install @objectql/types
66
66
  - `Driver` - Database driver interface
67
67
  - `DriverConfig` - Driver configuration
68
68
 
69
+ ### Migration & Schema Evolution Types
70
+ - `SchemaChangeType` - Types of schema change operations
71
+ - `SchemaChangeInstruction` - Union of all schema change instructions
72
+ - `FieldUpdateInstruction` - Instruction to update/modify a field
73
+ - `FieldDeleteInstruction` - Instruction to delete/remove a field
74
+ - `ObjectUpdateInstruction` - Instruction to update/modify an object
75
+ - `ObjectDeleteInstruction` - Instruction to delete/remove an object
76
+ - `MigrationConfig` - Complete migration configuration
77
+ - `MigrationStep` - Single step in a migration
78
+ - `MigrationStatus` - Execution status of a migration
79
+
69
80
  ## Usage Examples
70
81
 
71
82
  ### Object Definition with Validation
@@ -253,6 +264,128 @@ interface FieldValidation {
253
264
  }
254
265
  ```
255
266
 
267
+ ### Migration & Schema Evolution
268
+
269
+ Define schema changes declaratively for object and field updates/deletions:
270
+
271
+ ```typescript
272
+ import {
273
+ MigrationConfig,
274
+ FieldUpdateInstruction,
275
+ FieldDeleteInstruction,
276
+ ObjectUpdateInstruction,
277
+ ObjectDeleteInstruction
278
+ } from '@objectql/types';
279
+
280
+ // Define a migration with multiple schema changes
281
+ const migration: MigrationConfig = {
282
+ id: 'v1.2_refactor_user_fields',
283
+ version: '1.2.0',
284
+ name: 'Refactor User Fields',
285
+ description: 'Update user object schema and remove deprecated fields',
286
+ author: 'dev-team',
287
+ created_at: '2026-01-14T00:00:00Z',
288
+ steps: [
289
+ {
290
+ id: 'rename_username_field',
291
+ name: 'Rename username to user_name',
292
+ instruction: {
293
+ type: 'field_update',
294
+ object_name: 'users',
295
+ field_name: 'username',
296
+ new_field_name: 'user_name',
297
+ changes: {
298
+ label: 'User Name',
299
+ description: 'Updated field name for consistency'
300
+ },
301
+ data_migration_strategy: 'auto'
302
+ } as FieldUpdateInstruction,
303
+ reversible: true
304
+ },
305
+ {
306
+ id: 'update_email_field',
307
+ name: 'Make email field required',
308
+ instruction: {
309
+ type: 'field_update',
310
+ object_name: 'users',
311
+ field_name: 'email',
312
+ changes: {
313
+ required: true,
314
+ unique: true
315
+ },
316
+ data_migration_strategy: 'auto'
317
+ } as FieldUpdateInstruction,
318
+ reversible: true
319
+ },
320
+ {
321
+ id: 'delete_legacy_field',
322
+ name: 'Remove deprecated legacy_id field',
323
+ instruction: {
324
+ type: 'field_delete',
325
+ object_name: 'users',
326
+ field_name: 'legacy_id',
327
+ deletion_strategy: 'archive',
328
+ archive_location: 'backup/users_legacy_id'
329
+ } as FieldDeleteInstruction,
330
+ reversible: true
331
+ },
332
+ {
333
+ id: 'update_object_label',
334
+ name: 'Update Users object label',
335
+ instruction: {
336
+ type: 'object_update',
337
+ object_name: 'users',
338
+ changes: {
339
+ label: 'System Users',
340
+ description: 'Updated to reflect new naming convention'
341
+ }
342
+ } as ObjectUpdateInstruction,
343
+ reversible: true
344
+ }
345
+ ],
346
+ reversible: true,
347
+ tags: ['schema', 'refactor']
348
+ };
349
+ ```
350
+
351
+ **Field Update Example (Type Change):**
352
+
353
+ ```typescript
354
+ const changeFieldType: FieldUpdateInstruction = {
355
+ type: 'field_update',
356
+ object_name: 'products',
357
+ field_name: 'price',
358
+ changes: {
359
+ type: 'currency', // Changed from 'number' to 'currency'
360
+ defaultValue: 0
361
+ },
362
+ data_migration_strategy: 'manual',
363
+ transform_script: `
364
+ // Custom transformation for price field
365
+ return {
366
+ amount: oldValue,
367
+ currency: 'USD'
368
+ };
369
+ `,
370
+ description: 'Convert price from number to currency type',
371
+ reason: 'Support multi-currency pricing'
372
+ };
373
+ ```
374
+
375
+ **Object Deletion Example:**
376
+
377
+ ```typescript
378
+ const deleteObject: ObjectDeleteInstruction = {
379
+ type: 'object_delete',
380
+ object_name: 'temp_imports',
381
+ deletion_strategy: 'archive',
382
+ archive_location: 'backups/temp_imports_archive',
383
+ cascade_strategy: 'nullify',
384
+ description: 'Remove temporary import table',
385
+ reason: 'No longer needed after migration to new import system'
386
+ };
387
+ ```
388
+
256
389
  ## See Also
257
390
 
258
391
  - [@objectql/core](../core) - Core engine with Validator class
package/dist/app.d.ts CHANGED
@@ -8,6 +8,7 @@ export interface IObjectQL {
8
8
  getConfigs(): Record<string, ObjectConfig>;
9
9
  datasource(name: string): Driver;
10
10
  init(): Promise<void>;
11
+ close?(): Promise<void>;
11
12
  removePackage(name: string): void;
12
13
  metadata: MetadataRegistry;
13
14
  registerObject(object: ObjectConfig): void;
package/dist/index.d.ts CHANGED
@@ -16,4 +16,5 @@ export * from './page';
16
16
  export * from './loader';
17
17
  export * from './application';
18
18
  export * from './menu';
19
+ export * from './migration';
19
20
  export * from './api';
package/dist/index.js CHANGED
@@ -32,5 +32,6 @@ __exportStar(require("./page"), exports);
32
32
  __exportStar(require("./loader"), exports);
33
33
  __exportStar(require("./application"), exports);
34
34
  __exportStar(require("./menu"), exports);
35
+ __exportStar(require("./migration"), exports);
35
36
  __exportStar(require("./api"), exports);
36
37
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,2CAAyB;AACzB,2CAAyB;AACzB,0CAAwB;AACxB,6CAA2B;AAC3B,yCAAuB;AACvB,2CAAyB;AACzB,+CAA6B;AAC7B,wCAAsB;AACtB,2CAAyB;AACzB,2CAAyB;AACzB,4CAA0B;AAC1B,+CAA6B;AAC7B,+CAA6B;AAC7B,yCAAuB;AACvB,2CAAyB;AACzB,gDAA8B;AAC9B,yCAAuB;AACvB,wCAAsB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,2CAAyB;AACzB,2CAAyB;AACzB,0CAAwB;AACxB,6CAA2B;AAC3B,yCAAuB;AACvB,2CAAyB;AACzB,+CAA6B;AAC7B,wCAAsB;AACtB,2CAAyB;AACzB,2CAAyB;AACzB,4CAA0B;AAC1B,+CAA6B;AAC7B,+CAA6B;AAC7B,yCAAuB;AACvB,2CAAyB;AACzB,gDAA8B;AAC9B,yCAAuB;AACvB,8CAA4B;AAC5B,wCAAsB"}
@@ -0,0 +1,327 @@
1
+ import { FieldConfig } from './field';
2
+ /**
3
+ * Represents the type of schema change operation.
4
+ */
5
+ export type SchemaChangeType = 'field_update' | 'field_delete' | 'object_update' | 'object_delete';
6
+ /**
7
+ * Base interface for all schema change instructions.
8
+ */
9
+ export interface BaseSchemaChangeInstruction {
10
+ /** Type of schema change operation */
11
+ type: SchemaChangeType;
12
+ /** Human-readable description of the change */
13
+ description?: string;
14
+ /** Reason for the change (for audit trail) */
15
+ reason?: string;
16
+ /** Timestamp when the change was defined (ISO 8601) */
17
+ timestamp?: string;
18
+ /** Author of the change (username or ID) */
19
+ author?: string;
20
+ }
21
+ /**
22
+ * Database impact assessment for a field change.
23
+ * Describes how the change affects the underlying database schema.
24
+ */
25
+ export interface DatabaseImpact {
26
+ /**
27
+ * Type of database operation required.
28
+ * - 'alter_column': Modify column properties (type, constraints, default)
29
+ * - 'rename_column': Rename a column
30
+ * - 'drop_column': Remove a column
31
+ * - 'add_column': Add a new column
32
+ * - 'rebuild_table': Requires full table rebuild (complex changes)
33
+ * - 'no_change': Schema-level only, no database changes
34
+ */
35
+ operation: 'alter_column' | 'rename_column' | 'drop_column' | 'add_column' | 'rebuild_table' | 'no_change';
36
+ /**
37
+ * Whether this change requires data migration.
38
+ * If true, existing records must be updated.
39
+ */
40
+ requires_data_migration: boolean;
41
+ /**
42
+ * Whether this change may cause data loss.
43
+ * Examples: type narrowing, dropping columns, shortening text length
44
+ */
45
+ may_cause_data_loss: boolean;
46
+ /**
47
+ * Estimated risk level for this change.
48
+ * - 'low': Safe, reversible change (e.g., adding nullable field)
49
+ * - 'medium': May affect queries or require downtime (e.g., adding index)
50
+ * - 'high': Requires careful planning (e.g., type change with data migration)
51
+ * - 'critical': May cause data loss or extended downtime (e.g., dropping column)
52
+ */
53
+ risk_level?: 'low' | 'medium' | 'high' | 'critical';
54
+ /**
55
+ * Expected downtime for this change.
56
+ * Format: ISO 8601 duration (e.g., 'PT5M' for 5 minutes, 'PT2H' for 2 hours)
57
+ */
58
+ estimated_downtime?: string;
59
+ /**
60
+ * Notes about the database impact for human review.
61
+ */
62
+ notes?: string;
63
+ }
64
+ /**
65
+ * Database upgrade script information.
66
+ * Contains the generated DDL/DML statements for applying the migration.
67
+ */
68
+ export interface UpgradeScript {
69
+ /**
70
+ * Target database dialect.
71
+ * Examples: 'postgresql', 'mysql', 'sqlite', 'mongodb', 'mssql'
72
+ */
73
+ dialect: string;
74
+ /**
75
+ * SQL or database-specific statements to apply the change (forward migration).
76
+ * For SQL: DDL statements (ALTER TABLE, CREATE INDEX, etc.)
77
+ * For MongoDB: Update operations
78
+ */
79
+ up_statements: string[];
80
+ /**
81
+ * SQL or database-specific statements to rollback the change (reverse migration).
82
+ * Only present if the change is reversible.
83
+ */
84
+ down_statements?: string[];
85
+ /**
86
+ * Pre-migration checks or validations to run before applying changes.
87
+ * Examples: Check for data integrity, verify no duplicate values before adding UNIQUE constraint
88
+ */
89
+ pre_checks?: string[];
90
+ /**
91
+ * Post-migration validations to ensure the change was successful.
92
+ * Examples: Verify column exists, check data was migrated correctly
93
+ */
94
+ post_checks?: string[];
95
+ /**
96
+ * Estimated execution time for this script.
97
+ * Format: ISO 8601 duration
98
+ */
99
+ estimated_execution_time?: string;
100
+ }
101
+ /**
102
+ * Instruction to update (modify) a field in an object.
103
+ * Can rename, change type, or update properties of an existing field.
104
+ */
105
+ export interface FieldUpdateInstruction extends BaseSchemaChangeInstruction {
106
+ type: 'field_update';
107
+ /** Name of the object containing the field */
108
+ object_name: string;
109
+ /** Current name of the field to update */
110
+ field_name: string;
111
+ /** New name for the field (if renaming) */
112
+ new_field_name?: string;
113
+ /** Updated field configuration (partial) */
114
+ changes: Partial<FieldConfig>;
115
+ /**
116
+ * Strategy for handling existing data during type changes.
117
+ * - 'auto': Attempt automatic conversion
118
+ * - 'manual': Requires custom data migration script
119
+ * - 'preserve': Keep data as-is (may cause validation errors)
120
+ * - 'clear': Set field to null/default for all existing records
121
+ */
122
+ data_migration_strategy?: 'auto' | 'manual' | 'preserve' | 'clear';
123
+ /**
124
+ * Custom data transformation function (for manual strategy).
125
+ * Should be a valid JavaScript expression or function body.
126
+ */
127
+ transform_script?: string;
128
+ /**
129
+ * Database impact assessment for this field change.
130
+ * Describes the effect on the underlying database schema.
131
+ */
132
+ database_impact?: DatabaseImpact;
133
+ /**
134
+ * Generated upgrade scripts for different database dialects.
135
+ * Maps dialect name to upgrade script.
136
+ * Example: { postgresql: {...}, mysql: {...} }
137
+ */
138
+ upgrade_scripts?: Record<string, UpgradeScript>;
139
+ }
140
+ /**
141
+ * Instruction to delete (remove) a field from an object.
142
+ */
143
+ export interface FieldDeleteInstruction extends BaseSchemaChangeInstruction {
144
+ type: 'field_delete';
145
+ /** Name of the object containing the field */
146
+ object_name: string;
147
+ /** Name of the field to delete */
148
+ field_name: string;
149
+ /**
150
+ * Strategy for handling existing data in the field.
151
+ * - 'drop': Remove the field and its data (irreversible)
152
+ * - 'archive': Move data to an archive/backup location
153
+ * - 'soft': Mark as deleted but keep data (for rollback)
154
+ */
155
+ deletion_strategy?: 'drop' | 'archive' | 'soft';
156
+ /** Backup location if using 'archive' strategy */
157
+ archive_location?: string;
158
+ /**
159
+ * Database impact assessment for this field deletion.
160
+ * Describes the effect on the underlying database schema.
161
+ */
162
+ database_impact?: DatabaseImpact;
163
+ /**
164
+ * Generated upgrade scripts for different database dialects.
165
+ * Maps dialect name to upgrade script.
166
+ * Example: { postgresql: {...}, mysql: {...} }
167
+ */
168
+ upgrade_scripts?: Record<string, UpgradeScript>;
169
+ }
170
+ /**
171
+ * Configuration changes that can be applied to an object.
172
+ * Used by ObjectUpdateInstruction to modify object metadata.
173
+ */
174
+ export interface ObjectUpdateChanges {
175
+ /** Updated human-readable label */
176
+ label?: string;
177
+ /** Updated icon string */
178
+ icon?: string;
179
+ /** Updated description */
180
+ description?: string;
181
+ /** Updated datasource name */
182
+ datasource?: string;
183
+ }
184
+ /**
185
+ * Instruction to update (modify) an object definition.
186
+ * Can rename or change properties of an existing object.
187
+ */
188
+ export interface ObjectUpdateInstruction extends BaseSchemaChangeInstruction {
189
+ type: 'object_update';
190
+ /** Current name of the object to update */
191
+ object_name: string;
192
+ /** New name for the object (if renaming) */
193
+ new_object_name?: string;
194
+ /** Updated properties (label, description, icon, etc.) */
195
+ changes: ObjectUpdateChanges;
196
+ /**
197
+ * Database impact assessment for this object change.
198
+ * Describes the effect on the underlying database schema.
199
+ */
200
+ database_impact?: DatabaseImpact;
201
+ /**
202
+ * Generated upgrade scripts for different database dialects.
203
+ * Maps dialect name to upgrade script.
204
+ * Example: { postgresql: {...}, mysql: {...} }
205
+ */
206
+ upgrade_scripts?: Record<string, UpgradeScript>;
207
+ }
208
+ /**
209
+ * Instruction to delete (remove) an entire object.
210
+ */
211
+ export interface ObjectDeleteInstruction extends BaseSchemaChangeInstruction {
212
+ type: 'object_delete';
213
+ /** Name of the object to delete */
214
+ object_name: string;
215
+ /**
216
+ * Strategy for handling existing data in the object.
217
+ * - 'drop': Remove the object and all its data (irreversible)
218
+ * - 'archive': Move all data to an archive/backup location
219
+ * - 'soft': Mark as deleted but keep data (for rollback)
220
+ */
221
+ deletion_strategy?: 'drop' | 'archive' | 'soft';
222
+ /** Backup location if using 'archive' strategy */
223
+ archive_location?: string;
224
+ /**
225
+ * Handle dependent objects/relationships.
226
+ * - 'cascade': Also delete related records in other objects
227
+ * - 'fail': Fail if there are dependent records
228
+ * - 'nullify': Set foreign key references to null
229
+ */
230
+ cascade_strategy?: 'cascade' | 'fail' | 'nullify';
231
+ /**
232
+ * Database impact assessment for this object deletion.
233
+ * Describes the effect on the underlying database schema.
234
+ */
235
+ database_impact?: DatabaseImpact;
236
+ /**
237
+ * Generated upgrade scripts for different database dialects.
238
+ * Maps dialect name to upgrade script.
239
+ * Example: { postgresql: {...}, mysql: {...} }
240
+ */
241
+ upgrade_scripts?: Record<string, UpgradeScript>;
242
+ }
243
+ /**
244
+ * Union type for all schema change instructions.
245
+ */
246
+ export type SchemaChangeInstruction = FieldUpdateInstruction | FieldDeleteInstruction | ObjectUpdateInstruction | ObjectDeleteInstruction;
247
+ /**
248
+ * Represents a single migration step in a migration sequence.
249
+ */
250
+ export interface MigrationStep {
251
+ /** Unique identifier for this step */
252
+ id: string;
253
+ /** Human-readable name for this step */
254
+ name: string;
255
+ /** Description of what this step does */
256
+ description?: string;
257
+ /** Schema change instruction to execute */
258
+ instruction: SchemaChangeInstruction;
259
+ /**
260
+ * Whether this step can be automatically rolled back.
261
+ * Default: true
262
+ */
263
+ reversible?: boolean;
264
+ /**
265
+ * Optional rollback instruction (if different from automatic inverse).
266
+ */
267
+ rollback_instruction?: SchemaChangeInstruction;
268
+ /**
269
+ * Dependencies on other migration steps (by step ID).
270
+ * This step will only execute after dependencies are complete.
271
+ */
272
+ depends_on?: string[];
273
+ }
274
+ /**
275
+ * Configuration for a complete migration.
276
+ * Groups multiple schema changes into a versioned migration.
277
+ */
278
+ export interface MigrationConfig {
279
+ /** Unique identifier for this migration (e.g., 'v1.0_add_user_fields') */
280
+ id: string;
281
+ /** Semantic version number (e.g., '1.2.0') */
282
+ version: string;
283
+ /** Human-readable name for this migration */
284
+ name: string;
285
+ /** Detailed description of the migration purpose */
286
+ description?: string;
287
+ /** Author of the migration */
288
+ author?: string;
289
+ /** Timestamp when the migration was created (ISO 8601) */
290
+ created_at?: string;
291
+ /** Ordered list of migration steps to execute */
292
+ steps: MigrationStep[];
293
+ /**
294
+ * Whether this migration can be automatically rolled back.
295
+ * Default: true if all steps are reversible
296
+ */
297
+ reversible?: boolean;
298
+ /**
299
+ * Dependencies on other migrations (by migration ID).
300
+ * This migration will only run after dependencies are applied.
301
+ */
302
+ depends_on?: string[];
303
+ /**
304
+ * Tags for categorization and filtering.
305
+ * Examples: ['schema', 'data', 'hotfix', 'feature']
306
+ */
307
+ tags?: string[];
308
+ }
309
+ /**
310
+ * Represents the execution status of a migration.
311
+ */
312
+ export interface MigrationStatus {
313
+ /** Migration ID */
314
+ migration_id: string;
315
+ /** Execution status */
316
+ status: 'pending' | 'running' | 'completed' | 'failed' | 'rolled_back';
317
+ /** Timestamp when execution started */
318
+ started_at?: string;
319
+ /** Timestamp when execution completed */
320
+ completed_at?: string;
321
+ /** Error message if failed */
322
+ error?: string;
323
+ /** Number of steps completed */
324
+ steps_completed?: number;
325
+ /** Total number of steps */
326
+ steps_total?: number;
327
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=migration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migration.js","sourceRoot":"","sources":["../src/migration.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@objectql/types",
3
- "version": "1.8.2",
3
+ "version": "1.8.3",
4
4
  "description": "Pure TypeScript type definitions and interfaces for the ObjectQL protocol - The Contract",
5
5
  "keywords": [
6
6
  "objectql",