@twin.org/entity 0.0.1-next.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.
Files changed (53) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +25 -0
  3. package/dist/cjs/index.cjs +611 -0
  4. package/dist/esm/index.mjs +598 -0
  5. package/dist/types/decorators/entityDecorator.d.ts +8 -0
  6. package/dist/types/decorators/propertyDecorator.d.ts +8 -0
  7. package/dist/types/factories/entitySchemaFactory.d.ts +6 -0
  8. package/dist/types/index.d.ts +19 -0
  9. package/dist/types/models/IComparator.d.ts +18 -0
  10. package/dist/types/models/IComparatorGroup.d.ts +15 -0
  11. package/dist/types/models/IEntitySchema.d.ts +19 -0
  12. package/dist/types/models/IEntitySchemaOptions.d.ts +9 -0
  13. package/dist/types/models/IEntitySchemaProperty.d.ts +52 -0
  14. package/dist/types/models/IEntitySort.d.ts +19 -0
  15. package/dist/types/models/comparisonOperator.d.ts +52 -0
  16. package/dist/types/models/entityCondition.d.ts +6 -0
  17. package/dist/types/models/entitySchemaPropertyFormat.d.ts +77 -0
  18. package/dist/types/models/entitySchemaPropertyType.d.ts +33 -0
  19. package/dist/types/models/logicalOperator.d.ts +17 -0
  20. package/dist/types/models/sortDirection.d.ts +17 -0
  21. package/dist/types/utils/decoratorHelper.d.ts +19 -0
  22. package/dist/types/utils/entityConditions.d.ts +21 -0
  23. package/dist/types/utils/entitySchemaHelper.d.ts +38 -0
  24. package/dist/types/utils/entitySorter.d.ts +25 -0
  25. package/docs/changelog.md +5 -0
  26. package/docs/examples.md +1 -0
  27. package/docs/reference/classes/DecoratorHelper.md +63 -0
  28. package/docs/reference/classes/EntityConditions.md +69 -0
  29. package/docs/reference/classes/EntitySchemaHelper.md +117 -0
  30. package/docs/reference/classes/EntitySorter.md +81 -0
  31. package/docs/reference/functions/entity.md +17 -0
  32. package/docs/reference/functions/property.md +17 -0
  33. package/docs/reference/index.md +40 -0
  34. package/docs/reference/interfaces/IComparator.md +27 -0
  35. package/docs/reference/interfaces/IComparatorGroup.md +23 -0
  36. package/docs/reference/interfaces/IEntitySchema.md +31 -0
  37. package/docs/reference/interfaces/IEntitySchemaOptions.md +11 -0
  38. package/docs/reference/interfaces/IEntitySchemaProperty.md +95 -0
  39. package/docs/reference/interfaces/IEntitySort.md +31 -0
  40. package/docs/reference/type-aliases/ComparisonOperator.md +5 -0
  41. package/docs/reference/type-aliases/EntityCondition.md +9 -0
  42. package/docs/reference/type-aliases/EntitySchemaPropertyFormat.md +5 -0
  43. package/docs/reference/type-aliases/EntitySchemaPropertyType.md +5 -0
  44. package/docs/reference/type-aliases/LogicalOperator.md +5 -0
  45. package/docs/reference/type-aliases/SortDirection.md +5 -0
  46. package/docs/reference/variables/ComparisonOperator.md +68 -0
  47. package/docs/reference/variables/EntitySchemaFactory.md +5 -0
  48. package/docs/reference/variables/EntitySchemaPropertyFormat.md +109 -0
  49. package/docs/reference/variables/EntitySchemaPropertyType.md +43 -0
  50. package/docs/reference/variables/LogicalOperator.md +19 -0
  51. package/docs/reference/variables/SortDirection.md +19 -0
  52. package/locales/en.json +8 -0
  53. package/package.json +65 -0
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Definition for an entity schema options.
3
+ */
4
+ export interface IEntitySchemaOptions {
5
+ /**
6
+ * Description of the object.
7
+ */
8
+ description?: string;
9
+ }
@@ -0,0 +1,52 @@
1
+ import type { EntitySchemaPropertyFormat } from "./entitySchemaPropertyFormat";
2
+ import type { EntitySchemaPropertyType } from "./entitySchemaPropertyType";
3
+ import type { SortDirection } from "./sortDirection";
4
+ /**
5
+ * Definition for an entity schema property.
6
+ */
7
+ export interface IEntitySchemaProperty<T = unknown> {
8
+ /**
9
+ * The property name from the entity.
10
+ */
11
+ property: keyof T;
12
+ /**
13
+ * The type of the property.
14
+ */
15
+ type: EntitySchemaPropertyType;
16
+ /**
17
+ * The format of the property.
18
+ */
19
+ format?: EntitySchemaPropertyFormat;
20
+ /**
21
+ * Is this the primary index property.
22
+ */
23
+ isPrimary?: boolean;
24
+ /**
25
+ * Is this a secondary index property.
26
+ */
27
+ isSecondary?: boolean;
28
+ /**
29
+ * Default sort direction for this field, leave empty if not sortable.
30
+ */
31
+ sortDirection?: SortDirection;
32
+ /**
33
+ * Is the property optional.
34
+ */
35
+ optional?: boolean;
36
+ /**
37
+ * The type of the item (only applies when type is `array`).
38
+ */
39
+ itemType?: EntitySchemaPropertyType;
40
+ /**
41
+ * The type ref of the item (only applies when type is either `array` or `object`).
42
+ */
43
+ itemTypeRef?: string;
44
+ /**
45
+ * Description of the object.
46
+ */
47
+ description?: string;
48
+ /**
49
+ * Examples of the property values.
50
+ */
51
+ examples?: unknown[];
52
+ }
@@ -0,0 +1,19 @@
1
+ import type { EntitySchemaPropertyType } from "./entitySchemaPropertyType";
2
+ import type { SortDirection } from "./sortDirection";
3
+ /**
4
+ * Definition of an entity property sort details.
5
+ */
6
+ export interface IEntitySort<T> {
7
+ /**
8
+ * The name of the property.
9
+ */
10
+ property: keyof T;
11
+ /**
12
+ * The type of the property.
13
+ */
14
+ type: EntitySchemaPropertyType;
15
+ /**
16
+ * Default sort direction for this column, leave empty if not sortable.
17
+ */
18
+ sortDirection: SortDirection;
19
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * The types of comparisons.
3
+ */
4
+ export declare const ComparisonOperator: {
5
+ /**
6
+ * Equals.
7
+ */
8
+ readonly Equals: "equals";
9
+ /**
10
+ * Not Equals.
11
+ */
12
+ readonly NotEquals: "not-equals";
13
+ /**
14
+ * Greater Than.
15
+ */
16
+ readonly GreaterThan: "greater-than";
17
+ /**
18
+ * Greater Than Or Equal.
19
+ */
20
+ readonly GreaterThanOrEqual: "greater-than-or-equal";
21
+ /**
22
+ * Less Than.
23
+ */
24
+ readonly LessThan: "less-than";
25
+ /**
26
+ * Less Than Or Equal.
27
+ */
28
+ readonly LessThanOrEqual: "less-than-or-equal";
29
+ /**
30
+ * Includes.
31
+ * A string in a substring.
32
+ * A set contains an element.
33
+ * A list contains an element.
34
+ */
35
+ readonly Includes: "includes";
36
+ /**
37
+ * Not Includes.
38
+ * A string not in a substring.
39
+ * A set does not contain an element.
40
+ * A list does not contain an element.
41
+ */
42
+ readonly NotIncludes: "not-includes";
43
+ /**
44
+ * In.
45
+ * A element is in a set.
46
+ */
47
+ readonly In: "in";
48
+ };
49
+ /**
50
+ * The types of comparisons.
51
+ */
52
+ export type ComparisonOperator = (typeof ComparisonOperator)[keyof typeof ComparisonOperator];
@@ -0,0 +1,6 @@
1
+ import type { IComparator } from "./IComparator";
2
+ import type { IComparatorGroup } from "./IComparatorGroup";
3
+ /**
4
+ * Type defining condition for entities filtering.
5
+ */
6
+ export type EntityCondition<T> = IComparator | IComparatorGroup<T>;
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Definition of the entity property format.
3
+ */
4
+ export declare const EntitySchemaPropertyFormat: {
5
+ /**
6
+ * UUID.
7
+ */
8
+ readonly Uuid: "uuid";
9
+ /**
10
+ * URI.
11
+ */
12
+ readonly Uri: "uri";
13
+ /**
14
+ * email.
15
+ */
16
+ readonly Email: "email";
17
+ /**
18
+ * int8.
19
+ */
20
+ readonly Int8: "int8";
21
+ /**
22
+ * uint8.
23
+ */
24
+ readonly Uint8: "uint8";
25
+ /**
26
+ * int16.
27
+ */
28
+ readonly Int16: "int16";
29
+ /**
30
+ * uint16.
31
+ */
32
+ readonly Uint16: "uint16";
33
+ /**
34
+ * int32.
35
+ */
36
+ readonly Int32: "int32";
37
+ /**
38
+ * uint32.
39
+ */
40
+ readonly Uint32: "uint32";
41
+ /**
42
+ * float.
43
+ */
44
+ readonly Float: "float";
45
+ /**
46
+ * double.
47
+ */
48
+ readonly Double: "double";
49
+ /**
50
+ * int64.
51
+ */
52
+ readonly Int64: "int64";
53
+ /**
54
+ * uint64.
55
+ */
56
+ readonly Uint64: "uint64";
57
+ /**
58
+ * date.
59
+ */
60
+ readonly Date: "date";
61
+ /**
62
+ * time.
63
+ */
64
+ readonly Time: "time";
65
+ /**
66
+ * date-time.
67
+ */
68
+ readonly DateTime: "date-time";
69
+ /**
70
+ * JSON.
71
+ */
72
+ readonly Json: "json";
73
+ };
74
+ /**
75
+ * Definition of the entity property type's format.
76
+ */
77
+ export type EntitySchemaPropertyFormat = (typeof EntitySchemaPropertyFormat)[keyof typeof EntitySchemaPropertyFormat];
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Definition of the entity property types.
3
+ */
4
+ export declare const EntitySchemaPropertyType: {
5
+ /**
6
+ * String.
7
+ */
8
+ readonly String: "string";
9
+ /**
10
+ * Number.
11
+ */
12
+ readonly Number: "number";
13
+ /**
14
+ * Integer.
15
+ */
16
+ readonly Integer: "integer";
17
+ /**
18
+ * Boolean.
19
+ */
20
+ readonly Boolean: "boolean";
21
+ /**
22
+ * Array.
23
+ */
24
+ readonly Array: "array";
25
+ /**
26
+ * Object.
27
+ */
28
+ readonly Object: "object";
29
+ };
30
+ /**
31
+ * Definition of the entity property types.
32
+ */
33
+ export type EntitySchemaPropertyType = (typeof EntitySchemaPropertyType)[keyof typeof EntitySchemaPropertyType];
@@ -0,0 +1,17 @@
1
+ /**
2
+ * The logical operators for condition combining.
3
+ */
4
+ export declare const LogicalOperator: {
5
+ /**
6
+ * Logical operator AND.
7
+ */
8
+ readonly And: "and";
9
+ /**
10
+ * Logical operator OR.
11
+ */
12
+ readonly Or: "or";
13
+ };
14
+ /**
15
+ * The logical operators for condition combining.
16
+ */
17
+ export type LogicalOperator = (typeof LogicalOperator)[keyof typeof LogicalOperator];
@@ -0,0 +1,17 @@
1
+ /**
2
+ * The sort directions.
3
+ */
4
+ export declare const SortDirection: {
5
+ /**
6
+ * Ascending.
7
+ */
8
+ readonly Ascending: "asc";
9
+ /**
10
+ * Descending.
11
+ */
12
+ readonly Descending: "desc";
13
+ };
14
+ /**
15
+ * The sort directions.
16
+ */
17
+ export type SortDirection = (typeof SortDirection)[keyof typeof SortDirection];
@@ -0,0 +1,19 @@
1
+ import "reflect-metadata";
2
+ import type { IEntitySchema } from "../models/IEntitySchema";
3
+ /**
4
+ * Class to help with decorators.
5
+ */
6
+ export declare class DecoratorHelper {
7
+ /**
8
+ * Get the schema from the reflection metadata.
9
+ * @param target The object to get the schema data from.
10
+ * @returns The schema from the metadata if it can be found.
11
+ */
12
+ static getSchema<T = unknown>(target: any): IEntitySchema<T>;
13
+ /**
14
+ * Set the schema from the reflection metadata.
15
+ * @param target The object to get the schema data from.
16
+ * @param entitySchema The schema to set.
17
+ */
18
+ static setSchema<T = unknown>(target: any, entitySchema: IEntitySchema<T>): void;
19
+ }
@@ -0,0 +1,21 @@
1
+ import type { EntityCondition } from "../models/entityCondition";
2
+ import type { IComparator } from "../models/IComparator";
3
+ /**
4
+ * Class to perform condition checks.
5
+ */
6
+ export declare class EntityConditions {
7
+ /**
8
+ * See if the entity matches the conditions.
9
+ * @param entity The entity to test.
10
+ * @param condition The conditions to test.
11
+ * @returns True if the entity matches.
12
+ */
13
+ static check<T>(entity: T, condition?: EntityCondition<T>): boolean;
14
+ /**
15
+ * See if the entity matches the conditions.
16
+ * @param entity The entity to test.
17
+ * @param comparator The condition to test.
18
+ * @returns True if the entity matches.
19
+ */
20
+ static compare<T>(entity: T, comparator: IComparator): boolean;
21
+ }
@@ -0,0 +1,38 @@
1
+ import type { IEntitySchema } from "../models/IEntitySchema";
2
+ import type { IEntitySchemaProperty } from "../models/IEntitySchemaProperty";
3
+ import type { IEntitySort } from "../models/IEntitySort";
4
+ import type { SortDirection } from "../models/sortDirection";
5
+ /**
6
+ * Class to help with entity schema operations.
7
+ */
8
+ export declare class EntitySchemaHelper {
9
+ /**
10
+ * Get the schema for the specified object.
11
+ * @param target The object to get the schema data for.
12
+ * @returns The schema for the object if it can be found.
13
+ */
14
+ static getSchema<T = unknown>(target: any): IEntitySchema<T>;
15
+ /**
16
+ * Get the primary key from the entity schema.
17
+ * @param entitySchema The entity schema to find the primary key from.
18
+ * @returns The key if only one was found.
19
+ * @throws If no primary key was found, or more than one.
20
+ */
21
+ static getPrimaryKey<T>(entitySchema: IEntitySchema<T>): IEntitySchemaProperty<T>;
22
+ /**
23
+ * Get the sort properties from the schema.
24
+ * @param entitySchema The entity schema to find the primary key from.
25
+ * @returns The sort keys from the schema or undefined if there are none.
26
+ */
27
+ static getSortProperties<T>(entitySchema: IEntitySchema<T>): IEntitySort<T>[] | undefined;
28
+ /**
29
+ * Build sort properties from the schema and override if necessary.
30
+ * @param entitySchema The entity schema to retrieve the default sort keys.
31
+ * @param overrideSortKeys The override sort keys.
32
+ * @returns The finalised sort keys.
33
+ */
34
+ static buildSortProperties<T>(entitySchema: IEntitySchema<T>, overrideSortKeys?: {
35
+ property: keyof T;
36
+ sortDirection: SortDirection;
37
+ }[]): IEntitySort<T>[] | undefined;
38
+ }
@@ -0,0 +1,25 @@
1
+ import type { EntitySchemaPropertyType } from "../models/entitySchemaPropertyType";
2
+ import type { IEntitySort } from "../models/IEntitySort";
3
+ import { SortDirection } from "../models/sortDirection";
4
+ /**
5
+ * Class to perform sort operations on entities.
6
+ */
7
+ export declare class EntitySorter {
8
+ /**
9
+ * Sort a list of entities using multiple keys and direction.
10
+ * @param entities The list of entities.
11
+ * @param entitySorters The sort keys to use.
12
+ * @returns The sorted list.
13
+ */
14
+ static sort<T>(entities: T[], entitySorters?: IEntitySort<T>[]): T[];
15
+ /**
16
+ * Compare two properties.
17
+ * @param entity1 The first entity.
18
+ * @param entity2 The second entity.
19
+ * @param prop The property to compare.
20
+ * @param type The type of the property.
21
+ * @param direction The direction of the sort.
22
+ * @returns The result of the comparison.
23
+ */
24
+ static compare<T>(entity1: T, entity2: T, prop: keyof T, type: EntitySchemaPropertyType, direction?: SortDirection): number;
25
+ }
@@ -0,0 +1,5 @@
1
+ # @twin.org/entity - Changelog
2
+
3
+ ## v0.0.1
4
+
5
+ - Initial Release
@@ -0,0 +1 @@
1
+ # @twin.org/entity - Examples
@@ -0,0 +1,63 @@
1
+ # Class: DecoratorHelper
2
+
3
+ Class to help with decorators.
4
+
5
+ ## Constructors
6
+
7
+ ### new DecoratorHelper()
8
+
9
+ > **new DecoratorHelper**(): [`DecoratorHelper`](DecoratorHelper.md)
10
+
11
+ #### Returns
12
+
13
+ [`DecoratorHelper`](DecoratorHelper.md)
14
+
15
+ ## Methods
16
+
17
+ ### getSchema()
18
+
19
+ > `static` **getSchema**\<`T`\>(`target`): [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
20
+
21
+ Get the schema from the reflection metadata.
22
+
23
+ #### Type Parameters
24
+
25
+ • **T** = `unknown`
26
+
27
+ #### Parameters
28
+
29
+ • **target**: `any`
30
+
31
+ The object to get the schema data from.
32
+
33
+ #### Returns
34
+
35
+ [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
36
+
37
+ The schema from the metadata if it can be found.
38
+
39
+ ***
40
+
41
+ ### setSchema()
42
+
43
+ > `static` **setSchema**\<`T`\>(`target`, `entitySchema`): `void`
44
+
45
+ Set the schema from the reflection metadata.
46
+
47
+ #### Type Parameters
48
+
49
+ • **T** = `unknown`
50
+
51
+ #### Parameters
52
+
53
+ • **target**: `any`
54
+
55
+ The object to get the schema data from.
56
+
57
+ • **entitySchema**: [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
58
+
59
+ The schema to set.
60
+
61
+ #### Returns
62
+
63
+ `void`
@@ -0,0 +1,69 @@
1
+ # Class: EntityConditions
2
+
3
+ Class to perform condition checks.
4
+
5
+ ## Constructors
6
+
7
+ ### new EntityConditions()
8
+
9
+ > **new EntityConditions**(): [`EntityConditions`](EntityConditions.md)
10
+
11
+ #### Returns
12
+
13
+ [`EntityConditions`](EntityConditions.md)
14
+
15
+ ## Methods
16
+
17
+ ### check()
18
+
19
+ > `static` **check**\<`T`\>(`entity`, `condition`?): `boolean`
20
+
21
+ See if the entity matches the conditions.
22
+
23
+ #### Type Parameters
24
+
25
+ • **T**
26
+
27
+ #### Parameters
28
+
29
+ • **entity**: `T`
30
+
31
+ The entity to test.
32
+
33
+ • **condition?**: [`EntityCondition`](../type-aliases/EntityCondition.md)\<`T`\>
34
+
35
+ The conditions to test.
36
+
37
+ #### Returns
38
+
39
+ `boolean`
40
+
41
+ True if the entity matches.
42
+
43
+ ***
44
+
45
+ ### compare()
46
+
47
+ > `static` **compare**\<`T`\>(`entity`, `comparator`): `boolean`
48
+
49
+ See if the entity matches the conditions.
50
+
51
+ #### Type Parameters
52
+
53
+ • **T**
54
+
55
+ #### Parameters
56
+
57
+ • **entity**: `T`
58
+
59
+ The entity to test.
60
+
61
+ • **comparator**: [`IComparator`](../interfaces/IComparator.md)
62
+
63
+ The condition to test.
64
+
65
+ #### Returns
66
+
67
+ `boolean`
68
+
69
+ True if the entity matches.
@@ -0,0 +1,117 @@
1
+ # Class: EntitySchemaHelper
2
+
3
+ Class to help with entity schema operations.
4
+
5
+ ## Constructors
6
+
7
+ ### new EntitySchemaHelper()
8
+
9
+ > **new EntitySchemaHelper**(): [`EntitySchemaHelper`](EntitySchemaHelper.md)
10
+
11
+ #### Returns
12
+
13
+ [`EntitySchemaHelper`](EntitySchemaHelper.md)
14
+
15
+ ## Methods
16
+
17
+ ### getSchema()
18
+
19
+ > `static` **getSchema**\<`T`\>(`target`): [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
20
+
21
+ Get the schema for the specified object.
22
+
23
+ #### Type Parameters
24
+
25
+ • **T** = `unknown`
26
+
27
+ #### Parameters
28
+
29
+ • **target**: `any`
30
+
31
+ The object to get the schema data for.
32
+
33
+ #### Returns
34
+
35
+ [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
36
+
37
+ The schema for the object if it can be found.
38
+
39
+ ***
40
+
41
+ ### getPrimaryKey()
42
+
43
+ > `static` **getPrimaryKey**\<`T`\>(`entitySchema`): [`IEntitySchemaProperty`](../interfaces/IEntitySchemaProperty.md)\<`T`\>
44
+
45
+ Get the primary key from the entity schema.
46
+
47
+ #### Type Parameters
48
+
49
+ • **T**
50
+
51
+ #### Parameters
52
+
53
+ • **entitySchema**: [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
54
+
55
+ The entity schema to find the primary key from.
56
+
57
+ #### Returns
58
+
59
+ [`IEntitySchemaProperty`](../interfaces/IEntitySchemaProperty.md)\<`T`\>
60
+
61
+ The key if only one was found.
62
+
63
+ #### Throws
64
+
65
+ If no primary key was found, or more than one.
66
+
67
+ ***
68
+
69
+ ### getSortProperties()
70
+
71
+ > `static` **getSortProperties**\<`T`\>(`entitySchema`): `undefined` \| [`IEntitySort`](../interfaces/IEntitySort.md)\<`T`\>[]
72
+
73
+ Get the sort properties from the schema.
74
+
75
+ #### Type Parameters
76
+
77
+ • **T**
78
+
79
+ #### Parameters
80
+
81
+ • **entitySchema**: [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
82
+
83
+ The entity schema to find the primary key from.
84
+
85
+ #### Returns
86
+
87
+ `undefined` \| [`IEntitySort`](../interfaces/IEntitySort.md)\<`T`\>[]
88
+
89
+ The sort keys from the schema or undefined if there are none.
90
+
91
+ ***
92
+
93
+ ### buildSortProperties()
94
+
95
+ > `static` **buildSortProperties**\<`T`\>(`entitySchema`, `overrideSortKeys`?): `undefined` \| [`IEntitySort`](../interfaces/IEntitySort.md)\<`T`\>[]
96
+
97
+ Build sort properties from the schema and override if necessary.
98
+
99
+ #### Type Parameters
100
+
101
+ • **T**
102
+
103
+ #### Parameters
104
+
105
+ • **entitySchema**: [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
106
+
107
+ The entity schema to retrieve the default sort keys.
108
+
109
+ • **overrideSortKeys?**: `object`[]
110
+
111
+ The override sort keys.
112
+
113
+ #### Returns
114
+
115
+ `undefined` \| [`IEntitySort`](../interfaces/IEntitySort.md)\<`T`\>[]
116
+
117
+ The finalised sort keys.