@imbricate/core 3.7.3 → 3.8.0

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 (82) hide show
  1. package/.editorconfig +10 -0
  2. package/.gitattributes +4 -0
  3. package/.github/dependabot.yml +8 -0
  4. package/.github/workflows/ci.yml +48 -0
  5. package/.vscode/settings.json +8 -0
  6. package/.yarn/releases/yarn-4.4.0.cjs +925 -0
  7. package/.yarnrc.yml +3 -0
  8. package/babel.config.js +14 -0
  9. package/docs/README.md +70 -0
  10. package/eslint.config.mjs +64 -0
  11. package/jest.config.ts +14 -0
  12. package/package.json +27 -3
  13. package/{author/definition.d.ts → src/author/definition.ts} +4 -0
  14. package/{database/definition.d.ts → src/database/definition.ts} +67 -4
  15. package/{database/interface.d.ts → src/database/interface.ts} +69 -27
  16. package/{database/manager.d.ts → src/database/manager.ts} +24 -9
  17. package/src/database/schema.ts +165 -0
  18. package/src/database/validate.ts +77 -0
  19. package/{document/definition.d.ts → src/document/definition.ts} +31 -3
  20. package/{document/interface.d.ts → src/document/interface.ts} +44 -15
  21. package/src/document/property/default-value.ts +26 -0
  22. package/{document/property/definition.d.ts → src/document/property/definition.ts} +6 -1
  23. package/src/document/property/primary.ts +29 -0
  24. package/src/document/property/triage-base.ts +138 -0
  25. package/src/document/property/triage-manager.ts +68 -0
  26. package/src/document/property/triage.ts +15 -0
  27. package/{document/property.d.ts → src/document/property.ts} +23 -4
  28. package/{document/validate.js → src/document/validate.ts} +33 -21
  29. package/{index.d.ts → src/index.ts} +3 -0
  30. package/{loader/definition.d.ts → src/loader/definition.ts} +5 -1
  31. package/src/loader/origin-loader.ts +88 -0
  32. package/{loader/persistence.d.ts → src/loader/persistence.ts} +12 -3
  33. package/{origin/definition.d.ts → src/origin/definition.ts} +1 -0
  34. package/{origin/interface.d.ts → src/origin/interface.ts} +18 -7
  35. package/{origin/search.d.ts → src/origin/search.ts} +29 -13
  36. package/{static/definition.d.ts → src/static/definition.ts} +3 -0
  37. package/{static/interface.d.ts → src/static/interface.ts} +6 -1
  38. package/{static/manager.d.ts → src/static/manager.ts} +11 -4
  39. package/{text/definition.d.ts → src/text/definition.ts} +3 -0
  40. package/{text/interface.d.ts → src/text/interface.ts} +6 -1
  41. package/{text/manager.d.ts → src/text/manager.ts} +11 -4
  42. package/test/unit/database/schema.test.ts +95 -0
  43. package/test/unit/document/property/primary.test.ts +87 -0
  44. package/test/unit/document/property/triage.test.ts +64 -0
  45. package/test/unit/document/validate.test.ts +138 -0
  46. package/test/unit/loader/definition.test.ts +55 -0
  47. package/typescript/tsconfig.build.json +23 -0
  48. package/author/definition.js +0 -7
  49. package/database/definition.js +0 -17
  50. package/database/interface.js +0 -7
  51. package/database/manager.js +0 -7
  52. package/database/schema.d.ts +0 -70
  53. package/database/schema.js +0 -78
  54. package/document/definition.js +0 -17
  55. package/document/interface.js +0 -7
  56. package/document/property/default-value.d.ts +0 -7
  57. package/document/property/default-value.js +0 -25
  58. package/document/property/definition.js +0 -7
  59. package/document/property/primary.d.ts +0 -8
  60. package/document/property/primary.js +0 -20
  61. package/document/property/triage-base.d.ts +0 -31
  62. package/document/property/triage-base.js +0 -85
  63. package/document/property/triage-manager.d.ts +0 -30
  64. package/document/property/triage-manager.js +0 -50
  65. package/document/property/triage.d.ts +0 -8
  66. package/document/property/triage.js +0 -13
  67. package/document/property.js +0 -54
  68. package/document/validate.d.ts +0 -18
  69. package/index.js +0 -46
  70. package/loader/definition.js +0 -7
  71. package/loader/origin-loader.d.ts +0 -29
  72. package/loader/origin-loader.js +0 -104
  73. package/loader/persistence.js +0 -19
  74. package/origin/definition.js +0 -7
  75. package/origin/interface.js +0 -7
  76. package/origin/search.js +0 -14
  77. package/static/definition.js +0 -7
  78. package/static/interface.js +0 -7
  79. package/static/manager.js +0 -7
  80. package/text/definition.js +0 -7
  81. package/text/interface.js +0 -7
  82. package/text/manager.js +0 -7
@@ -0,0 +1,165 @@
1
+ /**
2
+ * @author WMXPY
3
+ * @namespace Database
4
+ * @description Schema
5
+ */
6
+
7
+ import { IMBRICATE_PROPERTY_TYPE } from "../document/property";
8
+
9
+ export type ImbricateDatabaseSchemaProperty<T extends IMBRICATE_PROPERTY_TYPE> = {
10
+
11
+ readonly propertyIdentifier: string;
12
+ } & ImbricateDatabaseSchemaPropertyForCreation<T>;
13
+
14
+ export type ImbricateDatabaseSchemaPropertyOptionsReferenceDatabase = {
15
+
16
+ readonly originUniqueIdentifier: string;
17
+ readonly databaseUniqueIdentifier: string;
18
+ };
19
+
20
+ export type ImbricateDatabaseSchemaPropertyOptionsLabelOption = {
21
+
22
+ readonly labelIdentifier: string;
23
+ readonly labelName: string;
24
+
25
+ readonly labelColor: string;
26
+ };
27
+
28
+ export type ImbricateDatabaseSchemaPropertyOptionsLabel = {
29
+
30
+ /**
31
+ * Allow multiple labels
32
+ */
33
+ readonly allowMultiple: boolean;
34
+ /**
35
+ * Label Options
36
+ */
37
+ readonly labelOptions: ImbricateDatabaseSchemaPropertyOptionsLabelOption[];
38
+ };
39
+
40
+ export type ImbricateDatabaseSchemaPropertyOptionsReference = {
41
+
42
+ /**
43
+ * Allow multiple references
44
+ */
45
+ readonly allowMultiple: boolean;
46
+ /**
47
+ * Allow references from these databases
48
+ * If empty, allow references from all databases
49
+ */
50
+ readonly databases: ImbricateDatabaseSchemaPropertyOptionsReferenceDatabase[];
51
+ };
52
+
53
+ // IMBRICATE_PROPERTY_TYPE SWITCH
54
+ export type ImbricateDatabaseSchemaPropertyOptions<T extends IMBRICATE_PROPERTY_TYPE> =
55
+ T extends IMBRICATE_PROPERTY_TYPE.BOOLEAN ? {} :
56
+ T extends IMBRICATE_PROPERTY_TYPE.STRING ? {} :
57
+ T extends IMBRICATE_PROPERTY_TYPE.NUMBER ? {} :
58
+ T extends IMBRICATE_PROPERTY_TYPE.MARKDOWN ? {} :
59
+ T extends IMBRICATE_PROPERTY_TYPE.JSON ? {} :
60
+ T extends IMBRICATE_PROPERTY_TYPE.IMBRISCRIPT ? {} :
61
+ T extends IMBRICATE_PROPERTY_TYPE.DATE ? {} :
62
+ T extends IMBRICATE_PROPERTY_TYPE.LABEL ? ImbricateDatabaseSchemaPropertyOptionsLabel :
63
+ T extends IMBRICATE_PROPERTY_TYPE.REFERENCE ? ImbricateDatabaseSchemaPropertyOptionsReference :
64
+ never;
65
+
66
+ export type ImbricateDatabaseSchemaPropertyForCreation<T extends IMBRICATE_PROPERTY_TYPE> = {
67
+
68
+ readonly propertyName: string;
69
+ readonly propertyType: T;
70
+ readonly propertyOptions: ImbricateDatabaseSchemaPropertyOptions<T>;
71
+
72
+ readonly isPrimaryKey?: boolean;
73
+ };
74
+
75
+ export type ImbricateDatabaseSchema = {
76
+
77
+ readonly properties: Array<ImbricateDatabaseSchemaProperty<IMBRICATE_PROPERTY_TYPE>>
78
+ };
79
+
80
+ export type ImbricateDatabaseSchemaForCreation = {
81
+
82
+ readonly properties: Array<ImbricateDatabaseSchemaPropertyForCreation<IMBRICATE_PROPERTY_TYPE>>
83
+ };
84
+
85
+ /**
86
+ * Validate a schema property
87
+ *
88
+ * @param property property to validate
89
+ *
90
+ * @returns a string error message if validation failed
91
+ * null if validation passed
92
+ */
93
+ export const validateImbricateSchemaProperty = (
94
+ property: ImbricateDatabaseSchemaProperty<IMBRICATE_PROPERTY_TYPE>,
95
+ ): string | null => {
96
+
97
+ if (typeof property.propertyIdentifier !== "string") {
98
+ return "Property identifier must be a string";
99
+ }
100
+ if (typeof property.propertyName !== "string") {
101
+ return "Property name must be a string";
102
+ }
103
+ if (!Object.values(IMBRICATE_PROPERTY_TYPE).includes(property.propertyType)) {
104
+ return "Property type must be a valid type";
105
+ }
106
+
107
+ switch (property.propertyType) {
108
+
109
+ case IMBRICATE_PROPERTY_TYPE.REFERENCE: {
110
+ if (typeof property.propertyOptions !== "object") {
111
+ return "Property options must be an object";
112
+ }
113
+ if (typeof (property.propertyOptions as any).allowMultiple !== "boolean") {
114
+ return "Property options allowMultiple must be a boolean";
115
+ }
116
+ if (!Array.isArray((property.propertyOptions as any).databases)) {
117
+ return "Property options databases must be an array";
118
+ }
119
+ for (const database of (property.propertyOptions as any).databases) {
120
+ if (typeof database.originUniqueIdentifier !== "string") {
121
+ return "Database originUniqueIdentifier must be a string";
122
+ }
123
+ if (typeof database.databaseUniqueIdentifier !== "string") {
124
+ return "Database databaseUniqueIdentifier must be a string";
125
+ }
126
+ }
127
+ break;
128
+ }
129
+ }
130
+
131
+ return null;
132
+ };
133
+
134
+ /**
135
+ * Validate a schema
136
+ *
137
+ * @param schema database schema to validate
138
+ *
139
+ * @returns a string error message if validation failed
140
+ * null if validation passed
141
+ */
142
+ export const validateImbricateSchema = (
143
+ schema: ImbricateDatabaseSchema,
144
+ ): string | null => {
145
+
146
+ if (!Array.isArray(schema.properties)) {
147
+ return "Properties must be an array";
148
+ }
149
+
150
+ const propertyNames: Set<string> = new Set();
151
+ for (const property of schema.properties) {
152
+
153
+ const propertyValidationResult: string | null = validateImbricateSchemaProperty(property);
154
+ if (typeof propertyValidationResult === "string") {
155
+ return `Invalid property ${property.propertyName}, ${propertyValidationResult}`;
156
+ }
157
+
158
+ if (propertyNames.has(property.propertyName)) {
159
+ return `Duplicated property name ${property.propertyName}`;
160
+ }
161
+ propertyNames.add(property.propertyName);
162
+ }
163
+
164
+ return null;
165
+ };
@@ -0,0 +1,77 @@
1
+ /**
2
+ * @author WMXPY
3
+ * @namespace Database
4
+ * @description Validate
5
+ */
6
+
7
+ import { ImbricateDocumentQuery } from "./definition";
8
+
9
+ /**
10
+ * Validate imbricate document query
11
+ *
12
+ * @param query query to validate
13
+ *
14
+ * @returns a string error message if validation failed
15
+ * null if validation passed
16
+ */
17
+ export const validateImbricateDocumentQuery = (query: ImbricateDocumentQuery): string | null => {
18
+
19
+ if (typeof query !== "object") {
20
+ return "Query must be an object";
21
+ }
22
+
23
+ if (query.limit !== undefined && typeof query.limit !== "number" && query.limit <= 0) {
24
+ return "Limit must be a number greater than 0 or undefined";
25
+ }
26
+ if (query.skip !== undefined && typeof query.skip !== "number" && query.skip < 0) {
27
+ return "Skip must be a number greater than or equal to 0 or undefined";
28
+ }
29
+
30
+ if (query.propertyFilters !== undefined) {
31
+ if (!Array.isArray(query.propertyFilters)) {
32
+ return "Property filters must be an array";
33
+ }
34
+
35
+ for (const filter of query.propertyFilters) {
36
+
37
+ if (typeof filter.propertyIdentifier !== "string") {
38
+ return "Property identifier must be a string";
39
+ }
40
+
41
+ if (!Array.isArray(filter.conditions)) {
42
+ return "Conditions must be an array";
43
+ }
44
+
45
+ for (const condition of filter.conditions) {
46
+
47
+ if (typeof condition.target !== "string") {
48
+ return "Target must be a string";
49
+ }
50
+ if (typeof condition.condition !== "string") {
51
+ return "Condition must be a string";
52
+ }
53
+ }
54
+ }
55
+ }
56
+
57
+ if (query.annotationFilters !== undefined) {
58
+ if (!Array.isArray(query.annotationFilters)) {
59
+ return "Annotation filters must be an array";
60
+ }
61
+
62
+ for (const filter of query.annotationFilters) {
63
+
64
+ if (typeof filter.namespace !== "string") {
65
+ return "Namespace must be a string";
66
+ }
67
+ if (typeof filter.identifier !== "string") {
68
+ return "Identifier must be a string";
69
+ }
70
+ if (typeof filter.condition !== "string") {
71
+ return "Condition must be a string";
72
+ }
73
+ }
74
+ }
75
+
76
+ return null;
77
+ };
@@ -3,48 +3,76 @@
3
3
  * @namespace Document
4
4
  * @description Definition
5
5
  */
6
+
6
7
  import { ImbricateAuthor } from "../author/definition";
7
8
  import { DocumentPropertyKey, DocumentPropertyValue, IMBRICATE_PROPERTY_TYPE } from "./property";
9
+
8
10
  /**
9
11
  * Edit record type of the document
10
12
  */
11
- export declare enum IMBRICATE_DOCUMENT_EDIT_TYPE {
13
+ export enum IMBRICATE_DOCUMENT_EDIT_TYPE {
14
+
12
15
  PUT_PROPERTY = "PUT_PROPERTY",
13
16
  PUT_ANNOTATION = "PUT_ANNOTATION",
14
- DELETE_ANNOTATION = "DELETE_ANNOTATION"
17
+ DELETE_ANNOTATION = "DELETE_ANNOTATION",
15
18
  }
19
+
16
20
  export type DocumentEditOperationValuePutProperty = {
21
+
17
22
  readonly key: DocumentPropertyKey;
18
23
  readonly value: DocumentPropertyValue<IMBRICATE_PROPERTY_TYPE>;
19
24
  };
25
+
20
26
  export type DocumentEditOperationPutAnnotation = {
27
+
21
28
  readonly annotationNamespace: string;
22
29
  readonly annotationIdentifier: string;
30
+
23
31
  readonly data: any;
24
32
  };
33
+
25
34
  export type DocumentEditOperationDeleteAnnotation = {
35
+
26
36
  readonly annotationNamespace: string;
27
37
  readonly annotationIdentifier: string;
28
38
  };
29
- export type DocumentEditOperationValue<T extends IMBRICATE_DOCUMENT_EDIT_TYPE> = T extends IMBRICATE_DOCUMENT_EDIT_TYPE.PUT_PROPERTY ? DocumentEditOperationValuePutProperty : T extends IMBRICATE_DOCUMENT_EDIT_TYPE.PUT_ANNOTATION ? DocumentEditOperationPutAnnotation : T extends IMBRICATE_DOCUMENT_EDIT_TYPE.DELETE_ANNOTATION ? DocumentEditOperationDeleteAnnotation : never;
39
+
40
+ // IMBRICATE_DOCUMENT_EDIT_TYPE SWITCH
41
+ export type DocumentEditOperationValue<T extends IMBRICATE_DOCUMENT_EDIT_TYPE> =
42
+ T extends IMBRICATE_DOCUMENT_EDIT_TYPE.PUT_PROPERTY ? DocumentEditOperationValuePutProperty :
43
+ T extends IMBRICATE_DOCUMENT_EDIT_TYPE.PUT_ANNOTATION ? DocumentEditOperationPutAnnotation :
44
+ T extends IMBRICATE_DOCUMENT_EDIT_TYPE.DELETE_ANNOTATION ? DocumentEditOperationDeleteAnnotation :
45
+ never;
46
+
30
47
  export type DocumentEditOperation<T extends IMBRICATE_DOCUMENT_EDIT_TYPE> = {
48
+
31
49
  readonly action: T;
32
50
  readonly value: DocumentEditOperationValue<T>;
33
51
  };
52
+
34
53
  export type DocumentEditRecord = {
54
+
35
55
  readonly uniqueIdentifier: string;
36
56
  readonly editAt: Date;
57
+
37
58
  readonly operations: Array<DocumentEditOperation<IMBRICATE_DOCUMENT_EDIT_TYPE>>;
59
+
38
60
  readonly author?: ImbricateAuthor;
39
61
  };
62
+
40
63
  export type DocumentAnnotations = Record<DocumentAnnotationKey, DocumentAnnotationValue>;
64
+
41
65
  export type DocumentAnnotationKey = string;
42
66
  export type DocumentAnnotationValue = {
67
+
43
68
  readonly namespace: string;
44
69
  readonly identifier: string;
70
+
45
71
  readonly data: any;
46
72
  };
73
+
47
74
  export type ImbricateDocumentAuditOptions = {
75
+
48
76
  /**
49
77
  * Do not add edit record, this is controlled an function may vary by origin
50
78
  */
@@ -3,84 +3,113 @@
3
3
  * @namespace Document
4
4
  * @description Interface
5
5
  */
6
+
6
7
  import { DocumentAnnotationValue, DocumentAnnotations, DocumentEditRecord, ImbricateDocumentAuditOptions } from "./definition";
7
8
  import { DocumentProperties, DocumentPropertyKey, DocumentPropertyValue, IMBRICATE_PROPERTY_TYPE } from "./property";
9
+
8
10
  export interface IImbricateDocument {
11
+
9
12
  /**
10
13
  * Unique identifier of the database
11
14
  */
12
15
  readonly uniqueIdentifier: string;
16
+
13
17
  /**
14
18
  * Properties of the document
15
19
  */
16
20
  readonly properties: DocumentProperties;
21
+
17
22
  /**
18
23
  * Annotations of the database
19
24
  */
20
25
  readonly annotations: DocumentAnnotations;
26
+
21
27
  /**
22
28
  * Update a property from the document
23
- *
29
+ *
24
30
  * @param key key of the property
25
31
  * @param value value of the property
26
32
  * @param auditOptions audit options of the document
27
- *
33
+ *
28
34
  * @returns a promise of the edit records of the document
29
35
  * Note: the edit records will not be added to the document if `noEditRecord` is true,
30
36
  * Call `addEditRecords` to add the edit records manually.
31
37
  */
32
- putProperty(key: DocumentPropertyKey, value: DocumentPropertyValue<IMBRICATE_PROPERTY_TYPE>, auditOptions?: ImbricateDocumentAuditOptions): PromiseLike<DocumentEditRecord[]>;
38
+ putProperty(
39
+ key: DocumentPropertyKey,
40
+ value: DocumentPropertyValue<IMBRICATE_PROPERTY_TYPE>,
41
+ auditOptions?: ImbricateDocumentAuditOptions,
42
+ ): PromiseLike<DocumentEditRecord[]>;
43
+
33
44
  /**
34
45
  * Put and replace all properties of the document
35
- *
46
+ *
36
47
  * @param properties properties of the document
37
48
  * @param auditOptions audit options of the document
38
- *
49
+ *
39
50
  * @returns a promise of the edit records of the document
40
51
  * Note: the edit records will not be added to the document if `noEditRecord` is true,
41
52
  * Call `addEditRecords` to add the edit records manually.
42
53
  */
43
- putProperties(properties: DocumentProperties, auditOptions?: ImbricateDocumentAuditOptions): PromiseLike<DocumentEditRecord[]>;
54
+ putProperties(
55
+ properties: DocumentProperties,
56
+ auditOptions?: ImbricateDocumentAuditOptions,
57
+ ): PromiseLike<DocumentEditRecord[]>;
58
+
44
59
  /**
45
60
  * put annotation to the document
46
- *
61
+ *
47
62
  * @param namespace namespace of the annotation
48
63
  * @param identifier identifier of the annotation
49
64
  * @param value value of the annotation
50
65
  * @param auditOptions audit options of the document
51
- *
66
+ *
52
67
  * @returns a promise of the edit records of the document
53
68
  * Note: if the origin supports Document Edit Record, the edit record will be added by default
54
69
  * If you do not want to add the edit record, set `noEditRecord` to true in audit options
55
70
  */
56
- putAnnotation(namespace: string, identifier: string, value: DocumentAnnotationValue, auditOptions?: ImbricateDocumentAuditOptions): PromiseLike<DocumentEditRecord[]>;
71
+ putAnnotation(
72
+ namespace: string,
73
+ identifier: string,
74
+ value: DocumentAnnotationValue,
75
+ auditOptions?: ImbricateDocumentAuditOptions,
76
+ ): PromiseLike<DocumentEditRecord[]>;
77
+
57
78
  /**
58
79
  * Delete annotation from the document
59
- *
80
+ *
60
81
  * @param namespace namespace of the annotation
61
82
  * @param identifier identifier of the annotation
62
83
  * @param auditOptions audit options of the document
63
- *
84
+ *
64
85
  * @returns a promise of the edit records of the document
65
86
  * Note: if the origin supports Document Edit Record, the edit record will be added by default
66
87
  * If you do not want to add the edit record, set `noEditRecord` to true in audit options
67
88
  */
68
- deleteAnnotation(namespace: string, identifier: string, auditOptions?: ImbricateDocumentAuditOptions): PromiseLike<DocumentEditRecord[]>;
89
+ deleteAnnotation(
90
+ namespace: string,
91
+ identifier: string,
92
+ auditOptions?: ImbricateDocumentAuditOptions,
93
+ ): PromiseLike<DocumentEditRecord[]>;
94
+
69
95
  /**
70
96
  * Add edit records to the document, optional
71
97
  * This method is optional, if not implemented, means the origin
72
98
  * 1. The origin does not support edit records
73
99
  * 2. The origin force to add edit records when put properties
74
- *
100
+ *
75
101
  * @param records document edit records
76
102
  */
77
- addEditRecords?(records: DocumentEditRecord[]): PromiseLike<void>;
103
+ addEditRecords?(
104
+ records: DocumentEditRecord[],
105
+ ): PromiseLike<void>;
106
+
78
107
  /**
79
108
  * Get edit records of the document, optional
80
109
  * This method is optional, if not implemented, means the origin
81
110
  * 1. The origin does not support edit records
82
111
  * 2. The origin force to add edit records when put properties
83
- *
112
+ *
84
113
  * @returns a promise of the edit records of the document
85
114
  */
86
115
  getEditRecords?(): PromiseLike<DocumentEditRecord[]>;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @author WMXPY
3
+ * @namespace Document_Property
4
+ * @description Default Value
5
+ */
6
+
7
+ import { DocumentPropertyValueObject, IMBRICATE_PROPERTY_TYPE } from "../property";
8
+
9
+ // IMBRICATE_PROPERTY_TYPE SWITCH
10
+ export const getImbricateDefaultValueOfProperty = (type: IMBRICATE_PROPERTY_TYPE): DocumentPropertyValueObject<IMBRICATE_PROPERTY_TYPE> => {
11
+
12
+ switch (type) {
13
+
14
+ case IMBRICATE_PROPERTY_TYPE.BOOLEAN: return false;
15
+ case IMBRICATE_PROPERTY_TYPE.STRING: return "";
16
+ case IMBRICATE_PROPERTY_TYPE.NUMBER: return 0;
17
+ case IMBRICATE_PROPERTY_TYPE.MARKDOWN: return "";
18
+ case IMBRICATE_PROPERTY_TYPE.JSON: return "";
19
+ case IMBRICATE_PROPERTY_TYPE.IMBRISCRIPT: return "";
20
+ case IMBRICATE_PROPERTY_TYPE.DATE: return new Date().toISOString();
21
+ case IMBRICATE_PROPERTY_TYPE.LABEL: return [];
22
+ case IMBRICATE_PROPERTY_TYPE.REFERENCE: return [];
23
+ }
24
+
25
+ return null as any;
26
+ };
@@ -3,5 +3,10 @@
3
3
  * @namespace Document_Property
4
4
  * @description Definition
5
5
  */
6
+
6
7
  import { DocumentPropertyKey, DocumentPropertyValue, IMBRICATE_PROPERTY_TYPE } from "../property";
7
- export type DocumentPropertyTriageFunction<T extends IMBRICATE_PROPERTY_TYPE, Result> = (propertyKey: DocumentPropertyKey, value: DocumentPropertyValue<T>) => Result;
8
+
9
+ export type DocumentPropertyTriageFunction<T extends IMBRICATE_PROPERTY_TYPE, Result> = (
10
+ propertyKey: DocumentPropertyKey,
11
+ value: DocumentPropertyValue<T>
12
+ ) => Result;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @author WMXPY
3
+ * @namespace Document_Property
4
+ * @description Primary
5
+ */
6
+
7
+ import { ImbricateDatabaseSchema } from "../../database/schema";
8
+ import { DocumentProperties, DocumentPropertyValue, IMBRICATE_PROPERTY_TYPE } from "../property";
9
+
10
+ export const findPrimaryProperty = (
11
+ schema: ImbricateDatabaseSchema,
12
+ properties: DocumentProperties,
13
+ ): DocumentPropertyValue<IMBRICATE_PROPERTY_TYPE> | null => {
14
+
15
+ for (const property of schema.properties) {
16
+
17
+ if (property.isPrimaryKey) {
18
+
19
+ const value: DocumentPropertyValue<IMBRICATE_PROPERTY_TYPE> | undefined =
20
+ properties[property.propertyIdentifier];
21
+
22
+ if (value) {
23
+ return value;
24
+ }
25
+ }
26
+ }
27
+
28
+ return null;
29
+ };
@@ -0,0 +1,138 @@
1
+ /**
2
+ * @author WMXPY
3
+ * @namespace Document_Property
4
+ * @description Triage Base
5
+ */
6
+
7
+ import { DocumentProperties, DocumentPropertyKey, IMBRICATE_PROPERTY_TYPE } from "../property";
8
+ import { DocumentPropertyTriageFunction } from "./definition";
9
+
10
+ // IMBRICATE_PROPERTY_TYPE SWITCH
11
+ export class ImbricateDocumentPropertyTriageBase<Result> {
12
+
13
+ private readonly _triageFunctionsByKey: Map<string, DocumentPropertyTriageFunction<any, Result>>;
14
+ private readonly _triageFunctionsByType: Map<IMBRICATE_PROPERTY_TYPE, DocumentPropertyTriageFunction<any, Result>>;
15
+
16
+ protected constructor() {
17
+
18
+ this._triageFunctionsByKey = new Map();
19
+ this._triageFunctionsByType = new Map();
20
+ }
21
+
22
+ /**
23
+ * Set triage function for property key,
24
+ * This action will override document value based triage functions
25
+ *
26
+ * @param propertyKey property key
27
+ * @param triageFunction triage function
28
+ * @returns triage manager
29
+ */
30
+ public forPropertyKey<T extends IMBRICATE_PROPERTY_TYPE>(
31
+ propertyKey: DocumentPropertyKey,
32
+ triageFunction: DocumentPropertyTriageFunction<T, Result>,
33
+ ): this {
34
+
35
+ this._triageFunctionsByKey.set(propertyKey, triageFunction);
36
+ return this;
37
+ }
38
+
39
+ public forBoolean(
40
+ triageFunction: DocumentPropertyTriageFunction<IMBRICATE_PROPERTY_TYPE.BOOLEAN, Result>,
41
+ ): this {
42
+
43
+ this._triageFunctionsByType.set(IMBRICATE_PROPERTY_TYPE.BOOLEAN, triageFunction);
44
+ return this;
45
+ }
46
+
47
+ public forString(
48
+ triageFunction: DocumentPropertyTriageFunction<IMBRICATE_PROPERTY_TYPE.STRING, Result>,
49
+ ): this {
50
+
51
+ this._triageFunctionsByType.set(IMBRICATE_PROPERTY_TYPE.STRING, triageFunction);
52
+ return this;
53
+ }
54
+
55
+ public forNumber(
56
+ triageFunction: DocumentPropertyTriageFunction<IMBRICATE_PROPERTY_TYPE.NUMBER, Result>,
57
+ ): this {
58
+
59
+ this._triageFunctionsByType.set(IMBRICATE_PROPERTY_TYPE.NUMBER, triageFunction);
60
+ return this;
61
+ }
62
+
63
+ public forMarkdown(
64
+ triageFunction: DocumentPropertyTriageFunction<IMBRICATE_PROPERTY_TYPE.MARKDOWN, Result>,
65
+ ): this {
66
+
67
+ this._triageFunctionsByType.set(IMBRICATE_PROPERTY_TYPE.MARKDOWN, triageFunction);
68
+ return this;
69
+ }
70
+
71
+ public forJson(
72
+ triageFunction: DocumentPropertyTriageFunction<IMBRICATE_PROPERTY_TYPE.JSON, Result>,
73
+ ): this {
74
+
75
+ this._triageFunctionsByType.set(IMBRICATE_PROPERTY_TYPE.JSON, triageFunction);
76
+ return this;
77
+ }
78
+
79
+ public forImbriscript(
80
+ triageFunction: DocumentPropertyTriageFunction<IMBRICATE_PROPERTY_TYPE.IMBRISCRIPT, Result>,
81
+ ): this {
82
+
83
+ this._triageFunctionsByType.set(IMBRICATE_PROPERTY_TYPE.IMBRISCRIPT, triageFunction);
84
+ return this;
85
+ }
86
+
87
+ public forDate(
88
+ triageFunction: DocumentPropertyTriageFunction<IMBRICATE_PROPERTY_TYPE.DATE, Result>,
89
+ ): this {
90
+
91
+ this._triageFunctionsByType.set(IMBRICATE_PROPERTY_TYPE.DATE, triageFunction);
92
+ return this;
93
+ }
94
+
95
+ public forLabel(
96
+ triageFunction: DocumentPropertyTriageFunction<IMBRICATE_PROPERTY_TYPE.LABEL, Result>,
97
+ ): this {
98
+
99
+ this._triageFunctionsByType.set(IMBRICATE_PROPERTY_TYPE.LABEL, triageFunction);
100
+ return this;
101
+ }
102
+
103
+ public forReference(
104
+ triageFunction: DocumentPropertyTriageFunction<IMBRICATE_PROPERTY_TYPE.REFERENCE, Result>,
105
+ ): this {
106
+
107
+ this._triageFunctionsByType.set(IMBRICATE_PROPERTY_TYPE.REFERENCE, triageFunction);
108
+ return this;
109
+ }
110
+
111
+ protected _collect(properties: DocumentProperties): Map<DocumentPropertyKey, Result> {
112
+
113
+ const keys: DocumentPropertyKey[] = Object.keys(properties);
114
+ const result: Map<DocumentPropertyKey, Result> = new Map();
115
+ for (const key of keys) {
116
+
117
+ const property = properties[key];
118
+ const triageFunction = this._triageFunctionsByKey.get(key);
119
+
120
+ if (typeof triageFunction === "function") {
121
+
122
+ const value: Result = triageFunction(key, property);
123
+ result.set(key, value);
124
+ continue;
125
+ }
126
+
127
+ const typeFunction = this._triageFunctionsByType.get(property.type);
128
+ if (typeof typeFunction === "function") {
129
+
130
+ const value: Result = typeFunction(key, property);
131
+ result.set(key, value);
132
+ continue;
133
+ }
134
+ }
135
+
136
+ return result;
137
+ }
138
+ }