@twin.org/entity-storage-models 0.0.3-next.2 → 0.0.3-next.20
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 +2 -2
- package/dist/es/helpers/entityStorageHelper.js +85 -0
- package/dist/es/helpers/entityStorageHelper.js.map +1 -0
- package/dist/es/helpers/migrationHelper.js +220 -0
- package/dist/es/helpers/migrationHelper.js.map +1 -0
- package/dist/es/index.js +9 -0
- package/dist/es/index.js.map +1 -1
- package/dist/es/models/IEntityStorageComponent.js.map +1 -1
- package/dist/es/models/IEntityStorageConnector.js.map +1 -1
- package/dist/es/models/IEntityStorageMigrationConnector.js +2 -0
- package/dist/es/models/IEntityStorageMigrationConnector.js.map +1 -0
- package/dist/es/models/IMigrationOptions.js +4 -0
- package/dist/es/models/IMigrationOptions.js.map +1 -0
- package/dist/es/models/api/IEntityStorageCountRequest.js +4 -0
- package/dist/es/models/api/IEntityStorageCountRequest.js.map +1 -0
- package/dist/es/models/api/IEntityStorageCountResponse.js +4 -0
- package/dist/es/models/api/IEntityStorageCountResponse.js.map +1 -0
- package/dist/es/models/api/IEntityStorageEmptyRequest.js +4 -0
- package/dist/es/models/api/IEntityStorageEmptyRequest.js.map +1 -0
- package/dist/es/models/api/IEntityStorageGetRequest.js.map +1 -1
- package/dist/es/models/api/IEntityStorageRemoveBatchRequest.js +4 -0
- package/dist/es/models/api/IEntityStorageRemoveBatchRequest.js.map +1 -0
- package/dist/es/models/api/IEntityStorageRemoveRequest.js.map +1 -1
- package/dist/es/models/api/IEntityStorageSetBatchRequest.js +4 -0
- package/dist/es/models/api/IEntityStorageSetBatchRequest.js.map +1 -0
- package/dist/es/models/api/IEntityStorageSetRequest.js.map +1 -1
- package/dist/types/helpers/entityStorageHelper.d.ts +40 -0
- package/dist/types/helpers/migrationHelper.d.ts +61 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/models/IEntityStorageComponent.d.ts +38 -3
- package/dist/types/models/IEntityStorageConnector.d.ts +23 -0
- package/dist/types/models/IEntityStorageMigrationConnector.d.ts +35 -0
- package/dist/types/models/IMigrationOptions.d.ts +32 -0
- package/dist/types/models/api/IEntityStorageCountRequest.d.ts +14 -0
- package/dist/types/models/api/IEntityStorageCountResponse.d.ts +14 -0
- package/dist/types/models/api/IEntityStorageEmptyRequest.d.ts +5 -0
- package/dist/types/models/api/IEntityStorageGetRequest.d.ts +4 -0
- package/dist/types/models/api/IEntityStorageRemoveBatchRequest.d.ts +9 -0
- package/dist/types/models/api/IEntityStorageRemoveRequest.d.ts +9 -0
- package/dist/types/models/api/IEntityStorageSetBatchRequest.d.ts +9 -0
- package/dist/types/models/api/IEntityStorageSetRequest.d.ts +9 -0
- package/docs/changelog.md +217 -43
- package/docs/examples.md +87 -1
- package/docs/reference/classes/EntityStorageHelper.md +135 -0
- package/docs/reference/classes/MigrationHelper.md +247 -0
- package/docs/reference/index.md +12 -0
- package/docs/reference/interfaces/IEntityStorageComponent.md +105 -7
- package/docs/reference/interfaces/IEntityStorageConnector.md +89 -5
- package/docs/reference/interfaces/IEntityStorageCountRequest.md +17 -0
- package/docs/reference/interfaces/IEntityStorageCountResponse.md +17 -0
- package/docs/reference/interfaces/IEntityStorageEmptyRequest.md +3 -0
- package/docs/reference/interfaces/IEntityStorageGetRequest.md +10 -4
- package/docs/reference/interfaces/IEntityStorageGetResponse.md +1 -1
- package/docs/reference/interfaces/IEntityStorageListRequest.md +8 -8
- package/docs/reference/interfaces/IEntityStorageListResponse.md +2 -2
- package/docs/reference/interfaces/IEntityStorageMigrationConnector.md +402 -0
- package/docs/reference/interfaces/IEntityStorageRemoveBatchRequest.md +11 -0
- package/docs/reference/interfaces/IEntityStorageRemoveRequest.md +15 -1
- package/docs/reference/interfaces/IEntityStorageSetBatchRequest.md +11 -0
- package/docs/reference/interfaces/IEntityStorageSetRequest.md +15 -1
- package/docs/reference/interfaces/IMigrationOptions.md +119 -0
- package/locales/en.json +7 -1
- package/package.json +5 -4
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type IContextIds } from "@twin.org/context";
|
|
2
|
+
import { type IEntitySchemaDiff } from "@twin.org/entity";
|
|
3
|
+
import type { IEntityStorageConnector } from "../models/IEntityStorageConnector.js";
|
|
4
|
+
import type { IEntityStorageMigrationConnector } from "../models/IEntityStorageMigrationConnector.js";
|
|
5
|
+
import type { IMigrationOptions } from "../models/IMigrationOptions.js";
|
|
6
|
+
/**
|
|
7
|
+
* Helper class for performing schema migrations between two connectors.
|
|
8
|
+
*/
|
|
9
|
+
export declare class MigrationHelper {
|
|
10
|
+
/**
|
|
11
|
+
* Runtime name for the class.
|
|
12
|
+
*/
|
|
13
|
+
static readonly CLASS_NAME: string;
|
|
14
|
+
/**
|
|
15
|
+
* Performs a migration between two connectors, using the provided options and schema diff to control the migration behaviour.
|
|
16
|
+
* @param sourceConnector The connector to migrate from to allow the migration helper to create the new connector and finalize the migration.
|
|
17
|
+
* @param targetEntitySchemaName The name of the new entity schema.
|
|
18
|
+
* @param renames An optional list of property renames to apply during migration.
|
|
19
|
+
* @param options Options controlling migration behaviour.
|
|
20
|
+
* @param loggingComponentType An optional logging component type to use for bootstrapping and starting connectors if necessary.
|
|
21
|
+
* @returns The connector for the new schema and the number of entities successfully migrated, the sourceConnector will no longer be usable, finalConnector will be undefined if no migration was necessary.
|
|
22
|
+
*/
|
|
23
|
+
static migrate<T, U>(sourceConnector: IEntityStorageMigrationConnector<T>, targetEntitySchemaName: string, renames?: {
|
|
24
|
+
from: string;
|
|
25
|
+
to: string;
|
|
26
|
+
}[], options?: IMigrationOptions<T, U>, loggingComponentType?: string): Promise<{
|
|
27
|
+
finalConnector?: IEntityStorageConnector<U>;
|
|
28
|
+
migrated: number;
|
|
29
|
+
}>;
|
|
30
|
+
/**
|
|
31
|
+
* Generic per-partition migration loop.
|
|
32
|
+
* @param source Connector to read from (current schema, already bootstrapped).
|
|
33
|
+
* @param target Connector to write to (new schema, already bootstrapped).
|
|
34
|
+
* @param partitionContextIds The context ids to use for the migration, used for partitioning and can be used in the transform function when `options.transformEntityProperty` is provided.
|
|
35
|
+
* @param schemaDiff The schema diff.
|
|
36
|
+
* @param options Optional migration controls (batchSize, transformEntity, onProgress).
|
|
37
|
+
* @returns The number of entities successfully migrated.
|
|
38
|
+
*/
|
|
39
|
+
static migrateEntities<T = unknown, U = T>(source: IEntityStorageMigrationConnector<T>, target: IEntityStorageConnector<U>, partitionContextIds: IContextIds[], schemaDiff: IEntitySchemaDiff<T, U>, options?: IMigrationOptions<T, U>): Promise<number>;
|
|
40
|
+
/**
|
|
41
|
+
* Generic per-partition migration loop.
|
|
42
|
+
* @param source Connector to read from (current schema, already bootstrapped).
|
|
43
|
+
* @param target Connector to write to (new schema, already bootstrapped).
|
|
44
|
+
* @param partitionTotal The total number of partitions to migrate, used for progress reporting.
|
|
45
|
+
* @param partitionIndex The index of the current partition being migrated, used for progress reporting.
|
|
46
|
+
* @param schemaDiff Schema diff used to add nullable defaults and drop removed fields when `options.transformEntity` is not provided.
|
|
47
|
+
* @param options Optional migration controls (batchSize, transformEntity, onProgress).
|
|
48
|
+
* @returns The number of entities successfully migrated.
|
|
49
|
+
*/
|
|
50
|
+
static migratePartition<T = unknown, U = unknown>(source: IEntityStorageConnector<T>, target: IEntityStorageConnector<U>, partitionTotal: number, partitionIndex: number, schemaDiff: IEntitySchemaDiff<T, U>, options?: IMigrationOptions<T, U>): Promise<number>;
|
|
51
|
+
/**
|
|
52
|
+
* Applies the entity transformation for migration, using the provided options and schema diff.
|
|
53
|
+
* @param entity The entity to transform.
|
|
54
|
+
* @param schemaDiff The schema diff between the old and new schemas.
|
|
55
|
+
* @param options The migration options.
|
|
56
|
+
* @returns The transformed entity ready to be written to the new schema.
|
|
57
|
+
* @throws GeneralError if a transformation is required for an object or array property but no `options.transformEntityProperty` function is provided.
|
|
58
|
+
* @throws GeneralError if coercion of a modified property results in undefined for a non-optional target property.
|
|
59
|
+
*/
|
|
60
|
+
static applyEntityTransform<T = unknown, U = unknown>(entity: Partial<T>, schemaDiff: IEntitySchemaDiff<T, U>, options?: IMigrationOptions<T, U>): U;
|
|
61
|
+
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
export * from "./factories/entityStorageConnectorFactory.js";
|
|
2
|
+
export * from "./helpers/entityStorageHelper.js";
|
|
3
|
+
export * from "./helpers/migrationHelper.js";
|
|
4
|
+
export * from "./models/api/IEntityStorageCountRequest.js";
|
|
5
|
+
export * from "./models/api/IEntityStorageCountResponse.js";
|
|
6
|
+
export * from "./models/api/IEntityStorageEmptyRequest.js";
|
|
2
7
|
export * from "./models/api/IEntityStorageGetRequest.js";
|
|
3
8
|
export * from "./models/api/IEntityStorageGetResponse.js";
|
|
4
9
|
export * from "./models/api/IEntityStorageListRequest.js";
|
|
5
10
|
export * from "./models/api/IEntityStorageListResponse.js";
|
|
11
|
+
export * from "./models/api/IEntityStorageRemoveBatchRequest.js";
|
|
6
12
|
export * from "./models/api/IEntityStorageRemoveRequest.js";
|
|
13
|
+
export * from "./models/api/IEntityStorageSetBatchRequest.js";
|
|
7
14
|
export * from "./models/api/IEntityStorageSetRequest.js";
|
|
8
15
|
export * from "./models/IEntityStorageComponent.js";
|
|
9
16
|
export * from "./models/IEntityStorageConnector.js";
|
|
17
|
+
export * from "./models/IEntityStorageMigrationConnector.js";
|
|
18
|
+
export * from "./models/IMigrationOptions.js";
|
|
@@ -7,22 +7,46 @@ export interface IEntityStorageComponent<T = unknown> extends IComponent {
|
|
|
7
7
|
/**
|
|
8
8
|
* Set an entity.
|
|
9
9
|
* @param entity The entity to set.
|
|
10
|
+
* @param conditions The optional conditions to match for the entities.
|
|
10
11
|
* @returns The id of the entity.
|
|
11
12
|
*/
|
|
12
|
-
set(entity: T
|
|
13
|
+
set(entity: T, conditions?: {
|
|
14
|
+
property: keyof T;
|
|
15
|
+
value: unknown;
|
|
16
|
+
}[]): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Set multiple entities in a batch.
|
|
19
|
+
* @param entities The entities to set.
|
|
20
|
+
* @returns Nothing.
|
|
21
|
+
*/
|
|
22
|
+
setBatch(entities: T[]): Promise<void>;
|
|
13
23
|
/**
|
|
14
24
|
* Get an entity.
|
|
15
25
|
* @param id The id of the entity to get, or the index value if secondaryIndex is set.
|
|
16
26
|
* @param secondaryIndex Get the item using a secondary index.
|
|
27
|
+
* @param conditions The optional conditions to match for the entities.
|
|
17
28
|
* @returns The object if it can be found or undefined.
|
|
18
29
|
*/
|
|
19
|
-
get(id: string, secondaryIndex?: keyof T
|
|
30
|
+
get(id: string, secondaryIndex?: keyof T, conditions?: {
|
|
31
|
+
property: keyof T;
|
|
32
|
+
value: unknown;
|
|
33
|
+
}[]): Promise<T | undefined>;
|
|
20
34
|
/**
|
|
21
35
|
* Remove the entity.
|
|
22
36
|
* @param id The id of the entity to remove.
|
|
37
|
+
* @param conditions The optional conditions to match for the entities.
|
|
38
|
+
* @returns Nothing.
|
|
39
|
+
*/
|
|
40
|
+
remove(id: string, conditions?: {
|
|
41
|
+
property: keyof T;
|
|
42
|
+
value: unknown;
|
|
43
|
+
}[]): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Remove multiple entities by id.
|
|
46
|
+
* @param ids The ids of the entities to remove.
|
|
23
47
|
* @returns Nothing.
|
|
24
48
|
*/
|
|
25
|
-
|
|
49
|
+
removeBatch(ids: string[]): Promise<void>;
|
|
26
50
|
/**
|
|
27
51
|
* Query all the entities which match the conditions.
|
|
28
52
|
* @param conditions The conditions to match for the entities.
|
|
@@ -44,4 +68,15 @@ export interface IEntityStorageComponent<T = unknown> extends IComponent {
|
|
|
44
68
|
*/
|
|
45
69
|
cursor?: string;
|
|
46
70
|
}>;
|
|
71
|
+
/**
|
|
72
|
+
* Remove all entities from the storage.
|
|
73
|
+
* @returns Nothing.
|
|
74
|
+
*/
|
|
75
|
+
empty(): Promise<void>;
|
|
76
|
+
/**
|
|
77
|
+
* Count all the entities which match the conditions.
|
|
78
|
+
* @param conditions The optional conditions to match for the entities.
|
|
79
|
+
* @returns The total count of entities in the storage.
|
|
80
|
+
*/
|
|
81
|
+
count(conditions?: EntityCondition<T>): Promise<number>;
|
|
47
82
|
}
|
|
@@ -19,6 +19,12 @@ export interface IEntityStorageConnector<T = unknown> extends IComponent {
|
|
|
19
19
|
property: keyof T;
|
|
20
20
|
value: unknown;
|
|
21
21
|
}[]): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Set multiple entities in a batch.
|
|
24
|
+
* @param entities The entities to set.
|
|
25
|
+
* @returns Nothing.
|
|
26
|
+
*/
|
|
27
|
+
setBatch(entities: T[]): Promise<void>;
|
|
22
28
|
/**
|
|
23
29
|
* Get an entity.
|
|
24
30
|
* @param id The id of the entity to get, or the index value if secondaryIndex is set.
|
|
@@ -40,6 +46,12 @@ export interface IEntityStorageConnector<T = unknown> extends IComponent {
|
|
|
40
46
|
property: keyof T;
|
|
41
47
|
value: unknown;
|
|
42
48
|
}[]): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Remove multiple entities by id.
|
|
51
|
+
* @param ids The ids of the entities to remove.
|
|
52
|
+
* @returns Nothing.
|
|
53
|
+
*/
|
|
54
|
+
removeBatch(ids: string[]): Promise<void>;
|
|
43
55
|
/**
|
|
44
56
|
* Query all the entities which match the conditions.
|
|
45
57
|
* @param conditions The conditions to match for the entities.
|
|
@@ -63,4 +75,15 @@ export interface IEntityStorageConnector<T = unknown> extends IComponent {
|
|
|
63
75
|
*/
|
|
64
76
|
cursor?: string;
|
|
65
77
|
}>;
|
|
78
|
+
/**
|
|
79
|
+
* Remove all entities from the storage.
|
|
80
|
+
* @returns Nothing.
|
|
81
|
+
*/
|
|
82
|
+
empty(): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Count all the entities which match the conditions.
|
|
85
|
+
* @param conditions The optional conditions to match for the entities.
|
|
86
|
+
* @returns The total count of entities in the storage.
|
|
87
|
+
*/
|
|
88
|
+
count(conditions?: EntityCondition<T>): Promise<number>;
|
|
66
89
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { IContextIds } from "@twin.org/context";
|
|
2
|
+
import type { IEntityStorageConnector } from "./IEntityStorageConnector.js";
|
|
3
|
+
import type { IMigrationOptions } from "./IMigrationOptions.js";
|
|
4
|
+
/**
|
|
5
|
+
* Interface describing an entity storage migration connector.
|
|
6
|
+
*/
|
|
7
|
+
export interface IEntityStorageMigrationConnector<T = unknown> extends IEntityStorageConnector<T> {
|
|
8
|
+
/**
|
|
9
|
+
* Get a unique list of all the context ids from the storage.
|
|
10
|
+
* @returns The list of unique context ids.
|
|
11
|
+
*/
|
|
12
|
+
getPartitionContextIds(): Promise<IContextIds[]>;
|
|
13
|
+
/**
|
|
14
|
+
* Create the target connector for performing the migration it will use a temporary storage location.
|
|
15
|
+
* @param newEntitySchema The name of the new entity schema to create the connector for.
|
|
16
|
+
* @returns Connector for performing the migration.
|
|
17
|
+
*/
|
|
18
|
+
createTargetConnector<U>(newEntitySchema: string): Promise<IEntityStorageConnector<U>>;
|
|
19
|
+
/**
|
|
20
|
+
* Finalize the migration by tearing down the old connector and replacing it with the target connector.
|
|
21
|
+
* @param targetConnector The target connector to finalize the migration with.
|
|
22
|
+
* @param options The options to control how the migration is finalized.
|
|
23
|
+
* @param loggingComponentType The optional component type to use for logging the migration progress.
|
|
24
|
+
* @returns A promise that resolves when the migration is finalized and returns the final connector.
|
|
25
|
+
*/
|
|
26
|
+
finalizeMigration<U>(targetConnector: IEntityStorageConnector<U>, options?: IMigrationOptions<T, U>, loggingComponentType?: string): Promise<IEntityStorageConnector<U>>;
|
|
27
|
+
/**
|
|
28
|
+
* Cleanup the migration if a migration fails or needs to be aborted.
|
|
29
|
+
* @param targetConnector The target connector to cleanup the migration with.
|
|
30
|
+
* @param options The options to control how the migration is cleaned up.
|
|
31
|
+
* @param loggingComponentType The optional component type to use for logging the migration progress.
|
|
32
|
+
* @returns A promise that resolves when the migration is cleaned up.
|
|
33
|
+
*/
|
|
34
|
+
cleanupMigration<U>(targetConnector: IEntityStorageConnector<U> | undefined, options?: IMigrationOptions<T, U>, loggingComponentType?: string): Promise<void>;
|
|
35
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { IEntitySchemaProperty } from "@twin.org/entity";
|
|
2
|
+
/**
|
|
3
|
+
* Options controlling how a schema migration is executed.
|
|
4
|
+
*/
|
|
5
|
+
export interface IMigrationOptions<T, U> {
|
|
6
|
+
/**
|
|
7
|
+
* Number of entities to read and write per batch.
|
|
8
|
+
* @default 100
|
|
9
|
+
*/
|
|
10
|
+
batchSize?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Optional transformation for properties, usually only called for object and array types.
|
|
13
|
+
* @param schema1Property The property schema in the old schema.
|
|
14
|
+
* @param schemaProperty2 The property schema in the new schema.
|
|
15
|
+
* @param value The value of the property in the old schema.
|
|
16
|
+
* @returns The transformed value to match the new schema.
|
|
17
|
+
*/
|
|
18
|
+
transformEntityProperty?: (schema1Property: IEntitySchemaProperty<T>, schemaProperty2: IEntitySchemaProperty<U>, value: unknown) => unknown;
|
|
19
|
+
/**
|
|
20
|
+
* Called for each partition for progress tracking.
|
|
21
|
+
* @param rowTotal The total number of rows to migrate.
|
|
22
|
+
* @param rowIndex The number of rows migrated so far.
|
|
23
|
+
*/
|
|
24
|
+
onPartitionProgress?: (rowTotal: number, rowIndex: number) => Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Called for overall progress tracking.
|
|
27
|
+
* @param stepKey The key representing the current step in the migration.
|
|
28
|
+
* @param itemTotal The total number of items in this progress.
|
|
29
|
+
* @param itemIndex The number of items processed so far.
|
|
30
|
+
*/
|
|
31
|
+
onStepProgress?: (stepKey: string, itemTotal: number, itemIndex: number) => Promise<void>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Count the entries in entity storage.
|
|
3
|
+
*/
|
|
4
|
+
export interface IEntityStorageCountRequest {
|
|
5
|
+
/**
|
|
6
|
+
* The query parameters.
|
|
7
|
+
*/
|
|
8
|
+
query?: {
|
|
9
|
+
/**
|
|
10
|
+
* The optional conditions to filter the count, JSON encoded EntityCondition.
|
|
11
|
+
*/
|
|
12
|
+
conditions?: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -19,5 +19,9 @@ export interface IEntityStorageGetRequest {
|
|
|
19
19
|
* The secondary index to query with the id.
|
|
20
20
|
*/
|
|
21
21
|
secondaryIndex?: string;
|
|
22
|
+
/**
|
|
23
|
+
* The optional conditions to match for the entity, JSON encoded array of property/value pairs.
|
|
24
|
+
*/
|
|
25
|
+
conditions?: string;
|
|
22
26
|
};
|
|
23
27
|
}
|
|
@@ -11,4 +11,13 @@ export interface IEntityStorageRemoveRequest {
|
|
|
11
11
|
*/
|
|
12
12
|
id: string;
|
|
13
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* The query parameters.
|
|
16
|
+
*/
|
|
17
|
+
query?: {
|
|
18
|
+
/**
|
|
19
|
+
* The optional conditions to match for the entity, JSON encoded array of property/value pairs.
|
|
20
|
+
*/
|
|
21
|
+
conditions?: string;
|
|
22
|
+
};
|
|
14
23
|
}
|
|
@@ -6,4 +6,13 @@ export interface IEntityStorageSetRequest {
|
|
|
6
6
|
* The data to be used in the entity.
|
|
7
7
|
*/
|
|
8
8
|
body: unknown;
|
|
9
|
+
/**
|
|
10
|
+
* The query parameters.
|
|
11
|
+
*/
|
|
12
|
+
query?: {
|
|
13
|
+
/**
|
|
14
|
+
* The optional conditions to match for the entity, JSON encoded array of property/value pairs.
|
|
15
|
+
*/
|
|
16
|
+
conditions?: string;
|
|
17
|
+
};
|
|
9
18
|
}
|