shelving 1.37.0 → 1.41.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 +29 -36
- package/db/Database.js +37 -54
- 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 +0 -1
- package/db/index.js +0 -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/index.d.ts +1 -0
- package/index.js +1 -0
- package/operation/AddOperation.d.ts +15 -0
- package/operation/AddOperation.js +23 -0
- package/operation/DeleteOperation.d.ts +9 -0
- package/operation/DeleteOperation.js +13 -0
- package/operation/Operation.d.ts +6 -0
- package/operation/Operation.js +3 -0
- package/operation/Operations.d.ts +21 -0
- package/operation/Operations.js +33 -0
- package/operation/SetOperation.d.ts +15 -0
- package/operation/SetOperation.js +23 -0
- package/operation/UpdateOperation.d.ts +16 -0
- package/operation/UpdateOperation.js +23 -0
- package/operation/WriteOperation.d.ts +16 -0
- package/operation/WriteOperation.js +25 -0
- package/operation/hydrations.d.ts +13 -0
- package/operation/hydrations.js +14 -0
- package/operation/index.d.ts +8 -0
- package/operation/index.js +8 -0
- package/package.json +6 -6
- 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/iterate.d.ts +10 -0
- package/util/iterate.js +17 -0
- 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,17 +47,22 @@ 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
|
-
* @return Number of documents
|
|
58
|
+
* @return Number of documents matching the query (possibly promised).
|
|
55
59
|
*/
|
|
56
60
|
get count(): number | PromiseLike<number>;
|
|
61
|
+
/**
|
|
62
|
+
* Does at least one document exist for this query?
|
|
63
|
+
* @return `true` if a document exists or `false` otherwise (possibly promised).
|
|
64
|
+
*/
|
|
65
|
+
get exists(): boolean | PromiseLike<boolean>;
|
|
57
66
|
/**
|
|
58
67
|
* Get an entry for the first document matched by this query or `undefined` if this query has no results.
|
|
59
68
|
*
|
|
@@ -76,14 +85,6 @@ export declare class DataQuery<T extends Data = Data> extends Query<T> implement
|
|
|
76
85
|
* @return Function that ends the subscription.
|
|
77
86
|
*/
|
|
78
87
|
subscribe(next: Observer<Results<T>> | Dispatcher<[Results<T>]>): Unsubscriber;
|
|
79
|
-
/**
|
|
80
|
-
* Subscribe to all matching documents.
|
|
81
|
-
* - `next()` is called once with the initial results, and again any time the results change.
|
|
82
|
-
*
|
|
83
|
-
* @param next Observer with `next`, `error`, or `complete` methods or a `next()` dispatcher.
|
|
84
|
-
* @return Function that ends the subscription.
|
|
85
|
-
*/
|
|
86
|
-
subscribeMap(next: Observer<ResultsMap<T>> | Dispatcher<[ResultsMap<T>]>): Unsubscriber;
|
|
87
88
|
/**
|
|
88
89
|
* Set all matching documents to the same exact value.
|
|
89
90
|
*
|
|
@@ -106,31 +107,29 @@ export declare class DataQuery<T extends Data = Data> extends Query<T> implement
|
|
|
106
107
|
/** Iterate over the resuls (will throw `Promise` if the results are asynchronous). */
|
|
107
108
|
[Symbol.iterator](): Iterator<Entry<T>, void>;
|
|
108
109
|
/** Validate a set of results for this query reference. */
|
|
109
|
-
validate(unsafeEntries:
|
|
110
|
+
validate(unsafeEntries: Entries): Entries<T>;
|
|
110
111
|
toString(): string;
|
|
111
112
|
}
|
|
112
113
|
/** Get the data for a document from a result for that document. */
|
|
113
|
-
export declare function getQueryData<T extends Data>(
|
|
114
|
+
export declare function getQueryData<T extends Data>(entries: Entries<T>, ref: DatabaseQuery<T>): Entry<T>;
|
|
114
115
|
/** A document reference within a specific database. */
|
|
115
|
-
export declare class
|
|
116
|
+
export declare class DatabaseDocument<T extends Data = Data> implements Observable<Result<T>>, Validatable<T> {
|
|
116
117
|
readonly provider: Provider;
|
|
117
118
|
readonly validator: Validator<T>;
|
|
118
119
|
readonly collection: string;
|
|
119
120
|
readonly id: string;
|
|
120
121
|
constructor(provider: Provider, validator: Validator<T>, collection: string, id: string);
|
|
121
122
|
/** Create a query on this document's collection. */
|
|
122
|
-
query(filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null):
|
|
123
|
+
query(filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null): DatabaseQuery<T>;
|
|
123
124
|
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
|
124
|
-
get optional():
|
|
125
|
+
get optional(): DatabaseQuery<T>;
|
|
125
126
|
/**
|
|
126
127
|
* Does this document exist?
|
|
127
|
-
*
|
|
128
|
-
* @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
|
|
128
|
+
* @return `true` if a document exists or `false` otherwise (possibly promised).
|
|
129
129
|
*/
|
|
130
130
|
get exists(): boolean | PromiseLike<boolean>;
|
|
131
131
|
/**
|
|
132
132
|
* Get the result of this document.
|
|
133
|
-
*
|
|
134
133
|
* @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
|
|
135
134
|
*/
|
|
136
135
|
get result(): Result<T> | PromiseLike<Result<T>>;
|
|
@@ -156,15 +155,9 @@ export declare class DataDocument<T extends Data = Data> implements Observable<R
|
|
|
156
155
|
update(updates: Update<T> | PropUpdates<T>): void | PromiseLike<void>;
|
|
157
156
|
/** Delete this document. */
|
|
158
157
|
delete(): void | PromiseLike<void>;
|
|
159
|
-
/** Represent a write that sets the complete data of this document in a database. */
|
|
160
|
-
setter(data: T): DocumentSet<T>;
|
|
161
|
-
/** Represent a write that updates this document in a database. */
|
|
162
|
-
updater(updates: Update<T> | PropUpdates<T>): DocumentUpdate<T>;
|
|
163
|
-
/** Represent a write that deletes this document in a database. */
|
|
164
|
-
deleter(): DocumentDelete<T>;
|
|
165
158
|
/** Validate data for this query reference. */
|
|
166
159
|
validate(unsafeData: Data): T;
|
|
167
160
|
toString(): string;
|
|
168
161
|
}
|
|
169
162
|
/** Get the data for a document from a result for that document. */
|
|
170
|
-
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,22 +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
|
-
* @return Number of documents
|
|
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
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Does at least one document exist for this query?
|
|
84
|
+
* @return `true` if a document exists or `false` otherwise (possibly promised).
|
|
85
|
+
*/
|
|
86
|
+
get exists() {
|
|
87
|
+
return callAsync(hasItems, this.provider.getQuery(this.max(1)));
|
|
81
88
|
}
|
|
82
89
|
/**
|
|
83
90
|
* Get an entry for the first document matched by this query or `undefined` if this query has no results.
|
|
@@ -86,7 +93,7 @@ export class DataQuery extends Query {
|
|
|
86
93
|
* @throws RequiredError if there were no results for this query.
|
|
87
94
|
*/
|
|
88
95
|
get result() {
|
|
89
|
-
return callAsync(getFirstItem, this.max(1)
|
|
96
|
+
return callAsync(getFirstItem, this.provider.getQuery(this.max(1)));
|
|
90
97
|
}
|
|
91
98
|
/**
|
|
92
99
|
* Get an entry for the first document matched by this query.
|
|
@@ -95,7 +102,7 @@ export class DataQuery extends Query {
|
|
|
95
102
|
* @throws RequiredError if there were no results for this query.
|
|
96
103
|
*/
|
|
97
104
|
get data() {
|
|
98
|
-
return callAsync(getQueryData, this.max(1)
|
|
105
|
+
return callAsync(getQueryData, this.provider.getQuery(this.max(1)), this);
|
|
99
106
|
}
|
|
100
107
|
/**
|
|
101
108
|
* Subscribe to all matching documents.
|
|
@@ -105,17 +112,7 @@ export class DataQuery extends Query {
|
|
|
105
112
|
* @return Function that ends the subscription.
|
|
106
113
|
*/
|
|
107
114
|
subscribe(next) {
|
|
108
|
-
return this.provider.subscribeQuery(this, typeof next === "function" ? { next } : next);
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Subscribe to all matching documents.
|
|
112
|
-
* - `next()` is called once with the initial results, and again any time the results change.
|
|
113
|
-
*
|
|
114
|
-
* @param next Observer with `next`, `error`, or `complete` methods or a `next()` dispatcher.
|
|
115
|
-
* @return Function that ends the subscription.
|
|
116
|
-
*/
|
|
117
|
-
subscribeMap(next) {
|
|
118
|
-
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));
|
|
119
116
|
}
|
|
120
117
|
/**
|
|
121
118
|
* Set all matching documents to the same exact value.
|
|
@@ -144,7 +141,7 @@ export class DataQuery extends Query {
|
|
|
144
141
|
}
|
|
145
142
|
/** Iterate over the resuls (will throw `Promise` if the results are asynchronous). */
|
|
146
143
|
[Symbol.iterator]() {
|
|
147
|
-
return throwAsync(this.
|
|
144
|
+
return throwAsync(this.entries)[Symbol.iterator]();
|
|
148
145
|
}
|
|
149
146
|
/** Validate a set of results for this query reference. */
|
|
150
147
|
*validate(unsafeEntries) {
|
|
@@ -170,14 +167,14 @@ export class DataQuery extends Query {
|
|
|
170
167
|
}
|
|
171
168
|
}
|
|
172
169
|
/** Get the data for a document from a result for that document. */
|
|
173
|
-
export function getQueryData(
|
|
174
|
-
const first = getFirstItem(
|
|
170
|
+
export function getQueryData(entries, ref) {
|
|
171
|
+
const first = getFirstItem(entries);
|
|
175
172
|
if (first)
|
|
176
173
|
return first;
|
|
177
174
|
throw new QueryRequiredError(ref);
|
|
178
175
|
}
|
|
179
176
|
/** A document reference within a specific database. */
|
|
180
|
-
export class
|
|
177
|
+
export class DatabaseDocument {
|
|
181
178
|
constructor(provider, validator, collection, id) {
|
|
182
179
|
this.provider = provider;
|
|
183
180
|
this.validator = validator;
|
|
@@ -186,23 +183,21 @@ export class DataDocument {
|
|
|
186
183
|
}
|
|
187
184
|
/** Create a query on this document's collection. */
|
|
188
185
|
query(filters, sorts, limit) {
|
|
189
|
-
return new
|
|
186
|
+
return new DatabaseQuery(this.provider, this.validator, this.collection, filters, sorts, limit);
|
|
190
187
|
}
|
|
191
188
|
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
|
192
189
|
get optional() {
|
|
193
|
-
return new
|
|
190
|
+
return new DatabaseQuery(this.provider, this.validator, this.collection, new Filters(new EqualFilter("id", this.id)));
|
|
194
191
|
}
|
|
195
192
|
/**
|
|
196
193
|
* Does this document exist?
|
|
197
|
-
*
|
|
198
|
-
* @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
|
|
194
|
+
* @return `true` if a document exists or `false` otherwise (possibly promised).
|
|
199
195
|
*/
|
|
200
196
|
get exists() {
|
|
201
197
|
return callAsync(Boolean, this.provider.get(this));
|
|
202
198
|
}
|
|
203
199
|
/**
|
|
204
200
|
* Get the result of this document.
|
|
205
|
-
*
|
|
206
201
|
* @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
|
|
207
202
|
*/
|
|
208
203
|
get result() {
|
|
@@ -240,18 +235,6 @@ export class DataDocument {
|
|
|
240
235
|
delete() {
|
|
241
236
|
return this.provider.delete(this);
|
|
242
237
|
}
|
|
243
|
-
/** Represent a write that sets the complete data of this document in a database. */
|
|
244
|
-
setter(data) {
|
|
245
|
-
return new DocumentSet(this, data);
|
|
246
|
-
}
|
|
247
|
-
/** Represent a write that updates this document in a database. */
|
|
248
|
-
updater(updates) {
|
|
249
|
-
return new DocumentUpdate(this, updates);
|
|
250
|
-
}
|
|
251
|
-
/** Represent a write that deletes this document in a database. */
|
|
252
|
-
deleter() {
|
|
253
|
-
return new DocumentDelete(this);
|
|
254
|
-
}
|
|
255
238
|
/** Validate data for this query reference. */
|
|
256
239
|
validate(unsafeData) {
|
|
257
240
|
try {
|
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
package/db/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Firestore } from "firebase/firestore";
|
|
2
|
-
import {
|
|
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 { Results, 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
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Firestore } from "firebase/firestore/lite";
|
|
2
|
-
import { Provider,
|
|
2
|
+
import { Provider, DatabaseDocument, DatabaseQuery, Result, Update, AsynchronousProvider, Data, Entries, Unsubscriber } from "../../index.js";
|
|
3
3
|
/**
|
|
4
4
|
* Firestore Lite client database provider.
|
|
5
5
|
* - Works with the Firebase JS SDK.
|
|
@@ -9,15 +9,15 @@ import { Provider, DataDocument, DataQuery, Result, Update, AsynchronousProvider
|
|
|
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:
|
|
12
|
+
get<T extends Data>(ref: DatabaseDocument<T>): Promise<Result<T>>;
|
|
13
13
|
subscribe(): Unsubscriber;
|
|
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:
|
|
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
19
|
subscribeQuery(): Unsubscriber;
|
|
20
|
-
setQuery<T extends Data>(ref:
|
|
21
|
-
updateQuery<T extends Data>(ref:
|
|
22
|
-
deleteQuery<T extends Data>(ref:
|
|
20
|
+
setQuery<T extends Data>(ref: DatabaseQuery<T>, data: T | Update<T> | undefined): 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
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Firestore } from "@google-cloud/firestore";
|
|
2
|
-
import { Provider,
|
|
2
|
+
import { Provider, DatabaseDocument, DatabaseQuery, Observer, Result, Update, Data, AsynchronousProvider, Entry, Entries, Unsubscriber } from "../../index.js";
|
|
3
3
|
/**
|
|
4
4
|
* Firestore server database provider.
|
|
5
5
|
* - Works with the Firebase Admin SDK for Node.JS
|
|
@@ -7,15 +7,15 @@ import { Provider, DataDocument, DataQuery, Observer, Result, Update, Data, Asyn
|
|
|
7
7
|
export declare class FirestoreServerProvider extends Provider implements AsynchronousProvider {
|
|
8
8
|
readonly firestore: Firestore;
|
|
9
9
|
constructor(firestore?: Firestore);
|
|
10
|
-
get<T extends Data>(ref:
|
|
11
|
-
subscribe<T extends Data>(ref:
|
|
12
|
-
add<T extends Data>(ref:
|
|
13
|
-
set<T extends Data>(ref:
|
|
14
|
-
update<T extends Data>(ref:
|
|
15
|
-
delete<T extends Data>(ref:
|
|
16
|
-
getQuery<T extends Data>(ref:
|
|
17
|
-
subscribeQuery<T extends Data>(ref:
|
|
18
|
-
setQuery<T extends Data>(ref:
|
|
19
|
-
updateQuery<T extends Data>(ref:
|
|
20
|
-
deleteQuery<T extends Data>(ref:
|
|
10
|
+
get<T extends Data>(ref: DatabaseDocument<T>): Promise<Result<T>>;
|
|
11
|
+
subscribe<T extends Data>(ref: DatabaseDocument<T>, observer: Observer<Result<T>>): Unsubscriber;
|
|
12
|
+
add<T extends Data>(ref: DatabaseQuery<T>, data: T): Promise<string>;
|
|
13
|
+
set<T extends Data>(ref: DatabaseDocument<T>, data: T): Promise<void>;
|
|
14
|
+
update<T extends Data>(ref: DatabaseDocument<T>, updates: Update<T>): Promise<void>;
|
|
15
|
+
delete<T extends Data>(ref: DatabaseDocument<T>): Promise<void>;
|
|
16
|
+
getQuery<T extends Data>(ref: DatabaseQuery<T>): Promise<Iterable<Entry<T>>>;
|
|
17
|
+
subscribeQuery<T extends Data>(ref: DatabaseQuery<T>, observer: Observer<Entries<T>>): Unsubscriber;
|
|
18
|
+
setQuery<T extends Data>(ref: DatabaseQuery<T>, data: T | Update<T> | undefined): Promise<number>;
|
|
19
|
+
updateQuery<T extends Data>(ref: DatabaseQuery<T>, updates: Update<T>): Promise<number>;
|
|
20
|
+
deleteQuery<T extends Data>(ref: DatabaseQuery<T>): Promise<number>;
|
|
21
21
|
}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -15,6 +15,7 @@ export * from "./feedback/index.js";
|
|
|
15
15
|
export * from "./markup/index.js";
|
|
16
16
|
export * from "./stream/index.js";
|
|
17
17
|
export * from "./update/index.js";
|
|
18
|
+
export * from "./operation/index.js";
|
|
18
19
|
export * from "./util/index.js";
|
|
19
20
|
// Integrations.
|
|
20
21
|
// export * from "./react/index.js"; // Not exported.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Database, DatabaseQuery } from "../db/index.js";
|
|
2
|
+
import { Data, Nullish, Key } from "../util/index.js";
|
|
3
|
+
import { SetOperation } from "./SetOperation.js";
|
|
4
|
+
import { WriteOperation } from "./WriteOperation.js";
|
|
5
|
+
/** Represent a add operation made to a collection in a database. */
|
|
6
|
+
export declare class AddOperation<T extends Data> extends WriteOperation {
|
|
7
|
+
readonly collection: string;
|
|
8
|
+
readonly data: T;
|
|
9
|
+
constructor(collection: string, data: T);
|
|
10
|
+
run(db: Database): Promise<SetOperation<T>>;
|
|
11
|
+
/** Set one of the props on this set operation to a different value. */
|
|
12
|
+
with<K extends Key<T>>(key: Nullish<K>, value: T[K]): this;
|
|
13
|
+
}
|
|
14
|
+
/** Create an add operation for a document. */
|
|
15
|
+
export declare const ADD: <T extends Data>({ collection }: DatabaseQuery<T>, data: T) => AddOperation<T>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isNullish } from "../util/index.js";
|
|
2
|
+
import { SetOperation } from "./SetOperation.js";
|
|
3
|
+
import { WriteOperation } from "./WriteOperation.js";
|
|
4
|
+
/** Represent a add operation made to a collection in a database. */
|
|
5
|
+
export class AddOperation extends WriteOperation {
|
|
6
|
+
constructor(collection, data) {
|
|
7
|
+
super();
|
|
8
|
+
this.collection = collection;
|
|
9
|
+
this.data = data;
|
|
10
|
+
}
|
|
11
|
+
async run(db) {
|
|
12
|
+
const id = await db.query(this.collection).add(this.data);
|
|
13
|
+
return new SetOperation(this.collection, id, this.data); // When an add operation is run it returns a set operation so the operation is repeatable.
|
|
14
|
+
}
|
|
15
|
+
/** Set one of the props on this set operation to a different value. */
|
|
16
|
+
with(key, value) {
|
|
17
|
+
if (isNullish(key))
|
|
18
|
+
return this;
|
|
19
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, data: { ...this.data, [key]: value } };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Create an add operation for a document. */
|
|
23
|
+
export const ADD = ({ collection }, data) => new AddOperation(collection, data);
|