@antelopejs/interface-database 0.1.6 → 0.1.8

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 (58) hide show
  1. package/README.md +0 -2
  2. package/dist/antelope.test.js +1 -1
  3. package/dist/atomic.d.ts +3 -37
  4. package/dist/atomic.js +39 -121
  5. package/dist/atomic.js.map +1 -1
  6. package/dist/common.d.ts +1 -58
  7. package/dist/common.js +15 -41
  8. package/dist/common.js.map +1 -1
  9. package/dist/datum.d.ts +1 -55
  10. package/dist/datum.js +2 -66
  11. package/dist/datum.js.map +1 -1
  12. package/dist/query.d.ts +11 -12
  13. package/dist/query.js +24 -22
  14. package/dist/query.js.map +1 -1
  15. package/dist/schema.d.ts +3 -120
  16. package/dist/schema.js +7 -88
  17. package/dist/schema.js.map +1 -1
  18. package/dist/selection.d.ts +1 -104
  19. package/dist/selection.js +5 -123
  20. package/dist/selection.js.map +1 -1
  21. package/dist/staged-query/atomic.d.ts +34 -0
  22. package/dist/staged-query/atomic.js +108 -0
  23. package/dist/staged-query/atomic.js.map +1 -0
  24. package/dist/staged-query/common.d.ts +58 -0
  25. package/dist/staged-query/common.js +45 -0
  26. package/dist/staged-query/common.js.map +1 -0
  27. package/dist/staged-query/datum.d.ts +55 -0
  28. package/dist/staged-query/datum.js +70 -0
  29. package/dist/staged-query/datum.js.map +1 -0
  30. package/dist/staged-query/index.d.ts +8 -0
  31. package/dist/staged-query/index.js +36 -0
  32. package/dist/staged-query/index.js.map +1 -0
  33. package/dist/staged-query/query.d.ts +3 -0
  34. package/dist/staged-query/query.js +9 -0
  35. package/dist/staged-query/query.js.map +1 -0
  36. package/dist/staged-query/schema.d.ts +137 -0
  37. package/dist/staged-query/schema.js +99 -0
  38. package/dist/staged-query/schema.js.map +1 -0
  39. package/dist/staged-query/selection.d.ts +104 -0
  40. package/dist/staged-query/selection.js +126 -0
  41. package/dist/staged-query/selection.js.map +1 -0
  42. package/dist/staged-query/stream.d.ts +185 -0
  43. package/dist/staged-query/stream.js +221 -0
  44. package/dist/staged-query/stream.js.map +1 -0
  45. package/dist/staged-query/valueproxy.d.ts +415 -0
  46. package/dist/staged-query/valueproxy.js +562 -0
  47. package/dist/staged-query/valueproxy.js.map +1 -0
  48. package/dist/stream.d.ts +1 -185
  49. package/dist/stream.js +2 -217
  50. package/dist/stream.js.map +1 -1
  51. package/dist/valueproxy.d.ts +1 -415
  52. package/dist/valueproxy.js +15 -559
  53. package/dist/valueproxy.js.map +1 -1
  54. package/docs/2.operations/5.indexes.md +24 -0
  55. package/docs/3.results/3.index_changes.md +11 -0
  56. package/docs/3.results/5.database_changes.md +2 -0
  57. package/package.json +13 -4
  58. package/skills/database-interface/SKILL.md +1 -1
@@ -0,0 +1,58 @@
1
+ import type { Datum } from "./datum";
2
+ import type { Query } from "./query";
3
+ import type { ValueProxy, ValueProxyOrValue } from "./valueproxy";
4
+ export type StagedConstructor<T extends StagedObject = StagedObject> = new (newStage: QueryStage, previous?: StagedObject) => T;
5
+ /**
6
+ * Recursive Partial generic type
7
+ */
8
+ export type DeepPartial<T> = {
9
+ [K in keyof T]?: T[K] extends Array<infer U1> ? Array<DeepPartial<U1>> : T[K] extends ReadonlyArray<infer U2> ? ReadonlyArray<DeepPartial<U2>> : DeepPartial<T[K]>;
10
+ };
11
+ /**
12
+ * Change event
13
+ */
14
+ export interface Changes<T> {
15
+ /**
16
+ * The type of change that occured
17
+ *
18
+ * Possible values: added, removed, modified
19
+ */
20
+ changeType: "added" | "removed" | "modified";
21
+ /**
22
+ * Value prior to the change
23
+ */
24
+ oldValue?: T;
25
+ /**
26
+ * New value after the change
27
+ */
28
+ newValue?: T;
29
+ }
30
+ export interface InsertOptions {
31
+ conflict?: "update" | "replace";
32
+ }
33
+ export interface QueryStage {
34
+ stage: string;
35
+ options?: any;
36
+ args: any[];
37
+ }
38
+ export declare function QueryStage(stage: string, options?: any, ...args: any[]): {
39
+ stage: string;
40
+ options: any;
41
+ args: any[];
42
+ };
43
+ export declare class StagedObject {
44
+ protected readonly stages: QueryStage[];
45
+ constructor(newStage: QueryStage, previous?: StagedObject);
46
+ build(): QueryStage[];
47
+ protected stage(type: undefined, stage: string, options?: any, ...args: any[]): this;
48
+ protected stage<T extends StagedObject>(type: StagedConstructor<T>, stage: string, options?: any, ...args: any[]): T;
49
+ private static nextargid;
50
+ protected callfunc<T extends StagedObject[]>(func: (...args: T) => any, ...args: (typeof StagedObject)[]): QueryStage;
51
+ }
52
+ export type Value<T> = Datum<T> | ValueProxyOrValue<T>;
53
+ type UnknownObject = Record<keyof any, unknown>;
54
+ type ExtractTypeObject<T extends UnknownObject> = T extends infer O ? {
55
+ [K in keyof O]: ExtractType<O[K]>;
56
+ } : never;
57
+ export type ExtractType<T> = T extends ValueProxy<infer A> ? A : T extends Query<infer A> ? A : T extends UnknownObject ? ExtractTypeObject<T> : T extends Array<infer A> ? Array<ExtractType<A>> : T;
58
+ export {};
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StagedObject = void 0;
4
+ exports.QueryStage = QueryStage;
5
+ function QueryStage(stage, options, ...args) {
6
+ return {
7
+ stage,
8
+ options,
9
+ args,
10
+ };
11
+ }
12
+ class StagedObject {
13
+ stages;
14
+ constructor(newStage, previous) {
15
+ this.stages = previous ? [...previous.stages, newStage] : [newStage];
16
+ }
17
+ //@internal
18
+ build() {
19
+ return this.stages;
20
+ }
21
+ stage(type, stage, options, ...args) {
22
+ const Constructor = type ?? this.constructor;
23
+ return new Constructor({
24
+ stage,
25
+ options,
26
+ args,
27
+ }, this);
28
+ }
29
+ static nextargid = 0;
30
+ callfunc(func, ...args) {
31
+ const argNumbers = [];
32
+ const argValues = [];
33
+ for (let i = 0; i < args.length; ++i) {
34
+ const id = StagedObject.nextargid++;
35
+ argNumbers[i] = id;
36
+ argValues[i] = new args[i](QueryStage("arg", undefined, id));
37
+ }
38
+ return {
39
+ stage: "func", // TODO: using the query stage structure here doesnt make any sense
40
+ args: [argNumbers, func(...argValues)],
41
+ };
42
+ }
43
+ }
44
+ exports.StagedObject = StagedObject;
45
+ //# sourceMappingURL=common.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/staged-query/common.ts"],"names":[],"mappings":";;;AAoDA,gCAMC;AAND,SAAgB,UAAU,CAAC,KAAa,EAAE,OAAa,EAAE,GAAG,IAAW;IACrE,OAAO;QACL,KAAK;QACL,OAAO;QACP,IAAI;KACL,CAAC;AACJ,CAAC;AAED,MAAa,YAAY;IACJ,MAAM,CAAe;IAExC,YAAmB,QAAoB,EAAE,QAAuB;QAC9D,IAAI,CAAC,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;IACvE,CAAC;IAED,WAAW;IACJ,KAAK;QACV,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAcS,KAAK,CACb,IAAsC,EACtC,KAAa,EACb,OAAa,EACb,GAAG,IAAW;QAEd,MAAM,WAAW,GAAG,IAAI,IAAK,IAAI,CAAC,WAAoC,CAAC;QACvE,OAAO,IAAI,WAAW,CACpB;YACE,KAAK;YACL,OAAO;YACP,IAAI;SACL,EACD,IAAI,CACL,CAAC;IACJ,CAAC;IAEO,MAAM,CAAC,SAAS,GAAG,CAAC,CAAC;IACnB,QAAQ,CAChB,IAAyB,EACzB,GAAG,IAA6B;QAEhC,MAAM,UAAU,GAAa,EAAE,CAAC;QAChC,MAAM,SAAS,GAAmB,EAAE,CAAC;QACrC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC;YACrC,MAAM,EAAE,GAAG,YAAY,CAAC,SAAS,EAAE,CAAC;YACpC,UAAU,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;YACnB,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,CAAC,CAAC,CAAC;QAC/D,CAAC;QACD,OAAO;YACL,KAAK,EAAE,MAAM,EAAE,mEAAmE;YAClF,IAAI,EAAE,CAAC,UAAU,EAAE,IAAI,CAAC,GAAI,SAAe,CAAC,CAAC;SAC9C,CAAC;IACJ,CAAC;;AAzDH,oCA0DC"}
@@ -0,0 +1,55 @@
1
+ import { Query } from "./query";
2
+ import { ValueProxy } from "./valueproxy";
3
+ import type { Selection } from "./selection";
4
+ import type { ExtractType, Value } from "./common";
5
+ export declare class Datum<T> extends Query<T> {
6
+ /**
7
+ * Changes the type of this datum.
8
+ * This does not actually perform any conversion, it only changes the typescript type.
9
+ *
10
+ * @returns Same datum with a different type
11
+ */
12
+ cast<U>(): Datum<U>;
13
+ /**
14
+ * Indexes the datum.
15
+ *
16
+ * TODO: Better name?
17
+ * TODO: typing for compound keys (a.b.c)
18
+ *
19
+ * @param key Field name
20
+ * @param def Default value
21
+ * @returns New datum with the value
22
+ */
23
+ key<K extends keyof NonNullable<T>, U = undefined>(key: K, def?: U): Datum<U extends undefined ? NonNullable<T>[K] : U | NonNullable<NonNullable<T>[K]>>;
24
+ /**
25
+ * Defaults the datum to a given value if it is null.
26
+ *
27
+ * @param value Default value
28
+ * @returns Current datum or given value
29
+ */
30
+ default<U>(val: Value<U>): Datum<U | Exclude<T, null | undefined>>;
31
+ /**
32
+ * Run a mapping function on the datum.
33
+ *
34
+ * @param mapper Mapping function
35
+ * @returns New datum with the result of the mapper
36
+ */
37
+ do<U>(mapper: (val: ValueProxy<T>) => U): Datum<ExtractType<U>>;
38
+ /**
39
+ * Perform a foreign key lookup
40
+ *
41
+ * @param other Other table
42
+ * @param localKey Key in local object
43
+ * @param otherKey Key in other table
44
+ */
45
+ lookup<U = any, TK extends keyof T = keyof T>(other: Selection<U>, localKey: TK, otherKey: keyof U): Datum<Omit<T, TK> & Record<TK, T[TK] extends any[] ? U[] : U>>;
46
+ /**
47
+ * Plucks fields from the documents.
48
+ *
49
+ * TODO: Better typing
50
+ *
51
+ * @param fields Fields to keep
52
+ * @returns New datum
53
+ */
54
+ pluck(...fields: string[]): Datum<Partial<T>>;
55
+ }
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Datum = void 0;
4
+ const query_1 = require("./query");
5
+ const valueproxy_1 = require("./valueproxy");
6
+ class Datum extends query_1.Query {
7
+ /**
8
+ * Changes the type of this datum.
9
+ * This does not actually perform any conversion, it only changes the typescript type.
10
+ *
11
+ * @returns Same datum with a different type
12
+ */
13
+ cast() {
14
+ return this;
15
+ }
16
+ /**
17
+ * Indexes the datum.
18
+ *
19
+ * TODO: Better name?
20
+ * TODO: typing for compound keys (a.b.c)
21
+ *
22
+ * @param key Field name
23
+ * @param def Default value
24
+ * @returns New datum with the value
25
+ */
26
+ key(key, def) {
27
+ return this.stage((Datum), "key", undefined, key, def);
28
+ }
29
+ /**
30
+ * Defaults the datum to a given value if it is null.
31
+ *
32
+ * @param value Default value
33
+ * @returns Current datum or given value
34
+ */
35
+ default(val) {
36
+ return this.stage((Datum), "default", undefined, val);
37
+ }
38
+ /**
39
+ * Run a mapping function on the datum.
40
+ *
41
+ * @param mapper Mapping function
42
+ * @returns New datum with the result of the mapper
43
+ */
44
+ do(mapper) {
45
+ return this.stage((Datum), "map", undefined, this.callfunc(mapper, (valueproxy_1.ValueProxy)));
46
+ }
47
+ /**
48
+ * Perform a foreign key lookup
49
+ *
50
+ * @param other Other table
51
+ * @param localKey Key in local object
52
+ * @param otherKey Key in other table
53
+ */
54
+ lookup(other, localKey, otherKey) {
55
+ return this.stage((Datum), "lookup", { localKey, otherKey }, other);
56
+ }
57
+ /**
58
+ * Plucks fields from the documents.
59
+ *
60
+ * TODO: Better typing
61
+ *
62
+ * @param fields Fields to keep
63
+ * @returns New datum
64
+ */
65
+ pluck(...fields) {
66
+ return this.stage((Datum), "pluck", undefined, fields);
67
+ }
68
+ }
69
+ exports.Datum = Datum;
70
+ //# sourceMappingURL=datum.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"datum.js","sourceRoot":"","sources":["../../src/staged-query/datum.ts"],"names":[],"mappings":";;;AAAA,mCAAgC;AAChC,6CAA0C;AAI1C,MAAa,KAAS,SAAQ,aAAQ;IACpC;;;;;OAKG;IACI,IAAI;QACT,OAAO,IAA2B,CAAC;IACrC,CAAC;IAED;;;;;;;;;OASG;IACI,GAAG,CAAgD,GAAM,EAAE,GAAO;QACvE,OAAO,IAAI,CAAC,KAAK,CACf,CAAA,KAIC,CAAA,EACD,KAAK,EACL,SAAS,EACT,GAAG,EACH,GAAG,CACJ,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACI,OAAO,CAAI,GAAa;QAC7B,OAAO,IAAI,CAAC,KAAK,CACf,CAAA,KAAuC,CAAA,EACvC,SAAS,EACT,SAAS,EACT,GAAG,CACJ,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACI,EAAE,CAAI,MAAiC;QAC5C,OAAO,IAAI,CAAC,KAAK,CACf,CAAA,KAAqB,CAAA,EACrB,KAAK,EACL,SAAS,EACT,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAA,uBAAa,CAAA,CAAC,CACrC,CAAC;IACJ,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CACX,KAAmB,EACnB,QAAY,EACZ,QAAiB;QAEjB,OAAO,IAAI,CAAC,KAAK,CACf,CAAA,KAA8D,CAAA,EAC9D,QAAQ,EACR,EAAE,QAAQ,EAAE,QAAQ,EAAE,EACtB,KAAK,CACN,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACI,KAAK,CAAC,GAAG,MAAgB;QAC9B,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA,KAAiB,CAAA,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;IACnE,CAAC;CACF;AAhGD,sBAgGC"}
@@ -0,0 +1,8 @@
1
+ export * from "./common";
2
+ export * from "./atomic";
3
+ export { Datum } from "./datum";
4
+ export { Query } from "./query";
5
+ export { CROSS_INSTANCE, Schema, SchemaInstance, type FieldType, type IndexDefinition, type InstanceId, type SchemaDefinition, type StringFieldType, type TableDefinition, } from "./schema";
6
+ export { Selection, SingleSelection, Table } from "./selection";
7
+ export { Stream } from "./stream";
8
+ export { ValueProxy, type ValueProxyOrValue } from "./valueproxy";
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.ValueProxy = exports.Stream = exports.Table = exports.SingleSelection = exports.Selection = exports.SchemaInstance = exports.Schema = exports.CROSS_INSTANCE = exports.Query = exports.Datum = void 0;
18
+ __exportStar(require("./common"), exports);
19
+ __exportStar(require("./atomic"), exports);
20
+ var datum_1 = require("./datum");
21
+ Object.defineProperty(exports, "Datum", { enumerable: true, get: function () { return datum_1.Datum; } });
22
+ var query_1 = require("./query");
23
+ Object.defineProperty(exports, "Query", { enumerable: true, get: function () { return query_1.Query; } });
24
+ var schema_1 = require("./schema");
25
+ Object.defineProperty(exports, "CROSS_INSTANCE", { enumerable: true, get: function () { return schema_1.CROSS_INSTANCE; } });
26
+ Object.defineProperty(exports, "Schema", { enumerable: true, get: function () { return schema_1.Schema; } });
27
+ Object.defineProperty(exports, "SchemaInstance", { enumerable: true, get: function () { return schema_1.SchemaInstance; } });
28
+ var selection_1 = require("./selection");
29
+ Object.defineProperty(exports, "Selection", { enumerable: true, get: function () { return selection_1.Selection; } });
30
+ Object.defineProperty(exports, "SingleSelection", { enumerable: true, get: function () { return selection_1.SingleSelection; } });
31
+ Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return selection_1.Table; } });
32
+ var stream_1 = require("./stream");
33
+ Object.defineProperty(exports, "Stream", { enumerable: true, get: function () { return stream_1.Stream; } });
34
+ var valueproxy_1 = require("./valueproxy");
35
+ Object.defineProperty(exports, "ValueProxy", { enumerable: true, get: function () { return valueproxy_1.ValueProxy; } });
36
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/staged-query/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,2CAAyB;AACzB,iCAAgC;AAAvB,8FAAA,KAAK,OAAA;AACd,iCAAgC;AAAvB,8FAAA,KAAK,OAAA;AACd,mCAUkB;AAThB,wGAAA,cAAc,OAAA;AACd,gGAAA,MAAM,OAAA;AACN,wGAAA,cAAc,OAAA;AAQhB,yCAAgE;AAAvD,sGAAA,SAAS,OAAA;AAAE,4GAAA,eAAe,OAAA;AAAE,kGAAA,KAAK,OAAA;AAC1C,mCAAkC;AAAzB,gGAAA,MAAM,OAAA;AACf,2CAAkE;AAAzD,wGAAA,UAAU,OAAA"}
@@ -0,0 +1,3 @@
1
+ import { StagedObject } from "./common";
2
+ export declare class Query<T> extends StagedObject {
3
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Query = void 0;
4
+ const common_1 = require("./common");
5
+ // oxlint-disable-next-line typescript/no-unused-vars -- The executable entry consumes T through declaration merging.
6
+ class Query extends common_1.StagedObject {
7
+ }
8
+ exports.Query = Query;
9
+ //# sourceMappingURL=query.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query.js","sourceRoot":"","sources":["../../src/staged-query/query.ts"],"names":[],"mappings":";;;AAAA,qCAAwC;AAExC,qHAAqH;AACrH,MAAa,KAAS,SAAQ,qBAAY;CAAG;AAA7C,sBAA6C"}
@@ -0,0 +1,137 @@
1
+ import { Query } from "./query";
2
+ import { Table } from "./selection";
3
+ import { StagedObject } from "./common";
4
+ /**
5
+ * Secondary table index definition
6
+ */
7
+ export interface IndexDefinition {
8
+ /**
9
+ * Fields to use for compound indexes
10
+ */
11
+ fields?: string[];
12
+ /**
13
+ * Whether or not this is a multi index
14
+ */
15
+ multi?: boolean;
16
+ /**
17
+ * Whether this index must also serve queries that run across all instances
18
+ * ({@link CROSS_INSTANCE})
19
+ *
20
+ * When `true`, the driver also maintains an index usable by cross-instance
21
+ * queries. Defaults to `false`: the index is optimized for queries scoped to
22
+ * one instance, and cross-instance queries using it may be slow.
23
+ *
24
+ * This flag never changes query results, only performance.
25
+ */
26
+ crossInstance?: boolean;
27
+ }
28
+ export type StringFieldType = string | Array<StringFieldType> | {
29
+ [subfield: string]: StringFieldType;
30
+ };
31
+ interface IoTsCodec {
32
+ readonly _A: unknown;
33
+ readonly _O: unknown;
34
+ readonly _I: unknown;
35
+ readonly name: string;
36
+ is(u: unknown): boolean;
37
+ encode(a: never): unknown;
38
+ decode(i: unknown): unknown;
39
+ }
40
+ export type FieldType = StringFieldType | IoTsCodec;
41
+ /**
42
+ * Schema table definition
43
+ */
44
+ export interface TableDefinition {
45
+ /**
46
+ * Field names and their data types
47
+ */
48
+ fields: Record<string, FieldType>;
49
+ /**
50
+ * List of secondary indexes
51
+ */
52
+ indexes: Record<string, IndexDefinition>;
53
+ }
54
+ /**
55
+ * Table names and their definitions
56
+ */
57
+ export interface SchemaDefinition {
58
+ [tableName: string]: TableDefinition;
59
+ }
60
+ /**
61
+ * Sentinel instance id meaning "operate across all instances of the schema".
62
+ *
63
+ * Secondary indexes are optimized for queries scoped to one instance; set
64
+ * {@link IndexDefinition.crossInstance} on the indexes that cross-instance
65
+ * queries rely on.
66
+ */
67
+ export declare const CROSS_INSTANCE: unique symbol;
68
+ /**
69
+ * Instance identifier accepted by {@link Schema.instance}.
70
+ */
71
+ export type InstanceId = string | typeof CROSS_INSTANCE;
72
+ /**
73
+ * A schema determines the structure of a database
74
+ *
75
+ * Each schema can have multiple instances. The internal organization of these
76
+ * instances is left up to the module implementation.
77
+ */
78
+ export declare class Schema<T = any, Definition = SchemaDefinition> extends StagedObject {
79
+ readonly id: string;
80
+ readonly definition: Definition;
81
+ private static readonly registry;
82
+ /**
83
+ * Retrieves a previously defined schema by its ID
84
+ *
85
+ * @param id Schema ID
86
+ * @returns The schema instance, or undefined if not found
87
+ */
88
+ static get<T = any, Definition = SchemaDefinition>(id: string): Schema<T, Definition> | undefined;
89
+ /**
90
+ * Define a new schema with the given ID
91
+ *
92
+ * @param id ID of this schema, changing this will leave previous data inaccessible
93
+ * @param definition Schema definition (tables, fields, indexes..)
94
+ */
95
+ constructor(id: string, definition: Definition);
96
+ /**
97
+ * Gets a specific instance of the schema
98
+ *
99
+ * @param id Instance ID, or {@link CROSS_INSTANCE} for a cross-instance query
100
+ * @returns Schema instance
101
+ */
102
+ instance(id?: InstanceId): SchemaInstance<T>;
103
+ /**
104
+ * Creates a new instance of the schema
105
+ *
106
+ * @param id Instance ID
107
+ * @returns Created instance ID
108
+ */
109
+ createInstance(id?: string): Query<string>;
110
+ /**
111
+ * Destroys an existing instance of the schema
112
+ *
113
+ * @param id Instance ID
114
+ */
115
+ destroyInstance(id?: string): Query<void>;
116
+ /**
117
+ * Lists the IDs of named instances of this schema.
118
+ *
119
+ * The default (unnamed) instance is not included.
120
+ *
121
+ * @returns IDs of named instances of this schema
122
+ */
123
+ listInstances(): Query<string[]>;
124
+ }
125
+ /**
126
+ * Schema instance, could be a database or a filtered portion of one depending on the implementation
127
+ */
128
+ export declare class SchemaInstance<T> extends StagedObject {
129
+ /**
130
+ * Gets a table from the instance
131
+ *
132
+ * @param id Table name
133
+ * @returns Table
134
+ */
135
+ table<TK extends keyof T>(id: TK): Table<T[TK]>;
136
+ }
137
+ export {};
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SchemaInstance = exports.Schema = exports.CROSS_INSTANCE = void 0;
4
+ const query_1 = require("./query");
5
+ const selection_1 = require("./selection");
6
+ const common_1 = require("./common");
7
+ /**
8
+ * Sentinel instance id meaning "operate across all instances of the schema".
9
+ *
10
+ * Secondary indexes are optimized for queries scoped to one instance; set
11
+ * {@link IndexDefinition.crossInstance} on the indexes that cross-instance
12
+ * queries rely on.
13
+ */
14
+ exports.CROSS_INSTANCE = Symbol("antelopejs:cross-instance");
15
+ /**
16
+ * A schema determines the structure of a database
17
+ *
18
+ * Each schema can have multiple instances. The internal organization of these
19
+ * instances is left up to the module implementation.
20
+ */
21
+ class Schema extends common_1.StagedObject {
22
+ id;
23
+ definition;
24
+ static registry = new Map();
25
+ /**
26
+ * Retrieves a previously defined schema by its ID
27
+ *
28
+ * @param id Schema ID
29
+ * @returns The schema instance, or undefined if not found
30
+ */
31
+ static get(id) {
32
+ return Schema.registry.get(id);
33
+ }
34
+ /**
35
+ * Define a new schema with the given ID
36
+ *
37
+ * @param id ID of this schema, changing this will leave previous data inaccessible
38
+ * @param definition Schema definition (tables, fields, indexes..)
39
+ */
40
+ constructor(id, definition) {
41
+ super((0, common_1.QueryStage)("schema", { id }));
42
+ this.id = id;
43
+ this.definition = 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
+ }
82
+ }
83
+ 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;
99
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/staged-query/schema.ts"],"names":[],"mappings":";;;AAAA,mCAAgC;AAChC,2CAAoC;AACpC,qCAAoD;AAoEpD;;;;;;GAMG;AACU,QAAA,cAAc,GAAkB,MAAM,CACjD,2BAA2B,CAC5B,CAAC;AAOF;;;;;GAKG;AACH,MAAa,MAGX,SAAQ,qBAAY;IAsBF;IACA;IAtBV,MAAM,CAAU,QAAQ,GAAG,IAAI,GAAG,EAAgC,CAAC;IAE3E;;;;;OAKG;IACI,MAAM,CAAC,GAAG,CACf,EAAU;QAEV,OAAO,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAsC,CAAC;IACtE,CAAC;IAED;;;;;OAKG;IACH,YACkB,EAAU,EACV,UAAsB;QAEtC,KAAK,CAAC,IAAA,mBAAU,EAAC,QAAQ,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;QAHpB,OAAE,GAAF,EAAE,CAAQ;QACV,eAAU,GAAV,UAAU,CAAY;QAGtC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;IAChC,CAAC;IAED;;;;;OAKG;IACI,QAAQ,CAAC,EAAe;QAC7B,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA,cAAiB,CAAA,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IAC3D,CAAC;IAED;;;;;OAKG;IACI,cAAc,CAAC,EAAW;QAC/B,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA,aAAa,CAAA,EAAE,gBAAgB,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IAC7D,CAAC;IAED;;;;OAIG;IACI,eAAe,CAAC,EAAW;QAChC,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA,aAAW,CAAA,EAAE,iBAAiB,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IAC5D,CAAC;IAED;;;;;;OAMG;IACI,aAAa;QAClB,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA,aAAe,CAAA,EAAE,eAAe,CAAC,CAAC;IACtD,CAAC;;AAtEH,wBAuEC;AAED;;GAEG;AACH,MAAa,cAAkB,SAAQ,qBAAY;IACjD;;;;;OAKG;IACI,KAAK,CAAqB,EAAM;QACrC,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA,iBAAY,CAAA,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IACnD,CAAC;CACF;AAVD,wCAUC"}
@@ -0,0 +1,104 @@
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 {};