shelving 1.39.0 → 1.43.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 +23 -25
- package/db/Database.js +39 -51
- package/db/Operation.d.ts +82 -0
- package/db/Operation.js +132 -0
- package/db/Pagination.d.ts +3 -3
- 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 +5 -5
- package/provider/CacheProvider.d.ts +14 -14
- package/provider/MemoryProvider.d.ts +12 -12
- package/provider/Provider.d.ts +30 -30
- package/provider/ThroughProvider.d.ts +12 -12
- package/provider/ValidationProvider.d.ts +10 -10
- package/react/useDocument.d.ts +9 -9
- package/react/useDocument.js +2 -2
- package/react/usePagination.d.ts +2 -2
- package/react/useQuery.d.ts +13 -13
- package/react/useQuery.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 +6 -6
- 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/async.d.ts +28 -4
- package/util/async.js +36 -3
- package/db/Write.d.ts +0 -46
- package/db/Write.js +0 -62
package/db/Database.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { Entry, Observable, Observer, Result, Unsubscriber, Results, Validatable, Validator, Key, Data, Entries, Datas, Validators, ValidatorType, Dispatcher
|
|
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
|
|
28
|
-
readonly
|
|
31
|
+
export declare class DatabaseQuery<T extends Data = Data> extends Query<T> implements Observable<Results<T>>, Validatable<Entries<T>>, Iterable<Entry<T>> {
|
|
32
|
+
readonly db: Database;
|
|
29
33
|
readonly validator: Validator<T>;
|
|
30
34
|
readonly collection: string;
|
|
31
|
-
constructor(
|
|
35
|
+
constructor(db: Database, 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.
|
|
@@ -107,18 +111,18 @@ export declare class DataQuery<T extends Data = Data> extends Query<T> implement
|
|
|
107
111
|
toString(): string;
|
|
108
112
|
}
|
|
109
113
|
/** Get the data for a document from a result for that document. */
|
|
110
|
-
export declare function getQueryData<T extends Data>(entries: Entries<T>, ref:
|
|
114
|
+
export declare function getQueryData<T extends Data>(entries: Entries<T>, ref: DatabaseQuery<T>): Entry<T>;
|
|
111
115
|
/** A document reference within a specific database. */
|
|
112
|
-
export declare class
|
|
113
|
-
readonly
|
|
116
|
+
export declare class DatabaseDocument<T extends Data = Data> implements Observable<Result<T>>, Validatable<T> {
|
|
117
|
+
readonly db: Database;
|
|
114
118
|
readonly validator: Validator<T>;
|
|
115
119
|
readonly collection: string;
|
|
116
120
|
readonly id: string;
|
|
117
|
-
constructor(
|
|
121
|
+
constructor(db: Database, validator: Validator<T>, collection: string, id: string);
|
|
118
122
|
/** Create a query on this document's collection. */
|
|
119
|
-
query(filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null):
|
|
123
|
+
query(filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null): DatabaseQuery<T>;
|
|
120
124
|
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
|
121
|
-
get optional():
|
|
125
|
+
get optional(): DatabaseQuery<T>;
|
|
122
126
|
/**
|
|
123
127
|
* Does this document exist?
|
|
124
128
|
* @return `true` if a document exists or `false` otherwise (possibly promised).
|
|
@@ -151,15 +155,9 @@ export declare class DataDocument<T extends Data = Data> implements Observable<R
|
|
|
151
155
|
update(updates: Update<T> | PropUpdates<T>): void | PromiseLike<void>;
|
|
152
156
|
/** Delete this document. */
|
|
153
157
|
delete(): void | PromiseLike<void>;
|
|
154
|
-
/** Represent a write that sets the complete data of this document in a database. */
|
|
155
|
-
setter(data: T): DocumentSet<T>;
|
|
156
|
-
/** Represent a write that updates this document in a database. */
|
|
157
|
-
updater(updates: Update<T> | PropUpdates<T>): DocumentUpdate<T>;
|
|
158
|
-
/** Represent a write that deletes this document in a database. */
|
|
159
|
-
deleter(): DocumentDelete<T>;
|
|
160
158
|
/** Validate data for this query reference. */
|
|
161
159
|
validate(unsafeData: Data): T;
|
|
162
160
|
toString(): string;
|
|
163
161
|
}
|
|
164
162
|
/** Get the data for a document from a result for that document. */
|
|
165
|
-
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, getMap, countItems,
|
|
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,34 +18,35 @@ 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, 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, 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
|
|
41
|
-
constructor(
|
|
40
|
+
export class DatabaseQuery extends Query {
|
|
41
|
+
constructor(db, validator, collection, filters, sorts, limit) {
|
|
42
42
|
super(filters, sorts, limit);
|
|
43
|
-
this.
|
|
43
|
+
this.db = db;
|
|
44
44
|
this.validator = validator;
|
|
45
45
|
this.collection = collection;
|
|
46
46
|
}
|
|
47
47
|
/** Reference a document in this query's collection. */
|
|
48
48
|
doc(id) {
|
|
49
|
-
return new
|
|
49
|
+
return new DatabaseDocument(this.db, this.validator, this.collection, id);
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
52
|
* Create a new document with a random ID.
|
|
@@ -56,21 +56,21 @@ export class DataQuery extends Query {
|
|
|
56
56
|
* @return String ID for the created document (possibly promised).
|
|
57
57
|
*/
|
|
58
58
|
add(data) {
|
|
59
|
-
return this.provider.add(this, data);
|
|
59
|
+
return this.db.provider.add(this, data);
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
62
|
* Get an iterable that yields the results of this entry.
|
|
63
63
|
* @return Map containing the results.
|
|
64
64
|
*/
|
|
65
65
|
get entries() {
|
|
66
|
-
return this.provider.getQuery(this);
|
|
66
|
+
return this.db.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
72
|
get results() {
|
|
73
|
-
return callAsync(getMap, this.provider.getQuery(this));
|
|
73
|
+
return callAsync(getMap, this.db.provider.getQuery(this));
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
76
|
* Count the number of results of this set of documents.
|
|
@@ -84,7 +84,7 @@ export class DataQuery extends 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.provider.getQuery(this.max(1)));
|
|
87
|
+
return callAsync(hasItems, this.db.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.provider.getQuery(this.max(1)));
|
|
96
|
+
return callAsync(getFirstItem, this.db.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.provider.getQuery(this.max(1)), this);
|
|
105
|
+
return callAsync(getQueryData, this.db.provider.getQuery(this.max(1)), this);
|
|
106
106
|
}
|
|
107
107
|
/**
|
|
108
108
|
* Subscribe to all matching documents.
|
|
@@ -112,7 +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, new ResultsObserver(typeof next === "function" ? { next } : next));
|
|
115
|
+
return this.db.provider.subscribeQuery(this, new ResultsObserver(typeof next === "function" ? { next } : next));
|
|
116
116
|
}
|
|
117
117
|
/**
|
|
118
118
|
* Set all matching documents to the same exact value.
|
|
@@ -121,7 +121,7 @@ export class DataQuery extends Query {
|
|
|
121
121
|
* @return Nothing (possibly promised).
|
|
122
122
|
*/
|
|
123
123
|
set(data) {
|
|
124
|
-
return this.provider.setQuery(this, data);
|
|
124
|
+
return this.db.provider.setQuery(this, data);
|
|
125
125
|
}
|
|
126
126
|
/**
|
|
127
127
|
* Update all matching documents with the same partial value.
|
|
@@ -130,14 +130,14 @@ export class DataQuery extends Query {
|
|
|
130
130
|
* @return Nothing (possibly promised).
|
|
131
131
|
*/
|
|
132
132
|
update(updates) {
|
|
133
|
-
return this.provider.updateQuery(this, updates instanceof Update ? updates : new DataUpdate(updates));
|
|
133
|
+
return this.db.provider.updateQuery(this, updates instanceof Update ? updates : new DataUpdate(updates));
|
|
134
134
|
}
|
|
135
135
|
/**
|
|
136
136
|
* Delete all matching documents.
|
|
137
137
|
* @return Nothing (possibly promised).
|
|
138
138
|
*/
|
|
139
139
|
delete() {
|
|
140
|
-
return this.provider.deleteQuery(this);
|
|
140
|
+
return this.db.provider.deleteQuery(this);
|
|
141
141
|
}
|
|
142
142
|
/** Iterate over the resuls (will throw `Promise` if the results are asynchronous). */
|
|
143
143
|
[Symbol.iterator]() {
|
|
@@ -174,34 +174,34 @@ export function getQueryData(entries, ref) {
|
|
|
174
174
|
throw new QueryRequiredError(ref);
|
|
175
175
|
}
|
|
176
176
|
/** A document reference within a specific database. */
|
|
177
|
-
export class
|
|
178
|
-
constructor(
|
|
179
|
-
this.
|
|
177
|
+
export class DatabaseDocument {
|
|
178
|
+
constructor(db, validator, collection, id) {
|
|
179
|
+
this.db = db;
|
|
180
180
|
this.validator = validator;
|
|
181
181
|
this.collection = collection;
|
|
182
182
|
this.id = id;
|
|
183
183
|
}
|
|
184
184
|
/** Create a query on this document's collection. */
|
|
185
185
|
query(filters, sorts, limit) {
|
|
186
|
-
return new
|
|
186
|
+
return new DatabaseQuery(this.db, this.validator, this.collection, filters, sorts, limit);
|
|
187
187
|
}
|
|
188
188
|
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
|
189
189
|
get optional() {
|
|
190
|
-
return new
|
|
190
|
+
return new DatabaseQuery(this.db, this.validator, this.collection, new Filters(new EqualFilter("id", this.id)));
|
|
191
191
|
}
|
|
192
192
|
/**
|
|
193
193
|
* Does this document exist?
|
|
194
194
|
* @return `true` if a document exists or `false` otherwise (possibly promised).
|
|
195
195
|
*/
|
|
196
196
|
get exists() {
|
|
197
|
-
return callAsync(Boolean, this.provider.get(this));
|
|
197
|
+
return callAsync(Boolean, this.db.provider.get(this));
|
|
198
198
|
}
|
|
199
199
|
/**
|
|
200
200
|
* Get the result of this document.
|
|
201
201
|
* @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
|
|
202
202
|
*/
|
|
203
203
|
get result() {
|
|
204
|
-
return this.provider.get(this);
|
|
204
|
+
return this.db.provider.get(this);
|
|
205
205
|
}
|
|
206
206
|
/**
|
|
207
207
|
* Get the data of this document.
|
|
@@ -211,7 +211,7 @@ export class DataDocument {
|
|
|
211
211
|
* @throws RequiredError if the document's result was undefined.
|
|
212
212
|
*/
|
|
213
213
|
get data() {
|
|
214
|
-
return callAsync(getDocumentData, this.provider.get(this), this);
|
|
214
|
+
return callAsync(getDocumentData, this.db.provider.get(this), this);
|
|
215
215
|
}
|
|
216
216
|
/**
|
|
217
217
|
* Subscribe to the result of this document (indefinitely).
|
|
@@ -221,31 +221,19 @@ export class DataDocument {
|
|
|
221
221
|
* @return Function that ends the subscription.
|
|
222
222
|
*/
|
|
223
223
|
subscribe(next) {
|
|
224
|
-
return this.provider.subscribe(this, typeof next === "function" ? { next } : next);
|
|
224
|
+
return this.db.provider.subscribe(this, typeof next === "function" ? { next } : next);
|
|
225
225
|
}
|
|
226
226
|
/** Set the complete data of this document. */
|
|
227
227
|
set(data) {
|
|
228
|
-
return this.provider.set(this, data);
|
|
228
|
+
return this.db.provider.set(this, data);
|
|
229
229
|
}
|
|
230
230
|
/** Update this document. */
|
|
231
231
|
update(updates) {
|
|
232
|
-
return this.provider.update(this, updates instanceof Update ? updates : new DataUpdate(updates));
|
|
232
|
+
return this.db.provider.update(this, updates instanceof Update ? updates : new DataUpdate(updates));
|
|
233
233
|
}
|
|
234
234
|
/** Delete this document. */
|
|
235
235
|
delete() {
|
|
236
|
-
return this.provider.delete(this);
|
|
237
|
-
}
|
|
238
|
-
/** Represent a write that sets the complete data of this document in a database. */
|
|
239
|
-
setter(data) {
|
|
240
|
-
return new DocumentSet(this, data);
|
|
241
|
-
}
|
|
242
|
-
/** Represent a write that updates this document in a database. */
|
|
243
|
-
updater(updates) {
|
|
244
|
-
return new DocumentUpdate(this, updates);
|
|
245
|
-
}
|
|
246
|
-
/** Represent a write that deletes this document in a database. */
|
|
247
|
-
deleter() {
|
|
248
|
-
return new DocumentDelete(this);
|
|
236
|
+
return this.db.provider.delete(this);
|
|
249
237
|
}
|
|
250
238
|
/** Validate data for this query reference. */
|
|
251
239
|
validate(unsafeData) {
|
|
@@ -0,0 +1,82 @@
|
|
|
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
|
+
/** Run this operation and return the result operation. */
|
|
7
|
+
abstract run(db: Database): Promise<Operation>;
|
|
8
|
+
}
|
|
9
|
+
/** Represent a list of write operations on a database run in series. */
|
|
10
|
+
export declare class Operations extends Operation {
|
|
11
|
+
/** Return a new write operations list with a set of write operations. */
|
|
12
|
+
static with(...operations: Nullish<Operation>[]): Operations;
|
|
13
|
+
readonly operations: ImmutableArray<Operation>;
|
|
14
|
+
constructor(operations: ImmutableArray<Nullish<Operation>>);
|
|
15
|
+
run(db: Database): Promise<Operations>;
|
|
16
|
+
/** Return a new write operations list with an additional write operation added. */
|
|
17
|
+
with(...operations: Nullish<Operation>[]): {
|
|
18
|
+
__proto__: any;
|
|
19
|
+
} & this & {
|
|
20
|
+
operations: (Operation | Nullish<Operation>[])[];
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** Represent a add operation made to a collection in a database. */
|
|
24
|
+
export declare class AddOperation<T extends Data> extends Operation {
|
|
25
|
+
/** Create a new add operation on a collection. */
|
|
26
|
+
static on<X extends Data>({ collection }: DatabaseDocument<X> | DatabaseQuery<X>, data: X): AddOperation<X>;
|
|
27
|
+
/** Run a new add operation on a collection and return the result operation. */
|
|
28
|
+
static run<X extends Data>({ collection, db }: DatabaseDocument<X> | DatabaseQuery<X>, data: X): Promise<SetOperation<X>>;
|
|
29
|
+
readonly collection: string;
|
|
30
|
+
readonly data: T;
|
|
31
|
+
constructor(collection: string, data: T);
|
|
32
|
+
run(db: Database): Promise<SetOperation<T>>;
|
|
33
|
+
/** Set one of the props on this set operation to a different value. */
|
|
34
|
+
with<K extends Key<T>>(key: Nullish<K>, value: T[K]): this;
|
|
35
|
+
}
|
|
36
|
+
/** Represent a set operation made to a single document in a database. */
|
|
37
|
+
export declare class SetOperation<T extends Data> extends Operation {
|
|
38
|
+
/** Create a new add operation on a collection. */
|
|
39
|
+
static on<X extends Data>({ collection, id }: DatabaseDocument<X>, data: X): SetOperation<X>;
|
|
40
|
+
/** Run a new set operation on a collection and return the result operation. */
|
|
41
|
+
static run<X extends Data>({ collection, id, db }: DatabaseDocument<X>, data: X): Promise<SetOperation<X>>;
|
|
42
|
+
readonly collection: string;
|
|
43
|
+
readonly id: string;
|
|
44
|
+
readonly data: T;
|
|
45
|
+
constructor(collection: string, id: string, data: T);
|
|
46
|
+
run(db: Database): Promise<this>;
|
|
47
|
+
/** Set one of the props on this set operation to a different value. */
|
|
48
|
+
with<K extends Key<T>>(key: Nullish<K>, value: T[K]): this;
|
|
49
|
+
}
|
|
50
|
+
/** Represent an update operation made to a single document in a database. */
|
|
51
|
+
export declare class UpdateOperation<T extends Data> extends Operation {
|
|
52
|
+
/** Create a new update operation on a document. */
|
|
53
|
+
static on<X extends Data>({ collection, id }: DatabaseDocument<X>, updates: PropUpdates<X>): UpdateOperation<X>;
|
|
54
|
+
/** Run a new set operation on a collection and return the result operation. */
|
|
55
|
+
static run<X extends Data>({ collection, id, db }: DatabaseDocument<X>, updates: PropUpdates<X>): Promise<UpdateOperation<X>>;
|
|
56
|
+
readonly collection: string;
|
|
57
|
+
readonly id: string;
|
|
58
|
+
readonly updates: PropUpdates<T>;
|
|
59
|
+
constructor(collection: string, id: string, updates: PropUpdates<T>);
|
|
60
|
+
run(db: Database): Promise<this>;
|
|
61
|
+
/** update one of the props on this set operation to a different value. */
|
|
62
|
+
with<K extends Key<T>>(key: Nullish<K>, value: T[K] | Update<T[K]>): this;
|
|
63
|
+
}
|
|
64
|
+
/** Represent a delete operation made to a single document in a database. */
|
|
65
|
+
export declare class DeleteOperation extends Operation {
|
|
66
|
+
/** Create a new delete operation on a document. */
|
|
67
|
+
static on({ collection, id }: DatabaseDocument): DeleteOperation;
|
|
68
|
+
/** Run a new delete operation on a document. */
|
|
69
|
+
static run({ collection, id, db }: DatabaseDocument): Promise<DeleteOperation>;
|
|
70
|
+
readonly collection: string;
|
|
71
|
+
readonly id: string;
|
|
72
|
+
constructor(collection: string, id: string);
|
|
73
|
+
run(db: Database): Promise<this>;
|
|
74
|
+
}
|
|
75
|
+
/** Set of hydrations for all change classes. */
|
|
76
|
+
export declare const OPERATION_HYDRATIONS: {
|
|
77
|
+
Operations: typeof Operations;
|
|
78
|
+
AddOperation: typeof AddOperation;
|
|
79
|
+
SetOperation: typeof SetOperation;
|
|
80
|
+
UpdateOperation: typeof UpdateOperation;
|
|
81
|
+
DeleteOperation: typeof DeleteOperation;
|
|
82
|
+
};
|
package/db/Operation.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
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
|
+
/** Run a new add operation on a collection and return the result operation. */
|
|
36
|
+
static run({ collection, db }, data) {
|
|
37
|
+
return new AddOperation(collection, data).run(db);
|
|
38
|
+
}
|
|
39
|
+
async run(db) {
|
|
40
|
+
const id = await db.query(this.collection).add(this.data);
|
|
41
|
+
return new SetOperation(this.collection, id, this.data); // When an add operation is run it returns a set operation so the operation is repeatable.
|
|
42
|
+
}
|
|
43
|
+
/** Set one of the props on this set operation to a different value. */
|
|
44
|
+
with(key, value) {
|
|
45
|
+
if (isNullish(key))
|
|
46
|
+
return this;
|
|
47
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, data: { ...this.data, [key]: value } };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Represent a set operation made to a single document in a database. */
|
|
51
|
+
export class SetOperation extends Operation {
|
|
52
|
+
constructor(collection, id, data) {
|
|
53
|
+
super();
|
|
54
|
+
this.collection = collection;
|
|
55
|
+
this.id = id;
|
|
56
|
+
this.data = data;
|
|
57
|
+
}
|
|
58
|
+
/** Create a new add operation on a collection. */
|
|
59
|
+
static on({ collection, id }, data) {
|
|
60
|
+
return new SetOperation(collection, id, data);
|
|
61
|
+
}
|
|
62
|
+
/** Run a new set operation on a collection and return the result operation. */
|
|
63
|
+
static run({ collection, id, db }, data) {
|
|
64
|
+
return new SetOperation(collection, id, data).run(db);
|
|
65
|
+
}
|
|
66
|
+
async run(db) {
|
|
67
|
+
await db.doc(this.collection, this.id).set(this.data);
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
/** Set one of the props on this set operation to a different value. */
|
|
71
|
+
with(key, value) {
|
|
72
|
+
if (isNullish(key))
|
|
73
|
+
return this;
|
|
74
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, data: { ...this.data, [key]: value } };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** Represent an update operation made to a single document in a database. */
|
|
78
|
+
export class UpdateOperation extends Operation {
|
|
79
|
+
constructor(collection, id, updates) {
|
|
80
|
+
super();
|
|
81
|
+
this.collection = collection;
|
|
82
|
+
this.id = id;
|
|
83
|
+
this.updates = updates;
|
|
84
|
+
}
|
|
85
|
+
/** Create a new update operation on a document. */
|
|
86
|
+
static on({ collection, id }, updates) {
|
|
87
|
+
return new UpdateOperation(collection, id, updates);
|
|
88
|
+
}
|
|
89
|
+
/** Run a new set operation on a collection and return the result operation. */
|
|
90
|
+
static run({ collection, id, db }, updates) {
|
|
91
|
+
return new UpdateOperation(collection, id, updates).run(db);
|
|
92
|
+
}
|
|
93
|
+
async run(db) {
|
|
94
|
+
await db.doc(this.collection, this.id).update(this.updates);
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
/** update one of the props on this set operation to a different value. */
|
|
98
|
+
with(key, value) {
|
|
99
|
+
if (isNullish(key))
|
|
100
|
+
return this;
|
|
101
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, updates: { ...this.updates, [key]: value } };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** Represent a delete operation made to a single document in a database. */
|
|
105
|
+
export class DeleteOperation extends Operation {
|
|
106
|
+
constructor(collection, id) {
|
|
107
|
+
super();
|
|
108
|
+
this.collection = collection;
|
|
109
|
+
this.id = id;
|
|
110
|
+
}
|
|
111
|
+
/** Create a new delete operation on a document. */
|
|
112
|
+
static on({ collection, id }) {
|
|
113
|
+
return new DeleteOperation(collection, id);
|
|
114
|
+
}
|
|
115
|
+
/** Run a new delete operation on a document. */
|
|
116
|
+
static run({ collection, id, db }) {
|
|
117
|
+
return new DeleteOperation(collection, id).run(db);
|
|
118
|
+
}
|
|
119
|
+
async run(db) {
|
|
120
|
+
await db.doc(this.collection, this.id).delete();
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** Set of hydrations for all change classes. */
|
|
125
|
+
export const OPERATION_HYDRATIONS = {
|
|
126
|
+
Operations: Operations,
|
|
127
|
+
AddOperation,
|
|
128
|
+
SetOperation,
|
|
129
|
+
UpdateOperation,
|
|
130
|
+
DeleteOperation,
|
|
131
|
+
};
|
|
132
|
+
OPERATION_HYDRATIONS;
|
package/db/Pagination.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
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.
|
|
@@ -8,9 +8,9 @@ import { DataQuery } from "./Database.js";
|
|
|
8
8
|
*/
|
|
9
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.
|
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
package/db/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Firestore } from "firebase/firestore";
|
|
2
|
-
import { Entries, Provider,
|
|
2
|
+
import { Entries, Provider, DatabaseDocument, DatabaseQuery, Result, Observer, Update, AsynchronousProvider, Data, Unsubscriber } from "../../index.js";
|
|
3
3
|
/**
|
|
4
4
|
* Firestore client database provider.
|
|
5
5
|
* - Works with the Firebase JS SDK.
|
|
@@ -9,15 +9,15 @@ import { Entries, Provider, DataDocument, DataQuery, Result, Observer, Update, A
|
|
|
9
9
|
export declare class FirestoreClientProvider extends Provider implements AsynchronousProvider {
|
|
10
10
|
readonly firestore: Firestore;
|
|
11
11
|
constructor(firestore: Firestore);
|
|
12
|
-
get<T extends Data>(ref:
|
|
13
|
-
subscribe<T extends Data>(ref:
|
|
14
|
-
add<T extends Data>(ref:
|
|
15
|
-
set<T extends Data>(ref:
|
|
16
|
-
update<T extends Data>(ref:
|
|
17
|
-
delete<T extends Data>(ref:
|
|
18
|
-
getQuery<T extends Data>(ref:
|
|
19
|
-
subscribeQuery<T extends Data>(ref:
|
|
20
|
-
setQuery<T extends Data>(ref:
|
|
21
|
-
updateQuery<T extends Data>(ref:
|
|
22
|
-
deleteQuery<T extends Data>(ref:
|
|
12
|
+
get<T extends Data>(ref: DatabaseDocument<T>): Promise<Result<T>>;
|
|
13
|
+
subscribe<T extends Data>(ref: DatabaseDocument<T>, observer: Observer<Result<T>>): Unsubscriber;
|
|
14
|
+
add<T extends Data>(ref: DatabaseQuery<T>, data: T): Promise<string>;
|
|
15
|
+
set<T extends Data>(ref: DatabaseDocument<T>, data: T): Promise<void>;
|
|
16
|
+
update<T extends Data>(ref: DatabaseDocument<T>, updates: Update<T>): Promise<void>;
|
|
17
|
+
delete<T extends Data>(ref: DatabaseDocument<T>): Promise<void>;
|
|
18
|
+
getQuery<T extends Data>(ref: DatabaseQuery<T>): Promise<Entries<T>>;
|
|
19
|
+
subscribeQuery<T extends Data>(ref: DatabaseQuery<T>, observer: Observer<Entries<T>>): Unsubscriber;
|
|
20
|
+
setQuery<T extends Data>(ref: DatabaseQuery<T>, data: T): Promise<number>;
|
|
21
|
+
updateQuery<T extends Data>(ref: DatabaseQuery<T>, updates: Update<T>): Promise<number>;
|
|
22
|
+
deleteQuery<T extends Data>(ref: DatabaseQuery<T>): Promise<number>;
|
|
23
23
|
}
|