@antelopejs/interface-database 0.1.6 → 0.1.7

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/dist/antelope.test.js +1 -1
  2. package/dist/atomic.d.ts +3 -37
  3. package/dist/atomic.js +39 -121
  4. package/dist/atomic.js.map +1 -1
  5. package/dist/common.d.ts +1 -58
  6. package/dist/common.js +15 -41
  7. package/dist/common.js.map +1 -1
  8. package/dist/datum.d.ts +1 -55
  9. package/dist/datum.js +2 -66
  10. package/dist/datum.js.map +1 -1
  11. package/dist/query.d.ts +11 -12
  12. package/dist/query.js +24 -22
  13. package/dist/query.js.map +1 -1
  14. package/dist/schema.d.ts +3 -120
  15. package/dist/schema.js +7 -88
  16. package/dist/schema.js.map +1 -1
  17. package/dist/selection.d.ts +1 -104
  18. package/dist/selection.js +5 -123
  19. package/dist/selection.js.map +1 -1
  20. package/dist/staged-query/atomic.d.ts +34 -0
  21. package/dist/staged-query/atomic.js +108 -0
  22. package/dist/staged-query/atomic.js.map +1 -0
  23. package/dist/staged-query/common.d.ts +58 -0
  24. package/dist/staged-query/common.js +45 -0
  25. package/dist/staged-query/common.js.map +1 -0
  26. package/dist/staged-query/datum.d.ts +55 -0
  27. package/dist/staged-query/datum.js +70 -0
  28. package/dist/staged-query/datum.js.map +1 -0
  29. package/dist/staged-query/index.d.ts +8 -0
  30. package/dist/staged-query/index.js +36 -0
  31. package/dist/staged-query/index.js.map +1 -0
  32. package/dist/staged-query/query.d.ts +3 -0
  33. package/dist/staged-query/query.js +9 -0
  34. package/dist/staged-query/query.js.map +1 -0
  35. package/dist/staged-query/schema.d.ts +122 -0
  36. package/dist/staged-query/schema.js +95 -0
  37. package/dist/staged-query/schema.js.map +1 -0
  38. package/dist/staged-query/selection.d.ts +104 -0
  39. package/dist/staged-query/selection.js +126 -0
  40. package/dist/staged-query/selection.js.map +1 -0
  41. package/dist/staged-query/stream.d.ts +185 -0
  42. package/dist/staged-query/stream.js +221 -0
  43. package/dist/staged-query/stream.js.map +1 -0
  44. package/dist/staged-query/valueproxy.d.ts +415 -0
  45. package/dist/staged-query/valueproxy.js +562 -0
  46. package/dist/staged-query/valueproxy.js.map +1 -0
  47. package/dist/stream.d.ts +1 -185
  48. package/dist/stream.js +2 -217
  49. package/dist/stream.js.map +1 -1
  50. package/dist/valueproxy.d.ts +1 -415
  51. package/dist/valueproxy.js +15 -559
  52. package/dist/valueproxy.js.map +1 -1
  53. package/package.json +9 -1
package/dist/schema.d.ts CHANGED
@@ -1,124 +1,7 @@
1
1
  import { RegisteringProxy } from "@antelopejs/interface-core";
2
- import { Query } from "./query";
3
- import { Table } from "./selection";
4
- import { StagedObject } from "./common";
5
- /**
6
- * Secondary table index definition
7
- */
8
- export interface IndexDefinition {
9
- /**
10
- * Fields to use for compound indexes
11
- */
12
- fields?: string[];
13
- /**
14
- * Whether or not this is a multi index
15
- */
16
- multi?: boolean;
17
- }
18
- export type StringFieldType = string | Array<StringFieldType> | {
19
- [subfield: string]: StringFieldType;
20
- };
21
- interface IoTsCodec {
22
- readonly _A: unknown;
23
- readonly _O: unknown;
24
- readonly _I: unknown;
25
- readonly name: string;
26
- is(u: unknown): boolean;
27
- encode(a: never): unknown;
28
- decode(i: unknown): unknown;
29
- }
30
- export type FieldType = StringFieldType | IoTsCodec;
31
- /**
32
- * Schema table definition
33
- */
34
- export interface TableDefinition {
35
- /**
36
- * Field names and their data types
37
- */
38
- fields: Record<string, FieldType>;
39
- /**
40
- * List of secondary indexes
41
- */
42
- indexes: Record<string, IndexDefinition>;
43
- }
44
- /**
45
- * Table names and their definitions
46
- */
47
- export interface SchemaDefinition {
48
- [tableName: string]: TableDefinition;
49
- }
50
- /**
51
- * Sentinel instance id meaning "operate across all instances of the schema".
52
- */
53
- export declare const CROSS_INSTANCE: unique symbol;
54
- /**
55
- * Instance identifier accepted by {@link Schema.instance}.
56
- */
57
- export type InstanceId = string | typeof CROSS_INSTANCE;
2
+ import { Schema as StagedSchema, type SchemaDefinition } from "./staged-query/schema";
58
3
  export declare const Schemas: RegisteringProxy<(name: string, def: SchemaDefinition) => void>;
59
- /**
60
- * A schema determines the structure of a database
61
- *
62
- * Each schema can have multiple instances. The internal organization of these
63
- * instances is left up to the module implementation.
64
- */
65
- export declare class Schema<T = any> extends StagedObject {
66
- readonly id: string;
67
- readonly definition: SchemaDefinition;
68
- private static readonly registry;
69
- /**
70
- * Retrieves a previously defined schema by its ID
71
- *
72
- * @param id Schema ID
73
- * @returns The schema instance, or undefined if not found
74
- */
75
- static get(id: string): Schema | undefined;
76
- /**
77
- * Define a new schema with the given ID
78
- *
79
- * @param id ID of this schema, changing this will leave previous data inaccessible
80
- * @param definition Schema definition (tables, fields, indexes..)
81
- */
4
+ export declare class Schema<T = any> extends StagedSchema<T, SchemaDefinition> {
82
5
  constructor(id: string, definition: SchemaDefinition);
83
- /**
84
- * Gets a specific instance of the schema
85
- *
86
- * @param id Instance ID, or {@link CROSS_INSTANCE} for a cross-instance query
87
- * @returns Schema instance
88
- */
89
- instance(id?: InstanceId): SchemaInstance<T>;
90
- /**
91
- * Creates a new instance of the schema
92
- *
93
- * @param id Instance ID
94
- * @returns Created instance ID
95
- */
96
- createInstance(id?: string): Query<string>;
97
- /**
98
- * Destroys an existing instance of the schema
99
- *
100
- * @param id Instance ID
101
- */
102
- destroyInstance(id?: string): Query<void>;
103
- /**
104
- * Lists the IDs of named instances of this schema.
105
- *
106
- * The default (unnamed) instance is not included.
107
- *
108
- * @returns IDs of named instances of this schema
109
- */
110
- listInstances(): Query<string[]>;
111
- }
112
- /**
113
- * Schema instance, could be a database or a filtered portion of one depending on the implementation
114
- */
115
- export declare class SchemaInstance<T> extends StagedObject {
116
- /**
117
- * Gets a table from the instance
118
- *
119
- * @param id Table name
120
- * @returns Table
121
- */
122
- table<TK extends keyof T>(id: TK): Table<T[TK]>;
123
6
  }
124
- export {};
7
+ export { CROSS_INSTANCE, type FieldType, type IndexDefinition, type InstanceId, type SchemaDefinition, SchemaInstance, type StringFieldType, type TableDefinition, } from "./staged-query/schema";
package/dist/schema.js CHANGED
@@ -1,99 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SchemaInstance = exports.Schema = exports.Schemas = exports.CROSS_INSTANCE = void 0;
3
+ exports.SchemaInstance = exports.CROSS_INSTANCE = exports.Schema = exports.Schemas = void 0;
4
4
  const interface_core_1 = require("@antelopejs/interface-core");
5
- const query_1 = require("./query");
6
- const selection_1 = require("./selection");
7
- const common_1 = require("./common");
8
- /**
9
- * Sentinel instance id meaning "operate across all instances of the schema".
10
- */
11
- exports.CROSS_INSTANCE = Symbol("antelopejs:cross-instance");
5
+ const schema_1 = require("./staged-query/schema");
12
6
  //@internal
13
7
  exports.Schemas = new interface_core_1.RegisteringProxy();
14
- /**
15
- * A schema determines the structure of a database
16
- *
17
- * Each schema can have multiple instances. The internal organization of these
18
- * instances is left up to the module implementation.
19
- */
20
- class Schema extends common_1.StagedObject {
21
- id;
22
- definition;
23
- static registry = new Map();
24
- /**
25
- * Retrieves a previously defined schema by its ID
26
- *
27
- * @param id Schema ID
28
- * @returns The schema instance, or undefined if not found
29
- */
30
- static get(id) {
31
- return Schema.registry.get(id);
32
- }
33
- /**
34
- * Define a new schema with the given ID
35
- *
36
- * @param id ID of this schema, changing this will leave previous data inaccessible
37
- * @param definition Schema definition (tables, fields, indexes..)
38
- */
8
+ class Schema extends schema_1.Schema {
39
9
  constructor(id, definition) {
40
- super((0, common_1.QueryStage)("schema", { id }));
41
- this.id = id;
42
- this.definition = definition;
10
+ super(id, definition);
43
11
  exports.Schemas.register(id, definition);
44
- Schema.registry.set(id, this);
45
- }
46
- /**
47
- * Gets a specific instance of the schema
48
- *
49
- * @param id Instance ID, or {@link CROSS_INSTANCE} for a cross-instance query
50
- * @returns Schema instance
51
- */
52
- instance(id) {
53
- return this.stage((SchemaInstance), "instance", { id });
54
- }
55
- /**
56
- * Creates a new instance of the schema
57
- *
58
- * @param id Instance ID
59
- * @returns Created instance ID
60
- */
61
- createInstance(id) {
62
- return this.stage((query_1.Query), "createInstance", { id });
63
- }
64
- /**
65
- * Destroys an existing instance of the schema
66
- *
67
- * @param id Instance ID
68
- */
69
- destroyInstance(id) {
70
- return this.stage((query_1.Query), "destroyInstance", { id });
71
- }
72
- /**
73
- * Lists the IDs of named instances of this schema.
74
- *
75
- * The default (unnamed) instance is not included.
76
- *
77
- * @returns IDs of named instances of this schema
78
- */
79
- listInstances() {
80
- return this.stage((query_1.Query), "listInstances");
81
12
  }
82
13
  }
83
14
  exports.Schema = Schema;
84
- /**
85
- * Schema instance, could be a database or a filtered portion of one depending on the implementation
86
- */
87
- class SchemaInstance extends common_1.StagedObject {
88
- /**
89
- * Gets a table from the instance
90
- *
91
- * @param id Table name
92
- * @returns Table
93
- */
94
- table(id) {
95
- return this.stage((selection_1.Table), "table", { id });
96
- }
97
- }
98
- exports.SchemaInstance = SchemaInstance;
15
+ var schema_2 = require("./staged-query/schema");
16
+ Object.defineProperty(exports, "CROSS_INSTANCE", { enumerable: true, get: function () { return schema_2.CROSS_INSTANCE; } });
17
+ Object.defineProperty(exports, "SchemaInstance", { enumerable: true, get: function () { return schema_2.SchemaInstance; } });
99
18
  //# sourceMappingURL=schema.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":";;;AAAA,+DAA8D;AAE9D,mCAAgC;AAChC,2CAAoC;AACpC,qCAAoD;AAwDpD;;GAEG;AACU,QAAA,cAAc,GAAkB,MAAM,CACjD,2BAA2B,CAC5B,CAAC;AAOF,WAAW;AACE,QAAA,OAAO,GAAG,IAAI,iCAAgB,EAExC,CAAC;AAEJ;;;;;GAKG;AACH,MAAa,MAAgB,SAAQ,qBAAY;IAoB7B;IACA;IApBV,MAAM,CAAU,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;IAE7D;;;;;OAKG;IACI,MAAM,CAAC,GAAG,C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D,CAAC;CACF;AAVD,wCAUC"}
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":";;;AAAA,+DAA8D;AAE9D,kDAG+B;AAE/B,WAAW;AACE,QAAA,OAAO,GAAG,IAAI,iCAAgB,EAExC,CAAC;AAEJ,MAAa,MAAgB,SAAQ,eAAiC;IACpE,YAAmB,EAAU,EAAE,UAA4B;QACzD,KAAK,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;QACtB,eAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;IACnC,CAAC;CACF;AALD,wBAKC;AAED,gDAS+B;AAR7B,wGAAA,cAAc,OAAA;AAKd,wGAAA,cAAc,OAAA"}
@@ -1,104 +1 @@
1
- import { Datum } from "./datum";
2
- import { Query } from "./query";
3
- import { Stream } from "./stream";
4
- import { ValueProxy, type ValueProxyOrValue } from "./valueproxy";
5
- import type { Changes, DeepPartial, ExtractType, InsertOptions } from "./common";
6
- import { type AtomicMutation, type AtomicMutationOutcome } from "./atomic";
7
- type SelectionKey = string | number | boolean;
8
- /**
9
- * Selection containing a single element
10
- */
11
- export declare class SingleSelection<T> extends Datum<T> {
12
- /**
13
- * Update fields of this selection with the given values
14
- *
15
- * @param document Partial document with new values
16
- * @returns Number of modified documents
17
- */
18
- update(document: DeepPartial<T>): Query<number>;
19
- update<U>(document: (val: ValueProxy<T>) => U): ExtractType<U> extends DeepPartial<T> ? Query<number> : never;
20
- /**
21
- * Replace documents of this selection
22
- *
23
- * @param document Partial document to replace with
24
- * @returns Number of modified documents
25
- */
26
- replace(document: DeepPartial<T>): Query<number>;
27
- /**
28
- * Delete selected documents
29
- *
30
- * @returns Number of deleted documents
31
- */
32
- delete(): Query<number>;
33
- /**
34
- * Turns this selection into a change feed
35
- *
36
- * @returns Change feed
37
- */
38
- changes(): Query<Changes<T>[]>;
39
- }
40
- /**
41
- * Selection containing any number of elements
42
- */
43
- export declare class Selection<T> extends Stream<T> {
44
- /**
45
- * Update fields of this selection with the given values
46
- *
47
- * @param document Partial document with new values
48
- * @returns Number of modified documents
49
- */
50
- update(document: DeepPartial<T>): Query<number>;
51
- update<U>(document: (val: ValueProxy<T>) => U): ExtractType<U> extends DeepPartial<T> ? Query<number> : never;
52
- /**
53
- * Replace documents of this selection
54
- *
55
- * @param document Partial document to replace with
56
- * @returns Number of modified documents
57
- */
58
- replace(document: DeepPartial<T>): Query<number>;
59
- /**
60
- * Delete selected documents
61
- *
62
- * @returns Number of deleted documents
63
- */
64
- delete(): Query<number>;
65
- }
66
- /**
67
- * Database table
68
- */
69
- export declare class Table<T> extends Selection<T> {
70
- /** Atomically checks one record's revision and patches or deletes it without upsert. */
71
- atomicMutation(key: string, request: AtomicMutation<T>): Query<AtomicMutationOutcome>;
72
- /**
73
- * Inserts one or more documents into this table
74
- *
75
- * @param obj Document(s) to insert
76
- * @returns Inserted IDs
77
- */
78
- insert(obj: DeepPartial<T> | DeepPartial<T>[], options?: InsertOptions): Query<string[]>;
79
- /**
80
- * Gets a document using its primary key
81
- *
82
- * @param key Primary key value
83
- * @returns Single document selection
84
- */
85
- get(key: ValueProxyOrValue<string>): SingleSelection<T>;
86
- /**
87
- * Gets multiple documents using a secondary index
88
- *
89
- * @param keys Key value(s)
90
- * @param index Secondary index, will use the primary key if undefined
91
- * @returns Multiple document selection
92
- */
93
- getAll(keys: ValueProxyOrValue<SelectionKey> | ValueProxyOrValue<SelectionKey>[], index?: string): Selection<T>;
94
- /**
95
- * Gets multiple documents using a secondary index and bounding values
96
- *
97
- * @param index Secondary index
98
- * @param low Lowest value of the range
99
- * @param high Highest value of the range (excluded)
100
- * @returns Multiple document selection
101
- */
102
- between<TK extends keyof T>(index: TK, low: T[TK], high: T[TK]): Selection<T>;
103
- }
104
- export {};
1
+ export { Selection, SingleSelection, Table } from "./staged-query/selection";
package/dist/selection.js CHANGED
@@ -1,126 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Table = exports.Selection = exports.SingleSelection = void 0;
4
- const datum_1 = require("./datum");
5
- const query_1 = require("./query");
6
- const stream_1 = require("./stream");
7
- const valueproxy_1 = require("./valueproxy");
8
- const atomic_1 = require("./atomic");
9
- /**
10
- * Selection containing a single element
11
- */
12
- class SingleSelection extends datum_1.Datum {
13
- update(document) {
14
- if (typeof document === "function") {
15
- return this.stage((query_1.Query), "update", undefined, this.callfunc(document, (valueproxy_1.ValueProxy)));
16
- }
17
- return this.stage((query_1.Query), "update", undefined, document);
18
- }
19
- /**
20
- * Replace documents of this selection
21
- *
22
- * @param document Partial document to replace with
23
- * @returns Number of modified documents
24
- */
25
- replace(document) {
26
- return this.stage((query_1.Query), "replace", undefined, document);
27
- }
28
- /**
29
- * Delete selected documents
30
- *
31
- * @returns Number of deleted documents
32
- */
33
- delete() {
34
- return this.stage((query_1.Query), "delete");
35
- }
36
- /**
37
- * Turns this selection into a change feed
38
- *
39
- * @returns Change feed
40
- */
41
- changes() {
42
- return this.stage((query_1.Query), "changes");
43
- }
44
- }
45
- exports.SingleSelection = SingleSelection;
46
- /**
47
- * Selection containing any number of elements
48
- */
49
- class Selection extends stream_1.Stream {
50
- update(document) {
51
- if (typeof document === "function") {
52
- return this.stage((query_1.Query), "update", undefined, this.callfunc(document, (valueproxy_1.ValueProxy)));
53
- }
54
- return this.stage((query_1.Query), "update", undefined, document);
55
- }
56
- /**
57
- * Replace documents of this selection
58
- *
59
- * @param document Partial document to replace with
60
- * @returns Number of modified documents
61
- */
62
- replace(document) {
63
- return this.stage((query_1.Query), "replace", undefined, document);
64
- }
65
- /**
66
- * Delete selected documents
67
- *
68
- * @returns Number of deleted documents
69
- */
70
- delete() {
71
- return this.stage((query_1.Query), "delete");
72
- }
73
- }
74
- exports.Selection = Selection;
75
- /**
76
- * Database table
77
- */
78
- class Table extends Selection {
79
- /** Atomically checks one record's revision and patches or deletes it without upsert. */
80
- atomicMutation(key, request) {
81
- (0, atomic_1.ValidateAtomicMutationTable)(this.stages);
82
- (0, atomic_1.ValidateAtomicMutation)(key, request);
83
- return this.stage(atomic_1.AtomicMutationQuery, "atomicMutation", undefined, key, request);
84
- }
85
- /**
86
- * Inserts one or more documents into this table
87
- *
88
- * @param obj Document(s) to insert
89
- * @returns Inserted IDs
90
- */
91
- insert(obj, options) {
92
- return this.stage((query_1.Query), "insert", options, obj);
93
- }
94
- /**
95
- * Gets a document using its primary key
96
- *
97
- * @param key Primary key value
98
- * @returns Single document selection
99
- */
100
- get(key) {
101
- return this.stage((SingleSelection), "get", undefined, key);
102
- }
103
- /**
104
- * Gets multiple documents using a secondary index
105
- *
106
- * @param keys Key value(s)
107
- * @param index Secondary index, will use the primary key if undefined
108
- * @returns Multiple document selection
109
- */
110
- getAll(keys, index) {
111
- return this.stage((Selection), "getAll", { index }, keys);
112
- }
113
- /**
114
- * Gets multiple documents using a secondary index and bounding values
115
- *
116
- * @param index Secondary index
117
- * @param low Lowest value of the range
118
- * @param high Highest value of the range (excluded)
119
- * @returns Multiple document selection
120
- */
121
- between(index, low, high) {
122
- return this.stage((Selection), "between", { index }, low, high);
123
- }
124
- }
125
- exports.Table = Table;
3
+ exports.Table = exports.SingleSelection = exports.Selection = void 0;
4
+ var selection_1 = require("./staged-query/selection");
5
+ Object.defineProperty(exports, "Selection", { enumerable: true, get: function () { return selection_1.Selection; } });
6
+ Object.defineProperty(exports, "SingleSelection", { enumerable: true, get: function () { return selection_1.SingleSelection; } });
7
+ Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return selection_1.Table; } });
126
8
  //# sourceMappingURL=selection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"selection.js","sourceRoot":"","sources":["../src/selection.ts"],"names":[],"mappings":";;;AAAA,mCAAgC;AAChC,mCAAgC;AAChC,qCAAkC;AAClC,6CAAkE;AAOlE,qCAMkB;AAIlB;;GAEG;AACH,MAAa,eAAmB,SAAQ,aAAQ;IAWvC,MAAM,CAAC,QAA4D;QACxE,IAAI,OAAO,QAAQ,KA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
+ {"version":3,"file":"selection.js","sourceRoot":"","sources":["../src/selection.ts"],"names":[],"mappings":";;;AAAA,sDAA6E;AAApE,sGAAA,SAAS,OAAA;AAAE,4GAAA,eAAe,OAAA;AAAE,kGAAA,KAAK,OAAA"}
@@ -0,0 +1,34 @@
1
+ import { Query } from "./query";
2
+ import type { QueryStage } from "./common";
3
+ /** Matches an absent revision field on an existing record, not stored null. */
4
+ export interface MissingRevision {
5
+ kind: "missing";
6
+ }
7
+ export interface AtomicCondition<T> {
8
+ revisionField: keyof T & string;
9
+ expectedRevision: string | MissingRevision;
10
+ }
11
+ /** Replaces supplied top-level fields and atomically installs a fresh revision. */
12
+ export interface AtomicUpdate<T> extends AtomicCondition<T> {
13
+ type: "update";
14
+ nextRevision: string;
15
+ patch: Partial<T>;
16
+ }
17
+ export interface AtomicDelete<T> extends AtomicCondition<T> {
18
+ type: "delete";
19
+ }
20
+ export type AtomicEqualityValue = string | number | boolean | Date;
21
+ /** Deletes by one observed scalar value; does not provide revision or ABA protection. */
22
+ export interface AtomicDeleteIfEqual<T> {
23
+ type: "deleteIfEqual";
24
+ field: keyof T & string;
25
+ expectedValue: AtomicEqualityValue;
26
+ }
27
+ export type AtomicMutation<T> = AtomicUpdate<T> | AtomicDelete<T> | AtomicDeleteIfEqual<T>;
28
+ export type AtomicMutationOutcome = "applied" | "not-applied" | "unknown";
29
+ /** Validates literal input; adapters must additionally supply scope fields. */
30
+ export declare function ValidateAtomicMutation<T>(key: string, request: AtomicMutation<T>, protectedFields?: readonly string[]): void;
31
+ /** Rejects selections and cross-instance operations before adapter dispatch. */
32
+ export declare function ValidateAtomicMutationTable(stages: QueryStage[]): void;
33
+ export declare class AtomicMutationQuery extends Query<AtomicMutationOutcome> {
34
+ }
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AtomicMutationQuery = void 0;
4
+ exports.ValidateAtomicMutation = ValidateAtomicMutation;
5
+ exports.ValidateAtomicMutationTable = ValidateAtomicMutationTable;
6
+ const query_1 = require("./query");
7
+ const tableStages = ["schema", "instance", "table"];
8
+ const identityFields = ["id", "_id"];
9
+ function isRecord(value) {
10
+ return (value !== null &&
11
+ typeof value === "object" &&
12
+ [Object.prototype, null].includes(Object.getPrototypeOf(value)));
13
+ }
14
+ function isToken(value) {
15
+ return typeof value === "string" && value.length > 0;
16
+ }
17
+ function validateField(field) {
18
+ if (!isToken(field) || field.includes(".") || field.startsWith("$")) {
19
+ throw new TypeError("Atomic mutation fields must be literal field names");
20
+ }
21
+ }
22
+ function validateConstant(value, ancestors = new Set()) {
23
+ if (value === null || ["string", "boolean"].includes(typeof value))
24
+ return;
25
+ if (typeof value === "number" && Number.isFinite(value))
26
+ return;
27
+ if (value instanceof Date && Number.isFinite(value.getTime()))
28
+ return;
29
+ if ((!Array.isArray(value) && !isRecord(value)) || ancestors.has(value)) {
30
+ throw new TypeError("Atomic mutation patches must contain constant data");
31
+ }
32
+ if (Array.isArray(value) && Object.keys(value).length !== value.length) {
33
+ throw new TypeError("Atomic mutation arrays must not contain holes");
34
+ }
35
+ ancestors.add(value);
36
+ for (const [field, child] of Object.entries(value)) {
37
+ validateField(field);
38
+ validateConstant(child, ancestors);
39
+ }
40
+ ancestors.delete(value);
41
+ }
42
+ /** Validates literal input; adapters must additionally supply scope fields. */
43
+ function ValidateAtomicMutation(key, request, protectedFields = []) {
44
+ if (!isToken(key) || !isRecord(request)) {
45
+ throw new TypeError("Atomic mutation requires one literal record identity");
46
+ }
47
+ const forbidden = [...identityFields, ...protectedFields];
48
+ if (request.type === "deleteIfEqual") {
49
+ validateEquality(request, forbidden);
50
+ return;
51
+ }
52
+ validateField(request.revisionField);
53
+ if (forbidden.includes(request.revisionField)) {
54
+ throw new TypeError("Atomic mutation revision cannot be an identity field");
55
+ }
56
+ const expected = request.expectedRevision;
57
+ const isMissing = isRecord(expected) &&
58
+ expected.kind === "missing" &&
59
+ Object.keys(expected).length === 1;
60
+ if (!isToken(expected) && !isMissing) {
61
+ throw new TypeError("Expected revision must be a token or missing tag");
62
+ }
63
+ if (request.type === "delete")
64
+ return;
65
+ if (request.type !== "update") {
66
+ throw new TypeError("Unknown atomic mutation operation");
67
+ }
68
+ validateUpdate(request, forbidden);
69
+ }
70
+ function validateEquality(request, forbidden) {
71
+ validateField(request.field);
72
+ if (forbidden.includes(request.field)) {
73
+ throw new TypeError("Atomic equality field cannot be an identity field");
74
+ }
75
+ const value = request.expectedValue;
76
+ if (!["string", "number", "boolean"].includes(typeof value) &&
77
+ !(value instanceof Date)) {
78
+ throw new TypeError("Atomic equality requires a scalar or Date");
79
+ }
80
+ validateConstant(value);
81
+ }
82
+ function validateUpdate(request, forbidden) {
83
+ if (!isToken(request.nextRevision) ||
84
+ request.nextRevision === request.expectedRevision) {
85
+ throw new TypeError("Atomic mutation requires a changed revision token");
86
+ }
87
+ if (!isRecord(request.patch)) {
88
+ throw new TypeError("Atomic mutation patch must be an object");
89
+ }
90
+ forbidden.push(request.revisionField);
91
+ if (Object.keys(request.patch).some((field) => forbidden.includes(field))) {
92
+ throw new TypeError("Atomic mutation patch cannot change identity or revision");
93
+ }
94
+ validateConstant(request.patch);
95
+ }
96
+ /** Rejects selections and cross-instance operations before adapter dispatch. */
97
+ function ValidateAtomicMutationTable(stages) {
98
+ if (stages.length !== tableStages.length ||
99
+ stages.some((stage, index) => stage.stage !== tableStages[index]) ||
100
+ (stages[1].options?.id !== undefined &&
101
+ typeof stages[1].options.id !== "string")) {
102
+ throw new TypeError("Atomic mutation requires one instance-scoped table");
103
+ }
104
+ }
105
+ class AtomicMutationQuery extends query_1.Query {
106
+ }
107
+ exports.AtomicMutationQuery = AtomicMutationQuery;
108
+ //# sourceMappingURL=atomic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"atomic.js","sourceRoot":"","sources":["../../src/staged-query/atomic.ts"],"names":[],"mappings":";;;AAgFA,wDA8BC;AAwCD,kEASC;AA/JD,mCAAgC;AAwChC,MAAM,WAAW,G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