@antelopejs/interface-database 0.1.5 → 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 (56) 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/tests/atomic_mutation_operations.test.d.ts +1 -0
  51. package/dist/tests/atomic_mutation_operations.test.js +278 -0
  52. package/dist/tests/atomic_mutation_operations.test.js.map +1 -0
  53. package/dist/valueproxy.d.ts +1 -415
  54. package/dist/valueproxy.js +15 -559
  55. package/dist/valueproxy.js.map +1 -1
  56. package/package.json +9 -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,122 @@
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
+ export type StringFieldType = string | Array<StringFieldType> | {
18
+ [subfield: string]: StringFieldType;
19
+ };
20
+ interface IoTsCodec {
21
+ readonly _A: unknown;
22
+ readonly _O: unknown;
23
+ readonly _I: unknown;
24
+ readonly name: string;
25
+ is(u: unknown): boolean;
26
+ encode(a: never): unknown;
27
+ decode(i: unknown): unknown;
28
+ }
29
+ export type FieldType = StringFieldType | IoTsCodec;
30
+ /**
31
+ * Schema table definition
32
+ */
33
+ export interface TableDefinition {
34
+ /**
35
+ * Field names and their data types
36
+ */
37
+ fields: Record<string, FieldType>;
38
+ /**
39
+ * List of secondary indexes
40
+ */
41
+ indexes: Record<string, IndexDefinition>;
42
+ }
43
+ /**
44
+ * Table names and their definitions
45
+ */
46
+ export interface SchemaDefinition {
47
+ [tableName: string]: TableDefinition;
48
+ }
49
+ /**
50
+ * Sentinel instance id meaning "operate across all instances of the schema".
51
+ */
52
+ export declare const CROSS_INSTANCE: unique symbol;
53
+ /**
54
+ * Instance identifier accepted by {@link Schema.instance}.
55
+ */
56
+ export type InstanceId = string | typeof CROSS_INSTANCE;
57
+ /**
58
+ * A schema determines the structure of a database
59
+ *
60
+ * Each schema can have multiple instances. The internal organization of these
61
+ * instances is left up to the module implementation.
62
+ */
63
+ export declare class Schema<T = any, Definition = SchemaDefinition> extends StagedObject {
64
+ readonly id: string;
65
+ readonly definition: Definition;
66
+ private static readonly registry;
67
+ /**
68
+ * Retrieves a previously defined schema by its ID
69
+ *
70
+ * @param id Schema ID
71
+ * @returns The schema instance, or undefined if not found
72
+ */
73
+ static get<T = any, Definition = SchemaDefinition>(id: string): Schema<T, Definition> | undefined;
74
+ /**
75
+ * Define a new schema with the given ID
76
+ *
77
+ * @param id ID of this schema, changing this will leave previous data inaccessible
78
+ * @param definition Schema definition (tables, fields, indexes..)
79
+ */
80
+ constructor(id: string, definition: Definition);
81
+ /**
82
+ * Gets a specific instance of the schema
83
+ *
84
+ * @param id Instance ID, or {@link CROSS_INSTANCE} for a cross-instance query
85
+ * @returns Schema instance
86
+ */
87
+ instance(id?: InstanceId): SchemaInstance<T>;
88
+ /**
89
+ * Creates a new instance of the schema
90
+ *
91
+ * @param id Instance ID
92
+ * @returns Created instance ID
93
+ */
94
+ createInstance(id?: string): Query<string>;
95
+ /**
96
+ * Destroys an existing instance of the schema
97
+ *
98
+ * @param id Instance ID
99
+ */
100
+ destroyInstance(id?: string): Query<void>;
101
+ /**
102
+ * Lists the IDs of named instances of this schema.
103
+ *
104
+ * The default (unnamed) instance is not included.
105
+ *
106
+ * @returns IDs of named instances of this schema
107
+ */
108
+ listInstances(): Query<string[]>;
109
+ }
110
+ /**
111
+ * Schema instance, could be a database or a filtered portion of one depending on the implementation
112
+ */
113
+ export declare class SchemaInstance<T> extends StagedObject {
114
+ /**
115
+ * Gets a table from the instance
116
+ *
117
+ * @param id Table name
118
+ * @returns Table
119
+ */
120
+ table<TK extends keyof T>(id: TK): Table<T[TK]>;
121
+ }
122
+ export {};
@@ -0,0 +1,95 @@
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
+ exports.CROSS_INSTANCE = Symbol("antelopejs:cross-instance");
11
+ /**
12
+ * A schema determines the structure of a database
13
+ *
14
+ * Each schema can have multiple instances. The internal organization of these
15
+ * instances is left up to the module implementation.
16
+ */
17
+ class Schema extends common_1.StagedObject {
18
+ id;
19
+ definition;
20
+ static registry = new Map();
21
+ /**
22
+ * Retrieves a previously defined schema by its ID
23
+ *
24
+ * @param id Schema ID
25
+ * @returns The schema instance, or undefined if not found
26
+ */
27
+ static get(id) {
28
+ return Schema.registry.get(id);
29
+ }
30
+ /**
31
+ * Define a new schema with the given ID
32
+ *
33
+ * @param id ID of this schema, changing this will leave previous data inaccessible
34
+ * @param definition Schema definition (tables, fields, indexes..)
35
+ */
36
+ constructor(id, definition) {
37
+ super((0, common_1.QueryStage)("schema", { id }));
38
+ this.id = id;
39
+ this.definition = definition;
40
+ Schema.registry.set(id, this);
41
+ }
42
+ /**
43
+ * Gets a specific instance of the schema
44
+ *
45
+ * @param id Instance ID, or {@link CROSS_INSTANCE} for a cross-instance query
46
+ * @returns Schema instance
47
+ */
48
+ instance(id) {
49
+ return this.stage((SchemaInstance), "instance", { id });
50
+ }
51
+ /**
52
+ * Creates a new instance of the schema
53
+ *
54
+ * @param id Instance ID
55
+ * @returns Created instance ID
56
+ */
57
+ createInstance(id) {
58
+ return this.stage((query_1.Query), "createInstance", { id });
59
+ }
60
+ /**
61
+ * Destroys an existing instance of the schema
62
+ *
63
+ * @param id Instance ID
64
+ */
65
+ destroyInstance(id) {
66
+ return this.stage((query_1.Query), "destroyInstance", { id });
67
+ }
68
+ /**
69
+ * Lists the IDs of named instances of this schema.
70
+ *
71
+ * The default (unnamed) instance is not included.
72
+ *
73
+ * @returns IDs of named instances of this schema
74
+ */
75
+ listInstances() {
76
+ return this.stage((query_1.Query), "listInstances");
77
+ }
78
+ }
79
+ exports.Schema = Schema;
80
+ /**
81
+ * Schema instance, could be a database or a filtered portion of one depending on the implementation
82
+ */
83
+ class SchemaInstance extends common_1.StagedObject {
84
+ /**
85
+ * Gets a table from the instance
86
+ *
87
+ * @param id Table name
88
+ * @returns Table
89
+ */
90
+ table(id) {
91
+ return this.stage((selection_1.Table), "table", { id });
92
+ }
93
+ }
94
+ exports.SchemaInstance = SchemaInstance;
95
+ //# 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;AAwDpD;;GAEG;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 {};