@rljson/db 0.0.9 → 0.0.11

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 (37) hide show
  1. package/dist/connector/connector.d.ts +30 -0
  2. package/dist/controller/base-controller.d.ts +49 -0
  3. package/dist/controller/cake-controller.d.ts +29 -0
  4. package/dist/controller/component-controller.d.ts +68 -0
  5. package/dist/controller/controller.d.ts +48 -0
  6. package/dist/controller/layer-controller.d.ts +26 -0
  7. package/dist/controller/slice-id-controller.d.ts +22 -0
  8. package/dist/db.d.ts +24 -2
  9. package/dist/db.js +2488 -263
  10. package/dist/db.js.map +1 -0
  11. package/dist/edit/edit-action.d.ts +53 -0
  12. package/dist/edit/edit.d.ts +23 -0
  13. package/dist/edit/multi-edit-manager.d.ts +30 -0
  14. package/dist/edit/multi-edit-processor.d.ts +80 -0
  15. package/dist/example-static/example-static.d.ts +45 -0
  16. package/dist/example-static/mass-data/convert-mass-data.d.ts +5 -0
  17. package/dist/index.d.ts +7 -1
  18. package/dist/join/filter/boolean-filter-processor.d.ts +13 -0
  19. package/dist/join/filter/boolean-filter.d.ts +15 -0
  20. package/dist/join/filter/column-filter-processor.d.ts +9 -0
  21. package/dist/join/filter/column-filter.d.ts +18 -0
  22. package/dist/join/filter/number-filter-processor.d.ts +16 -0
  23. package/dist/join/filter/number-filter.d.ts +7 -0
  24. package/dist/join/filter/row-filter-processor.d.ts +19 -0
  25. package/dist/join/filter/row-filter.d.ts +19 -0
  26. package/dist/join/filter/string-filter-processor.d.ts +15 -0
  27. package/dist/join/filter/string-filter.d.ts +8 -0
  28. package/dist/join/join.d.ts +141 -0
  29. package/dist/join/selection/column-selection.d.ts +48 -0
  30. package/dist/join/set-value/set-value.d.ts +11 -0
  31. package/dist/join/sort/row-sort.d.ts +18 -0
  32. package/dist/notify.d.ts +5 -0
  33. package/dist/tools/inject.d.ts +1 -0
  34. package/dist/tools/isolate.d.ts +1 -0
  35. package/dist/tools/make-unique.d.ts +4 -0
  36. package/dist/tools/merge-trees.d.ts +5 -0
  37. package/package.json +12 -11
@@ -0,0 +1,30 @@
1
+ import { Socket } from '@rljson/io';
2
+ import { Route } from '@rljson/rljson';
3
+ import { Db } from '../db.ts';
4
+ export type ConnectorPayload = {
5
+ o: string;
6
+ r: string;
7
+ };
8
+ export type ConnectorCallback = (ref: string) => Promise<any>;
9
+ export declare class Connector {
10
+ private readonly _db;
11
+ private readonly _route;
12
+ private readonly _socket;
13
+ private _origin;
14
+ private _callbacks;
15
+ private _isListening;
16
+ private _sentRefs;
17
+ private _receivedRefs;
18
+ constructor(_db: Db, _route: Route, _socket: Socket);
19
+ send(ref: string): void;
20
+ listen(callback: (editHistoryRef: string) => Promise<void>): void;
21
+ private _init;
22
+ teardown(): void;
23
+ private _notifyCallbacks;
24
+ private _registerSocketObserver;
25
+ private _registerDbObserver;
26
+ get socket(): Socket;
27
+ get route(): Route;
28
+ get origin(): string;
29
+ get isListening(): boolean;
30
+ }
@@ -0,0 +1,49 @@
1
+ import { Json, JsonValue } from '@rljson/json';
2
+ import { ContentType, InsertHistoryRow, Ref, Rljson, TableCfg, TableKey, TableType } from '@rljson/rljson';
3
+ import { Core } from '../core.ts';
4
+ import { CakeControllerCommands } from './cake-controller.ts';
5
+ import { Controller, ControllerChildProperty, ControllerRefs } from './controller.ts';
6
+ export declare abstract class BaseController<T extends TableType, C extends JsonValue> implements Controller<T, C, string> {
7
+ protected readonly _core: Core;
8
+ protected readonly _tableKey: TableKey;
9
+ protected _contentType?: ContentType;
10
+ protected _tableCfg?: TableCfg;
11
+ constructor(_core: Core, _tableKey: TableKey);
12
+ abstract insert(command: CakeControllerCommands, value: C, origin?: Ref, refs?: ControllerRefs): Promise<InsertHistoryRow<any>[]>;
13
+ abstract init(): Promise<void>;
14
+ abstract getChildRefs(where: string | Json, filter?: Json): Promise<ControllerChildProperty[]>;
15
+ abstract filterRow(row: Json, key: string, value: JsonValue): Promise<boolean>;
16
+ /**
17
+ * Retrieves the current state of the table.
18
+ * @returns A promise that resolves to the current state of the table.
19
+ */
20
+ table(): Promise<T>;
21
+ /**
22
+ * Fetches a specific entry from the table by its reference or by a partial match.
23
+ * @param where A string representing the reference of the entry to fetch, or an object representing a partial match.
24
+ * @returns A promise that resolves to an array of entries matching the criteria, or null if no entries are found.
25
+ */
26
+ get(where: string | Json, filter?: Json): Promise<Rljson>;
27
+ /**
28
+ * Fetches a specific entry from the table by its reference.
29
+ * @param hash A string representing the reference of the entry to fetch.
30
+ * @returns A promise that resolves to the entry matching the reference, or null if no entry is found.
31
+ */
32
+ protected _getByHash(hash: string, filter?: Json): Promise<Rljson>;
33
+ /**
34
+ * Fetches entries from the table that match the specified criteria.
35
+ * @param where An object representing the criteria to match.
36
+ * @returns A promise that resolves to an array of entries matching the criteria, or null if no entries are found.
37
+ */
38
+ protected _getByWhere(where: Json, filter?: Json): Promise<Rljson>;
39
+ /**
40
+ * Gets the content type of the controller.
41
+ * @returns The content type managed by the controller.
42
+ */
43
+ contentType(): ContentType;
44
+ /**
45
+ * Gets the table configuration of the controller.
46
+ * @returns The table configuration managed by the controller.
47
+ */
48
+ tableCfg(): TableCfg;
49
+ }
@@ -0,0 +1,29 @@
1
+ import { Json, JsonValue } from '@rljson/json';
2
+ import { Cake, CakesTable, InsertHistoryRow, LayerRef, Ref, Rljson, SliceIdsRef, TableKey } from '@rljson/rljson';
3
+ import { Core } from '../core.ts';
4
+ import { BaseController } from './base-controller.ts';
5
+ import { Controller, ControllerChildProperty, ControllerCommands, ControllerRefs } from './controller.ts';
6
+ export interface CakeValue extends Json {
7
+ layers: {
8
+ [layerTable: TableKey]: LayerRef;
9
+ };
10
+ id?: string;
11
+ }
12
+ export type CakeControllerCommands = ControllerCommands | `add@${string}`;
13
+ export interface CakeControllerRefs extends Partial<Cake> {
14
+ sliceIdsTable: TableKey;
15
+ sliceIdsRow: SliceIdsRef;
16
+ base?: Ref;
17
+ }
18
+ export declare class CakeController<N extends string, C extends Cake> extends BaseController<CakesTable, C> implements Controller<CakesTable, C, N> {
19
+ protected readonly _core: Core;
20
+ protected readonly _tableKey: TableKey;
21
+ private _refs?;
22
+ constructor(_core: Core, _tableKey: TableKey, _refs?: CakeControllerRefs | undefined);
23
+ private _baseLayers;
24
+ init(): Promise<void>;
25
+ getChildRefs(where: string | Json, filter?: Json): Promise<ControllerChildProperty[]>;
26
+ insert(command: CakeControllerCommands, value: C, origin?: Ref, refs?: ControllerRefs): Promise<InsertHistoryRow<any>[]>;
27
+ get(where: string | Json, filter?: Json): Promise<Rljson>;
28
+ filterRow(row: Json, key: string, value: JsonValue): Promise<boolean>;
29
+ }
@@ -0,0 +1,68 @@
1
+ import { Json, JsonValue } from '@rljson/json';
2
+ import { ComponentsTable, InsertHistoryRow, Ref, Rljson, TableKey } from '@rljson/rljson';
3
+ import { Core } from '../core.ts';
4
+ import { BaseController } from './base-controller.ts';
5
+ import { Controller, ControllerChildProperty, ControllerCommands, ControllerRefs } from './controller.ts';
6
+ export declare class ComponentController<N extends string, C extends Json, T extends Json> extends BaseController<ComponentsTable<T>, C> implements Controller<ComponentsTable<T>, C, N> {
7
+ protected readonly _core: Core;
8
+ protected readonly _tableKey: TableKey;
9
+ private _refs?;
10
+ private _allowedContentTypes;
11
+ private _resolvedColumns;
12
+ private _refTableKeyToColumnKeyMap;
13
+ constructor(_core: Core, _tableKey: TableKey, _refs?: ControllerRefs | undefined);
14
+ init(): Promise<void>;
15
+ insert(command: ControllerCommands, value: Json, origin?: Ref, refs?: ControllerRefs): Promise<InsertHistoryRow<N>[]>;
16
+ /**
17
+ * Retrieves references to child entries in related tables based on a condition.
18
+ * @param where - The condition to filter the data.
19
+ * @param filter - Optional filter to apply to the data.
20
+ * @returns
21
+ */
22
+ getChildRefs(where: string | Json, filter?: Json): Promise<ControllerChildProperty[]>;
23
+ /**
24
+ * Fetches a specific entry from the table by a partial match. Resolves references as needed.
25
+ * @param where - An object representing a partial match.
26
+ * @returns A promise that resolves to an array of entries matching the criteria, or null if no entries are found.
27
+ */
28
+ protected _getByWhere(where: Json, filter?: Json): Promise<Rljson>;
29
+ private _referencesToWhereClauses;
30
+ private _createRefTableKeyToColumnKeyMap;
31
+ /**
32
+ * Extracts reference columns from the where clause.
33
+ * @param where - The condition to filter the data.
34
+ * @returns An object representing only the reference columns in the where clause.
35
+ */
36
+ private _getWhereReferences;
37
+ /**
38
+ * Removes reference columns from the where clause.
39
+ * @param where - The condition to filter the data.
40
+ * @returns An object representing the where clause without reference columns.
41
+ */
42
+ private _getWhereBase;
43
+ /**
44
+ * Retrieves all reference columns from the resolved columns.
45
+ * @returns An array of ColumnCfg representing the reference columns.
46
+ */
47
+ private get _referenceColumns();
48
+ /**
49
+ * Checks if the where clause contains any reference columns.
50
+ * @param where - The condition to filter the data.
51
+ * @returns A promise that resolves to true if reference columns are present, false otherwise.
52
+ */
53
+ private _hasReferenceColumns;
54
+ /**
55
+ * Resolves reference columns in the where clause.
56
+ * @param columns - The columns to resolve.
57
+ * @returns A promise that resolves to an object containing base and reference columns.
58
+ */
59
+ private _resolveReferenceColumns;
60
+ /**
61
+ * Resolves references based on the where clause.
62
+ * @param where - The condition to filter the data.
63
+ * @returns - A promise that resolves to an object containing resolved references.
64
+ */
65
+ private _resolveReferences;
66
+ private _readRowsWithReferences;
67
+ filterRow(row: Json, key: string, value: JsonValue): Promise<boolean>;
68
+ }
@@ -0,0 +1,48 @@
1
+ import { Json, JsonValue } from '@rljson/json';
2
+ import { ContentType, InsertCommand, InsertHistoryRow, Ref, Rljson, SliceId, TableCfg, TableKey, TableType } from '@rljson/rljson';
3
+ import { Core } from '../core.ts';
4
+ import { CakeControllerRefs } from './cake-controller.ts';
5
+ import { LayerControllerRefs } from './layer-controller.ts';
6
+ export type ControllerRefs = CakeControllerRefs | LayerControllerRefs;
7
+ export type ControllerCommands = InsertCommand;
8
+ export type ControllerRunFn<N extends string, C extends JsonValue> = (command: ControllerCommands, value: C, origin?: Ref, refs?: ControllerRefs) => Promise<InsertHistoryRow<N>[]>;
9
+ export type ControllerChildProperty = {
10
+ tableKey: TableKey;
11
+ columnKey?: string;
12
+ ref: Ref;
13
+ sliceIds?: SliceId[];
14
+ };
15
+ /**
16
+ * Generic interface for a controller that manages a specific table in the database.
17
+ * @template T The type of the table being managed.
18
+ * @template C The type of the Insert.
19
+ * @template N The name of the table being managed.
20
+ * @property {ControllerRunFn<N>} insert - Function to execute a command on the table.
21
+ * @property {() => Promise<void>} init - Initializes the controller.
22
+ * @property {() => Promise<T>} table - Retrieves the current state of the table.
23
+ * @property {(where: string | { [column: string]: JsonValue }) => Promise<Rljson>} get - Fetches data from the table based on a condition.
24
+ * @property {(where: string | Json, filter?: Json) => Promise<Array<{ tableKey: TableKey; ref: Ref }>>} getChildRefs - Retrieves references to child entries in related tables based on a condition.
25
+ * @param {string | Json }} where - The condition to filter the data.
26
+ * @returns {Promise<Json[] | null>} A promise that resolves to an array of JSON objects or null if no data is found.
27
+ * @throws {Error} If the data is invalid.
28
+ */
29
+ export interface Controller<T extends TableType, C extends JsonValue, N extends string> {
30
+ insert: ControllerRunFn<N, C>;
31
+ init(): Promise<void>;
32
+ table(): Promise<T>;
33
+ get(where: string | Json, filter?: Json): Promise<Rljson>;
34
+ getChildRefs(where: string | Json, filter?: Json): Promise<ControllerChildProperty[]>;
35
+ filterRow(row: Json, key: string, value: JsonValue): Promise<boolean>;
36
+ contentType(): ContentType;
37
+ tableCfg(): TableCfg;
38
+ }
39
+ /**
40
+ * Factory function to create a controller based on the content type.
41
+ * @param {ContentType} type - The type of content (e.g., 'layers', 'components', 'cakes').
42
+ * @param {Core} core - The core instance managing the database.
43
+ * @param {TableKey} tableKey - The key identifying the table to be managed.
44
+ * @param {ControllerRefs} [refs] - Optional references for the controller.
45
+ * @returns {Promise<Controller<any, string>>} A promise that resolves to the created controller.
46
+ * @throws {Error} If the controller for the specified type is not implemented.
47
+ */
48
+ export declare const createController: (type: ContentType, core: Core, tableKey: TableKey, refs?: ControllerRefs) => Promise<Controller<TableType, any, string>>;
@@ -0,0 +1,26 @@
1
+ import { Json, JsonValue } from '@rljson/json';
2
+ import { InsertCommand, InsertHistoryRow, Layer, LayerRef, LayersTable, Ref, Rljson, SliceId, SliceIdsRef, TableKey } from '@rljson/rljson';
3
+ import { Core } from '../core.ts';
4
+ import { BaseController } from './base-controller.ts';
5
+ import { Controller, ControllerChildProperty, ControllerRefs } from './controller.ts';
6
+ export interface LayerControllerRefs extends Partial<Layer> {
7
+ base?: LayerRef;
8
+ sliceIdsTable: TableKey;
9
+ sliceIdsTableRow: SliceIdsRef;
10
+ componentsTable: TableKey;
11
+ }
12
+ export declare class LayerController<N extends string, C extends Layer> extends BaseController<LayersTable, C> implements Controller<LayersTable, C, N> {
13
+ protected readonly _core: Core;
14
+ protected readonly _tableKey: TableKey;
15
+ private _refs?;
16
+ constructor(_core: Core, _tableKey: TableKey, _refs?: LayerControllerRefs | undefined);
17
+ init(): Promise<void>;
18
+ insert(command: InsertCommand, value: C, origin?: Ref, refs?: ControllerRefs): Promise<InsertHistoryRow<any>[]>;
19
+ get(where: string | Json, filter?: Json): Promise<Rljson>;
20
+ resolveBaseLayer(layer: Layer): Promise<{
21
+ add: Record<string, string>;
22
+ sliceIds: SliceId[];
23
+ }>;
24
+ getChildRefs(where: string | Json, filter?: Json): Promise<ControllerChildProperty[]>;
25
+ filterRow(row: Json, _: string, value: JsonValue): Promise<boolean>;
26
+ }
@@ -0,0 +1,22 @@
1
+ import { Json, JsonValue } from '@rljson/json';
2
+ import { InsertCommand, InsertHistoryRow, Ref, Rljson, SliceId, SliceIds, SliceIdsRef, SliceIdsTable, TableKey } from '@rljson/rljson';
3
+ import { Core } from '../core.ts';
4
+ import { BaseController } from './base-controller.ts';
5
+ import { Controller, ControllerChildProperty, ControllerRefs } from './controller.ts';
6
+ export interface SliceIdControllerRefs extends Partial<SliceIds> {
7
+ base?: SliceIdsRef;
8
+ }
9
+ export declare class SliceIdController<N extends string, C extends SliceId[]> extends BaseController<SliceIdsTable, C> implements Controller<SliceIdsTable, C, N> {
10
+ protected readonly _core: Core;
11
+ protected readonly _tableKey: TableKey;
12
+ private _refs?;
13
+ constructor(_core: Core, _tableKey: TableKey, _refs?: SliceIdControllerRefs | undefined);
14
+ init(): Promise<void>;
15
+ insert(command: InsertCommand, value: SliceId[], origin?: Ref, refs?: ControllerRefs): Promise<InsertHistoryRow<any>[]>;
16
+ get(where: string | Json, filter?: Json): Promise<Rljson>;
17
+ resolveBaseSliceIds(sliceIds: SliceIds): Promise<{
18
+ add: SliceId[];
19
+ }>;
20
+ getChildRefs(): Promise<ControllerChildProperty[]>;
21
+ filterRow(row: Json, _: string, value: JsonValue): Promise<boolean>;
22
+ }
package/dist/db.d.ts CHANGED
@@ -17,6 +17,11 @@ export type Container = {
17
17
  tree: Json;
18
18
  cell: Cell[];
19
19
  };
20
+ export type GetOptions = {
21
+ skipRljson?: boolean;
22
+ skipTree?: boolean;
23
+ skipCell?: boolean;
24
+ };
20
25
  export type ContainerWithControllers = Container & {
21
26
  controllers: Record<string, Controller<any, any, any>>;
22
27
  };
@@ -48,7 +53,7 @@ export declare class Db {
48
53
  * @returns An array of Rljson objects matching the route and filter
49
54
  * @throws {Error} If the route is not valid or if any controller cannot be created
50
55
  */
51
- get(route: Route, where: string | Json, filter?: ControllerChildProperty[], sliceIds?: SliceId[]): Promise<ContainerWithControllers>;
56
+ get(route: Route, where: string | Json, filter?: ControllerChildProperty[], sliceIds?: SliceId[], options?: GetOptions): Promise<ContainerWithControllers>;
52
57
  /**
53
58
  * Resolves the route and returns corresponding data for any segment of the route,
54
59
  * matching recursive filters and where clauses
@@ -58,9 +63,11 @@ export declare class Db {
58
63
  * @param controllers - The controllers to use for fetching data
59
64
  * @param filter - Optional filter to apply to the data at the current route segment
60
65
  * @param sliceIds - Optional slice IDs to filter the data at the current route segment
66
+ * @param routeAccumulator - The accumulated route up to the current segment
67
+ * @param options - Additional options for fetching data
61
68
  * @returns - An Rljson object matching the route and filters
62
69
  */
63
- _get(route: Route, where: string | Json, controllers: Record<string, Controller<any, any, any>>, filter?: ControllerChildProperty[], sliceIds?: SliceId[], routeAccumulator?: Route): Promise<Container>;
70
+ _get(route: Route, where: string | Json, controllers: Record<string, Controller<any, any, any>>, filter?: ControllerChildProperty[], sliceIds?: SliceId[], routeAccumulator?: Route, options?: GetOptions): Promise<Container>;
64
71
  /**
65
72
  * Get the reference (hash) of a route segment, considering default refs and insertHistory refs
66
73
  * @param segment - The route segment to get the reference for
@@ -104,6 +111,10 @@ export declare class Db {
104
111
  * @param callback - The callback to be unregistered
105
112
  */
106
113
  unregisterObserver(route: Route, callback: NotifyCallback<any>): void;
114
+ /**
115
+ * Unregisters all observers from all routes
116
+ */
117
+ unregisterAllObservers(route: Route): void;
107
118
  /**
108
119
  * Get a controller for a specific table
109
120
  * @param tableKey - The key of the table to get the controller for
@@ -216,8 +227,19 @@ export declare class Db {
216
227
  * @returns A new Rljson with only the isolated property
217
228
  */
218
229
  isolatePropertyFromComponents(rljson: Rljson, propertyKey: string): Rljson;
230
+ /**
231
+ * Clone the Db instance with a new Io instance
232
+ * @param io - The new Io instance
233
+ * @returns A new Db instance with the same cache as the current instance
234
+ */
235
+ clone(io: Io): Db;
219
236
  /**
220
237
  * Get the current cache of the Db instance
221
238
  */
222
239
  get cache(): Map<string, Container>;
240
+ /**
241
+ * Set the cache of the Db instance
242
+ * @param cache - The new cache to set
243
+ */
244
+ setCache(cache: Map<string, Container>): void;
223
245
  }