@imbricate/core 3.0.2 → 3.0.4

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.
@@ -3,6 +3,8 @@
3
3
  * @namespace Database
4
4
  * @description Definition
5
5
  */
6
+ import { ImbricateAuthor } from "../author/definition";
7
+ import { ImbricateDatabaseSchema } from "./schema";
6
8
  /**
7
9
  * Query of the document
8
10
  *
@@ -13,3 +15,19 @@ export type ImbricateDocumentQuery = {
13
15
  readonly limit?: number;
14
16
  readonly skip?: number;
15
17
  };
18
+ /**
19
+ * Edit record type of the document
20
+ */
21
+ export declare enum IMBRICATE_DATABASE_EDIT_TYPE {
22
+ PUT_SCHEMA = "PUT_SCHEMA"
23
+ }
24
+ export type DatabaseEditOperation = {
25
+ readonly action: IMBRICATE_DATABASE_EDIT_TYPE;
26
+ readonly value: ImbricateDatabaseSchema;
27
+ };
28
+ export type DatabaseEditRecord = {
29
+ readonly uniqueIdentifier: string;
30
+ readonly editAt: Date;
31
+ readonly author: ImbricateAuthor;
32
+ readonly operations: DatabaseEditOperation[];
33
+ };
@@ -5,3 +5,11 @@
5
5
  * @description Definition
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.IMBRICATE_DATABASE_EDIT_TYPE = void 0;
9
+ /**
10
+ * Edit record type of the document
11
+ */
12
+ var IMBRICATE_DATABASE_EDIT_TYPE;
13
+ (function (IMBRICATE_DATABASE_EDIT_TYPE) {
14
+ IMBRICATE_DATABASE_EDIT_TYPE["PUT_SCHEMA"] = "PUT_SCHEMA";
15
+ })(IMBRICATE_DATABASE_EDIT_TYPE || (exports.IMBRICATE_DATABASE_EDIT_TYPE = IMBRICATE_DATABASE_EDIT_TYPE = {}));
@@ -5,7 +5,7 @@
5
5
  */
6
6
  import { IImbricateDocument } from "../document/interface";
7
7
  import { DocumentProperties } from "../document/property";
8
- import { ImbricateDocumentQuery } from "./definition";
8
+ import { DatabaseEditRecord, ImbricateDocumentQuery } from "./definition";
9
9
  import { ImbricateDatabaseSchema } from "./schema";
10
10
  export interface IImbricateDatabase {
11
11
  /**
@@ -20,6 +20,18 @@ export interface IImbricateDatabase {
20
20
  * Schema of the database
21
21
  */
22
22
  readonly schema: ImbricateDatabaseSchema;
23
+ /**
24
+ * Put and replace the schema of the database
25
+ * Existing documents will still be kept, and stays unchanged
26
+ *
27
+ * @param schema schema of the database
28
+ * @param noEditRecord do not add edit record, optional
29
+ *
30
+ * @returns a promise of the updated schema
31
+ * Note: if the origin supports Document Edit Record, the edit record will be added by default
32
+ * If you do not want to add the edit record, set `noEditRecord` to true
33
+ */
34
+ putSchema(schema: ImbricateDatabaseSchema, noEditRecord?: boolean): PromiseLike<void>;
23
35
  /**
24
36
  * Create a new document in the database
25
37
  * If origin supports Document Edit Record, the edit record will be added by default
@@ -47,4 +59,22 @@ export interface IImbricateDatabase {
47
59
  * @returns a promise of the documents in the database
48
60
  */
49
61
  queryDocuments(query: ImbricateDocumentQuery): PromiseLike<IImbricateDocument[]>;
62
+ /**
63
+ * Add edit records to the database, optional
64
+ * This method is optional, if not implemented, means the origin
65
+ * 1. The origin does not support edit records
66
+ * 2. The origin force to add edit records when put properties
67
+ *
68
+ * @param records database edit records
69
+ */
70
+ addEditRecords?(records: DatabaseEditRecord[]): PromiseLike<void>;
71
+ /**
72
+ * Get edit records of the database, optional
73
+ * This method is optional, if not implemented, means the origin
74
+ * 1. The origin does not support edit records
75
+ * 2. The origin force to add edit records when put properties
76
+ *
77
+ * @returns a promise of the edit records of the database
78
+ */
79
+ getEditRecords?(): PromiseLike<DatabaseEditRecord[]>;
50
80
  }
@@ -4,11 +4,11 @@
4
4
  * @description Definition
5
5
  */
6
6
  import { ImbricateAuthor } from "../author/definition";
7
- import { DocumentPropertyKey, DocumentPropertyValue, IMBRICATE_DOCUMENT_EDIT_TYPE } from "./property";
7
+ import { DocumentPropertyKey, DocumentPropertyValue, IMBRICATE_DOCUMENT_EDIT_TYPE, IMBRICATE_PROPERTY_TYPE } from "./property";
8
8
  export type DocumentEditOperation = {
9
9
  readonly key: DocumentPropertyKey;
10
10
  readonly action: IMBRICATE_DOCUMENT_EDIT_TYPE;
11
- readonly value: DocumentPropertyValue;
11
+ readonly value: DocumentPropertyValue<IMBRICATE_PROPERTY_TYPE>;
12
12
  };
13
13
  export type DocumentEditRecord = {
14
14
  readonly uniqueIdentifier: string;
@@ -4,7 +4,7 @@
4
4
  * @description Interface
5
5
  */
6
6
  import { DocumentEditRecord } from "./definition";
7
- import { DocumentProperties, DocumentPropertyKey, DocumentPropertyValue } from "./property";
7
+ import { DocumentProperties, DocumentPropertyKey, DocumentPropertyValue, IMBRICATE_PROPERTY_TYPE } from "./property";
8
8
  export interface IImbricateDocument {
9
9
  /**
10
10
  * Unique identifier of the database
@@ -22,7 +22,7 @@ export interface IImbricateDocument {
22
22
  * Note: the edit records will not be added to the document if `noEditRecord` is true,
23
23
  * Call `addEditRecords` to add the edit records manually.
24
24
  */
25
- putProperty(key: DocumentPropertyKey, value: DocumentPropertyValue, noEditRecord?: boolean): PromiseLike<DocumentEditRecord[]>;
25
+ putProperty(key: DocumentPropertyKey, value: DocumentPropertyValue<IMBRICATE_PROPERTY_TYPE>, noEditRecord?: boolean): PromiseLike<DocumentEditRecord[]>;
26
26
  /**
27
27
  * Put and replace all properties of the document
28
28
  *
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @author WMXPY
3
+ * @namespace Document_Property
4
+ * @description Definition
5
+ */
6
+ import { DocumentPropertyKey, DocumentPropertyValue, IMBRICATE_PROPERTY_TYPE } from "../property";
7
+ export type DocumentPropertyTriageFunction<T extends IMBRICATE_PROPERTY_TYPE, Result> = (propertyKey: DocumentPropertyKey, value: DocumentPropertyValue<T>) => Result;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ /**
3
+ * @author WMXPY
4
+ * @namespace Document_Property
5
+ * @description Definition
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @author WMXPY
3
+ * @namespace Document_Property
4
+ * @description Triage Base
5
+ */
6
+ import { DocumentProperties, DocumentPropertyKey, IMBRICATE_PROPERTY_TYPE } from "../property";
7
+ import { DocumentPropertyTriageFunction } from "./definition";
8
+ export declare class ImbricateDocumentPropertyTriageBase<Result> {
9
+ private readonly _triageFunctionsByKey;
10
+ private readonly _triageFunctionsByType;
11
+ protected constructor();
12
+ /**
13
+ * Set triage function for property key,
14
+ * This action will override document value based triage functions
15
+ *
16
+ * @param propertyKey property key
17
+ * @param triageFunction triage function
18
+ * @returns triage manager
19
+ */
20
+ forPropertyKey<T extends IMBRICATE_PROPERTY_TYPE>(propertyKey: DocumentPropertyKey, triageFunction: DocumentPropertyTriageFunction<T, Result>): this;
21
+ forString(triageFunction: DocumentPropertyTriageFunction<IMBRICATE_PROPERTY_TYPE.STRING, Result>): this;
22
+ forMarkdown(triageFunction: DocumentPropertyTriageFunction<IMBRICATE_PROPERTY_TYPE.MARKDOWN, Result>): this;
23
+ protected _collect(properties: DocumentProperties): Map<DocumentPropertyKey, Result>;
24
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ /**
3
+ * @author WMXPY
4
+ * @namespace Document_Property
5
+ * @description Triage Base
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.ImbricateDocumentPropertyTriageBase = void 0;
9
+ const property_1 = require("../property");
10
+ class ImbricateDocumentPropertyTriageBase {
11
+ constructor() {
12
+ this._triageFunctionsByKey = new Map();
13
+ this._triageFunctionsByType = new Map();
14
+ }
15
+ /**
16
+ * Set triage function for property key,
17
+ * This action will override document value based triage functions
18
+ *
19
+ * @param propertyKey property key
20
+ * @param triageFunction triage function
21
+ * @returns triage manager
22
+ */
23
+ forPropertyKey(propertyKey, triageFunction) {
24
+ this._triageFunctionsByKey.set(propertyKey, triageFunction);
25
+ return this;
26
+ }
27
+ forString(triageFunction) {
28
+ this._triageFunctionsByType.set(property_1.IMBRICATE_PROPERTY_TYPE.STRING, triageFunction);
29
+ return this;
30
+ }
31
+ forMarkdown(triageFunction) {
32
+ this._triageFunctionsByType.set(property_1.IMBRICATE_PROPERTY_TYPE.MARKDOWN, triageFunction);
33
+ return this;
34
+ }
35
+ _collect(properties) {
36
+ const keys = Object.keys(properties);
37
+ const result = new Map();
38
+ for (const key of keys) {
39
+ const property = properties[key];
40
+ const triageFunction = this._triageFunctionsByKey.get(key);
41
+ if (typeof triageFunction === "function") {
42
+ const value = triageFunction(key, property);
43
+ result.set(key, value);
44
+ continue;
45
+ }
46
+ const typeFunction = this._triageFunctionsByType.get(property.type);
47
+ if (typeof typeFunction === "function") {
48
+ const value = typeFunction(key, property);
49
+ result.set(key, value);
50
+ continue;
51
+ }
52
+ }
53
+ return result;
54
+ }
55
+ }
56
+ exports.ImbricateDocumentPropertyTriageBase = ImbricateDocumentPropertyTriageBase;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @author WMXPY
3
+ * @namespace Document_Property
4
+ * @description Triage Manager
5
+ */
6
+ import { DocumentProperties, DocumentPropertyKey } from "../property";
7
+ import { ImbricateDocumentPropertyTriageBase } from "./triage-base";
8
+ export declare class ImbricateDocumentPropertyTriageManager<Result> extends ImbricateDocumentPropertyTriageBase<Result> {
9
+ static create<Result>(properties: DocumentProperties): ImbricateDocumentPropertyTriageManager<Result>;
10
+ private readonly _properties;
11
+ private constructor();
12
+ /**
13
+ * Collect the result as array
14
+ *
15
+ * @returns collected result as array
16
+ */
17
+ collectAsArray(): Result[];
18
+ /**
19
+ * Collect the result as map
20
+ *
21
+ * @returns collected result as map
22
+ */
23
+ collectAsMap(): Map<DocumentPropertyKey, Result>;
24
+ /**
25
+ * Collect the result as object
26
+ *
27
+ * @returns collected result as object
28
+ */
29
+ collectAsObject(): Record<DocumentPropertyKey, Result>;
30
+ }
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ /**
3
+ * @author WMXPY
4
+ * @namespace Document_Property
5
+ * @description Triage Manager
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.ImbricateDocumentPropertyTriageManager = void 0;
9
+ const triage_base_1 = require("./triage-base");
10
+ class ImbricateDocumentPropertyTriageManager extends triage_base_1.ImbricateDocumentPropertyTriageBase {
11
+ static create(properties) {
12
+ return new ImbricateDocumentPropertyTriageManager(properties);
13
+ }
14
+ constructor(properties) {
15
+ super();
16
+ this._properties = properties;
17
+ }
18
+ /**
19
+ * Collect the result as array
20
+ *
21
+ * @returns collected result as array
22
+ */
23
+ collectAsArray() {
24
+ const result = super._collect(this._properties);
25
+ return Array.from(result.values());
26
+ }
27
+ /**
28
+ * Collect the result as map
29
+ *
30
+ * @returns collected result as map
31
+ */
32
+ collectAsMap() {
33
+ return super._collect(this._properties);
34
+ }
35
+ /**
36
+ * Collect the result as object
37
+ *
38
+ * @returns collected result as object
39
+ */
40
+ collectAsObject() {
41
+ const result = super._collect(this._properties);
42
+ const keys = Array.from(result.keys());
43
+ const object = {};
44
+ for (const key of keys) {
45
+ object[key] = result.get(key);
46
+ }
47
+ return object;
48
+ }
49
+ }
50
+ exports.ImbricateDocumentPropertyTriageManager = ImbricateDocumentPropertyTriageManager;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @author WMXPY
3
+ * @namespace Document_Property
4
+ * @description Triage
5
+ */
6
+ import { DocumentProperties } from "../property";
7
+ import { ImbricateDocumentPropertyTriageManager } from "./triage-manager";
8
+ export declare const triageImbricateDocumentProperties: <Result>(properties: DocumentProperties) => ImbricateDocumentPropertyTriageManager<Result>;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ /**
3
+ * @author WMXPY
4
+ * @namespace Document_Property
5
+ * @description Triage
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.triageImbricateDocumentProperties = void 0;
9
+ const triage_manager_1 = require("./triage-manager");
10
+ const triageImbricateDocumentProperties = (properties) => {
11
+ return triage_manager_1.ImbricateDocumentPropertyTriageManager.create(properties);
12
+ };
13
+ exports.triageImbricateDocumentProperties = triageImbricateDocumentProperties;
@@ -16,12 +16,13 @@ export declare enum IMBRICATE_PROPERTY_TYPE {
16
16
  /**
17
17
  * Document properties
18
18
  */
19
- export type DocumentProperties = Record<DocumentPropertyKey, DocumentPropertyValue>;
19
+ export type DocumentProperties = Record<DocumentPropertyKey, DocumentPropertyValue<IMBRICATE_PROPERTY_TYPE>>;
20
20
  export type DocumentPropertyKey = string;
21
- export type DocumentPropertyValue = {
21
+ export type DocumentPropertyValue<T extends IMBRICATE_PROPERTY_TYPE> = {
22
22
  readonly type: IMBRICATE_PROPERTY_TYPE;
23
- readonly value: any;
23
+ readonly value: DocumentPropertyValueObject<T>;
24
24
  };
25
+ export type DocumentPropertyValueObject<T extends IMBRICATE_PROPERTY_TYPE> = T extends IMBRICATE_PROPERTY_TYPE.STRING ? string : T extends IMBRICATE_PROPERTY_TYPE.MARKDOWN ? string : never;
25
26
  /**
26
27
  * Edit record type of the document
27
28
  */
package/index.d.ts CHANGED
@@ -10,6 +10,9 @@ export * from "./database/schema";
10
10
  export * from "./document/definition";
11
11
  export * from "./document/interface";
12
12
  export * from "./document/property";
13
+ export * from "./document/property/definition";
14
+ export * from "./document/property/triage";
15
+ export * from "./document/property/triage-manager";
13
16
  export * from "./document/validate";
14
17
  export * from "./loader/definition";
15
18
  export * from "./loader/origin-loader";
package/index.js CHANGED
@@ -26,6 +26,9 @@ __exportStar(require("./database/schema"), exports);
26
26
  __exportStar(require("./document/definition"), exports);
27
27
  __exportStar(require("./document/interface"), exports);
28
28
  __exportStar(require("./document/property"), exports);
29
+ __exportStar(require("./document/property/definition"), exports);
30
+ __exportStar(require("./document/property/triage"), exports);
31
+ __exportStar(require("./document/property/triage-manager"), exports);
29
32
  __exportStar(require("./document/validate"), exports);
30
33
  __exportStar(require("./loader/definition"), exports);
31
34
  __exportStar(require("./loader/origin-loader"), exports);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@imbricate/core",
3
3
  "main": "index.js",
4
- "version": "3.0.2",
4
+ "version": "3.0.4",
5
5
  "description": "Imbricate Core, Notebook for Engineers",
6
6
  "repository": {
7
7
  "type": "git",