shelving 1.38.0 → 1.42.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/api/Resource.js +2 -2
- package/db/Database.d.ts +22 -32
- package/db/Database.js +29 -51
- package/db/Operation.d.ts +73 -0
- package/db/Operation.js +116 -0
- package/db/Pagination.d.ts +6 -6
- package/db/Pagination.js +5 -5
- package/db/errors.d.ts +9 -9
- package/db/index.d.ts +1 -1
- package/db/index.js +1 -1
- package/firestore/client/FirestoreClientProvider.d.ts +12 -12
- package/firestore/lite/FirestoreLiteProvider.d.ts +10 -10
- package/firestore/server/FirestoreServerProvider.d.ts +12 -12
- package/package.json +1 -1
- package/provider/BatchProvider.d.ts +6 -6
- package/provider/BatchProvider.js +3 -3
- package/provider/CacheProvider.d.ts +17 -17
- package/provider/CacheProvider.js +6 -6
- package/provider/MemoryProvider.d.ts +13 -13
- package/provider/Provider.d.ts +31 -31
- package/provider/ThroughProvider.d.ts +13 -13
- package/provider/ValidationProvider.d.ts +11 -11
- package/query/Filter.d.ts +8 -9
- package/query/Filter.js +17 -18
- package/query/Filters.d.ts +2 -2
- package/query/Filters.js +6 -6
- package/query/Query.d.ts +2 -2
- package/query/Query.js +3 -3
- package/query/Rule.d.ts +3 -3
- package/query/Sort.d.ts +2 -2
- package/query/Sort.js +3 -3
- package/query/Sorts.d.ts +2 -2
- package/react/useDocument.d.ts +9 -9
- package/react/usePagination.d.ts +2 -2
- package/react/useQuery.d.ts +13 -13
- package/react/useQuery.js +7 -7
- package/schema/ArraySchema.js +2 -2
- package/stream/LazyState.d.ts +1 -1
- package/stream/LazyState.js +2 -2
- package/stream/LazyStream.d.ts +1 -1
- package/stream/LazyStream.js +2 -2
- package/stream/State.d.ts +1 -1
- package/stream/State.js +2 -2
- package/stream/Stream.d.ts +2 -2
- package/stream/Stream.js +4 -4
- package/test/util.js +3 -3
- package/update/ArrayUpdate.d.ts +4 -4
- package/update/ArrayUpdate.js +4 -4
- package/update/DataUpdate.d.ts +3 -3
- package/update/DataUpdate.js +7 -7
- package/update/ObjectUpdate.d.ts +5 -5
- package/update/ObjectUpdate.js +9 -9
- package/update/Update.d.ts +1 -1
- package/update/Update.js +1 -1
- package/update/util.d.ts +2 -4
- package/update/util.js +3 -3
- package/util/array.d.ts +14 -12
- package/util/array.js +3 -3
- package/util/assert.d.ts +10 -10
- package/util/assert.js +10 -10
- package/util/async.d.ts +28 -4
- package/util/async.js +36 -3
- package/util/data.d.ts +10 -12
- package/util/data.js +6 -6
- package/util/date.d.ts +3 -3
- package/util/date.js +3 -3
- package/util/entry.d.ts +23 -11
- package/util/entry.js +9 -6
- package/util/filter.d.ts +22 -22
- package/util/filter.js +24 -24
- package/util/map.d.ts +4 -2
- package/util/map.js +1 -1
- package/util/null.d.ts +7 -7
- package/util/null.js +6 -6
- package/util/number.d.ts +1 -1
- package/util/number.js +1 -1
- package/util/observable.d.ts +6 -0
- package/util/observable.js +10 -0
- package/util/sort.d.ts +6 -6
- package/util/sort.js +12 -12
- package/util/string.d.ts +1 -1
- package/util/string.js +1 -1
- package/util/transform.js +2 -2
- package/util/undefined.d.ts +3 -4
- package/util/undefined.js +3 -4
- package/db/Write.d.ts +0 -46
- package/db/Write.js +0 -62
package/api/Resource.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UNDEFINED, validate } from "../util/index.js";
|
|
2
2
|
import { Feedback } from "../feedback/index.js";
|
|
3
3
|
import { ResourceValidationError } from "./errors.js";
|
|
4
4
|
/**
|
|
@@ -37,6 +37,6 @@ export class Resource {
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
|
-
export function RESOURCE(payload =
|
|
40
|
+
export function RESOURCE(payload = UNDEFINED, result = UNDEFINED) {
|
|
41
41
|
return new Resource(payload, result);
|
|
42
42
|
}
|
package/db/Database.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { Entry, Observable, Observer, Result, Unsubscriber,
|
|
1
|
+
import { Entry, Observable, Observer, Result, Unsubscriber, Results, Validatable, Validator, Key, Data, Entries, Datas, Validators, ValidatorType, Dispatcher } from "../util/index.js";
|
|
2
2
|
import { PropUpdates, Update } from "../update/index.js";
|
|
3
3
|
import type { Provider } from "../provider/Provider.js";
|
|
4
4
|
import { Filters, Sorts, Query } from "../query/index.js";
|
|
5
|
-
import { DocumentDelete, DocumentSet, DocumentUpdate, Write } from "./Write.js";
|
|
6
5
|
/**
|
|
7
6
|
* Combines a database model and a provider.
|
|
8
7
|
*
|
|
@@ -15,22 +14,27 @@ export declare class Database<V extends Validators<Datas> = Validators<Datas>> {
|
|
|
15
14
|
readonly provider: Provider;
|
|
16
15
|
constructor(validators: V, provider: Provider);
|
|
17
16
|
/** Create a query on a collection in this model. */
|
|
18
|
-
query<K extends Key<V>>(collection: K, filters?: Filters<ValidatorType<V[K]>>, sorts?: Sorts<ValidatorType<V[K]>>, limit?: number | null):
|
|
17
|
+
query<K extends Key<V>>(collection: K, filters?: Filters<ValidatorType<V[K]>>, sorts?: Sorts<ValidatorType<V[K]>>, limit?: number | null): DatabaseQuery<ValidatorType<V[K]>>;
|
|
19
18
|
/** Reference a document in a collection in this model. */
|
|
20
|
-
doc<K extends Key<V>>(collection: K, id: string):
|
|
21
|
-
/**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
doc<K extends Key<V>>(collection: K, id: string): DatabaseDocument<ValidatorType<V[K]>>;
|
|
20
|
+
/**
|
|
21
|
+
* Create a new document with a random ID.
|
|
22
|
+
* - Created document is guaranteed to have a unique ID.
|
|
23
|
+
*
|
|
24
|
+
* @param collection Name of the collection to add the document to.
|
|
25
|
+
* @param data Complete data to set the document to.
|
|
26
|
+
* @return String ID for the created document (possibly promised).
|
|
27
|
+
*/
|
|
28
|
+
add<K extends Key<V>>(collection: K, data: ValidatorType<V[K]>): string | PromiseLike<string>;
|
|
25
29
|
}
|
|
26
30
|
/** A documents reference within a specific database. */
|
|
27
|
-
export declare class
|
|
31
|
+
export declare class DatabaseQuery<T extends Data = Data> extends Query<T> implements Observable<Results<T>>, Validatable<Entries<T>>, Iterable<Entry<T>> {
|
|
28
32
|
readonly provider: Provider;
|
|
29
33
|
readonly validator: Validator<T>;
|
|
30
34
|
readonly collection: string;
|
|
31
35
|
constructor(provider: Provider, validator: Validator<T>, collection: string, filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null);
|
|
32
36
|
/** Reference a document in this query's collection. */
|
|
33
|
-
doc(id: string):
|
|
37
|
+
doc(id: string): DatabaseDocument<T>;
|
|
34
38
|
/**
|
|
35
39
|
* Create a new document with a random ID.
|
|
36
40
|
* - Created document is guaranteed to have a unique ID.
|
|
@@ -43,12 +47,12 @@ export declare class DataQuery<T extends Data = Data> extends Query<T> implement
|
|
|
43
47
|
* Get an iterable that yields the results of this entry.
|
|
44
48
|
* @return Map containing the results.
|
|
45
49
|
*/
|
|
46
|
-
get
|
|
50
|
+
get entries(): Entries<T> | PromiseLike<Entries<T>>;
|
|
47
51
|
/**
|
|
48
52
|
* Get an iterable that yields the results of this entry.
|
|
49
53
|
* @return Map containing the results.
|
|
50
54
|
*/
|
|
51
|
-
get
|
|
55
|
+
get results(): Results<T> | PromiseLike<Results<T>>;
|
|
52
56
|
/**
|
|
53
57
|
* Count the number of results of this set of documents.
|
|
54
58
|
* @return Number of documents matching the query (possibly promised).
|
|
@@ -81,14 +85,6 @@ export declare class DataQuery<T extends Data = Data> extends Query<T> implement
|
|
|
81
85
|
* @return Function that ends the subscription.
|
|
82
86
|
*/
|
|
83
87
|
subscribe(next: Observer<Results<T>> | Dispatcher<[Results<T>]>): Unsubscriber;
|
|
84
|
-
/**
|
|
85
|
-
* Subscribe to all matching documents.
|
|
86
|
-
* - `next()` is called once with the initial results, and again any time the results change.
|
|
87
|
-
*
|
|
88
|
-
* @param next Observer with `next`, `error`, or `complete` methods or a `next()` dispatcher.
|
|
89
|
-
* @return Function that ends the subscription.
|
|
90
|
-
*/
|
|
91
|
-
subscribeMap(next: Observer<ResultsMap<T>> | Dispatcher<[ResultsMap<T>]>): Unsubscriber;
|
|
92
88
|
/**
|
|
93
89
|
* Set all matching documents to the same exact value.
|
|
94
90
|
*
|
|
@@ -111,22 +107,22 @@ export declare class DataQuery<T extends Data = Data> extends Query<T> implement
|
|
|
111
107
|
/** Iterate over the resuls (will throw `Promise` if the results are asynchronous). */
|
|
112
108
|
[Symbol.iterator](): Iterator<Entry<T>, void>;
|
|
113
109
|
/** Validate a set of results for this query reference. */
|
|
114
|
-
validate(unsafeEntries:
|
|
110
|
+
validate(unsafeEntries: Entries): Entries<T>;
|
|
115
111
|
toString(): string;
|
|
116
112
|
}
|
|
117
113
|
/** Get the data for a document from a result for that document. */
|
|
118
|
-
export declare function getQueryData<T extends Data>(
|
|
114
|
+
export declare function getQueryData<T extends Data>(entries: Entries<T>, ref: DatabaseQuery<T>): Entry<T>;
|
|
119
115
|
/** A document reference within a specific database. */
|
|
120
|
-
export declare class
|
|
116
|
+
export declare class DatabaseDocument<T extends Data = Data> implements Observable<Result<T>>, Validatable<T> {
|
|
121
117
|
readonly provider: Provider;
|
|
122
118
|
readonly validator: Validator<T>;
|
|
123
119
|
readonly collection: string;
|
|
124
120
|
readonly id: string;
|
|
125
121
|
constructor(provider: Provider, validator: Validator<T>, collection: string, id: string);
|
|
126
122
|
/** Create a query on this document's collection. */
|
|
127
|
-
query(filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null):
|
|
123
|
+
query(filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null): DatabaseQuery<T>;
|
|
128
124
|
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
|
129
|
-
get optional():
|
|
125
|
+
get optional(): DatabaseQuery<T>;
|
|
130
126
|
/**
|
|
131
127
|
* Does this document exist?
|
|
132
128
|
* @return `true` if a document exists or `false` otherwise (possibly promised).
|
|
@@ -159,15 +155,9 @@ export declare class DataDocument<T extends Data = Data> implements Observable<R
|
|
|
159
155
|
update(updates: Update<T> | PropUpdates<T>): void | PromiseLike<void>;
|
|
160
156
|
/** Delete this document. */
|
|
161
157
|
delete(): void | PromiseLike<void>;
|
|
162
|
-
/** Represent a write that sets the complete data of this document in a database. */
|
|
163
|
-
setter(data: T): DocumentSet<T>;
|
|
164
|
-
/** Represent a write that updates this document in a database. */
|
|
165
|
-
updater(updates: Update<T> | PropUpdates<T>): DocumentUpdate<T>;
|
|
166
|
-
/** Represent a write that deletes this document in a database. */
|
|
167
|
-
deleter(): DocumentDelete<T>;
|
|
168
158
|
/** Validate data for this query reference. */
|
|
169
159
|
validate(unsafeData: Data): T;
|
|
170
160
|
toString(): string;
|
|
171
161
|
}
|
|
172
162
|
/** Get the data for a document from a result for that document. */
|
|
173
|
-
export declare function getDocumentData<T extends Data>(result: Result<T>, ref:
|
|
163
|
+
export declare function getDocumentData<T extends Data>(result: Result<T>, ref: DatabaseDocument<T>): T;
|
package/db/Database.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { callAsync, getFirstItem, throwAsync, validate,
|
|
1
|
+
import { callAsync, getFirstItem, throwAsync, validate, getMap, countItems, hasItems, ResultsObserver, } from "../util/index.js";
|
|
2
2
|
import { DataUpdate, Update } from "../update/index.js";
|
|
3
3
|
import { Feedback, InvalidFeedback } from "../feedback/index.js";
|
|
4
4
|
import { Filters, Query, EqualFilter } from "../query/index.js";
|
|
5
5
|
import { DocumentRequiredError, DocumentValidationError, QueryRequiredError, QueryValidationError } from "./errors.js";
|
|
6
|
-
import { DocumentDelete, DocumentSet, DocumentUpdate, Writes } from "./Write.js";
|
|
7
6
|
/**
|
|
8
7
|
* Combines a database model and a provider.
|
|
9
8
|
*
|
|
@@ -19,25 +18,26 @@ export class Database {
|
|
|
19
18
|
}
|
|
20
19
|
/** Create a query on a collection in this model. */
|
|
21
20
|
query(collection, filters, sorts, limit) {
|
|
22
|
-
return new
|
|
21
|
+
return new DatabaseQuery(this.provider, this.validators[collection], collection, filters, sorts, limit);
|
|
23
22
|
}
|
|
24
23
|
/** Reference a document in a collection in this model. */
|
|
25
24
|
doc(collection, id) {
|
|
26
|
-
return new
|
|
25
|
+
return new DatabaseDocument(this.provider, this.validators[collection], collection, id);
|
|
27
26
|
}
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Create a new document with a random ID.
|
|
29
|
+
* - Created document is guaranteed to have a unique ID.
|
|
30
|
+
*
|
|
31
|
+
* @param collection Name of the collection to add the document to.
|
|
32
|
+
* @param data Complete data to set the document to.
|
|
33
|
+
* @return String ID for the created document (possibly promised).
|
|
34
|
+
*/
|
|
35
|
+
add(collection, data) {
|
|
36
|
+
return this.query(collection).add(data);
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
/** A documents reference within a specific database. */
|
|
40
|
-
export class
|
|
40
|
+
export class DatabaseQuery extends Query {
|
|
41
41
|
constructor(provider, validator, collection, filters, sorts, limit) {
|
|
42
42
|
super(filters, sorts, limit);
|
|
43
43
|
this.provider = provider;
|
|
@@ -46,7 +46,7 @@ export class DataQuery extends Query {
|
|
|
46
46
|
}
|
|
47
47
|
/** Reference a document in this query's collection. */
|
|
48
48
|
doc(id) {
|
|
49
|
-
return new
|
|
49
|
+
return new DatabaseDocument(this.provider, this.validator, this.collection, id);
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* Create a new document with a random ID.
|
|
@@ -62,29 +62,29 @@ export class DataQuery extends Query {
|
|
|
62
62
|
* Get an iterable that yields the results of this entry.
|
|
63
63
|
* @return Map containing the results.
|
|
64
64
|
*/
|
|
65
|
-
get
|
|
65
|
+
get entries() {
|
|
66
66
|
return this.provider.getQuery(this);
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
69
|
* Get an iterable that yields the results of this entry.
|
|
70
70
|
* @return Map containing the results.
|
|
71
71
|
*/
|
|
72
|
-
get
|
|
73
|
-
return callAsync(
|
|
72
|
+
get results() {
|
|
73
|
+
return callAsync(getMap, this.provider.getQuery(this));
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
76
|
* Count the number of results of this set of documents.
|
|
77
77
|
* @return Number of documents matching the query (possibly promised).
|
|
78
78
|
*/
|
|
79
79
|
get count() {
|
|
80
|
-
return callAsync(countItems, this.
|
|
80
|
+
return callAsync(countItems, this.entries);
|
|
81
81
|
}
|
|
82
82
|
/**
|
|
83
83
|
* Does at least one document exist for this query?
|
|
84
84
|
* @return `true` if a document exists or `false` otherwise (possibly promised).
|
|
85
85
|
*/
|
|
86
86
|
get exists() {
|
|
87
|
-
return callAsync(hasItems, this.max(1)
|
|
87
|
+
return callAsync(hasItems, this.provider.getQuery(this.max(1)));
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
90
90
|
* Get an entry for the first document matched by this query or `undefined` if this query has no results.
|
|
@@ -93,7 +93,7 @@ export class DataQuery extends Query {
|
|
|
93
93
|
* @throws RequiredError if there were no results for this query.
|
|
94
94
|
*/
|
|
95
95
|
get result() {
|
|
96
|
-
return callAsync(getFirstItem, this.max(1)
|
|
96
|
+
return callAsync(getFirstItem, this.provider.getQuery(this.max(1)));
|
|
97
97
|
}
|
|
98
98
|
/**
|
|
99
99
|
* Get an entry for the first document matched by this query.
|
|
@@ -102,7 +102,7 @@ export class DataQuery extends Query {
|
|
|
102
102
|
* @throws RequiredError if there were no results for this query.
|
|
103
103
|
*/
|
|
104
104
|
get data() {
|
|
105
|
-
return callAsync(getQueryData, this.max(1)
|
|
105
|
+
return callAsync(getQueryData, this.provider.getQuery(this.max(1)), this);
|
|
106
106
|
}
|
|
107
107
|
/**
|
|
108
108
|
* Subscribe to all matching documents.
|
|
@@ -112,17 +112,7 @@ export class DataQuery extends Query {
|
|
|
112
112
|
* @return Function that ends the subscription.
|
|
113
113
|
*/
|
|
114
114
|
subscribe(next) {
|
|
115
|
-
return this.provider.subscribeQuery(this, typeof next === "function" ? { next } : next);
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Subscribe to all matching documents.
|
|
119
|
-
* - `next()` is called once with the initial results, and again any time the results change.
|
|
120
|
-
*
|
|
121
|
-
* @param next Observer with `next`, `error`, or `complete` methods or a `next()` dispatcher.
|
|
122
|
-
* @return Function that ends the subscription.
|
|
123
|
-
*/
|
|
124
|
-
subscribeMap(next) {
|
|
125
|
-
return this.provider.subscribeQuery(this, new TransformObserver(toMap, typeof next === "function" ? { next } : next));
|
|
115
|
+
return this.provider.subscribeQuery(this, new ResultsObserver(typeof next === "function" ? { next } : next));
|
|
126
116
|
}
|
|
127
117
|
/**
|
|
128
118
|
* Set all matching documents to the same exact value.
|
|
@@ -151,7 +141,7 @@ export class DataQuery extends Query {
|
|
|
151
141
|
}
|
|
152
142
|
/** Iterate over the resuls (will throw `Promise` if the results are asynchronous). */
|
|
153
143
|
[Symbol.iterator]() {
|
|
154
|
-
return throwAsync(this.
|
|
144
|
+
return throwAsync(this.entries)[Symbol.iterator]();
|
|
155
145
|
}
|
|
156
146
|
/** Validate a set of results for this query reference. */
|
|
157
147
|
*validate(unsafeEntries) {
|
|
@@ -177,14 +167,14 @@ export class DataQuery extends Query {
|
|
|
177
167
|
}
|
|
178
168
|
}
|
|
179
169
|
/** Get the data for a document from a result for that document. */
|
|
180
|
-
export function getQueryData(
|
|
181
|
-
const first = getFirstItem(
|
|
170
|
+
export function getQueryData(entries, ref) {
|
|
171
|
+
const first = getFirstItem(entries);
|
|
182
172
|
if (first)
|
|
183
173
|
return first;
|
|
184
174
|
throw new QueryRequiredError(ref);
|
|
185
175
|
}
|
|
186
176
|
/** A document reference within a specific database. */
|
|
187
|
-
export class
|
|
177
|
+
export class DatabaseDocument {
|
|
188
178
|
constructor(provider, validator, collection, id) {
|
|
189
179
|
this.provider = provider;
|
|
190
180
|
this.validator = validator;
|
|
@@ -193,11 +183,11 @@ export class DataDocument {
|
|
|
193
183
|
}
|
|
194
184
|
/** Create a query on this document's collection. */
|
|
195
185
|
query(filters, sorts, limit) {
|
|
196
|
-
return new
|
|
186
|
+
return new DatabaseQuery(this.provider, this.validator, this.collection, filters, sorts, limit);
|
|
197
187
|
}
|
|
198
188
|
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
|
199
189
|
get optional() {
|
|
200
|
-
return new
|
|
190
|
+
return new DatabaseQuery(this.provider, this.validator, this.collection, new Filters(new EqualFilter("id", this.id)));
|
|
201
191
|
}
|
|
202
192
|
/**
|
|
203
193
|
* Does this document exist?
|
|
@@ -245,18 +235,6 @@ export class DataDocument {
|
|
|
245
235
|
delete() {
|
|
246
236
|
return this.provider.delete(this);
|
|
247
237
|
}
|
|
248
|
-
/** Represent a write that sets the complete data of this document in a database. */
|
|
249
|
-
setter(data) {
|
|
250
|
-
return new DocumentSet(this, data);
|
|
251
|
-
}
|
|
252
|
-
/** Represent a write that updates this document in a database. */
|
|
253
|
-
updater(updates) {
|
|
254
|
-
return new DocumentUpdate(this, updates);
|
|
255
|
-
}
|
|
256
|
-
/** Represent a write that deletes this document in a database. */
|
|
257
|
-
deleter() {
|
|
258
|
-
return new DocumentDelete(this);
|
|
259
|
-
}
|
|
260
238
|
/** Validate data for this query reference. */
|
|
261
239
|
validate(unsafeData) {
|
|
262
240
|
try {
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { PropUpdates, Update } from "../update/index.js";
|
|
2
|
+
import { ImmutableArray, Nullish, Data, Key } from "../util/index.js";
|
|
3
|
+
import type { Database, DatabaseDocument, DatabaseQuery } from "./Database.js";
|
|
4
|
+
/** Represent a write operation on a database. */
|
|
5
|
+
export declare abstract class Operation {
|
|
6
|
+
abstract run(db: Database): Promise<Operation>;
|
|
7
|
+
}
|
|
8
|
+
/** Represent a list of write operations on a database run in series. */
|
|
9
|
+
export declare class Operations extends Operation {
|
|
10
|
+
/** Return a new write operations list with a set of write operations. */
|
|
11
|
+
static with(...operations: Nullish<Operation>[]): Operations;
|
|
12
|
+
readonly operations: ImmutableArray<Operation>;
|
|
13
|
+
constructor(operations: ImmutableArray<Nullish<Operation>>);
|
|
14
|
+
run(db: Database): Promise<Operations>;
|
|
15
|
+
/** Return a new write operations list with an additional write operation added. */
|
|
16
|
+
with(...operations: Nullish<Operation>[]): {
|
|
17
|
+
__proto__: any;
|
|
18
|
+
} & this & {
|
|
19
|
+
operations: (Operation | Nullish<Operation>[])[];
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Represent a add operation made to a collection in a database. */
|
|
23
|
+
export declare class AddOperation<T extends Data> extends Operation {
|
|
24
|
+
/** Create a new add operation on a collection. */
|
|
25
|
+
static on<X extends Data>({ collection }: DatabaseDocument | DatabaseQuery, data: X): AddOperation<X>;
|
|
26
|
+
readonly collection: string;
|
|
27
|
+
readonly data: T;
|
|
28
|
+
constructor(collection: string, data: T);
|
|
29
|
+
run(db: Database): Promise<SetOperation<T>>;
|
|
30
|
+
/** Set one of the props on this set operation to a different value. */
|
|
31
|
+
with<K extends Key<T>>(key: Nullish<K>, value: T[K]): this;
|
|
32
|
+
}
|
|
33
|
+
/** Represent a set operation made to a single document in a database. */
|
|
34
|
+
export declare class SetOperation<T extends Data> extends Operation {
|
|
35
|
+
/** Create a new add operation on a collection. */
|
|
36
|
+
static on<X extends Data>({ collection, id }: DatabaseDocument, data: X): SetOperation<X>;
|
|
37
|
+
readonly collection: string;
|
|
38
|
+
readonly id: string;
|
|
39
|
+
readonly data: T;
|
|
40
|
+
constructor(collection: string, id: string, data: T);
|
|
41
|
+
run(db: Database): Promise<this>;
|
|
42
|
+
/** Set one of the props on this set operation to a different value. */
|
|
43
|
+
with<K extends Key<T>>(key: Nullish<K>, value: T[K]): this;
|
|
44
|
+
}
|
|
45
|
+
/** Represent an update operation made to a single document in a database. */
|
|
46
|
+
export declare class UpdateOperation<T extends Data> extends Operation {
|
|
47
|
+
/** Create a new update operation on a document. */
|
|
48
|
+
static on<X extends Data>({ collection, id }: DatabaseDocument<X>, updates?: PropUpdates<X>): UpdateOperation<X>;
|
|
49
|
+
readonly collection: string;
|
|
50
|
+
readonly id: string;
|
|
51
|
+
readonly updates: PropUpdates<T>;
|
|
52
|
+
constructor(collection: string, id: string, updates: PropUpdates<T>);
|
|
53
|
+
run(db: Database): Promise<this>;
|
|
54
|
+
/** update one of the props on this set operation to a different value. */
|
|
55
|
+
with<K extends Key<T>>(key: Nullish<K>, value: T[K] | Update<T[K]>): this;
|
|
56
|
+
}
|
|
57
|
+
/** Represent a delete operation made to a single document in a database. */
|
|
58
|
+
export declare class DeleteOperation extends Operation {
|
|
59
|
+
/** Create a new update operation on a document. */
|
|
60
|
+
static on({ collection, id }: DatabaseDocument): DeleteOperation;
|
|
61
|
+
readonly collection: string;
|
|
62
|
+
readonly id: string;
|
|
63
|
+
constructor(collection: string, id: string);
|
|
64
|
+
run(db: Database): Promise<this>;
|
|
65
|
+
}
|
|
66
|
+
/** Set of hydrations for all change classes. */
|
|
67
|
+
export declare const OPERATION_HYDRATIONS: {
|
|
68
|
+
Operations: typeof Operations;
|
|
69
|
+
AddOperation: typeof AddOperation;
|
|
70
|
+
SetOperation: typeof SetOperation;
|
|
71
|
+
UpdateOperation: typeof UpdateOperation;
|
|
72
|
+
DeleteOperation: typeof DeleteOperation;
|
|
73
|
+
};
|
package/db/Operation.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { callAsyncSeries, isNotNullish, isNullish } from "../util/index.js";
|
|
2
|
+
/** Represent a write operation on a database. */
|
|
3
|
+
export class Operation {
|
|
4
|
+
}
|
|
5
|
+
/** Represent a list of write operations on a database run in series. */
|
|
6
|
+
export class Operations extends Operation {
|
|
7
|
+
constructor(operations) {
|
|
8
|
+
super();
|
|
9
|
+
this.operations = operations.filter(isNotNullish);
|
|
10
|
+
}
|
|
11
|
+
/** Return a new write operations list with a set of write operations. */
|
|
12
|
+
static with(...operations) {
|
|
13
|
+
return new Operations(operations);
|
|
14
|
+
}
|
|
15
|
+
async run(db) {
|
|
16
|
+
return new Operations(await callAsyncSeries(_write, this.operations, db));
|
|
17
|
+
}
|
|
18
|
+
/** Return a new write operations list with an additional write operation added. */
|
|
19
|
+
with(...operations) {
|
|
20
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, operations: [...this.operations, operations] };
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const _write = (operation, db) => operation.run(db);
|
|
24
|
+
/** Represent a add operation made to a collection in a database. */
|
|
25
|
+
export class AddOperation extends Operation {
|
|
26
|
+
constructor(collection, data) {
|
|
27
|
+
super();
|
|
28
|
+
this.collection = collection;
|
|
29
|
+
this.data = data;
|
|
30
|
+
}
|
|
31
|
+
/** Create a new add operation on a collection. */
|
|
32
|
+
static on({ collection }, data) {
|
|
33
|
+
return new AddOperation(collection, data);
|
|
34
|
+
}
|
|
35
|
+
async run(db) {
|
|
36
|
+
const id = await db.query(this.collection).add(this.data);
|
|
37
|
+
return new SetOperation(this.collection, id, this.data); // When an add operation is run it returns a set operation so the operation is repeatable.
|
|
38
|
+
}
|
|
39
|
+
/** Set one of the props on this set operation to a different value. */
|
|
40
|
+
with(key, value) {
|
|
41
|
+
if (isNullish(key))
|
|
42
|
+
return this;
|
|
43
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, data: { ...this.data, [key]: value } };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** Represent a set operation made to a single document in a database. */
|
|
47
|
+
export class SetOperation extends Operation {
|
|
48
|
+
constructor(collection, id, data) {
|
|
49
|
+
super();
|
|
50
|
+
this.collection = collection;
|
|
51
|
+
this.id = id;
|
|
52
|
+
this.data = data;
|
|
53
|
+
}
|
|
54
|
+
/** Create a new add operation on a collection. */
|
|
55
|
+
static on({ collection, id }, data) {
|
|
56
|
+
return new SetOperation(collection, id, data);
|
|
57
|
+
}
|
|
58
|
+
async run(db) {
|
|
59
|
+
await db.doc(this.collection, this.id).set(this.data);
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
/** Set one of the props on this set operation to a different value. */
|
|
63
|
+
with(key, value) {
|
|
64
|
+
if (isNullish(key))
|
|
65
|
+
return this;
|
|
66
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, data: { ...this.data, [key]: value } };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Represent an update operation made to a single document in a database. */
|
|
70
|
+
export class UpdateOperation extends Operation {
|
|
71
|
+
constructor(collection, id, updates) {
|
|
72
|
+
super();
|
|
73
|
+
this.collection = collection;
|
|
74
|
+
this.id = id;
|
|
75
|
+
this.updates = updates;
|
|
76
|
+
}
|
|
77
|
+
/** Create a new update operation on a document. */
|
|
78
|
+
static on({ collection, id }, updates = {}) {
|
|
79
|
+
return new UpdateOperation(collection, id, updates);
|
|
80
|
+
}
|
|
81
|
+
async run(db) {
|
|
82
|
+
await db.doc(this.collection, this.id).update(this.updates);
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
/** update one of the props on this set operation to a different value. */
|
|
86
|
+
with(key, value) {
|
|
87
|
+
if (isNullish(key))
|
|
88
|
+
return this;
|
|
89
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, updates: { ...this.updates, [key]: value } };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** Represent a delete operation made to a single document in a database. */
|
|
93
|
+
export class DeleteOperation extends Operation {
|
|
94
|
+
constructor(collection, id) {
|
|
95
|
+
super();
|
|
96
|
+
this.collection = collection;
|
|
97
|
+
this.id = id;
|
|
98
|
+
}
|
|
99
|
+
/** Create a new update operation on a document. */
|
|
100
|
+
static on({ collection, id }) {
|
|
101
|
+
return new DeleteOperation(collection, id);
|
|
102
|
+
}
|
|
103
|
+
async run(db) {
|
|
104
|
+
await db.doc(this.collection, this.id).delete();
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/** Set of hydrations for all change classes. */
|
|
109
|
+
export const OPERATION_HYDRATIONS = {
|
|
110
|
+
Operations: Operations,
|
|
111
|
+
AddOperation,
|
|
112
|
+
SetOperation,
|
|
113
|
+
UpdateOperation,
|
|
114
|
+
DeleteOperation,
|
|
115
|
+
};
|
|
116
|
+
OPERATION_HYDRATIONS;
|
package/db/Pagination.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Results, Entry, Entries, Data } from "../util/index.js";
|
|
2
2
|
import { State } from "../stream/index.js";
|
|
3
|
-
import {
|
|
3
|
+
import { DatabaseQuery } 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<T extends Data> extends State<
|
|
9
|
+
export declare class Pagination<T extends Data> extends State<Results<T>> implements Iterable<Entry<T>> {
|
|
10
10
|
protected _pending: boolean;
|
|
11
|
-
readonly ref:
|
|
11
|
+
readonly ref: DatabaseQuery<T>;
|
|
12
12
|
readonly limit: number;
|
|
13
|
-
constructor(ref:
|
|
13
|
+
constructor(ref: DatabaseQuery<T>);
|
|
14
14
|
/**
|
|
15
15
|
* Load more results after the end.
|
|
16
16
|
* - Promise that needs to be handled.
|
|
@@ -20,7 +20,7 @@ export declare class Pagination<T extends Data> extends State<ResultsMap<T>> imp
|
|
|
20
20
|
* Merge more results into this pagination.
|
|
21
21
|
* @return The change in the number of results.
|
|
22
22
|
*/
|
|
23
|
-
merge(more:
|
|
23
|
+
merge(more: Entries<T>): void;
|
|
24
24
|
/** Iterate over the entries of the values currently in the pagination. */
|
|
25
25
|
[Symbol.iterator](): Iterator<Entry<T>>;
|
|
26
26
|
}
|
package/db/Pagination.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getLastItem, assertNumber, yieldMerged,
|
|
1
|
+
import { getLastItem, assertNumber, yieldMerged, getMap, LOADING, assertMax } from "../util/index.js";
|
|
2
2
|
import { State } from "../stream/index.js";
|
|
3
3
|
import { ConditionError } from "../index.js";
|
|
4
4
|
/**
|
|
@@ -22,7 +22,7 @@ export class Pagination extends State {
|
|
|
22
22
|
if (!this.loading) {
|
|
23
23
|
const lastItem = getLastItem(this.value);
|
|
24
24
|
if (lastItem) {
|
|
25
|
-
const next = await this.ref.after(lastItem[0], lastItem[1]).
|
|
25
|
+
const next = await this.ref.after(lastItem[0], lastItem[1]).results;
|
|
26
26
|
this.merge(next);
|
|
27
27
|
if (next.size < this.limit)
|
|
28
28
|
this.complete();
|
|
@@ -31,7 +31,7 @@ export class Pagination extends State {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
this._value === LOADING;
|
|
34
|
-
const next = await this.ref.
|
|
34
|
+
const next = await this.ref.results;
|
|
35
35
|
this.next(next);
|
|
36
36
|
if (next.size < this.limit)
|
|
37
37
|
this.complete();
|
|
@@ -40,7 +40,7 @@ export class Pagination extends State {
|
|
|
40
40
|
};
|
|
41
41
|
this.ref = ref;
|
|
42
42
|
assertNumber(ref.limit); // Collection must have a numeric limit to paginate (otherwise you'd be retrieving the entire set of documents).
|
|
43
|
-
|
|
43
|
+
assertMax(ref.sorts.size, 1); // Collection must have at least one sort order to paginate.
|
|
44
44
|
this.limit = ref.limit;
|
|
45
45
|
}
|
|
46
46
|
/**
|
|
@@ -48,7 +48,7 @@ export class Pagination extends State {
|
|
|
48
48
|
* @return The change in the number of results.
|
|
49
49
|
*/
|
|
50
50
|
merge(more) {
|
|
51
|
-
this.next(
|
|
51
|
+
this.next(getMap(this.ref.sorts.transform(yieldMerged(more, this.value))));
|
|
52
52
|
}
|
|
53
53
|
/** Iterate over the entries of the values currently in the pagination. */
|
|
54
54
|
[Symbol.iterator]() {
|
package/db/errors.d.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import type { Data } from "../util/index.js";
|
|
2
2
|
import { RequiredError, ValidationError } from "../error/index.js";
|
|
3
3
|
import type { Feedback } from "../feedback/index.js";
|
|
4
|
-
import type {
|
|
4
|
+
import type { DatabaseDocument, DatabaseQuery } from "./Database.js";
|
|
5
5
|
/** Thrown if a document doesn't exist. */
|
|
6
6
|
export declare class DocumentRequiredError<T extends Data> extends RequiredError {
|
|
7
|
-
ref:
|
|
8
|
-
constructor(ref:
|
|
7
|
+
ref: DatabaseDocument<T>;
|
|
8
|
+
constructor(ref: DatabaseDocument<T>);
|
|
9
9
|
}
|
|
10
10
|
/** Thrown if a query doesn't exist. */
|
|
11
11
|
export declare class QueryRequiredError<T extends Data> extends RequiredError {
|
|
12
|
-
ref:
|
|
13
|
-
constructor(ref:
|
|
12
|
+
ref: DatabaseQuery<T>;
|
|
13
|
+
constructor(ref: DatabaseQuery<T>);
|
|
14
14
|
}
|
|
15
15
|
/** Thrown if a document can't validate. */
|
|
16
16
|
export declare class DocumentValidationError<T extends Data> extends ValidationError {
|
|
17
|
-
ref:
|
|
18
|
-
constructor(ref:
|
|
17
|
+
ref: DatabaseDocument<T>;
|
|
18
|
+
constructor(ref: DatabaseDocument<T>, feedback: Feedback);
|
|
19
19
|
}
|
|
20
20
|
/** Thrown if a query can't validate a set of results. */
|
|
21
21
|
export declare class QueryValidationError<T extends Data> extends ValidationError {
|
|
22
|
-
ref:
|
|
23
|
-
constructor(ref:
|
|
22
|
+
ref: DatabaseQuery<T>;
|
|
23
|
+
constructor(ref: DatabaseQuery<T>, feedback: Feedback);
|
|
24
24
|
}
|
package/db/index.d.ts
CHANGED