shelving 1.66.0 → 1.67.0
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/db/Database.d.ts +3 -5
- package/db/Database.js +3 -4
- package/db/Reference.d.ts +4 -8
- package/db/Reference.js +5 -6
- package/package.json +1 -1
- package/query/Filter.js +21 -1
- package/query/Query.d.ts +10 -3
- package/query/Query.js +51 -13
- package/query/Rules.js +2 -1
- package/query/Sort.js +1 -1
package/db/Database.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import type { Validators, ValidatorType } from "../util/validate.js";
|
|
2
|
-
import type { Key, Datas } from "../util/data.js";
|
|
2
|
+
import type { Key, Datas, Entity } from "../util/data.js";
|
|
3
3
|
import type { Provider } from "../provider/Provider.js";
|
|
4
|
-
import type {
|
|
5
|
-
import type { FilterProps } from "../query/Filter.js";
|
|
6
|
-
import { Entity } from "../util/data.js";
|
|
4
|
+
import type { QueryProps } from "../query/Query.js";
|
|
7
5
|
import { DocumentReference, QueryReference } from "./Reference.js";
|
|
8
6
|
/**
|
|
9
7
|
* Combines a database model and a provider.
|
|
@@ -17,7 +15,7 @@ export declare class Database<V extends Validators<Datas> = Validators<Datas>> {
|
|
|
17
15
|
readonly provider: Provider;
|
|
18
16
|
constructor(validators: V, provider: Provider);
|
|
19
17
|
/** Create a query on a collection in this model. */
|
|
20
|
-
query<K extends Key<V>>(collection: K,
|
|
18
|
+
query<K extends Key<V>>(collection: K, query?: QueryProps<Entity<ValidatorType<V[K]>>>): QueryReference<ValidatorType<V[K]>>;
|
|
21
19
|
/** Reference a document in a collection in this model. */
|
|
22
20
|
doc<K extends Key<V>>(collection: K, id: string): DocumentReference<ValidatorType<V[K]>>;
|
|
23
21
|
/**
|
package/db/Database.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { Filters } from "../query/Filters.js";
|
|
2
|
-
import { Sorts } from "../query/Sorts.js";
|
|
3
1
|
import { DocumentReference, QueryReference } from "./Reference.js";
|
|
4
2
|
/**
|
|
5
3
|
* Combines a database model and a provider.
|
|
@@ -15,8 +13,9 @@ export class Database {
|
|
|
15
13
|
this.provider = provider;
|
|
16
14
|
}
|
|
17
15
|
/** Create a query on a collection in this model. */
|
|
18
|
-
query(collection,
|
|
19
|
-
|
|
16
|
+
query(collection, query) {
|
|
17
|
+
const validator = this.validators[collection];
|
|
18
|
+
return new QueryReference(this, validator, collection, query);
|
|
20
19
|
}
|
|
21
20
|
/** Reference a document in a collection in this model. */
|
|
22
21
|
doc(collection, id) {
|
package/db/Reference.d.ts
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
import type { Data, OptionalData, Entity, OptionalEntity, Entities } from "../util/data.js";
|
|
2
2
|
import type { Dispatch } from "../util/function.js";
|
|
3
|
-
import type { SortKeys } from "../query/Sort.js";
|
|
4
3
|
import { ImmutableArray } from "../util/array.js";
|
|
5
4
|
import type { PartialObserver } from "../observe/Observer.js";
|
|
6
5
|
import type { Validator } from "../util/validate.js";
|
|
7
|
-
import { Query } from "../query/Query.js";
|
|
8
|
-
import { Filters } from "../query/Filters.js";
|
|
9
|
-
import { Sorts } from "../query/Sorts.js";
|
|
6
|
+
import { Query, QueryProps } from "../query/Query.js";
|
|
10
7
|
import { DataUpdate, PropUpdates } from "../update/DataUpdate.js";
|
|
11
|
-
import {
|
|
12
|
-
import { Observable, Unsubscribe } from "../observe/Observable.js";
|
|
8
|
+
import type { Observable, Unsubscribe } from "../observe/Observable.js";
|
|
13
9
|
import type { Database } from "./Database.js";
|
|
14
10
|
/** A refence to a location in a database. */
|
|
15
11
|
export interface Reference {
|
|
@@ -21,7 +17,7 @@ export declare class QueryReference<T extends Data = Data> extends Query<Entity<
|
|
|
21
17
|
readonly db: Database;
|
|
22
18
|
readonly validator: Validator<T>;
|
|
23
19
|
readonly collection: string;
|
|
24
|
-
constructor(db: Database, validator: Validator<T>, collection: string, filters
|
|
20
|
+
constructor(db: Database, validator: Validator<T>, collection: string, { sort, limit, ...filters }?: QueryProps<Entity<T>>);
|
|
25
21
|
/** Reference a document in this query's collection. */
|
|
26
22
|
doc(id: string): DocumentReference<T>;
|
|
27
23
|
/**
|
|
@@ -104,7 +100,7 @@ export declare class DocumentReference<T extends Data = Data> implements Observa
|
|
|
104
100
|
readonly id: string;
|
|
105
101
|
constructor(db: Database, validator: Validator<T>, collection: string, id: string);
|
|
106
102
|
/** Create a query on this document's collection. */
|
|
107
|
-
query(
|
|
103
|
+
query(query?: QueryProps<Entity<T>>): QueryReference<T>;
|
|
108
104
|
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
|
109
105
|
get optional(): QueryReference<T>;
|
|
110
106
|
/**
|
package/db/Reference.js
CHANGED
|
@@ -5,12 +5,11 @@ import { Sorts } from "../query/Sorts.js";
|
|
|
5
5
|
import { callAsync } from "../util/async.js";
|
|
6
6
|
import { countItems, hasItems } from "../util/iterate.js";
|
|
7
7
|
import { DataUpdate } from "../update/DataUpdate.js";
|
|
8
|
-
import { Filter } from "../query/Filter.js";
|
|
9
8
|
import { RequiredError } from "../error/RequiredError.js";
|
|
10
9
|
/** A query reference within a specific database. */
|
|
11
10
|
export class QueryReference extends Query {
|
|
12
|
-
constructor(db, validator, collection,
|
|
13
|
-
super(filters,
|
|
11
|
+
constructor(db, validator, collection, { sort, limit, ...filters } = {}) {
|
|
12
|
+
super(Filters.on(filters), sort && Sorts.on(sort), limit);
|
|
14
13
|
this.db = db;
|
|
15
14
|
this.validator = validator;
|
|
16
15
|
this.collection = collection;
|
|
@@ -127,12 +126,12 @@ export class DocumentReference {
|
|
|
127
126
|
this.id = id;
|
|
128
127
|
}
|
|
129
128
|
/** Create a query on this document's collection. */
|
|
130
|
-
query(
|
|
131
|
-
return new QueryReference(this.db, this.validator, this.collection,
|
|
129
|
+
query(query) {
|
|
130
|
+
return new QueryReference(this.db, this.validator, this.collection, query);
|
|
132
131
|
}
|
|
133
132
|
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
|
134
133
|
get optional() {
|
|
135
|
-
return new QueryReference(this.db, this.validator, this.collection,
|
|
134
|
+
return new QueryReference(this.db, this.validator, this.collection, { id: this.id, limit: 1 });
|
|
136
135
|
}
|
|
137
136
|
/**
|
|
138
137
|
* Does this document exist?
|
package/package.json
CHANGED
package/query/Filter.js
CHANGED
|
@@ -51,6 +51,26 @@ export class Filter extends Rule {
|
|
|
51
51
|
return filterItems(items, this);
|
|
52
52
|
}
|
|
53
53
|
toString() {
|
|
54
|
-
return
|
|
54
|
+
return `"${_formatKey(this)}":${JSON.stringify(this.value)}`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Convert a `Filter` */
|
|
58
|
+
function _formatKey({ key, operator }) {
|
|
59
|
+
switch (operator) {
|
|
60
|
+
case "NOT":
|
|
61
|
+
case "OUT":
|
|
62
|
+
return `!${key}`;
|
|
63
|
+
case "CONTAINS":
|
|
64
|
+
return `${key}[]`;
|
|
65
|
+
case "LT":
|
|
66
|
+
return `${key}<`;
|
|
67
|
+
case "LTE":
|
|
68
|
+
return `${key}<=`;
|
|
69
|
+
case "GT":
|
|
70
|
+
return `${key}>`;
|
|
71
|
+
case "GTE":
|
|
72
|
+
return `${key}>=`;
|
|
73
|
+
default:
|
|
74
|
+
return key;
|
|
55
75
|
}
|
|
56
76
|
}
|
package/query/Query.d.ts
CHANGED
|
@@ -5,6 +5,11 @@ import { Sortable, Sorts } from "./Sorts.js";
|
|
|
5
5
|
import { Rule } from "./Rule.js";
|
|
6
6
|
import { FilterProps } from "./Filter.js";
|
|
7
7
|
import { SortKey, SortKeys } from "./Sort.js";
|
|
8
|
+
/** Set of props for a query defined as an object. */
|
|
9
|
+
export declare type QueryProps<T extends Data> = FilterProps<T> & {
|
|
10
|
+
readonly sort?: SortKey<T> | ImmutableArray<SortKey<T>>;
|
|
11
|
+
readonly limit?: number | null;
|
|
12
|
+
};
|
|
8
13
|
/** Interface that combines Filterable, Sortable, Sliceable. */
|
|
9
14
|
export interface Queryable<T extends Data> extends Filterable<T>, Sortable<T> {
|
|
10
15
|
/**
|
|
@@ -26,11 +31,13 @@ export interface Queryable<T extends Data> extends Filterable<T>, Sortable<T> {
|
|
|
26
31
|
readonly limit: number | null;
|
|
27
32
|
/** Return a new instance of this class with a limit set. */
|
|
28
33
|
max(max: number | null): this;
|
|
34
|
+
/** Return a new instance of this class with new filters, sorts, limits set. */
|
|
35
|
+
query(query: QueryProps<T>): this;
|
|
29
36
|
}
|
|
30
37
|
/** Allows filtering, sorting, and limiting on a set of results. */
|
|
31
38
|
export declare class Query<T extends Data> extends Rule<T> implements Queryable<T> {
|
|
32
39
|
/** Create a new `Query` object from a set of `QueryProps` */
|
|
33
|
-
static on<X extends Data>(
|
|
40
|
+
static on<X extends Data>({ sort, limit, ...filters }: QueryProps<X>): Query<X>;
|
|
34
41
|
readonly filters: Filters<T>;
|
|
35
42
|
readonly sorts: Sorts<T>;
|
|
36
43
|
readonly limit: number | null;
|
|
@@ -44,10 +51,10 @@ export declare class Query<T extends Data> extends Rule<T> implements Queryable<
|
|
|
44
51
|
sort(...keys: SortKeys<T>[]): this;
|
|
45
52
|
get unsort(): this;
|
|
46
53
|
rank(left: T, right: T): number;
|
|
47
|
-
/** Return a new instance of this class with a limit defined. */
|
|
48
|
-
max(limit: number | null): this;
|
|
49
54
|
after(item: T): this;
|
|
50
55
|
before(item: T): this;
|
|
56
|
+
max(limit: number | null): this;
|
|
57
|
+
query({ sort, limit, ...filters }: QueryProps<T>): this;
|
|
51
58
|
transform(items: Iterable<T>): Iterable<T>;
|
|
52
59
|
toString(): string;
|
|
53
60
|
}
|
package/query/Query.js
CHANGED
|
@@ -17,32 +17,44 @@ export class Query extends Rule {
|
|
|
17
17
|
this.limit = limit;
|
|
18
18
|
}
|
|
19
19
|
/** Create a new `Query` object from a set of `QueryProps` */
|
|
20
|
-
static on(
|
|
21
|
-
return new Query(filters && Filters.on(filters),
|
|
20
|
+
static on({ sort, limit, ...filters }) {
|
|
21
|
+
return new Query(filters && Filters.on(filters), sort && Sorts.on(sort), limit);
|
|
22
22
|
}
|
|
23
23
|
filter(input, value) {
|
|
24
|
-
return {
|
|
24
|
+
return {
|
|
25
|
+
__proto__: Object.getPrototypeOf(this),
|
|
26
|
+
...this,
|
|
27
|
+
filters: this.filters.filter(input, value), // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
28
|
+
};
|
|
25
29
|
}
|
|
26
30
|
get unfilter() {
|
|
27
|
-
return {
|
|
31
|
+
return {
|
|
32
|
+
__proto__: Object.getPrototypeOf(this),
|
|
33
|
+
...this,
|
|
34
|
+
filters: EMPTY_FILTERS,
|
|
35
|
+
};
|
|
28
36
|
}
|
|
29
37
|
match(item) {
|
|
30
38
|
return this.filters.match(item);
|
|
31
39
|
}
|
|
32
40
|
// Implement `Sortable`
|
|
33
41
|
sort(...keys) {
|
|
34
|
-
return {
|
|
42
|
+
return {
|
|
43
|
+
__proto__: Object.getPrototypeOf(this),
|
|
44
|
+
...this,
|
|
45
|
+
sorts: this.sorts.sort(...keys),
|
|
46
|
+
};
|
|
35
47
|
}
|
|
36
48
|
get unsort() {
|
|
37
|
-
return {
|
|
49
|
+
return {
|
|
50
|
+
__proto__: Object.getPrototypeOf(this),
|
|
51
|
+
...this,
|
|
52
|
+
sorts: EMPTY_SORTS,
|
|
53
|
+
};
|
|
38
54
|
}
|
|
39
55
|
rank(left, right) {
|
|
40
56
|
return this.sorts.rank(left, right);
|
|
41
57
|
}
|
|
42
|
-
/** Return a new instance of this class with a limit defined. */
|
|
43
|
-
max(limit) {
|
|
44
|
-
return limit === this.limit ? this : { __proto__: Object.getPrototypeOf(this), ...this, limit };
|
|
45
|
-
}
|
|
46
58
|
// Implement `Queryable`
|
|
47
59
|
after(item) {
|
|
48
60
|
const filters = [...this.filters];
|
|
@@ -52,7 +64,11 @@ export class Query extends Rule {
|
|
|
52
64
|
const { key, direction } = sort;
|
|
53
65
|
filters.push(new Filter(key, direction === "ASC" ? (sort === lastSort ? "GT" : "GTE") : sort === lastSort ? "LT" : "LTE", getProp(item, key)));
|
|
54
66
|
}
|
|
55
|
-
return {
|
|
67
|
+
return {
|
|
68
|
+
__proto__: Object.getPrototypeOf(this),
|
|
69
|
+
...this,
|
|
70
|
+
filters: new Filters(...filters),
|
|
71
|
+
};
|
|
56
72
|
}
|
|
57
73
|
before(item) {
|
|
58
74
|
const filters = [...this.filters];
|
|
@@ -62,7 +78,27 @@ export class Query extends Rule {
|
|
|
62
78
|
const { key, direction } = sort;
|
|
63
79
|
filters.push(new Filter(key, direction === "ASC" ? (sort === lastSort ? "LT" : "LTE") : sort === lastSort ? "GT" : "GTE", getProp(item, key)));
|
|
64
80
|
}
|
|
65
|
-
return {
|
|
81
|
+
return {
|
|
82
|
+
__proto__: Object.getPrototypeOf(this),
|
|
83
|
+
...this,
|
|
84
|
+
filters: new Filters(...filters),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
max(limit) {
|
|
88
|
+
return {
|
|
89
|
+
__proto__: Object.getPrototypeOf(this),
|
|
90
|
+
...this,
|
|
91
|
+
limit,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
query({ sort, limit, ...filters }) {
|
|
95
|
+
return {
|
|
96
|
+
__proto__: Object.getPrototypeOf(this),
|
|
97
|
+
...this,
|
|
98
|
+
filters: this.filters.filter(filters),
|
|
99
|
+
sorts: sort ? this.sorts.sort(sort) : this.sorts,
|
|
100
|
+
limit: limit !== undefined ? limit : this.limit,
|
|
101
|
+
};
|
|
66
102
|
}
|
|
67
103
|
// Implement `Rule`
|
|
68
104
|
transform(items) {
|
|
@@ -71,6 +107,8 @@ export class Query extends Rule {
|
|
|
71
107
|
}
|
|
72
108
|
// Implement toString()
|
|
73
109
|
toString() {
|
|
74
|
-
|
|
110
|
+
const filters = this.filters.toString();
|
|
111
|
+
const sorts = this.sorts.toString();
|
|
112
|
+
return `{${filters}${sorts ? `${filters ? "," : ""}"sort":[${sorts}]` : ""}${typeof this.limit === "number" ? `${filters || sorts ? "," : ""}"limit":${this.limit}` : ""}}`;
|
|
75
113
|
}
|
|
76
114
|
}
|
package/query/Rules.js
CHANGED
|
@@ -18,8 +18,9 @@ export class Rules extends Rule {
|
|
|
18
18
|
get size() {
|
|
19
19
|
return this._rules.length;
|
|
20
20
|
}
|
|
21
|
+
// Override to join the strings from the rules together with `,` commas.
|
|
21
22
|
toString() {
|
|
22
|
-
return this._rules.map(
|
|
23
|
+
return this._rules.map(String).join(",");
|
|
23
24
|
}
|
|
24
25
|
/** Clone this set of rules but add additional rules. */
|
|
25
26
|
with(...rules) {
|