@antelopejs/interface-database 0.0.2
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.
- package/LICENSE +190 -0
- package/README.md +45 -0
- package/dist/antelope.test.d.ts +2 -0
- package/dist/antelope.test.js +35 -0
- package/dist/antelope.test.js.map +1 -0
- package/dist/common.d.ts +58 -0
- package/dist/common.js +44 -0
- package/dist/common.js.map +1 -0
- package/dist/datum.d.ts +55 -0
- package/dist/datum.js +70 -0
- package/dist/datum.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/query.d.ts +15 -0
- package/dist/query.js +79 -0
- package/dist/query.js.map +1 -0
- package/dist/schema.d.ts +102 -0
- package/dist/schema.js +88 -0
- package/dist/schema.js.map +1 -0
- package/dist/selection.d.ts +99 -0
- package/dist/selection.js +119 -0
- package/dist/selection.js.map +1 -0
- package/dist/stream.d.ts +185 -0
- package/dist/stream.js +221 -0
- package/dist/stream.js.map +1 -0
- package/dist/tests/aggregation_operations.test.d.ts +1 -0
- package/dist/tests/aggregation_operations.test.js +157 -0
- package/dist/tests/aggregation_operations.test.js.map +1 -0
- package/dist/tests/basic_operations.test.d.ts +1 -0
- package/dist/tests/basic_operations.test.js +216 -0
- package/dist/tests/basic_operations.test.js.map +1 -0
- package/dist/tests/between_operations.test.d.ts +1 -0
- package/dist/tests/between_operations.test.js +76 -0
- package/dist/tests/between_operations.test.js.map +1 -0
- package/dist/tests/change_feeds.test.d.ts +1 -0
- package/dist/tests/change_feeds.test.js +90 -0
- package/dist/tests/change_feeds.test.js.map +1 -0
- package/dist/tests/datasets/index.d.ts +4 -0
- package/dist/tests/datasets/index.js +22 -0
- package/dist/tests/datasets/index.js.map +1 -0
- package/dist/tests/datasets/orders.d.ts +62 -0
- package/dist/tests/datasets/orders.js +158 -0
- package/dist/tests/datasets/orders.js.map +1 -0
- package/dist/tests/datasets/products.d.ts +48 -0
- package/dist/tests/datasets/products.js +139 -0
- package/dist/tests/datasets/products.js.map +1 -0
- package/dist/tests/datasets/users.d.ts +50 -0
- package/dist/tests/datasets/users.js +145 -0
- package/dist/tests/datasets/users.js.map +1 -0
- package/dist/tests/datasets/vehicles.d.ts +23 -0
- package/dist/tests/datasets/vehicles.js +41 -0
- package/dist/tests/datasets/vehicles.js.map +1 -0
- package/dist/tests/date_operations.test.d.ts +1 -0
- package/dist/tests/date_operations.test.js +119 -0
- package/dist/tests/date_operations.test.js.map +1 -0
- package/dist/tests/do_operations.test.d.ts +1 -0
- package/dist/tests/do_operations.test.js +209 -0
- package/dist/tests/do_operations.test.js.map +1 -0
- package/dist/tests/filters.test.d.ts +1 -0
- package/dist/tests/filters.test.js +158 -0
- package/dist/tests/filters.test.js.map +1 -0
- package/dist/tests/group_operations.test.d.ts +1 -0
- package/dist/tests/group_operations.test.js +190 -0
- package/dist/tests/group_operations.test.js.map +1 -0
- package/dist/tests/instance_modes.test.d.ts +1 -0
- package/dist/tests/instance_modes.test.js +192 -0
- package/dist/tests/instance_modes.test.js.map +1 -0
- package/dist/tests/joins.test.d.ts +1 -0
- package/dist/tests/joins.test.js +163 -0
- package/dist/tests/joins.test.js.map +1 -0
- package/dist/tests/lookup_operations.test.d.ts +1 -0
- package/dist/tests/lookup_operations.test.js +103 -0
- package/dist/tests/lookup_operations.test.js.map +1 -0
- package/dist/tests/merge_operations.test.d.ts +1 -0
- package/dist/tests/merge_operations.test.js +216 -0
- package/dist/tests/merge_operations.test.js.map +1 -0
- package/dist/tests/row_level_operations.test.d.ts +1 -0
- package/dist/tests/row_level_operations.test.js +467 -0
- package/dist/tests/row_level_operations.test.js.map +1 -0
- package/dist/tests/sorting.test.d.ts +1 -0
- package/dist/tests/sorting.test.js +161 -0
- package/dist/tests/sorting.test.js.map +1 -0
- package/dist/tests/union_operations.test.d.ts +1 -0
- package/dist/tests/union_operations.test.js +129 -0
- package/dist/tests/union_operations.test.js.map +1 -0
- package/dist/valueproxy.d.ts +415 -0
- package/dist/valueproxy.js +562 -0
- package/dist/valueproxy.js.map +1 -0
- package/package.json +88 -0
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { RegisteringProxy } from "@antelopejs/interface-core";
|
|
2
|
+
import { StagedObject } from "./common";
|
|
3
|
+
import { Query } from "./query";
|
|
4
|
+
import { Table } from "./selection";
|
|
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 FieldType = string | Array<FieldType> | {
|
|
19
|
+
[subfield: string]: FieldType;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Schema table definition
|
|
23
|
+
*/
|
|
24
|
+
export interface TableDefinition {
|
|
25
|
+
/**
|
|
26
|
+
* Field names and their data types
|
|
27
|
+
*/
|
|
28
|
+
fields: Record<string, FieldType>;
|
|
29
|
+
/**
|
|
30
|
+
* List of secondary indexes
|
|
31
|
+
*/
|
|
32
|
+
indexes: Record<string, IndexDefinition>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Table names and their definitions
|
|
36
|
+
*/
|
|
37
|
+
export interface SchemaDefinition {
|
|
38
|
+
[tableName: string]: TableDefinition;
|
|
39
|
+
}
|
|
40
|
+
export interface SchemaOptions {
|
|
41
|
+
rowLevel?: boolean;
|
|
42
|
+
}
|
|
43
|
+
export declare const Schemas: RegisteringProxy<(name: string, def: SchemaDefinition, options: SchemaOptions) => void>;
|
|
44
|
+
/**
|
|
45
|
+
* A schema determines the structure of a database
|
|
46
|
+
*
|
|
47
|
+
* Each schema can have multiple instances
|
|
48
|
+
*
|
|
49
|
+
* The internal organization of these instances is left up to the module implementation
|
|
50
|
+
*/
|
|
51
|
+
export declare class Schema<T = any> extends StagedObject {
|
|
52
|
+
readonly id: string;
|
|
53
|
+
readonly definition: SchemaDefinition;
|
|
54
|
+
readonly options: SchemaOptions;
|
|
55
|
+
private static readonly registry;
|
|
56
|
+
/**
|
|
57
|
+
* Retrieves a previously defined schema by its ID
|
|
58
|
+
*
|
|
59
|
+
* @param id Schema ID
|
|
60
|
+
* @returns The schema instance, or undefined if not found
|
|
61
|
+
*/
|
|
62
|
+
static get(id: string): Schema | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Define a new schema with the given ID
|
|
65
|
+
*
|
|
66
|
+
* @param id ID of this schema, changing this will leave previous data inaccessible
|
|
67
|
+
* @param definition Schema definition (tables, fields, indexes..)
|
|
68
|
+
*/
|
|
69
|
+
constructor(id: string, definition: SchemaDefinition, options?: SchemaOptions);
|
|
70
|
+
/**
|
|
71
|
+
* Gets a specific instance of the schema
|
|
72
|
+
*
|
|
73
|
+
* @param id Instance ID
|
|
74
|
+
* @returns Schema instance
|
|
75
|
+
*/
|
|
76
|
+
instance(id?: string): SchemaInstance<T>;
|
|
77
|
+
/**
|
|
78
|
+
* Creates a new instance of the schema
|
|
79
|
+
*
|
|
80
|
+
* @param id Instance ID
|
|
81
|
+
* @returns Created instance ID
|
|
82
|
+
*/
|
|
83
|
+
createInstance(id?: string): Query<string>;
|
|
84
|
+
/**
|
|
85
|
+
* Destroys an existing instance of the schema
|
|
86
|
+
*
|
|
87
|
+
* @param id Instance ID
|
|
88
|
+
*/
|
|
89
|
+
destroyInstance(id?: string): Query<void>;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Schema instance, could be a database or a filtered portion of one depending on the implementation
|
|
93
|
+
*/
|
|
94
|
+
export declare class SchemaInstance<T> extends StagedObject {
|
|
95
|
+
/**
|
|
96
|
+
* Gets a table from the instance
|
|
97
|
+
*
|
|
98
|
+
* @param id Table name
|
|
99
|
+
* @returns Table
|
|
100
|
+
*/
|
|
101
|
+
table<TK extends keyof T>(id: TK): Table<T[TK]>;
|
|
102
|
+
}
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SchemaInstance = exports.Schema = exports.Schemas = void 0;
|
|
4
|
+
const interface_core_1 = require("@antelopejs/interface-core");
|
|
5
|
+
const common_1 = require("./common");
|
|
6
|
+
const query_1 = require("./query");
|
|
7
|
+
const selection_1 = require("./selection");
|
|
8
|
+
//@internal
|
|
9
|
+
exports.Schemas = new interface_core_1.RegisteringProxy();
|
|
10
|
+
/**
|
|
11
|
+
* A schema determines the structure of a database
|
|
12
|
+
*
|
|
13
|
+
* Each schema can have multiple instances
|
|
14
|
+
*
|
|
15
|
+
* The internal organization of these instances is left up to the module implementation
|
|
16
|
+
*/
|
|
17
|
+
class Schema extends common_1.StagedObject {
|
|
18
|
+
id;
|
|
19
|
+
definition;
|
|
20
|
+
options;
|
|
21
|
+
static registry = new Map();
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves a previously defined schema by its ID
|
|
24
|
+
*
|
|
25
|
+
* @param id Schema ID
|
|
26
|
+
* @returns The schema instance, or undefined if not found
|
|
27
|
+
*/
|
|
28
|
+
static get(id) {
|
|
29
|
+
return Schema.registry.get(id);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Define a new schema with the given ID
|
|
33
|
+
*
|
|
34
|
+
* @param id ID of this schema, changing this will leave previous data inaccessible
|
|
35
|
+
* @param definition Schema definition (tables, fields, indexes..)
|
|
36
|
+
*/
|
|
37
|
+
constructor(id, definition, options = {}) {
|
|
38
|
+
super((0, common_1.QueryStage)("schema", { id }));
|
|
39
|
+
this.id = id;
|
|
40
|
+
this.definition = definition;
|
|
41
|
+
this.options = options;
|
|
42
|
+
exports.Schemas.register(id, definition, options);
|
|
43
|
+
Schema.registry.set(id, this);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Gets a specific instance of the schema
|
|
47
|
+
*
|
|
48
|
+
* @param id Instance ID
|
|
49
|
+
* @returns Schema instance
|
|
50
|
+
*/
|
|
51
|
+
instance(id) {
|
|
52
|
+
return this.stage((SchemaInstance), "instance", { id });
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Creates a new instance of the schema
|
|
56
|
+
*
|
|
57
|
+
* @param id Instance ID
|
|
58
|
+
* @returns Created instance ID
|
|
59
|
+
*/
|
|
60
|
+
createInstance(id) {
|
|
61
|
+
return this.stage((query_1.Query), "createInstance", { id });
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Destroys an existing instance of the schema
|
|
65
|
+
*
|
|
66
|
+
* @param id Instance ID
|
|
67
|
+
*/
|
|
68
|
+
destroyInstance(id) {
|
|
69
|
+
return this.stage((query_1.Query), "destroyInstance", { id });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.Schema = Schema;
|
|
73
|
+
/**
|
|
74
|
+
* Schema instance, could be a database or a filtered portion of one depending on the implementation
|
|
75
|
+
*/
|
|
76
|
+
class SchemaInstance extends common_1.StagedObject {
|
|
77
|
+
/**
|
|
78
|
+
* Gets a table from the instance
|
|
79
|
+
*
|
|
80
|
+
* @param id Table name
|
|
81
|
+
* @returns Table
|
|
82
|
+
*/
|
|
83
|
+
table(id) {
|
|
84
|
+
return this.stage((selection_1.Table), "table", { id });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exports.SchemaInstance = SchemaInstance;
|
|
88
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":";;;AAAA,+DAA8D;AAC9D,qCAAoD;AACpD,mCAAgC;AAChC,2CAAoC;AAgDpC,WAAW;AACE,QAAA,OAAO,GAAG,IAAI,iCAAgB,EAExC,CAAC;AAEJ;;;;;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C,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA,iBAAY,CAAA,EAAE,OAAO,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IACnD,CAAC;CACF;AAVD,wCAUC"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { Changes, DeepPartial, ExtractType, InsertOptions } from "./common";
|
|
2
|
+
import { Datum } from "./datum";
|
|
3
|
+
import { Query } from "./query";
|
|
4
|
+
import { Stream } from "./stream";
|
|
5
|
+
import { ValueProxy } from "./valueproxy";
|
|
6
|
+
/**
|
|
7
|
+
* Selection containing a single element
|
|
8
|
+
*/
|
|
9
|
+
export declare class SingleSelection<T> extends Datum<T> {
|
|
10
|
+
/**
|
|
11
|
+
* Update fields of this selection with the given values
|
|
12
|
+
*
|
|
13
|
+
* @param document Partial document with new values
|
|
14
|
+
* @returns Number of modified documents
|
|
15
|
+
*/
|
|
16
|
+
update(document: DeepPartial<T>): Query<number>;
|
|
17
|
+
update<U>(document: (val: ValueProxy<T>) => U): ExtractType<U> extends DeepPartial<T> ? Query<number> : never;
|
|
18
|
+
/**
|
|
19
|
+
* Replace documents of this selection
|
|
20
|
+
*
|
|
21
|
+
* @param document Partial document to replace with
|
|
22
|
+
* @returns Number of modified documents
|
|
23
|
+
*/
|
|
24
|
+
replace(document: DeepPartial<T>): Query<number>;
|
|
25
|
+
/**
|
|
26
|
+
* Delete selected documents
|
|
27
|
+
*
|
|
28
|
+
* @returns Number of deleted documents
|
|
29
|
+
*/
|
|
30
|
+
delete(): Query<number>;
|
|
31
|
+
/**
|
|
32
|
+
* Turns this selection into a change feed
|
|
33
|
+
*
|
|
34
|
+
* @returns Change feed
|
|
35
|
+
*/
|
|
36
|
+
changes(): Query<Changes<T>[]>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Selection containing any number of elements
|
|
40
|
+
*/
|
|
41
|
+
export declare class Selection<T> extends Stream<T> {
|
|
42
|
+
/**
|
|
43
|
+
* Update fields of this selection with the given values
|
|
44
|
+
*
|
|
45
|
+
* @param document Partial document with new values
|
|
46
|
+
* @returns Number of modified documents
|
|
47
|
+
*/
|
|
48
|
+
update(document: DeepPartial<T>): Query<number>;
|
|
49
|
+
update<U>(document: (val: ValueProxy<T>) => U): ExtractType<U> extends DeepPartial<T> ? Query<number> : never;
|
|
50
|
+
/**
|
|
51
|
+
* Replace documents of this selection
|
|
52
|
+
*
|
|
53
|
+
* @param document Partial document to replace with
|
|
54
|
+
* @returns Number of modified documents
|
|
55
|
+
*/
|
|
56
|
+
replace(document: DeepPartial<T>): Query<number>;
|
|
57
|
+
/**
|
|
58
|
+
* Delete selected documents
|
|
59
|
+
*
|
|
60
|
+
* @returns Number of deleted documents
|
|
61
|
+
*/
|
|
62
|
+
delete(): Query<number>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Database table
|
|
66
|
+
*/
|
|
67
|
+
export declare class Table<T> extends Selection<T> {
|
|
68
|
+
/**
|
|
69
|
+
* Inserts one or more documents into this table
|
|
70
|
+
*
|
|
71
|
+
* @param obj Document(s) to insert
|
|
72
|
+
* @returns Inserted IDs
|
|
73
|
+
*/
|
|
74
|
+
insert(obj: DeepPartial<T> | DeepPartial<T>[], options?: InsertOptions): Query<string[]>;
|
|
75
|
+
/**
|
|
76
|
+
* Gets a document using its primary key
|
|
77
|
+
*
|
|
78
|
+
* @param key Primary key value
|
|
79
|
+
* @returns Single document selection
|
|
80
|
+
*/
|
|
81
|
+
get(key: string): SingleSelection<T>;
|
|
82
|
+
/**
|
|
83
|
+
* Gets multiple documents using a secondary index
|
|
84
|
+
*
|
|
85
|
+
* @param keys Key value(s)
|
|
86
|
+
* @param index Secondary index, will use the primary key if undefined
|
|
87
|
+
* @returns Multiple document selection
|
|
88
|
+
*/
|
|
89
|
+
getAll(keys: string | number | boolean | (string | number | boolean)[], index?: string): Selection<T>;
|
|
90
|
+
/**
|
|
91
|
+
* Gets multiple documents using a secondary index and bounding values
|
|
92
|
+
*
|
|
93
|
+
* @param index Secondary index
|
|
94
|
+
* @param low Lowest value of the range
|
|
95
|
+
* @param high Highest value of the range (excluded)
|
|
96
|
+
* @returns Multiple document selection
|
|
97
|
+
*/
|
|
98
|
+
between<TK extends keyof T>(index: TK, low: T[TK], high: T[TK]): Selection<T>;
|
|
99
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
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
|
+
/**
|
|
9
|
+
* Selection containing a single element
|
|
10
|
+
*/
|
|
11
|
+
class SingleSelection extends datum_1.Datum {
|
|
12
|
+
update(document) {
|
|
13
|
+
if (typeof document === "function") {
|
|
14
|
+
return this.stage((query_1.Query), "update", undefined, this.callfunc(document, (valueproxy_1.ValueProxy)));
|
|
15
|
+
}
|
|
16
|
+
return this.stage((query_1.Query), "update", undefined, document);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Replace documents of this selection
|
|
20
|
+
*
|
|
21
|
+
* @param document Partial document to replace with
|
|
22
|
+
* @returns Number of modified documents
|
|
23
|
+
*/
|
|
24
|
+
replace(document) {
|
|
25
|
+
return this.stage((query_1.Query), "replace", undefined, document);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Delete selected documents
|
|
29
|
+
*
|
|
30
|
+
* @returns Number of deleted documents
|
|
31
|
+
*/
|
|
32
|
+
delete() {
|
|
33
|
+
return this.stage((query_1.Query), "delete");
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Turns this selection into a change feed
|
|
37
|
+
*
|
|
38
|
+
* @returns Change feed
|
|
39
|
+
*/
|
|
40
|
+
changes() {
|
|
41
|
+
return this.stage((query_1.Query), "changes");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.SingleSelection = SingleSelection;
|
|
45
|
+
/**
|
|
46
|
+
* Selection containing any number of elements
|
|
47
|
+
*/
|
|
48
|
+
class Selection extends stream_1.Stream {
|
|
49
|
+
update(document) {
|
|
50
|
+
if (typeof document === "function") {
|
|
51
|
+
return this.stage((query_1.Query), "update", undefined, this.callfunc(document, (valueproxy_1.ValueProxy)));
|
|
52
|
+
}
|
|
53
|
+
return this.stage((query_1.Query), "update", undefined, document);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Replace documents of this selection
|
|
57
|
+
*
|
|
58
|
+
* @param document Partial document to replace with
|
|
59
|
+
* @returns Number of modified documents
|
|
60
|
+
*/
|
|
61
|
+
replace(document) {
|
|
62
|
+
return this.stage((query_1.Query), "replace", undefined, document);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Delete selected documents
|
|
66
|
+
*
|
|
67
|
+
* @returns Number of deleted documents
|
|
68
|
+
*/
|
|
69
|
+
delete() {
|
|
70
|
+
return this.stage((query_1.Query), "delete");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.Selection = Selection;
|
|
74
|
+
/**
|
|
75
|
+
* Database table
|
|
76
|
+
*/
|
|
77
|
+
class Table extends Selection {
|
|
78
|
+
/**
|
|
79
|
+
* Inserts one or more documents into this table
|
|
80
|
+
*
|
|
81
|
+
* @param obj Document(s) to insert
|
|
82
|
+
* @returns Inserted IDs
|
|
83
|
+
*/
|
|
84
|
+
insert(obj, options) {
|
|
85
|
+
return this.stage((query_1.Query), "insert", options, obj);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Gets a document using its primary key
|
|
89
|
+
*
|
|
90
|
+
* @param key Primary key value
|
|
91
|
+
* @returns Single document selection
|
|
92
|
+
*/
|
|
93
|
+
get(key) {
|
|
94
|
+
return this.stage((SingleSelection), "get", undefined, key);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Gets multiple documents using a secondary index
|
|
98
|
+
*
|
|
99
|
+
* @param keys Key value(s)
|
|
100
|
+
* @param index Secondary index, will use the primary key if undefined
|
|
101
|
+
* @returns Multiple document selection
|
|
102
|
+
*/
|
|
103
|
+
getAll(keys, index) {
|
|
104
|
+
return this.stage((Selection), "getAll", { index }, keys);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Gets multiple documents using a secondary index and bounding values
|
|
108
|
+
*
|
|
109
|
+
* @param index Secondary index
|
|
110
|
+
* @param low Lowest value of the range
|
|
111
|
+
* @param high Highest value of the range (excluded)
|
|
112
|
+
* @returns Multiple document selection
|
|
113
|
+
*/
|
|
114
|
+
between(index, low, high) {
|
|
115
|
+
return this.stage((Selection), "between", { index }, low, high);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.Table = Table;
|
|
119
|
+
//# sourceMappingURL=selection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selection.js","sourceRoot":"","sources":["../src/selection.ts"],"names":[],"mappings":";;;AAMA,mCAAgC;AAChC,mCAAgC;AAChC,qCAAkC;AAClC,6CAA0C;AAE1C;;GAEG;AACH,MAAa,eAAmB,SAAQ,aAAQ;IAWvC,MAAM,CAAC,QAA4D;QACxE,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;YACnC,OAAO,IAAI,CAAC,KAAK,CACf,CAAA,aAAa,CAAA,EACb,QAAQ,EACR,SAAS,EACT,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAA,uBAAa,CAAA,CAAC,CACvC,CAAC;QACJ,CAAC;QACD,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA,aAAa,CAAA,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC;IAClE,CAAC;IAED;;;;;OAKG;IACI,OAAO,CAAC,QAAwB;QACrC,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA,aAAa,CAAA,EAAE,SAA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,GAAG,CAAC,GAAW;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA,eAAkB,CAAA,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,CAAC,CAAC;IAC/D,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CACX,IAA+D,EAC/D,KAAc;QAEd,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA,SAAY,CAAA,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,EAAE,IAAI,CAAC,CAAC;IAC7D,CAAC;IAED;;;;;;;OAOG;IACI,OAAO,CAAqB,KAAS,EAAE,GAAU,EAAE,IAAW;QACnE,OAAO,IAAI,CAAC,KAAK,CAAC,CAAA,SAAY,CAAA,EAAE,SAAS,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;IACnE,CAAC;CACF;AAjDD,sBAiDC"}
|
package/dist/stream.d.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { Changes, ExtractType, Value } from "./common";
|
|
2
|
+
import { Datum } from "./datum";
|
|
3
|
+
import { Query } from "./query";
|
|
4
|
+
import type { Selection } from "./selection";
|
|
5
|
+
import { ValueProxy, type ValueProxyOrValue } from "./valueproxy";
|
|
6
|
+
export declare class Stream<T> extends Query<T[]> {
|
|
7
|
+
/**
|
|
8
|
+
* Changes the type of the value in this stream.
|
|
9
|
+
* This does not actually perform any conversion, it only changes the typescript type.
|
|
10
|
+
*
|
|
11
|
+
* @returns Same stream with a different type
|
|
12
|
+
*/
|
|
13
|
+
cast<U>(): Stream<U>;
|
|
14
|
+
/**
|
|
15
|
+
* Indexes the stream value.
|
|
16
|
+
*
|
|
17
|
+
* TODO: Better name?
|
|
18
|
+
*
|
|
19
|
+
* @param key Field name
|
|
20
|
+
* @param def Default value
|
|
21
|
+
* @returns New stream
|
|
22
|
+
*/
|
|
23
|
+
key<K extends keyof T, U = undefined>(key: K, def?: U): Stream<U extends undefined ? T[K] : U | Exclude<T[K], null | undefined>>;
|
|
24
|
+
/**
|
|
25
|
+
* Defaults the stream value to a given value if it is null.
|
|
26
|
+
*
|
|
27
|
+
* @param value Default value
|
|
28
|
+
* @returns Stream with non-null value
|
|
29
|
+
*/
|
|
30
|
+
default<U>(val: Value<U>): Stream<U | Exclude<T, null | undefined>>;
|
|
31
|
+
/**
|
|
32
|
+
* Maps the array values using a mapping function.
|
|
33
|
+
*
|
|
34
|
+
* @param mapper Mapping function
|
|
35
|
+
* @returns New stream
|
|
36
|
+
*/
|
|
37
|
+
map<U>(mapper: (val: ValueProxy<T>) => U): Stream<ExtractType<U>>;
|
|
38
|
+
/**
|
|
39
|
+
* Filters the array using a predicate function.
|
|
40
|
+
*
|
|
41
|
+
* @param predicate Predicate function.
|
|
42
|
+
* @returns Filtered stream
|
|
43
|
+
*/
|
|
44
|
+
filter(predicate: (val: ValueProxy<T>) => ValueProxyOrValue<boolean>): this;
|
|
45
|
+
/**
|
|
46
|
+
* Selects specific fields in the documents, discarding the rest.
|
|
47
|
+
*
|
|
48
|
+
* @param fields Selected fields
|
|
49
|
+
* @returns New stream
|
|
50
|
+
*/
|
|
51
|
+
pluck(...fields: string[]): Stream<Partial<T>>;
|
|
52
|
+
/**
|
|
53
|
+
* Excludes specific fields in the documents
|
|
54
|
+
*
|
|
55
|
+
* @param fields Excluded fields
|
|
56
|
+
* @returns New stream
|
|
57
|
+
*/
|
|
58
|
+
without(...fields: string[]): Stream<Partial<T>>;
|
|
59
|
+
/**
|
|
60
|
+
* Concatenates the results of another stream into this stream without deduplication
|
|
61
|
+
*
|
|
62
|
+
* @param other Stream to concatenate
|
|
63
|
+
* @returns New stream containing elements from both streams
|
|
64
|
+
*/
|
|
65
|
+
union<U>(other: Stream<U>): Stream<T | U>;
|
|
66
|
+
/**
|
|
67
|
+
* Perform a left join operation between this stream (left) and another stream (right)
|
|
68
|
+
*
|
|
69
|
+
* @param right Right stream
|
|
70
|
+
* @param predicate Predicate to match elements from the left stream to the right stream
|
|
71
|
+
* @param mapper Mapping function for each pair of documents
|
|
72
|
+
* @returns New stream with results of the mapping function
|
|
73
|
+
*/
|
|
74
|
+
join<U, V>(right: Stream<U>, predicate: (left: ValueProxy<T>, right: ValueProxy<U>) => ValueProxyOrValue<boolean>, mapper: (left: ValueProxy<T>, right: ValueProxy<U | null>) => V): Stream<ExtractType<V>>;
|
|
75
|
+
/**
|
|
76
|
+
* Perform an inner join operation between this stream (left) and another stream (right)
|
|
77
|
+
*
|
|
78
|
+
* @param right Right stream
|
|
79
|
+
* @param predicate Predicate to match elements from the left stream to the right stream
|
|
80
|
+
* @param mapper Mapping function for each pair of documents
|
|
81
|
+
* @returns New stream with results of the mapping function
|
|
82
|
+
*/
|
|
83
|
+
joinInner<U, V>(right: Stream<U>, predicate: (left: ValueProxy<T>, right: ValueProxy<U>) => ValueProxyOrValue<boolean>, mapper: (left: ValueProxy<T>, right: ValueProxy<U>) => V): Stream<ExtractType<V>>;
|
|
84
|
+
/**
|
|
85
|
+
* Transform a foreign key or array of foreign keys into a document from another stream
|
|
86
|
+
*
|
|
87
|
+
* @param right Stream containing the other documents
|
|
88
|
+
* @param localKey Key in the local document to search with and replace
|
|
89
|
+
* @param otherKey Key in the other document to match against
|
|
90
|
+
* @returns New stream
|
|
91
|
+
*/
|
|
92
|
+
lookup<U, TK extends keyof T>(right: Selection<U>, localKey: TK, otherKey: keyof U): Stream<Omit<T, TK> & Record<TK, T[TK] extends any[] ? U[] : U>>;
|
|
93
|
+
/**
|
|
94
|
+
* Group the documents using the given index and maps the result using a mapping function
|
|
95
|
+
*
|
|
96
|
+
* The parameters of this function are:
|
|
97
|
+
* - The stream with all the documents inside the group
|
|
98
|
+
* - The index value for this group
|
|
99
|
+
*
|
|
100
|
+
* The result of this function is used as the element in the new stream
|
|
101
|
+
*
|
|
102
|
+
* @param index Index to group on
|
|
103
|
+
* @param mapper Mapping function
|
|
104
|
+
* @returns New stream of grouped data
|
|
105
|
+
*/
|
|
106
|
+
group<U, K extends keyof T | string>(index: K, mapper: (stream: Stream<T>, group: ValueProxy<K extends keyof T ? T[K] : unknown>) => U): Stream<ExtractType<U>>;
|
|
107
|
+
/**
|
|
108
|
+
* Sort the stream using the given index and direction
|
|
109
|
+
*
|
|
110
|
+
* @param index Index to sort
|
|
111
|
+
* @param direction Sort direction
|
|
112
|
+
* @returns New (sorted) stream
|
|
113
|
+
*/
|
|
114
|
+
orderBy(index: string, direction?: "asc" | "desc"): this;
|
|
115
|
+
/**
|
|
116
|
+
* Obtain a slice (subsection) of the stream
|
|
117
|
+
*
|
|
118
|
+
* @param offset Offset into the stream
|
|
119
|
+
* @param count Number of documents to pick
|
|
120
|
+
* @returns New stream
|
|
121
|
+
*/
|
|
122
|
+
slice(offset: Value<number>, count?: Value<number>): this;
|
|
123
|
+
/**
|
|
124
|
+
* Obtain the Nth document of the stream
|
|
125
|
+
*
|
|
126
|
+
* @param n N
|
|
127
|
+
* @returns Single document
|
|
128
|
+
*/
|
|
129
|
+
nth(n: Value<number>): Datum<T | null>;
|
|
130
|
+
/**
|
|
131
|
+
* Gets the count of documents or the count of distinct values of a given field
|
|
132
|
+
*
|
|
133
|
+
* @param field Field to count distinct entries
|
|
134
|
+
* @returns Count
|
|
135
|
+
*/
|
|
136
|
+
count(field?: keyof T): Datum<number>;
|
|
137
|
+
/**
|
|
138
|
+
* Sum of the values on the given field
|
|
139
|
+
*
|
|
140
|
+
* @param field Field to use
|
|
141
|
+
* @returns Sum
|
|
142
|
+
*/
|
|
143
|
+
sum(field?: keyof T): Datum<number>;
|
|
144
|
+
/**
|
|
145
|
+
* Average of the values on the given field
|
|
146
|
+
*
|
|
147
|
+
* @param field Field to use
|
|
148
|
+
* @returns Average
|
|
149
|
+
*/
|
|
150
|
+
avg(field?: keyof T): Datum<number>;
|
|
151
|
+
/**
|
|
152
|
+
* Minimum of the values on the given field
|
|
153
|
+
*
|
|
154
|
+
* @param field Field to use
|
|
155
|
+
* @returns Minimum value
|
|
156
|
+
*/
|
|
157
|
+
min(field?: keyof T): Datum<number>;
|
|
158
|
+
/**
|
|
159
|
+
* Maximum of the values on the given field
|
|
160
|
+
*
|
|
161
|
+
* @param field Field to use
|
|
162
|
+
* @returns Maximum value
|
|
163
|
+
*/
|
|
164
|
+
max(field?: keyof T): Datum<number>;
|
|
165
|
+
/**
|
|
166
|
+
* Gets an array of distinct documents in the stream
|
|
167
|
+
*
|
|
168
|
+
* @returns Array of documents
|
|
169
|
+
*/
|
|
170
|
+
distinct(): Datum<T[]>;
|
|
171
|
+
distinct(field: undefined): Datum<T[]>;
|
|
172
|
+
/**
|
|
173
|
+
* Gets a stream of the distinct values of a field
|
|
174
|
+
*
|
|
175
|
+
* @param index Field to use
|
|
176
|
+
* @returns New stream
|
|
177
|
+
*/
|
|
178
|
+
distinct<TK extends keyof T>(field: TK): Stream<T[TK]>;
|
|
179
|
+
/**
|
|
180
|
+
* Turns this stream into a change feed
|
|
181
|
+
*
|
|
182
|
+
* @returns Change feed
|
|
183
|
+
*/
|
|
184
|
+
changes(): Query<Changes<T>[]>;
|
|
185
|
+
}
|