@twin.org/entity 0.0.3-next.4 → 0.0.3-next.41
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 +1 -5
- package/dist/es/decorators/entityDecorator.js +2 -1
- package/dist/es/decorators/entityDecorator.js.map +1 -1
- package/dist/es/decorators/propertyDecorator.js +1 -0
- package/dist/es/decorators/propertyDecorator.js.map +1 -1
- package/dist/es/index.js +2 -0
- package/dist/es/index.js.map +1 -1
- package/dist/es/models/IEntitySchemaDiff.js +2 -0
- package/dist/es/models/IEntitySchemaDiff.js.map +1 -0
- package/dist/es/utils/decoratorHelper.js +1 -0
- package/dist/es/utils/decoratorHelper.js.map +1 -1
- package/dist/es/utils/entityConditions.js +8 -0
- package/dist/es/utils/entityConditions.js.map +1 -1
- package/dist/es/utils/entitySchemaDiff.js +67 -0
- package/dist/es/utils/entitySchemaDiff.js.map +1 -0
- package/dist/types/decorators/entityDecorator.d.ts +1 -0
- package/dist/types/decorators/propertyDecorator.d.ts +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/models/IEntitySchemaDiff.d.ts +35 -0
- package/dist/types/utils/decoratorHelper.d.ts +1 -0
- package/dist/types/utils/entitySchemaDiff.d.ts +22 -0
- package/docs/changelog.md +802 -84
- package/docs/examples.md +70 -1
- package/docs/reference/classes/DecoratorHelper.md +2 -2
- package/docs/reference/classes/EntityConditions.md +2 -2
- package/docs/reference/classes/EntitySchemaHelper.md +6 -6
- package/docs/reference/classes/EntitySorter.md +4 -4
- package/docs/reference/functions/entitySchemaDiff.md +38 -0
- package/docs/reference/functions/isEmptyDiff.md +25 -0
- package/docs/reference/index.md +3 -0
- package/docs/reference/interfaces/IComparator.md +3 -3
- package/docs/reference/interfaces/IComparatorGroup.md +3 -3
- package/docs/reference/interfaces/IEntitySchema.md +5 -5
- package/docs/reference/interfaces/IEntitySchemaDiff.md +53 -0
- package/docs/reference/interfaces/IEntitySchemaOptions.md +2 -2
- package/docs/reference/interfaces/IEntitySchemaProperty.md +20 -20
- package/docs/reference/interfaces/IEntitySort.md +3 -3
- package/docs/reference/variables/ComparisonOperator.md +9 -9
- package/docs/reference/variables/EntitySchemaPropertyFormat.md +17 -17
- package/docs/reference/variables/EntitySchemaPropertyType.md +6 -6
- package/docs/reference/variables/LogicalOperator.md +2 -2
- package/docs/reference/variables/SortDirection.md +2 -2
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
# TWIN Entity
|
|
2
2
|
|
|
3
|
-
This package
|
|
4
|
-
|
|
5
|
-
- Defining
|
|
6
|
-
- Sorting
|
|
7
|
-
- Comparing
|
|
3
|
+
This package is part of the framework workspace and provides helpers for defining and working with entities to support consistent development workflows across the ecosystem.
|
|
8
4
|
|
|
9
5
|
## Installation
|
|
10
6
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
// Copyright
|
|
1
|
+
// Copyright 2026 IOTA Stiftung.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0.
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
4
4
|
import "reflect-metadata";
|
|
5
|
+
import "tslib";
|
|
5
6
|
import { DecoratorHelper } from "../utils/decoratorHelper.js";
|
|
6
7
|
/**
|
|
7
8
|
* Decorator to produce schema data for entity.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"entityDecorator.js","sourceRoot":"","sources":["../../../src/decorators/entityDecorator.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,uDAAuD;AACvD,OAAO,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"entityDecorator.js","sourceRoot":"","sources":["../../../src/decorators/entityDecorator.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,uDAAuD;AACvD,OAAO,kBAAkB,CAAC;AAC1B,OAAO,OAAO,CAAC;AAEf,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAE9D;;;;GAIG;AACH,MAAM,UAAU,MAAM,CAAC,OAA8B;IACpD,OAAO,CAAC,MAAW,EAAE,EAAE;QACtB,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QACvD,YAAY,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QAChC,YAAY,CAAC,OAAO,GAAG,OAAO,CAAC;QAC/B,eAAe,CAAC,SAAS,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IACjD,CAAC,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport \"reflect-metadata\";\nimport \"tslib\";\nimport type { IEntitySchemaOptions } from \"../models/IEntitySchemaOptions.js\";\nimport { DecoratorHelper } from \"../utils/decoratorHelper.js\";\n\n/**\n * Decorator to produce schema data for entity.\n * @param options The options for the entity.\n * @returns The class decorator.\n */\nexport function entity(options?: IEntitySchemaOptions): any {\n\treturn (target: any) => {\n\t\tconst entitySchema = DecoratorHelper.getSchema(target);\n\t\tentitySchema.type = target.name;\n\t\tentitySchema.options = options;\n\t\tDecoratorHelper.setSchema(target, entitySchema);\n\t};\n}\n"]}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0.
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
4
4
|
import "reflect-metadata";
|
|
5
|
+
import "tslib";
|
|
5
6
|
import { DecoratorHelper } from "../utils/decoratorHelper.js";
|
|
6
7
|
/**
|
|
7
8
|
* Decorator to produce schema property data for entities.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"propertyDecorator.js","sourceRoot":"","sources":["../../../src/decorators/propertyDecorator.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,uDAAuD;AACvD,OAAO,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"propertyDecorator.js","sourceRoot":"","sources":["../../../src/decorators/propertyDecorator.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,uDAAuD;AACvD,OAAO,kBAAkB,CAAC;AAC1B,OAAO,OAAO,CAAC;AAEf,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAE9D;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,OAAgD;IACxE,OAAO,CAAC,MAAW,EAAE,WAAmB,EAAE,EAAE;QAC3C,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAM,MAAM,CAAC,CAAC;QAC5D,YAAY,CAAC,UAAU,KAAK,EAAE,CAAC;QAC/B,MAAM,GAAG,GAAG,YAAY,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,KAAK,WAAW,CAAC,CAAC;QAC/E,IAAI,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC;YAChB,YAAY,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG;gBAC9B,GAAG,OAAO;gBACV,QAAQ,EAAE,WAAW;aACrB,CAAC;QACH,CAAC;aAAM,CAAC;YACP,YAAY,CAAC,UAAU,CAAC,IAAI,CAAC;gBAC5B,GAAG,OAAO;gBACV,QAAQ,EAAE,WAAW;aACrB,CAAC,CAAC;QACJ,CAAC;QACD,eAAe,CAAC,SAAS,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IACjD,CAAC,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport \"reflect-metadata\";\nimport \"tslib\";\nimport type { IEntitySchemaProperty } from \"../models/IEntitySchemaProperty.js\";\nimport { DecoratorHelper } from \"../utils/decoratorHelper.js\";\n\n/**\n * Decorator to produce schema property data for entities.\n * @param options The options for the property.\n * @returns The property decorator.\n */\nexport function property(options: Omit<IEntitySchemaProperty, \"property\">): any {\n\treturn (target: any, propertyKey: string) => {\n\t\tconst entitySchema = DecoratorHelper.getSchema<any>(target);\n\t\tentitySchema.properties ??= [];\n\t\tconst idx = entitySchema.properties.findIndex(p => p.property === propertyKey);\n\t\tif (idx !== -1) {\n\t\t\tentitySchema.properties[idx] = {\n\t\t\t\t...options,\n\t\t\t\tproperty: propertyKey\n\t\t\t};\n\t\t} else {\n\t\t\tentitySchema.properties.push({\n\t\t\t\t...options,\n\t\t\t\tproperty: propertyKey\n\t\t\t});\n\t\t}\n\t\tDecoratorHelper.setSchema(target, entitySchema);\n\t};\n}\n"]}
|
package/dist/es/index.js
CHANGED
|
@@ -18,5 +18,7 @@ export * from "./models/sortDirection.js";
|
|
|
18
18
|
export * from "./utils/decoratorHelper.js";
|
|
19
19
|
export * from "./utils/entityConditions.js";
|
|
20
20
|
export * from "./utils/entitySchemaHelper.js";
|
|
21
|
+
export * from "./utils/entitySchemaDiff.js";
|
|
21
22
|
export * from "./utils/entitySorter.js";
|
|
23
|
+
export * from "./models/IEntitySchemaDiff.js";
|
|
22
24
|
//# sourceMappingURL=index.js.map
|
package/dist/es/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,iCAAiC,CAAC;AAChD,cAAc,mCAAmC,CAAC;AAClD,cAAc,oCAAoC,CAAC;AACnD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wCAAwC,CAAC;AACvD,cAAc,sCAAsC,CAAC;AACrD,cAAc,yBAAyB,CAAC;AACxC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,kCAAkC,CAAC;AACjD,cAAc,mCAAmC,CAAC;AAClD,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,yBAAyB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./decorators/entityDecorator.js\";\nexport * from \"./decorators/propertyDecorator.js\";\nexport * from \"./factories/entitySchemaFactory.js\";\nexport * from \"./models/comparisonOperator.js\";\nexport * from \"./models/entityCondition.js\";\nexport * from \"./models/entitySchemaPropertyFormat.js\";\nexport * from \"./models/entitySchemaPropertyType.js\";\nexport * from \"./models/IComparator.js\";\nexport * from \"./models/IComparatorGroup.js\";\nexport * from \"./models/IEntitySchema.js\";\nexport * from \"./models/IEntitySchemaOptions.js\";\nexport * from \"./models/IEntitySchemaProperty.js\";\nexport * from \"./models/IEntitySort.js\";\nexport * from \"./models/logicalOperator.js\";\nexport * from \"./models/sortDirection.js\";\nexport * from \"./utils/decoratorHelper.js\";\nexport * from \"./utils/entityConditions.js\";\nexport * from \"./utils/entitySchemaHelper.js\";\nexport * from \"./utils/entitySorter.js\";\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,cAAc,iCAAiC,CAAC;AAChD,cAAc,mCAAmC,CAAC;AAClD,cAAc,oCAAoC,CAAC;AACnD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wCAAwC,CAAC;AACvD,cAAc,sCAAsC,CAAC;AACrD,cAAc,yBAAyB,CAAC;AACxC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,kCAAkC,CAAC;AACjD,cAAc,mCAAmC,CAAC;AAClD,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,+BAA+B,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nexport * from \"./decorators/entityDecorator.js\";\nexport * from \"./decorators/propertyDecorator.js\";\nexport * from \"./factories/entitySchemaFactory.js\";\nexport * from \"./models/comparisonOperator.js\";\nexport * from \"./models/entityCondition.js\";\nexport * from \"./models/entitySchemaPropertyFormat.js\";\nexport * from \"./models/entitySchemaPropertyType.js\";\nexport * from \"./models/IComparator.js\";\nexport * from \"./models/IComparatorGroup.js\";\nexport * from \"./models/IEntitySchema.js\";\nexport * from \"./models/IEntitySchemaOptions.js\";\nexport * from \"./models/IEntitySchemaProperty.js\";\nexport * from \"./models/IEntitySort.js\";\nexport * from \"./models/logicalOperator.js\";\nexport * from \"./models/sortDirection.js\";\nexport * from \"./utils/decoratorHelper.js\";\nexport * from \"./utils/entityConditions.js\";\nexport * from \"./utils/entitySchemaHelper.js\";\nexport * from \"./utils/entitySchemaDiff.js\";\nexport * from \"./utils/entitySorter.js\";\nexport * from \"./models/IEntitySchemaDiff.js\";\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IEntitySchemaDiff.js","sourceRoot":"","sources":["../../../src/models/IEntitySchemaDiff.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IEntitySchemaProperty } from \"./IEntitySchemaProperty.js\";\n\n/**\n * The result of comparing two sets of entity schema properties.\n */\nexport interface IEntitySchemaDiff<T = unknown> {\n\t/**\n\t * Properties present in the new schema but absent from the old one.\n\t * Each entry is the full property descriptor from the new schema, so it can\n\t * be passed directly to bootstrap / table-creation logic.\n\t */\n\tadded: IEntitySchemaProperty<T>[];\n\n\t/**\n\t * Properties present in the old schema but absent from the new one.\n\t * Each entry is the full property descriptor from the old schema, allowing\n\t * connectors to drop the correct column, index, or field by name.\n\t */\n\tremoved: IEntitySchemaProperty<T>[];\n\n\t/**\n\t * Properties that exist in both schemas but differ in at least one structural\n\t * field (type, format, isSecondary, sortDirection, optional, itemType, itemTypeRef).\n\t * `from` is the old descriptor; `to` is the new one.\n\t * Both are full property objects so connectors can drop the old definition and\n\t * create the new one using the same bootstrap code path.\n\t */\n\tmodified: {\n\t\t/**\n\t\t * The old property descriptor.\n\t\t */\n\t\tfrom: IEntitySchemaProperty<T>;\n\t\t/**\n\t\t * The new property descriptor.\n\t\t */\n\t\tto: IEntitySchemaProperty<T>;\n\t}[];\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decoratorHelper.js","sourceRoot":"","sources":["../../../src/utils/decoratorHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"decoratorHelper.js","sourceRoot":"","sources":["../../../src/utils/decoratorHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,kBAAkB,CAAC;AAC1B,OAAO,OAAO,CAAC;AAGf,MAAM,aAAa,GAAG,sBAAsB,CAAC;AAE7C;;GAEG;AACH,MAAM,OAAO,eAAe;IAC3B;;;;OAIG;IACH,8DAA8D;IACvD,MAAM,CAAC,SAAS,CAAc,MAAW;QAC/C,OAAO,CACN,OAAO,CAAC,WAAW,CAAC,aAAa,EAAE,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC;YAC1F,EAAE,CACF,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,8DAA8D;IACvD,MAAM,CAAC,SAAS,CAAc,MAAW,EAAE,YAA8B;QAC/E,OAAO,CAAC,cAAc,CACrB,aAAa,EACb,YAAY,EACZ,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CACtD,CAAC;IACH,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport \"reflect-metadata\";\nimport \"tslib\";\nimport type { IEntitySchema } from \"../models/IEntitySchema.js\";\n\nconst META_DATA_KEY = \"EntitySchemaMetadata\";\n\n/**\n * Class to help with decorators.\n */\nexport class DecoratorHelper {\n\t/**\n\t * Get the schema from the reflection metadata.\n\t * @param target The object to get the schema data from.\n\t * @returns The schema from the metadata if it can be found.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static getSchema<T = unknown>(target: any): IEntitySchema<T> {\n\t\treturn (\n\t\t\tReflect.getMetadata(META_DATA_KEY, typeof target === \"object\" ? target : target.prototype) ??\n\t\t\t{}\n\t\t);\n\t}\n\n\t/**\n\t * Set the schema from the reflection metadata.\n\t * @param target The object to get the schema data from.\n\t * @param entitySchema The schema to set.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static setSchema<T = unknown>(target: any, entitySchema: IEntitySchema<T>): void {\n\t\tReflect.defineMetadata(\n\t\t\tMETA_DATA_KEY,\n\t\t\tentitySchema,\n\t\t\ttypeof target === \"object\" ? target : target.prototype\n\t\t);\n\t}\n}\n"]}
|
|
@@ -169,6 +169,14 @@ export class EntityConditions {
|
|
|
169
169
|
}
|
|
170
170
|
return false;
|
|
171
171
|
}
|
|
172
|
+
else if (Is.object(val)) {
|
|
173
|
+
if (comparator.comparison === ComparisonOperator.Equals) {
|
|
174
|
+
return ObjectHelper.equal(val, conditionValue);
|
|
175
|
+
}
|
|
176
|
+
else if (comparator.comparison === ComparisonOperator.NotEquals) {
|
|
177
|
+
return !ObjectHelper.equal(val, conditionValue);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
172
180
|
return false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AC,CAAC;oBAC9C,IACC,CAAC,CACA,CAAC,UAAU,CAAC,UAAU,KAAK,kBAAkB,CAAC,QAAQ,IAAI,QAAQ,CAAC;wBACnE,CAAC,UAAU,CAAC,UAAU,KAAK,kBAAkB,CAAC,WAAW,IAAI,CAAC,QAAQ,CAAC;wBACvE,CAAC,UAAU,CAAC,UAAU,KAAK,kBAAkB,CAAC,EAAE,IAAI,QAAQ,CAAC,CAC7D,EACA,CAAC;wBACF,OAAO,KAAK,CAAC;oBACd,CAAC;oBACD,OAAO,IAAI,CAAC;gBACb,CAAC;gBACD,OAAO,KAAK,CAAC;YACd,CAAC;iBAAM,IAAI,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE,CAAC;gBACtC,IAAI,UAAU,CAAC,UAAU,KAAK,kBAAkB,CAAC,QAAQ,EAAE,CAAC;oBAC3D,KAAK,MAAM,CAAC,IAAI,GAAG,EAAE,CAAC;wBACrB,IAAI,YAAY,CAAC,KAAK,CAAC,CAAC,EAAE,cAAc,CAAC,EAAE,CAAC;4BAC3C,OAAO,IAAI,CAAC;wBACb,CAAC;oBACF,CAAC;gBACF,CAAC;qBAAM,IAAI,UAAU,CAAC,UAAU,KAAK,kBAAkB,CAAC,WAAW,EAAE,CAAC;oBACrE,KAAK,MAAM,CAAC,IAAI,GAAG,EAAE,CAAC;wBACrB,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,EAAE,cAAc,CAAC,EAAE,CAAC;4BAC5C,OAAO,IAAI,CAAC;wBACb,CAAC;oBACF,CAAC;gBACF,CAAC;YACF,CAAC;YACD,OAAO,KAAK,CAAC;QACd,CAAC;QAED,OAAO,KAAK,CAAC;IACd,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { ArrayHelper, Is, ObjectHelper } from \"@twin.org/core\";\nimport { ComparisonOperator } from \"../models/comparisonOperator.js\";\nimport type { EntityCondition } from \"../models/entityCondition.js\";\nimport type { IComparator } from \"../models/IComparator.js\";\nimport { LogicalOperator } from \"../models/logicalOperator.js\";\n\n/**\n * Class to perform condition checks.\n */\nexport class EntityConditions {\n\t/**\n\t * See if the entity matches the conditions.\n\t * @param entity The entity to test.\n\t * @param condition The conditions to test.\n\t * @returns True if the entity matches.\n\t */\n\tpublic static check<T>(entity: T, condition?: EntityCondition<T>): boolean {\n\t\t// If no conditions are defined then it's a match\n\t\tif (Is.undefined(condition)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (\"conditions\" in condition) {\n\t\t\t// It's a group of comparisons, so check the individual items and combine with the logical operator\n\t\t\tconst results: boolean[] = condition.conditions.map(c => EntityConditions.check(entity, c));\n\t\t\tif ((condition.logicalOperator ?? LogicalOperator.And) === LogicalOperator.And) {\n\t\t\t\treturn results.every(Boolean);\n\t\t\t}\n\t\t\treturn results.some(Boolean);\n\t\t}\n\n\t\tif (condition.property.includes(\".\")) {\n\t\t\t// It's a child property comparison, so evaluate the child property\n\t\t\t// and then compare it to the conditions\n\t\t\tconst path = condition.property.split(\".\");\n\n\t\t\tconst child = ObjectHelper.propertyGet(entity, path[0]);\n\n\t\t\t// If the child is an array then check each item\n\t\t\tif (Is.array(child)) {\n\t\t\t\tfor (const c of child) {\n\t\t\t\t\tconst check = EntityConditions.check(c, {\n\t\t\t\t\t\t...condition,\n\t\t\t\t\t\tproperty: path.slice(1).join(\".\")\n\t\t\t\t\t});\n\t\t\t\t\tif (check) {\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\t// It's a single value so just check the condition\n\t\treturn EntityConditions.compare(entity, condition);\n\t}\n\n\t/**\n\t * See if the entity matches the conditions.\n\t * @param entity The entity to test.\n\t * @param comparator The condition to test.\n\t * @returns True if the entity matches.\n\t */\n\tpublic static compare<T>(entity: T, comparator: IComparator): boolean {\n\t\tconst val = ObjectHelper.propertyGet(entity, comparator.property);\n\t\tconst conditionValue = comparator.value;\n\n\t\tif (Is.undefined(conditionValue)) {\n\t\t\tconst valUndefined = Is.undefined(val);\n\t\t\tif (valUndefined && comparator.comparison === ComparisonOperator.Equals) {\n\t\t\t\treturn true;\n\t\t\t} else if (!valUndefined && comparator.comparison === ComparisonOperator.NotEquals) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.string(val)) {\n\t\t\tif (Is.string(conditionValue)) {\n\t\t\t\tif (\n\t\t\t\t\t!(\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Equals && val === conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotEquals && val !== conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.GreaterThan && val > conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.LessThan && val < conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.GreaterThanOrEqual &&\n\t\t\t\t\t\t\tval >= conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.LessThanOrEqual &&\n\t\t\t\t\t\t\tval <= conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Includes &&\n\t\t\t\t\t\t\tval.includes(conditionValue)) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotIncludes &&\n\t\t\t\t\t\t\t!val.includes(conditionValue))\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t} else if (Is.array(conditionValue)) {\n\t\t\t\tif (!(comparator.comparison === ComparisonOperator.In && conditionValue.includes(val))) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.number(val)) {\n\t\t\tif (Is.number(conditionValue)) {\n\t\t\t\tif (\n\t\t\t\t\t!(\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Equals && val === conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotEquals && val !== conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.GreaterThan && val > conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.LessThan && val < conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.GreaterThanOrEqual &&\n\t\t\t\t\t\t\tval >= conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.LessThanOrEqual && val <= conditionValue)\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t} else if (Is.array(conditionValue)) {\n\t\t\t\tif (!(comparator.comparison === ComparisonOperator.In && conditionValue.includes(val))) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.boolean(val)) {\n\t\t\tif (Is.boolean(conditionValue)) {\n\t\t\t\tif (\n\t\t\t\t\t!(\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Equals && val === conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotEquals && val !== conditionValue)\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.array(val)) {\n\t\t\tif (Is.array(conditionValue)) {\n\t\t\t\tif (\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.Equals ||\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.NotEquals\n\t\t\t\t) {\n\t\t\t\t\tconst matches = ArrayHelper.matches(val, conditionValue);\n\t\t\t\t\tif (\n\t\t\t\t\t\t!(\n\t\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Equals && matches) ||\n\t\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotEquals && !matches)\n\t\t\t\t\t\t)\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t} else if (Is.number(conditionValue) || Is.string(conditionValue)) {\n\t\t\t\tif (\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.Includes ||\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.NotIncludes ||\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.In\n\t\t\t\t) {\n\t\t\t\t\tconst includes = val.includes(conditionValue);\n\t\t\t\t\tif (\n\t\t\t\t\t\t!(\n\t\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Includes && includes) ||\n\t\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotIncludes && !includes) ||\n\t\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.In && includes)\n\t\t\t\t\t\t)\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t} else if (Is.object(conditionValue)) {\n\t\t\t\tif (comparator.comparison === ComparisonOperator.Includes) {\n\t\t\t\t\tfor (const v of val) {\n\t\t\t\t\t\tif (ObjectHelper.equal(v, conditionValue)) {\n\t\t\t\t\t\t\treturn true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else if (comparator.comparison === ComparisonOperator.NotIncludes) {\n\t\t\t\t\tfor (const v of val) {\n\t\t\t\t\t\tif (!ObjectHelper.equal(v, conditionValue)) {\n\t\t\t\t\t\t\treturn true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn false;\n\t\t}\n\n\t\treturn false;\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"entityConditions.js","sourceRoot":"","sources":["../../../src/utils/entityConditions.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC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AK,CAAC;IACd,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { ArrayHelper, Is, ObjectHelper } from \"@twin.org/core\";\nimport { ComparisonOperator } from \"../models/comparisonOperator.js\";\nimport type { EntityCondition } from \"../models/entityCondition.js\";\nimport type { IComparator } from \"../models/IComparator.js\";\nimport { LogicalOperator } from \"../models/logicalOperator.js\";\n\n/**\n * Class to perform condition checks.\n */\nexport class EntityConditions {\n\t/**\n\t * See if the entity matches the conditions.\n\t * @param entity The entity to test.\n\t * @param condition The conditions to test.\n\t * @returns True if the entity matches.\n\t */\n\tpublic static check<T>(entity: T, condition?: EntityCondition<T>): boolean {\n\t\t// If no conditions are defined then it's a match\n\t\tif (Is.undefined(condition)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (\"conditions\" in condition) {\n\t\t\t// It's a group of comparisons, so check the individual items and combine with the logical operator\n\t\t\tconst results: boolean[] = condition.conditions.map(c => EntityConditions.check(entity, c));\n\t\t\tif ((condition.logicalOperator ?? LogicalOperator.And) === LogicalOperator.And) {\n\t\t\t\treturn results.every(Boolean);\n\t\t\t}\n\t\t\treturn results.some(Boolean);\n\t\t}\n\n\t\tif (condition.property.includes(\".\")) {\n\t\t\t// It's a child property comparison, so evaluate the child property\n\t\t\t// and then compare it to the conditions\n\t\t\tconst path = condition.property.split(\".\");\n\n\t\t\tconst child = ObjectHelper.propertyGet(entity, path[0]);\n\n\t\t\t// If the child is an array then check each item\n\t\t\tif (Is.array(child)) {\n\t\t\t\tfor (const c of child) {\n\t\t\t\t\tconst check = EntityConditions.check(c, {\n\t\t\t\t\t\t...condition,\n\t\t\t\t\t\tproperty: path.slice(1).join(\".\")\n\t\t\t\t\t});\n\t\t\t\t\tif (check) {\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\t// It's a single value so just check the condition\n\t\treturn EntityConditions.compare(entity, condition);\n\t}\n\n\t/**\n\t * See if the entity matches the conditions.\n\t * @param entity The entity to test.\n\t * @param comparator The condition to test.\n\t * @returns True if the entity matches.\n\t */\n\tpublic static compare<T>(entity: T, comparator: IComparator): boolean {\n\t\tconst val = ObjectHelper.propertyGet(entity, comparator.property);\n\t\tconst conditionValue = comparator.value;\n\n\t\tif (Is.undefined(conditionValue)) {\n\t\t\tconst valUndefined = Is.undefined(val);\n\t\t\tif (valUndefined && comparator.comparison === ComparisonOperator.Equals) {\n\t\t\t\treturn true;\n\t\t\t} else if (!valUndefined && comparator.comparison === ComparisonOperator.NotEquals) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.string(val)) {\n\t\t\tif (Is.string(conditionValue)) {\n\t\t\t\tif (\n\t\t\t\t\t!(\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Equals && val === conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotEquals && val !== conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.GreaterThan && val > conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.LessThan && val < conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.GreaterThanOrEqual &&\n\t\t\t\t\t\t\tval >= conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.LessThanOrEqual &&\n\t\t\t\t\t\t\tval <= conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Includes &&\n\t\t\t\t\t\t\tval.includes(conditionValue)) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotIncludes &&\n\t\t\t\t\t\t\t!val.includes(conditionValue))\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t} else if (Is.array(conditionValue)) {\n\t\t\t\tif (!(comparator.comparison === ComparisonOperator.In && conditionValue.includes(val))) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.number(val)) {\n\t\t\tif (Is.number(conditionValue)) {\n\t\t\t\tif (\n\t\t\t\t\t!(\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Equals && val === conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotEquals && val !== conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.GreaterThan && val > conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.LessThan && val < conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.GreaterThanOrEqual &&\n\t\t\t\t\t\t\tval >= conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.LessThanOrEqual && val <= conditionValue)\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t} else if (Is.array(conditionValue)) {\n\t\t\t\tif (!(comparator.comparison === ComparisonOperator.In && conditionValue.includes(val))) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.boolean(val)) {\n\t\t\tif (Is.boolean(conditionValue)) {\n\t\t\t\tif (\n\t\t\t\t\t!(\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Equals && val === conditionValue) ||\n\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotEquals && val !== conditionValue)\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.array(val)) {\n\t\t\tif (Is.array(conditionValue)) {\n\t\t\t\tif (\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.Equals ||\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.NotEquals\n\t\t\t\t) {\n\t\t\t\t\tconst matches = ArrayHelper.matches(val, conditionValue);\n\t\t\t\t\tif (\n\t\t\t\t\t\t!(\n\t\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Equals && matches) ||\n\t\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotEquals && !matches)\n\t\t\t\t\t\t)\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t} else if (Is.number(conditionValue) || Is.string(conditionValue)) {\n\t\t\t\tif (\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.Includes ||\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.NotIncludes ||\n\t\t\t\t\tcomparator.comparison === ComparisonOperator.In\n\t\t\t\t) {\n\t\t\t\t\tconst includes = val.includes(conditionValue);\n\t\t\t\t\tif (\n\t\t\t\t\t\t!(\n\t\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.Includes && includes) ||\n\t\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.NotIncludes && !includes) ||\n\t\t\t\t\t\t\t(comparator.comparison === ComparisonOperator.In && includes)\n\t\t\t\t\t\t)\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t} else if (Is.object(conditionValue)) {\n\t\t\t\tif (comparator.comparison === ComparisonOperator.Includes) {\n\t\t\t\t\tfor (const v of val) {\n\t\t\t\t\t\tif (ObjectHelper.equal(v, conditionValue)) {\n\t\t\t\t\t\t\treturn true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t} else if (comparator.comparison === ComparisonOperator.NotIncludes) {\n\t\t\t\t\tfor (const v of val) {\n\t\t\t\t\t\tif (!ObjectHelper.equal(v, conditionValue)) {\n\t\t\t\t\t\t\treturn true;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn false;\n\t\t} else if (Is.object(val)) {\n\t\t\tif (comparator.comparison === ComparisonOperator.Equals) {\n\t\t\t\treturn ObjectHelper.equal(val, conditionValue);\n\t\t\t} else if (comparator.comparison === ComparisonOperator.NotEquals) {\n\t\t\t\treturn !ObjectHelper.equal(val, conditionValue);\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compare two arrays of entity schema properties and return a structured diff.
|
|
3
|
+
*
|
|
4
|
+
* Properties are matched by their `property` key name. A property is considered
|
|
5
|
+
* modified when any structural field differs: `type`, `format`, `isPrimary`,
|
|
6
|
+
* `isSecondary`, `sortDirection`, `optional`, `itemType`, or `itemTypeRef`.
|
|
7
|
+
* Documentation-only fields (`description`, `examples`) are intentionally
|
|
8
|
+
* excluded from the comparison to avoid spurious diffs.
|
|
9
|
+
* @param oldProperties The property descriptors from the current (live) schema.
|
|
10
|
+
* @param newProperties The property descriptors from the target (new) schema.
|
|
11
|
+
* @returns A diff object with `added`, `removed`, and `modified` arrays, each
|
|
12
|
+
* containing full `IEntitySchemaProperty` descriptors.
|
|
13
|
+
*/
|
|
14
|
+
export function entitySchemaDiff(oldProperties, newProperties) {
|
|
15
|
+
const added = [];
|
|
16
|
+
const removed = [];
|
|
17
|
+
const modified = [];
|
|
18
|
+
const oldMap = new Map();
|
|
19
|
+
for (const prop of oldProperties) {
|
|
20
|
+
oldMap.set(prop.property, prop);
|
|
21
|
+
}
|
|
22
|
+
const newMap = new Map();
|
|
23
|
+
for (const prop of newProperties) {
|
|
24
|
+
newMap.set(prop.property, prop);
|
|
25
|
+
}
|
|
26
|
+
for (const newProp of newProperties) {
|
|
27
|
+
const oldProp = oldMap.get(newProp.property);
|
|
28
|
+
if (!oldProp) {
|
|
29
|
+
added.push(newProp);
|
|
30
|
+
}
|
|
31
|
+
else if (!schemaPropertiesEqual(oldProp, newProp)) {
|
|
32
|
+
modified.push({ from: oldProp, to: newProp });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
for (const oldProp of oldProperties) {
|
|
36
|
+
if (!newMap.has(oldProp.property)) {
|
|
37
|
+
removed.push(oldProp);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return { added, removed, modified };
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Returns true when the diff contains no changes (nothing added, removed, or modified).
|
|
44
|
+
* @param diff The diff to check.
|
|
45
|
+
* @returns True if the diff is empty.
|
|
46
|
+
*/
|
|
47
|
+
export function isEmptyDiff(diff) {
|
|
48
|
+
return diff.added.length === 0 && diff.removed.length === 0 && diff.modified.length === 0;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Compare two property descriptors for structural equality.
|
|
52
|
+
* Documentation fields (description, examples) are excluded.
|
|
53
|
+
* @param a The first property descriptor.
|
|
54
|
+
* @param b The second property descriptor.
|
|
55
|
+
* @returns True if all structural fields are equal.
|
|
56
|
+
*/
|
|
57
|
+
function schemaPropertiesEqual(a, b) {
|
|
58
|
+
return (a.type === b.type &&
|
|
59
|
+
a.format === b.format &&
|
|
60
|
+
a.isPrimary === b.isPrimary &&
|
|
61
|
+
a.isSecondary === b.isSecondary &&
|
|
62
|
+
a.sortDirection === b.sortDirection &&
|
|
63
|
+
a.optional === b.optional &&
|
|
64
|
+
a.itemType === b.itemType &&
|
|
65
|
+
a.itemTypeRef === b.itemTypeRef);
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=entitySchemaDiff.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entitySchemaDiff.js","sourceRoot":"","sources":["../../../src/utils/entitySchemaDiff.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,gBAAgB,CAC/B,aAAyC,EACzC,aAAyC;IAEzC,MAAM,KAAK,GAA+B,EAAE,CAAC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AAC,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,CAAC;AAC3F,CAAC;AAED;;;;;;GAMG;AACH,SAAS,qBAAqB,CAC7B,CAA2B,EAC3B,CAA2B;IAE3B,OAAO,CACN,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,IAAI;QACjB,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QACrB,CAAC,CAAC,SAAS,KAAK,CAAC,CAAC,SAAS;QAC3B,CAAC,CAAC,WAAW,KAAK,CAAC,CAAC,WAAW;QAC/B,CAAC,CAAC,aAAa,KAAK,CAAC,CAAC,aAAa;QACnC,CAAC,CAAC,QAAQ,KAAK,CAAC,CAAC,QAAQ;QACzB,CAAC,CAAC,QAAQ,KAAK,CAAC,CAAC,QAAQ;QACzB,CAAC,CAAC,WAAW,KAAK,CAAC,CAAC,WAAW,CAC/B,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IEntitySchemaDiff } from \"../models/IEntitySchemaDiff.js\";\nimport type { IEntitySchemaProperty } from \"../models/IEntitySchemaProperty.js\";\n\n/**\n * Compare two arrays of entity schema properties and return a structured diff.\n *\n * Properties are matched by their `property` key name. A property is considered\n * modified when any structural field differs: `type`, `format`, `isPrimary`,\n * `isSecondary`, `sortDirection`, `optional`, `itemType`, or `itemTypeRef`.\n * Documentation-only fields (`description`, `examples`) are intentionally\n * excluded from the comparison to avoid spurious diffs.\n * @param oldProperties The property descriptors from the current (live) schema.\n * @param newProperties The property descriptors from the target (new) schema.\n * @returns A diff object with `added`, `removed`, and `modified` arrays, each\n * containing full `IEntitySchemaProperty` descriptors.\n */\nexport function entitySchemaDiff<T>(\n\toldProperties: IEntitySchemaProperty<T>[],\n\tnewProperties: IEntitySchemaProperty<T>[]\n): IEntitySchemaDiff<T> {\n\tconst added: IEntitySchemaProperty<T>[] = [];\n\tconst removed: IEntitySchemaProperty<T>[] = [];\n\tconst modified: IEntitySchemaDiff<T>[\"modified\"] = [];\n\n\tconst oldMap = new Map<string, IEntitySchemaProperty<T>>();\n\tfor (const prop of oldProperties) {\n\t\toldMap.set(prop.property as string, prop);\n\t}\n\n\tconst newMap = new Map<string, IEntitySchemaProperty<T>>();\n\tfor (const prop of newProperties) {\n\t\tnewMap.set(prop.property as string, prop);\n\t}\n\n\tfor (const newProp of newProperties) {\n\t\tconst oldProp = oldMap.get(newProp.property as string);\n\t\tif (!oldProp) {\n\t\t\tadded.push(newProp);\n\t\t} else if (!schemaPropertiesEqual(oldProp, newProp)) {\n\t\t\tmodified.push({ from: oldProp, to: newProp });\n\t\t}\n\t}\n\n\tfor (const oldProp of oldProperties) {\n\t\tif (!newMap.has(oldProp.property as string)) {\n\t\t\tremoved.push(oldProp);\n\t\t}\n\t}\n\n\treturn { added, removed, modified };\n}\n\n/**\n * Returns true when the diff contains no changes (nothing added, removed, or modified).\n * @param diff The diff to check.\n * @returns True if the diff is empty.\n */\nexport function isEmptyDiff<T>(diff: IEntitySchemaDiff<T>): boolean {\n\treturn diff.added.length === 0 && diff.removed.length === 0 && diff.modified.length === 0;\n}\n\n/**\n * Compare two property descriptors for structural equality.\n * Documentation fields (description, examples) are excluded.\n * @param a The first property descriptor.\n * @param b The second property descriptor.\n * @returns True if all structural fields are equal.\n */\nfunction schemaPropertiesEqual<T>(\n\ta: IEntitySchemaProperty<T>,\n\tb: IEntitySchemaProperty<T>\n): boolean {\n\treturn (\n\t\ta.type === b.type &&\n\t\ta.format === b.format &&\n\t\ta.isPrimary === b.isPrimary &&\n\t\ta.isSecondary === b.isSecondary &&\n\t\ta.sortDirection === b.sortDirection &&\n\t\ta.optional === b.optional &&\n\t\ta.itemType === b.itemType &&\n\t\ta.itemTypeRef === b.itemTypeRef\n\t);\n}\n"]}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -16,4 +16,6 @@ export * from "./models/sortDirection.js";
|
|
|
16
16
|
export * from "./utils/decoratorHelper.js";
|
|
17
17
|
export * from "./utils/entityConditions.js";
|
|
18
18
|
export * from "./utils/entitySchemaHelper.js";
|
|
19
|
+
export * from "./utils/entitySchemaDiff.js";
|
|
19
20
|
export * from "./utils/entitySorter.js";
|
|
21
|
+
export * from "./models/IEntitySchemaDiff.js";
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { IEntitySchemaProperty } from "./IEntitySchemaProperty.js";
|
|
2
|
+
/**
|
|
3
|
+
* The result of comparing two sets of entity schema properties.
|
|
4
|
+
*/
|
|
5
|
+
export interface IEntitySchemaDiff<T = unknown> {
|
|
6
|
+
/**
|
|
7
|
+
* Properties present in the new schema but absent from the old one.
|
|
8
|
+
* Each entry is the full property descriptor from the new schema, so it can
|
|
9
|
+
* be passed directly to bootstrap / table-creation logic.
|
|
10
|
+
*/
|
|
11
|
+
added: IEntitySchemaProperty<T>[];
|
|
12
|
+
/**
|
|
13
|
+
* Properties present in the old schema but absent from the new one.
|
|
14
|
+
* Each entry is the full property descriptor from the old schema, allowing
|
|
15
|
+
* connectors to drop the correct column, index, or field by name.
|
|
16
|
+
*/
|
|
17
|
+
removed: IEntitySchemaProperty<T>[];
|
|
18
|
+
/**
|
|
19
|
+
* Properties that exist in both schemas but differ in at least one structural
|
|
20
|
+
* field (type, format, isSecondary, sortDirection, optional, itemType, itemTypeRef).
|
|
21
|
+
* `from` is the old descriptor; `to` is the new one.
|
|
22
|
+
* Both are full property objects so connectors can drop the old definition and
|
|
23
|
+
* create the new one using the same bootstrap code path.
|
|
24
|
+
*/
|
|
25
|
+
modified: {
|
|
26
|
+
/**
|
|
27
|
+
* The old property descriptor.
|
|
28
|
+
*/
|
|
29
|
+
from: IEntitySchemaProperty<T>;
|
|
30
|
+
/**
|
|
31
|
+
* The new property descriptor.
|
|
32
|
+
*/
|
|
33
|
+
to: IEntitySchemaProperty<T>;
|
|
34
|
+
}[];
|
|
35
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { IEntitySchemaDiff } from "../models/IEntitySchemaDiff.js";
|
|
2
|
+
import type { IEntitySchemaProperty } from "../models/IEntitySchemaProperty.js";
|
|
3
|
+
/**
|
|
4
|
+
* Compare two arrays of entity schema properties and return a structured diff.
|
|
5
|
+
*
|
|
6
|
+
* Properties are matched by their `property` key name. A property is considered
|
|
7
|
+
* modified when any structural field differs: `type`, `format`, `isPrimary`,
|
|
8
|
+
* `isSecondary`, `sortDirection`, `optional`, `itemType`, or `itemTypeRef`.
|
|
9
|
+
* Documentation-only fields (`description`, `examples`) are intentionally
|
|
10
|
+
* excluded from the comparison to avoid spurious diffs.
|
|
11
|
+
* @param oldProperties The property descriptors from the current (live) schema.
|
|
12
|
+
* @param newProperties The property descriptors from the target (new) schema.
|
|
13
|
+
* @returns A diff object with `added`, `removed`, and `modified` arrays, each
|
|
14
|
+
* containing full `IEntitySchemaProperty` descriptors.
|
|
15
|
+
*/
|
|
16
|
+
export declare function entitySchemaDiff<T>(oldProperties: IEntitySchemaProperty<T>[], newProperties: IEntitySchemaProperty<T>[]): IEntitySchemaDiff<T>;
|
|
17
|
+
/**
|
|
18
|
+
* Returns true when the diff contains no changes (nothing added, removed, or modified).
|
|
19
|
+
* @param diff The diff to check.
|
|
20
|
+
* @returns True if the diff is empty.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isEmptyDiff<T>(diff: IEntitySchemaDiff<T>): boolean;
|