shelving 1.19.0 → 1.20.3
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/Change.d.ts +16 -18
- package/db/Change.js +7 -13
- package/db/Database.d.ts +59 -45
- package/db/Database.js +52 -44
- package/db/Pagination.d.ts +19 -11
- package/db/Pagination.js +49 -32
- package/db/errors.d.ts +12 -12
- package/error/ConditionError.d.ts +7 -0
- package/error/ConditionError.js +10 -0
- package/error/index.d.ts +1 -0
- package/error/index.js +1 -0
- package/firestore/client/FirestoreClientProvider.d.ts +9 -9
- package/firestore/lite/FirestoreLiteProvider.d.ts +7 -7
- package/firestore/server/FirestoreServerProvider.d.ts +9 -9
- package/package.json +8 -8
- package/provider/BatchProvider.d.ts +7 -7
- package/provider/BatchProvider.js +16 -12
- package/provider/CacheProvider.d.ts +14 -14
- package/provider/CacheProvider.js +11 -20
- package/provider/MemoryProvider.d.ts +10 -10
- package/provider/MemoryProvider.js +1 -1
- package/provider/Provider.d.ts +22 -22
- package/provider/ThroughProvider.d.ts +13 -13
- package/provider/ValidationProvider.d.ts +10 -10
- package/provider/ValidationProvider.js +5 -19
- package/query/Query.d.ts +1 -1
- package/query/Query.js +2 -2
- package/react/index.d.ts +2 -2
- package/react/index.js +2 -2
- package/react/{useResult.d.ts → useDocument.d.ts} +5 -5
- package/react/{useResult.js → useDocument.js} +7 -9
- package/react/{useData.d.ts → useDocumentData.d.ts} +5 -5
- package/react/useDocumentData.js +9 -0
- package/react/useFetch.d.ts +1 -1
- package/react/useFetch.js +2 -3
- package/react/usePagination.d.ts +2 -2
- package/react/useQuery.d.ts +8 -7
- package/react/useQuery.js +13 -14
- package/react/useSubscribe.js +2 -2
- package/schema/AllowSchema.d.ts +17 -7
- package/schema/AllowSchema.js +21 -10
- package/schema/ArraySchema.js +1 -1
- package/schema/ColorSchema.d.ts +4 -3
- package/schema/ColorSchema.js +5 -4
- package/schema/DataSchema.d.ts +3 -3
- package/schema/DataSchema.js +3 -3
- package/schema/DateSchema.d.ts +2 -2
- package/schema/DateSchema.js +3 -3
- package/schema/EmailSchema.d.ts +4 -3
- package/schema/EmailSchema.js +5 -4
- package/schema/KeySchema.d.ts +2 -2
- package/schema/KeySchema.js +3 -3
- package/schema/{UrlSchema.d.ts → LinkSchema.d.ts} +6 -5
- package/schema/{UrlSchema.js → LinkSchema.js} +7 -6
- package/schema/MapSchema.d.ts +0 -2
- package/schema/MapSchema.js +6 -9
- package/schema/NumberSchema.d.ts +1 -3
- package/schema/NumberSchema.js +2 -2
- package/schema/ObjectSchema.d.ts +1 -2
- package/schema/ObjectSchema.js +2 -2
- package/schema/{NullableSchema.d.ts → OptionalSchema.d.ts} +3 -3
- package/schema/{NullableSchema.js → OptionalSchema.js} +4 -4
- package/schema/PhoneSchema.d.ts +4 -3
- package/schema/PhoneSchema.js +5 -4
- package/schema/StringSchema.d.ts +5 -5
- package/schema/StringSchema.js +1 -2
- package/schema/ThroughSchema.d.ts +1 -1
- package/schema/ThroughSchema.js +2 -2
- package/schema/index.d.ts +2 -2
- package/schema/index.js +2 -2
- package/stream/ArrayState.d.ts +1 -0
- package/stream/ArrayState.js +5 -0
- package/stream/State.d.ts +38 -17
- package/stream/State.js +66 -20
- package/stream/Stream.d.ts +56 -7
- package/stream/Stream.js +114 -8
- package/stream/index.d.ts +0 -6
- package/stream/index.js +0 -6
- package/test/basics.js +2 -2
- package/util/assert.d.ts +4 -2
- package/util/assert.js +7 -2
- package/util/async.d.ts +41 -0
- package/util/async.js +71 -0
- package/util/constants.d.ts +3 -3
- package/util/constants.js +3 -3
- package/util/data.d.ts +114 -4
- package/util/data.js +62 -3
- package/util/derive.d.ts +0 -3
- package/util/derive.js +4 -12
- package/util/diff.d.ts +3 -3
- package/util/dispatch.d.ts +3 -3
- package/util/dispatch.js +5 -15
- package/util/entry.d.ts +5 -0
- package/util/equal.d.ts +2 -2
- package/util/equal.js +5 -2
- package/util/error.d.ts +0 -4
- package/util/hydrate.js +3 -2
- package/util/index.d.ts +2 -2
- package/util/index.js +2 -2
- package/util/{iterable.d.ts → iterate.d.ts} +45 -18
- package/util/{iterable.js → iterate.js} +72 -13
- package/util/map.d.ts +2 -3
- package/util/map.js +2 -2
- package/util/merge.d.ts +7 -4
- package/util/merge.js +7 -5
- package/util/object.d.ts +7 -114
- package/util/object.js +3 -60
- package/util/observable.d.ts +57 -9
- package/util/observable.js +132 -44
- package/util/string.js +2 -2
- package/util/validate.d.ts +4 -2
- package/util/validate.js +2 -3
- package/react/useData.js +0 -9
- package/stream/AbstractState.d.ts +0 -35
- package/stream/AbstractState.js +0 -84
- package/stream/AbstractStream.d.ts +0 -66
- package/stream/AbstractStream.js +0 -123
- package/stream/DeriveStream.d.ts +0 -12
- package/stream/DeriveStream.js +0 -29
- package/stream/LimitStream.d.ts +0 -13
- package/stream/LimitStream.js +0 -26
- package/stream/ValidateStream.d.ts +0 -8
- package/stream/ValidateStream.js +0 -18
- package/stream/errors.d.ts +0 -4
- package/stream/errors.js +0 -6
- package/util/promise.d.ts +0 -14
- package/util/promise.js +0 -27
package/db/Change.d.ts
CHANGED
|
@@ -1,36 +1,34 @@
|
|
|
1
1
|
import { Transforms, Transform } from "../transform/index.js";
|
|
2
|
-
import {
|
|
3
|
-
import type { Database,
|
|
2
|
+
import { ImmutableArray, Data } from "../util/index.js";
|
|
3
|
+
import type { Database, DataDocument } from "./Database.js";
|
|
4
4
|
/** A single change that can be made to a database. */
|
|
5
|
-
export declare abstract class Change
|
|
5
|
+
export declare abstract class Change {
|
|
6
6
|
/** Apply this change to a database. */
|
|
7
|
-
abstract apply(db: Database
|
|
7
|
+
abstract apply(db: Database): void | PromiseLike<void>;
|
|
8
8
|
}
|
|
9
9
|
/** A change that writes a document in a database. */
|
|
10
|
-
export declare class Write<
|
|
11
|
-
|
|
12
|
-
readonly collection: C;
|
|
10
|
+
export declare class Write<T extends Data> extends Change {
|
|
11
|
+
readonly collection: string;
|
|
13
12
|
readonly id: string;
|
|
14
|
-
readonly value:
|
|
15
|
-
constructor(collection
|
|
16
|
-
apply(db: Database
|
|
13
|
+
readonly value: Data | Transform<Data> | undefined;
|
|
14
|
+
constructor({ collection, id }: DataDocument<T>, value: T | Transform<T> | undefined);
|
|
15
|
+
apply(db: Database): void | PromiseLike<void>;
|
|
17
16
|
}
|
|
18
17
|
/**
|
|
19
18
|
* Set of writes that can be applied to documents in a database.
|
|
20
19
|
* - Sets of changes are predictable and repeatable, so unpredictable operations like `create()` and query operations are not supported.
|
|
21
20
|
* - Every change must be applied to a specific database document in a specific collection.
|
|
22
21
|
*/
|
|
23
|
-
export declare class Writes
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
constructor(...changes: ImmutableArray<Change<D>>);
|
|
22
|
+
export declare class Writes extends Change {
|
|
23
|
+
readonly changes: ImmutableArray<Change>;
|
|
24
|
+
constructor(...changes: Change[]);
|
|
27
25
|
/** Return a new `Changes` instance with an additional `Write` instance in its changes list. */
|
|
28
|
-
set<
|
|
26
|
+
set<T extends Data>(ref: DataDocument<T>, data: T): this;
|
|
29
27
|
/** Return a new `Changes` instance with an additional `Write` instance in its changes list. */
|
|
30
|
-
delete<
|
|
28
|
+
delete<T extends Data>(ref: DataDocument<T>): this;
|
|
31
29
|
/** Return a new `Changes` instance with an additional `Write` instance in its changes list. */
|
|
32
|
-
update<
|
|
33
|
-
apply(db: Database
|
|
30
|
+
update<T extends Data>(ref: DataDocument<T>, transforms: Transforms<T> | Transform<T>): this;
|
|
31
|
+
apply(db: Database): Promise<void> | undefined;
|
|
34
32
|
}
|
|
35
33
|
/** Set of hydrations for all change classes. */
|
|
36
34
|
export declare const CHANGE_HYDRATIONS: {
|
package/db/Change.js
CHANGED
|
@@ -5,15 +5,12 @@ export class Change {
|
|
|
5
5
|
}
|
|
6
6
|
/** A change that writes a document in a database. */
|
|
7
7
|
export class Write extends Change {
|
|
8
|
-
constructor(collection, id, value) {
|
|
8
|
+
constructor({ collection, id }, value) {
|
|
9
9
|
super();
|
|
10
10
|
this.collection = collection;
|
|
11
11
|
this.id = id;
|
|
12
12
|
this.value = value;
|
|
13
13
|
}
|
|
14
|
-
static on({ collection, id }, value) {
|
|
15
|
-
return new Write(collection, id, value);
|
|
16
|
-
}
|
|
17
14
|
apply(db) {
|
|
18
15
|
return db.doc(this.collection, this.id).write(this.value);
|
|
19
16
|
}
|
|
@@ -28,23 +25,20 @@ export class Writes extends Change {
|
|
|
28
25
|
super();
|
|
29
26
|
this.changes = changes;
|
|
30
27
|
}
|
|
31
|
-
static on(db, ...changes) {
|
|
32
|
-
return new Writes(...changes);
|
|
33
|
-
}
|
|
34
28
|
/** Return a new `Changes` instance with an additional `Write` instance in its changes list. */
|
|
35
|
-
set(
|
|
36
|
-
return { __proto__: Object.getPrototypeOf(this), ...this, changes: [...this.changes, new Write(
|
|
29
|
+
set(ref, data) {
|
|
30
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, changes: [...this.changes, new Write(ref, data)] };
|
|
37
31
|
}
|
|
38
32
|
/** Return a new `Changes` instance with an additional `Write` instance in its changes list. */
|
|
39
|
-
delete(
|
|
40
|
-
return { __proto__: Object.getPrototypeOf(this), ...this, changes: [...this.changes, new Write(
|
|
33
|
+
delete(ref) {
|
|
34
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, changes: [...this.changes, new Write(ref, undefined)] };
|
|
41
35
|
}
|
|
42
36
|
/** Return a new `Changes` instance with an additional `Write` instance in its changes list. */
|
|
43
|
-
update(
|
|
37
|
+
update(ref, transforms) {
|
|
44
38
|
return {
|
|
45
39
|
__proto__: Object.getPrototypeOf(this),
|
|
46
40
|
...this,
|
|
47
|
-
changes: [...this.changes, new Write(
|
|
41
|
+
changes: [...this.changes, new Write(ref, transforms instanceof Transform ? transforms : new DataTransform(transforms))],
|
|
48
42
|
};
|
|
49
43
|
}
|
|
50
44
|
apply(db) {
|
package/db/Database.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Dispatcher, Entry, Observable, Observer, Result, Unsubscriber,
|
|
1
|
+
import { Dispatcher, Entry, Observable, Observer, Result, Unsubscriber, ResultsMap, Validatable, Validator, Key, Data, Results, Datas, Validators, ValidatorType } from "../util/index.js";
|
|
2
2
|
import { Transform, Transforms } from "../transform/index.js";
|
|
3
3
|
import type { Provider } from "../provider/Provider.js";
|
|
4
4
|
import { Filters, Sorts, Query } from "../query/index.js";
|
|
@@ -9,23 +9,23 @@ import { Filters, Sorts, Query } from "../query/index.js";
|
|
|
9
9
|
* @param collections Set of loci describing collections at the root level of the database.
|
|
10
10
|
* @param provider Provider that allows data to be read/written.
|
|
11
11
|
*/
|
|
12
|
-
export declare class Database<
|
|
13
|
-
readonly validators:
|
|
14
|
-
readonly provider: Provider
|
|
15
|
-
constructor(validators:
|
|
12
|
+
export declare class Database<V extends Validators<Datas> = Validators<Datas>> {
|
|
13
|
+
readonly validators: V;
|
|
14
|
+
readonly provider: Provider;
|
|
15
|
+
constructor(validators: V, provider: Provider);
|
|
16
16
|
/** Create a query on a collection in this model. */
|
|
17
|
-
query<
|
|
17
|
+
query<K extends Key<V>>(collection: K, filters?: Filters<ValidatorType<V[K]>>, sorts?: Sorts<ValidatorType<V[K]>>, limit?: number | null): DataQuery<ValidatorType<V[K]>>;
|
|
18
18
|
/** Reference a document in a collection in this model. */
|
|
19
|
-
doc<
|
|
19
|
+
doc<K extends Key<V>>(collection: K, id: string): DataDocument<ValidatorType<V[K]>>;
|
|
20
20
|
}
|
|
21
21
|
/** A documents reference within a specific database. */
|
|
22
|
-
export declare class
|
|
23
|
-
readonly
|
|
24
|
-
readonly validator: Validator<
|
|
25
|
-
readonly collection:
|
|
26
|
-
constructor(
|
|
22
|
+
export declare class DataQuery<T extends Data = Data> extends Query<T> implements Observable<Results<T>>, Validatable<Results<T>>, Iterable<Entry<T>> {
|
|
23
|
+
readonly provider: Provider;
|
|
24
|
+
readonly validator: Validator<T>;
|
|
25
|
+
readonly collection: string;
|
|
26
|
+
constructor(provider: Provider, validator: Validator<T>, collection: string, filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null);
|
|
27
27
|
/** Reference a document in this query's collection. */
|
|
28
|
-
doc(id: string):
|
|
28
|
+
doc(id: string): DataDocument<T>;
|
|
29
29
|
/**
|
|
30
30
|
* Create a new document with a random ID.
|
|
31
31
|
* - Created document is guaranteed to have a unique ID.
|
|
@@ -33,22 +33,27 @@ export declare class DatabaseQuery<D extends Datas, C extends Key<D>> extends Qu
|
|
|
33
33
|
* @param data Complete data to set the document to.
|
|
34
34
|
* @return String ID for the created document (possibly promised).
|
|
35
35
|
*/
|
|
36
|
-
add(data:
|
|
36
|
+
add(data: T): string | PromiseLike<string>;
|
|
37
37
|
/**
|
|
38
38
|
* Get an iterable that yields the results of this entry.
|
|
39
39
|
* @return Map containing the results.
|
|
40
40
|
*/
|
|
41
|
-
get results(): Results<
|
|
41
|
+
get results(): Results<T> | PromiseLike<Results<T>>;
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
44
|
-
* @return
|
|
43
|
+
* Get an iterable that yields the results of this entry.
|
|
44
|
+
* @return Map containing the results.
|
|
45
45
|
*/
|
|
46
|
-
get
|
|
46
|
+
get resultsMap(): ResultsMap<T> | PromiseLike<ResultsMap<T>>;
|
|
47
47
|
/**
|
|
48
48
|
* Count the number of results of this set of documents.
|
|
49
49
|
* @return Number of documents in the collection (possibly promised).
|
|
50
50
|
*/
|
|
51
|
-
get count(): number |
|
|
51
|
+
get count(): number | PromiseLike<number>;
|
|
52
|
+
/**
|
|
53
|
+
* Get an entry for the first document matching this query.
|
|
54
|
+
* @return Entry in `[id, data]` format for the first document, or `undefined` if there are no matching documents (possibly promised).
|
|
55
|
+
*/
|
|
56
|
+
get first(): Entry<T> | undefined | PromiseLike<Entry<T> | undefined>;
|
|
52
57
|
/**
|
|
53
58
|
* Subscribe to all matching documents.
|
|
54
59
|
* - `next()` is called once with the initial results, and again any time the results change.
|
|
@@ -60,19 +65,26 @@ export declare class DatabaseQuery<D extends Datas, C extends Key<D>> extends Qu
|
|
|
60
65
|
*
|
|
61
66
|
* @return Function that ends the subscription.
|
|
62
67
|
*/
|
|
63
|
-
subscribe(next: Observer<Results<
|
|
68
|
+
subscribe(next: Observer<Results<T>> | Dispatcher<Results<T>>, error?: Dispatcher<Error | unknown>, complete?: Dispatcher<void>): Unsubscriber;
|
|
64
69
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
70
|
+
* Subscribe to all matching documents.
|
|
71
|
+
* - `next()` is called once with the initial results, and again any time the results change.
|
|
72
|
+
*
|
|
73
|
+
* @param observer Observer with `next`, `error`, or `complete` methods that the document results are reported back to.
|
|
74
|
+
* @param next Callback that is called once initially and again whenever the results change.
|
|
75
|
+
* @param error Callback that is called if an error occurs.
|
|
76
|
+
* @param complete Callback that is called when the subscription is done.
|
|
77
|
+
*
|
|
78
|
+
* @return Function that ends the subscription.
|
|
67
79
|
*/
|
|
68
|
-
|
|
80
|
+
subscribeMap(next: Observer<ResultsMap<T>> | Dispatcher<ResultsMap<T>>, error?: Dispatcher<Error | unknown>, complete?: Dispatcher<void>): Unsubscriber;
|
|
69
81
|
/**
|
|
70
82
|
* Set all matching documents to the same exact value.
|
|
71
83
|
*
|
|
72
84
|
* @param data Complete data to set the document to.
|
|
73
85
|
* @return Nothing (possibly promised).
|
|
74
86
|
*/
|
|
75
|
-
set(data:
|
|
87
|
+
set(data: T): void | PromiseLike<void>;
|
|
76
88
|
/**
|
|
77
89
|
* Update all matching documents with the same partial value.
|
|
78
90
|
*
|
|
@@ -81,46 +93,46 @@ export declare class DatabaseQuery<D extends Datas, C extends Key<D>> extends Qu
|
|
|
81
93
|
*
|
|
82
94
|
* @return Nothing (possibly promised).
|
|
83
95
|
*/
|
|
84
|
-
update(transform: Transform<
|
|
96
|
+
update(transform: Transform<T> | Transforms<T>): void | PromiseLike<void>;
|
|
85
97
|
/**
|
|
86
98
|
* Delete all matching documents.
|
|
87
99
|
* @return Nothing (possibly promised).
|
|
88
100
|
*/
|
|
89
|
-
delete(): void |
|
|
101
|
+
delete(): void | PromiseLike<void>;
|
|
90
102
|
/**
|
|
91
103
|
* Combine `set()`, `update()`, `delete()` into a single method.
|
|
92
104
|
* @return Nothing (possibly promised).
|
|
93
105
|
*/
|
|
94
|
-
write(value: Result<
|
|
106
|
+
write(value: Result<T> | Transform<T>): void | PromiseLike<void>;
|
|
95
107
|
/** Iterate over the resuls (will throw `Promise` if the results are asynchronous). */
|
|
96
|
-
[Symbol.iterator](): Iterator<Entry<
|
|
108
|
+
[Symbol.iterator](): Iterator<Entry<T>, void>;
|
|
97
109
|
/** Validate a set of results for this query reference. */
|
|
98
|
-
validate(unsafeEntries: Results): Results<
|
|
110
|
+
validate(unsafeEntries: Results): Results<T>;
|
|
99
111
|
toString(): string;
|
|
100
112
|
}
|
|
101
113
|
/** A document reference within a specific database. */
|
|
102
|
-
export declare class
|
|
103
|
-
readonly
|
|
104
|
-
readonly validator: Validator<
|
|
105
|
-
readonly collection:
|
|
114
|
+
export declare class DataDocument<T extends Data = Data> implements Observable<Result<T>>, Validatable<T> {
|
|
115
|
+
readonly provider: Provider;
|
|
116
|
+
readonly validator: Validator<T>;
|
|
117
|
+
readonly collection: string;
|
|
106
118
|
readonly id: string;
|
|
107
|
-
constructor(
|
|
119
|
+
constructor(provider: Provider, validator: Validator<T>, collection: string, id: string);
|
|
108
120
|
/** Create a query on this document's collection. */
|
|
109
|
-
query(filters?: Filters<
|
|
121
|
+
query(filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null): DataQuery<T>;
|
|
110
122
|
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
|
111
|
-
get optional():
|
|
123
|
+
get optional(): DataQuery<T>;
|
|
112
124
|
/**
|
|
113
125
|
* Does this document exist?
|
|
114
126
|
*
|
|
115
127
|
* @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
|
|
116
128
|
*/
|
|
117
|
-
get exists(): boolean |
|
|
129
|
+
get exists(): boolean | PromiseLike<boolean>;
|
|
118
130
|
/**
|
|
119
131
|
* Get the result of this document.
|
|
120
132
|
*
|
|
121
133
|
* @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
|
|
122
134
|
*/
|
|
123
|
-
get result(): Result<
|
|
135
|
+
get result(): Result<T> | PromiseLike<Result<T>>;
|
|
124
136
|
/**
|
|
125
137
|
* Get the data of this document.
|
|
126
138
|
* - Useful for destructuring, e.g. `{ name, title } = await documentThatMustExist.asyncData`
|
|
@@ -128,7 +140,7 @@ export declare class DatabaseDocument<D extends Datas, C extends Key<D>> impleme
|
|
|
128
140
|
* @return Document's data (possibly promised).
|
|
129
141
|
* @throws RequiredError if the document's result was undefined.
|
|
130
142
|
*/
|
|
131
|
-
get data():
|
|
143
|
+
get data(): T | PromiseLike<T>;
|
|
132
144
|
/**
|
|
133
145
|
* Subscribe to the result of this document (indefinitely).
|
|
134
146
|
* - `next()` is called once with the initial result, and again any time the result changes.
|
|
@@ -140,7 +152,7 @@ export declare class DatabaseDocument<D extends Datas, C extends Key<D>> impleme
|
|
|
140
152
|
*
|
|
141
153
|
* @return Function that ends the subscription.
|
|
142
154
|
*/
|
|
143
|
-
subscribe(next: Observer<Result<
|
|
155
|
+
subscribe(next: Observer<Result<T>> | Dispatcher<Result<T>>, error?: Dispatcher<Error | unknown>, complete?: Dispatcher<void>): Unsubscriber;
|
|
144
156
|
/**
|
|
145
157
|
* Set the complete data of this document.
|
|
146
158
|
*
|
|
@@ -148,7 +160,7 @@ export declare class DatabaseDocument<D extends Datas, C extends Key<D>> impleme
|
|
|
148
160
|
*
|
|
149
161
|
* @return Nothing (possibly promised).
|
|
150
162
|
*/
|
|
151
|
-
set(data:
|
|
163
|
+
set(data: T): void | PromiseLike<void>;
|
|
152
164
|
/**
|
|
153
165
|
* Update this document with partial data.
|
|
154
166
|
* - If the document exists, merge the partial data into it.
|
|
@@ -160,19 +172,21 @@ export declare class DatabaseDocument<D extends Datas, C extends Key<D>> impleme
|
|
|
160
172
|
* @return Nothing (possibly promised).
|
|
161
173
|
* @throws Error If the document does not exist (ideally a `RequiredError` but may be provider-specific).
|
|
162
174
|
*/
|
|
163
|
-
update(transforms: Transform<
|
|
175
|
+
update(transforms: Transform<T> | Transforms<T>): void | PromiseLike<void>;
|
|
164
176
|
/**
|
|
165
177
|
* Delete this document.
|
|
166
178
|
* - Will not throw an error if the document doesn't exist.
|
|
167
179
|
*
|
|
168
180
|
* @return Nothing (possibly promised).
|
|
169
181
|
*/
|
|
170
|
-
delete(): void |
|
|
182
|
+
delete(): void | PromiseLike<void>;
|
|
171
183
|
/**
|
|
172
184
|
* Combine `set()`, `update()`, `delete()` into a single method.
|
|
173
185
|
*/
|
|
174
|
-
write(value: Result<
|
|
186
|
+
write(value: Result<T> | Transform<T>): void | PromiseLike<void>;
|
|
175
187
|
/** Validate data for this query reference. */
|
|
176
|
-
validate(unsafeData: Data):
|
|
188
|
+
validate(unsafeData: Data): T;
|
|
177
189
|
toString(): string;
|
|
178
190
|
}
|
|
191
|
+
/** Get the data for a document from a result for that document. */
|
|
192
|
+
export declare function getDocumentData<T extends Data>(result: Result<T>, ref: DataDocument<T>): T;
|
package/db/Database.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { callAsync, createObserver, getFirstItem, throwAsync, validate, toMap, countItems, DeriveObserver, } from "../util/index.js";
|
|
2
2
|
import { DataTransform, Transform } from "../transform/index.js";
|
|
3
3
|
import { Feedback, InvalidFeedback } from "../feedback/index.js";
|
|
4
4
|
import { Filters, Query, EqualFilter } from "../query/index.js";
|
|
@@ -10,6 +10,7 @@ import { DocumentRequiredError, DocumentValidationError, QueryValidationError }
|
|
|
10
10
|
* @param collections Set of loci describing collections at the root level of the database.
|
|
11
11
|
* @param provider Provider that allows data to be read/written.
|
|
12
12
|
*/
|
|
13
|
+
// Note: typing this with `Validators` rather than raw `Datas` works better for inference — type for props in each collection tends to get lost.
|
|
13
14
|
export class Database {
|
|
14
15
|
constructor(validators, provider) {
|
|
15
16
|
this.validators = validators;
|
|
@@ -17,24 +18,24 @@ export class Database {
|
|
|
17
18
|
}
|
|
18
19
|
/** Create a query on a collection in this model. */
|
|
19
20
|
query(collection, filters, sorts, limit) {
|
|
20
|
-
return new
|
|
21
|
+
return new DataQuery(this.provider, this.validators[collection], collection, filters, sorts, limit);
|
|
21
22
|
}
|
|
22
23
|
/** Reference a document in a collection in this model. */
|
|
23
24
|
doc(collection, id) {
|
|
24
|
-
return new
|
|
25
|
+
return new DataDocument(this.provider, this.validators[collection], collection, id);
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
28
|
/** A documents reference within a specific database. */
|
|
28
|
-
export class
|
|
29
|
-
constructor(
|
|
29
|
+
export class DataQuery extends Query {
|
|
30
|
+
constructor(provider, validator, collection, filters, sorts, limit) {
|
|
30
31
|
super(filters, sorts, limit);
|
|
31
|
-
this.
|
|
32
|
-
this.validator =
|
|
32
|
+
this.provider = provider;
|
|
33
|
+
this.validator = validator;
|
|
33
34
|
this.collection = collection;
|
|
34
35
|
}
|
|
35
36
|
/** Reference a document in this query's collection. */
|
|
36
37
|
doc(id) {
|
|
37
|
-
return new
|
|
38
|
+
return new DataDocument(this.provider, this.validator, this.collection, id);
|
|
38
39
|
}
|
|
39
40
|
/**
|
|
40
41
|
* Create a new document with a random ID.
|
|
@@ -44,28 +45,35 @@ export class DatabaseQuery extends Query {
|
|
|
44
45
|
* @return String ID for the created document (possibly promised).
|
|
45
46
|
*/
|
|
46
47
|
add(data) {
|
|
47
|
-
return this.
|
|
48
|
+
return this.provider.add(this, data);
|
|
48
49
|
}
|
|
49
50
|
/**
|
|
50
51
|
* Get an iterable that yields the results of this entry.
|
|
51
52
|
* @return Map containing the results.
|
|
52
53
|
*/
|
|
53
54
|
get results() {
|
|
54
|
-
return this.
|
|
55
|
+
return this.provider.getQuery(this);
|
|
55
56
|
}
|
|
56
57
|
/**
|
|
57
|
-
*
|
|
58
|
-
* @return
|
|
58
|
+
* Get an iterable that yields the results of this entry.
|
|
59
|
+
* @return Map containing the results.
|
|
59
60
|
*/
|
|
60
|
-
get
|
|
61
|
-
return
|
|
61
|
+
get resultsMap() {
|
|
62
|
+
return callAsync(toMap, this.provider.getQuery(this));
|
|
62
63
|
}
|
|
63
64
|
/**
|
|
64
65
|
* Count the number of results of this set of documents.
|
|
65
66
|
* @return Number of documents in the collection (possibly promised).
|
|
66
67
|
*/
|
|
67
68
|
get count() {
|
|
68
|
-
return
|
|
69
|
+
return callAsync(countItems, this.results);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Get an entry for the first document matching this query.
|
|
73
|
+
* @return Entry in `[id, data]` format for the first document, or `undefined` if there are no matching documents (possibly promised).
|
|
74
|
+
*/
|
|
75
|
+
get first() {
|
|
76
|
+
return callAsync(getFirstItem, this.max(1).results);
|
|
69
77
|
}
|
|
70
78
|
/**
|
|
71
79
|
* Subscribe to all matching documents.
|
|
@@ -79,14 +87,21 @@ export class DatabaseQuery extends Query {
|
|
|
79
87
|
* @return Function that ends the subscription.
|
|
80
88
|
*/
|
|
81
89
|
subscribe(next, error, complete) {
|
|
82
|
-
return this.
|
|
90
|
+
return this.provider.subscribeQuery(this, createObserver(next, error, complete));
|
|
83
91
|
}
|
|
84
92
|
/**
|
|
85
|
-
*
|
|
86
|
-
*
|
|
93
|
+
* Subscribe to all matching documents.
|
|
94
|
+
* - `next()` is called once with the initial results, and again any time the results change.
|
|
95
|
+
*
|
|
96
|
+
* @param observer Observer with `next`, `error`, or `complete` methods that the document results are reported back to.
|
|
97
|
+
* @param next Callback that is called once initially and again whenever the results change.
|
|
98
|
+
* @param error Callback that is called if an error occurs.
|
|
99
|
+
* @param complete Callback that is called when the subscription is done.
|
|
100
|
+
*
|
|
101
|
+
* @return Function that ends the subscription.
|
|
87
102
|
*/
|
|
88
|
-
|
|
89
|
-
return
|
|
103
|
+
subscribeMap(next, error, complete) {
|
|
104
|
+
return this.provider.subscribeQuery(this, new DeriveObserver(toMap, createObserver(next, error, complete)));
|
|
90
105
|
}
|
|
91
106
|
/**
|
|
92
107
|
* Set all matching documents to the same exact value.
|
|
@@ -120,7 +135,7 @@ export class DatabaseQuery extends Query {
|
|
|
120
135
|
* @return Nothing (possibly promised).
|
|
121
136
|
*/
|
|
122
137
|
write(value) {
|
|
123
|
-
return this.
|
|
138
|
+
return this.provider.writeQuery(this, value);
|
|
124
139
|
}
|
|
125
140
|
/** Iterate over the resuls (will throw `Promise` if the results are asynchronous). */
|
|
126
141
|
[Symbol.iterator]() {
|
|
@@ -150,20 +165,20 @@ export class DatabaseQuery extends Query {
|
|
|
150
165
|
}
|
|
151
166
|
}
|
|
152
167
|
/** A document reference within a specific database. */
|
|
153
|
-
export class
|
|
154
|
-
constructor(
|
|
155
|
-
this.
|
|
156
|
-
this.validator =
|
|
168
|
+
export class DataDocument {
|
|
169
|
+
constructor(provider, validator, collection, id) {
|
|
170
|
+
this.provider = provider;
|
|
171
|
+
this.validator = validator;
|
|
157
172
|
this.collection = collection;
|
|
158
173
|
this.id = id;
|
|
159
174
|
}
|
|
160
175
|
/** Create a query on this document's collection. */
|
|
161
176
|
query(filters, sorts, limit) {
|
|
162
|
-
return new
|
|
177
|
+
return new DataQuery(this.provider, this.validator, this.collection, filters, sorts, limit);
|
|
163
178
|
}
|
|
164
179
|
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
|
165
180
|
get optional() {
|
|
166
|
-
return new
|
|
181
|
+
return new DataQuery(this.provider, this.validator, this.collection, new Filters(new EqualFilter("id", this.id)));
|
|
167
182
|
}
|
|
168
183
|
/**
|
|
169
184
|
* Does this document exist?
|
|
@@ -171,8 +186,7 @@ export class DatabaseDocument {
|
|
|
171
186
|
* @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
|
|
172
187
|
*/
|
|
173
188
|
get exists() {
|
|
174
|
-
|
|
175
|
-
return isAsync(result) ? result.then(Boolean) : !!result;
|
|
189
|
+
return callAsync(Boolean, this.provider.get(this));
|
|
176
190
|
}
|
|
177
191
|
/**
|
|
178
192
|
* Get the result of this document.
|
|
@@ -180,7 +194,7 @@ export class DatabaseDocument {
|
|
|
180
194
|
* @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
|
|
181
195
|
*/
|
|
182
196
|
get result() {
|
|
183
|
-
return this.
|
|
197
|
+
return this.provider.get(this);
|
|
184
198
|
}
|
|
185
199
|
/**
|
|
186
200
|
* Get the data of this document.
|
|
@@ -190,12 +204,7 @@ export class DatabaseDocument {
|
|
|
190
204
|
* @throws RequiredError if the document's result was undefined.
|
|
191
205
|
*/
|
|
192
206
|
get data() {
|
|
193
|
-
|
|
194
|
-
if (isAsync(result))
|
|
195
|
-
return _awaitRequired(this, result);
|
|
196
|
-
if (!result)
|
|
197
|
-
throw new DocumentRequiredError(this);
|
|
198
|
-
return result;
|
|
207
|
+
return callAsync(getDocumentData, this.provider.get(this), this);
|
|
199
208
|
}
|
|
200
209
|
/**
|
|
201
210
|
* Subscribe to the result of this document (indefinitely).
|
|
@@ -209,7 +218,7 @@ export class DatabaseDocument {
|
|
|
209
218
|
* @return Function that ends the subscription.
|
|
210
219
|
*/
|
|
211
220
|
subscribe(next, error, complete) {
|
|
212
|
-
return this.
|
|
221
|
+
return this.provider.subscribe(this, createObserver(next, error, complete));
|
|
213
222
|
}
|
|
214
223
|
/**
|
|
215
224
|
* Set the complete data of this document.
|
|
@@ -248,7 +257,7 @@ export class DatabaseDocument {
|
|
|
248
257
|
* Combine `set()`, `update()`, `delete()` into a single method.
|
|
249
258
|
*/
|
|
250
259
|
write(value) {
|
|
251
|
-
return this.
|
|
260
|
+
return this.provider.write(this, value);
|
|
252
261
|
}
|
|
253
262
|
/** Validate data for this query reference. */
|
|
254
263
|
validate(unsafeData) {
|
|
@@ -264,10 +273,9 @@ export class DatabaseDocument {
|
|
|
264
273
|
return `${this.collection}/${this.id}`;
|
|
265
274
|
}
|
|
266
275
|
}
|
|
267
|
-
/**
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
return result;
|
|
276
|
+
/** Get the data for a document from a result for that document. */
|
|
277
|
+
export function getDocumentData(result, ref) {
|
|
278
|
+
if (result)
|
|
279
|
+
return result;
|
|
280
|
+
throw new DocumentRequiredError(ref);
|
|
273
281
|
}
|
package/db/Pagination.d.ts
CHANGED
|
@@ -1,26 +1,34 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { ResultsMap, Entry, Results, Data } from "../util/index.js";
|
|
2
|
+
import { State } from "../stream/index.js";
|
|
3
|
+
import { DataQuery } from "./Database.js";
|
|
4
4
|
/**
|
|
5
5
|
* State that wraps a `Documents` reference to enable pagination.
|
|
6
6
|
* - If you pass in initial values, it will use that as the first page.
|
|
7
7
|
* - If you don't pass in initial values, it will autoload the first page.
|
|
8
8
|
*/
|
|
9
|
-
export declare class Pagination<
|
|
10
|
-
|
|
11
|
-
readonly
|
|
12
|
-
|
|
9
|
+
export declare class Pagination<T extends Data> extends State<ResultsMap<T>> implements Iterable<Entry<T>> {
|
|
10
|
+
readonly ref: DataQuery<T>;
|
|
11
|
+
readonly limit: number;
|
|
12
|
+
readonly startLoading: boolean;
|
|
13
|
+
readonly startDone: boolean;
|
|
14
|
+
readonly endLoading: boolean;
|
|
15
|
+
readonly endDone: boolean;
|
|
16
|
+
constructor(ref: DataQuery<T>);
|
|
13
17
|
/**
|
|
14
18
|
* Load more results before the start.
|
|
15
19
|
* - Promise that needs to be handled.
|
|
16
20
|
*/
|
|
17
|
-
|
|
21
|
+
loadStart: () => Promise<void>;
|
|
18
22
|
/**
|
|
19
23
|
* Load more results after the end.
|
|
20
24
|
* - Promise that needs to be handled.
|
|
21
25
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
26
|
+
loadEnd: () => Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Merge more results into this pagination.
|
|
29
|
+
* @return The change in the number of results.
|
|
30
|
+
*/
|
|
31
|
+
merge(more: Results<T>): void;
|
|
24
32
|
/** Iterate over the entries of the values currently in the pagination. */
|
|
25
|
-
[Symbol.iterator](): Iterator<Entry<
|
|
33
|
+
[Symbol.iterator](): Iterator<Entry<T>>;
|
|
26
34
|
}
|