shelving 1.59.1 → 1.61.1
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 +5 -150
- package/db/Database.js +4 -237
- package/db/Operation.d.ts +10 -9
- package/db/PaginationState.d.ts +3 -3
- package/db/Reference.d.ts +140 -0
- package/db/Reference.js +212 -0
- package/db/errors.d.ts +9 -9
- package/db/helpers.d.ts +35 -0
- package/db/helpers.js +52 -0
- package/db/index.d.ts +2 -0
- package/db/index.js +2 -0
- 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/ErrorProvider.d.ts +16 -16
- 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/index.d.ts +2 -0
- package/react/index.js +2 -0
- package/react/useCompare.d.ts +3 -0
- package/react/useCompare.js +8 -0
- package/react/useDocument.d.ts +9 -9
- package/react/useDocument.js +7 -5
- package/react/usePagination.d.ts +2 -2
- package/react/usePagination.js +10 -8
- package/react/usePureEffect.js +2 -4
- package/react/usePureState.js +2 -10
- package/react/useQuery.d.ts +13 -13
- package/react/useQuery.js +4 -2
- package/react/useReduce.d.ts +3 -0
- package/react/useReduce.js +7 -0
- package/react/useState.d.ts +16 -1
- package/react/useState.js +32 -1
- package/react/useSubscribe.js +3 -4
- package/util/observable.d.ts +9 -4
- package/util/observable.js +13 -4
- package/util/string.d.ts +6 -0
- package/util/string.js +11 -0
package/db/Database.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DataUpdate, PropUpdates } from "../update/index.js";
|
|
1
|
+
import { Key, Datas, Validators, ValidatorType } from "../util/index.js";
|
|
3
2
|
import type { Provider } from "../provider/Provider.js";
|
|
4
|
-
import {
|
|
3
|
+
import { FilterProps, SortKeys } from "../query/index.js";
|
|
4
|
+
import { DocumentReference, QueryReference } from "./Reference.js";
|
|
5
5
|
/**
|
|
6
6
|
* Combines a database model and a provider.
|
|
7
7
|
*
|
|
@@ -14,9 +14,9 @@ export declare class Database<V extends Validators<Datas> = Validators<Datas>> {
|
|
|
14
14
|
readonly provider: Provider;
|
|
15
15
|
constructor(validators: V, provider: Provider);
|
|
16
16
|
/** Create a query on a collection in this model. */
|
|
17
|
-
query<K extends Key<V>>(collection: K, filters?: FilterProps<ValidatorType<V[K]>>, sorts?: SortKeys<ValidatorType<V[K]>>, limit?: number | null):
|
|
17
|
+
query<K extends Key<V>>(collection: K, filters?: FilterProps<ValidatorType<V[K]>>, sorts?: SortKeys<ValidatorType<V[K]>>, limit?: number | null): QueryReference<ValidatorType<V[K]>>;
|
|
18
18
|
/** Reference a document in a collection in this model. */
|
|
19
|
-
doc<K extends Key<V>>(collection: K, id: string):
|
|
19
|
+
doc<K extends Key<V>>(collection: K, id: string): DocumentReference<ValidatorType<V[K]>>;
|
|
20
20
|
/**
|
|
21
21
|
* Create a new document with a random ID.
|
|
22
22
|
* - Created document is guaranteed to have a unique ID.
|
|
@@ -27,148 +27,3 @@ export declare class Database<V extends Validators<Datas> = Validators<Datas>> {
|
|
|
27
27
|
*/
|
|
28
28
|
add<K extends Key<V>>(collection: K, data: ValidatorType<V[K]>): string | PromiseLike<string>;
|
|
29
29
|
}
|
|
30
|
-
/** A documents reference within a specific database. */
|
|
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;
|
|
33
|
-
readonly validator: Validator<T>;
|
|
34
|
-
readonly collection: string;
|
|
35
|
-
constructor(db: Database, validator: Validator<T>, collection: string, filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null);
|
|
36
|
-
/** Reference a document in this query's collection. */
|
|
37
|
-
doc(id: string): DatabaseDocument<T>;
|
|
38
|
-
/**
|
|
39
|
-
* Create a new document with a random ID.
|
|
40
|
-
* - Created document is guaranteed to have a unique ID.
|
|
41
|
-
*
|
|
42
|
-
* @param data Complete data to set the document to.
|
|
43
|
-
* @return String ID for the created document (possibly promised).
|
|
44
|
-
*/
|
|
45
|
-
add(data: T): string | PromiseLike<string>;
|
|
46
|
-
/**
|
|
47
|
-
* Get an iterable that yields the results of this entry.
|
|
48
|
-
* @return Map containing the results.
|
|
49
|
-
*/
|
|
50
|
-
get entries(): Entries<T> | PromiseLike<Entries<T>>;
|
|
51
|
-
/**
|
|
52
|
-
* Get an iterable that yields the results of this entry.
|
|
53
|
-
* @return Map containing the results.
|
|
54
|
-
*/
|
|
55
|
-
get results(): Results<T> | PromiseLike<Results<T>>;
|
|
56
|
-
/**
|
|
57
|
-
* Count the number of results of this set of documents.
|
|
58
|
-
* @return Number of documents matching the query (possibly promised).
|
|
59
|
-
*/
|
|
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>;
|
|
66
|
-
/**
|
|
67
|
-
* Get an entry for the first document matched by this query or `undefined` if this query has no results.
|
|
68
|
-
*
|
|
69
|
-
* @return Entry in `[id, data]` format for the first document.
|
|
70
|
-
* @throws RequiredError if there were no results for this query.
|
|
71
|
-
*/
|
|
72
|
-
get result(): DocumentResult<T> | PromiseLike<DocumentResult<T>>;
|
|
73
|
-
/**
|
|
74
|
-
* Get an entry for the first document matched by this query.
|
|
75
|
-
*
|
|
76
|
-
* @return Entry in `[id, data]` format for the first document.
|
|
77
|
-
* @throws RequiredError if there were no results for this query.
|
|
78
|
-
*/
|
|
79
|
-
get data(): DocumentData<T> | PromiseLike<DocumentData<T>>;
|
|
80
|
-
/**
|
|
81
|
-
* Subscribe to all matching documents.
|
|
82
|
-
* - `next()` is called once with the initial results, and again any time the results change.
|
|
83
|
-
*
|
|
84
|
-
* @param next Observer with `next`, `error`, or `complete` methods or a `next()` dispatcher.
|
|
85
|
-
* @return Function that ends the subscription.
|
|
86
|
-
*/
|
|
87
|
-
subscribe(next: Observer<Results<T>> | Dispatcher<[Results<T>]>): Unsubscriber;
|
|
88
|
-
/**
|
|
89
|
-
* Set all matching documents to the same exact value.
|
|
90
|
-
*
|
|
91
|
-
* @param data Complete data to set the document to.
|
|
92
|
-
* @return Nothing (possibly promised).
|
|
93
|
-
*/
|
|
94
|
-
set(data: T): number | PromiseLike<number>;
|
|
95
|
-
/**
|
|
96
|
-
* Update all matching documents with the same partial value.
|
|
97
|
-
*
|
|
98
|
-
* @param updates `Update` instance or set of updates to apply to every matching document.
|
|
99
|
-
* @return Nothing (possibly promised).
|
|
100
|
-
*/
|
|
101
|
-
update(updates: DataUpdate<T> | PropUpdates<T>): number | PromiseLike<number>;
|
|
102
|
-
/**
|
|
103
|
-
* Delete all matching documents.
|
|
104
|
-
* @return Nothing (possibly promised).
|
|
105
|
-
*/
|
|
106
|
-
delete(): number | PromiseLike<number>;
|
|
107
|
-
/** Iterate over the resuls (will throw `Promise` if the results are asynchronous). */
|
|
108
|
-
[Symbol.iterator](): Iterator<Entry<T>, void>;
|
|
109
|
-
/** Validate a set of results for this query reference. */
|
|
110
|
-
validate(unsafeEntries: Entries): Entries<T>;
|
|
111
|
-
toString(): string;
|
|
112
|
-
}
|
|
113
|
-
/** Get the data for a document from a result for that document. */
|
|
114
|
-
export declare function getQueryData<T extends Data>(entries: Entries<T>, ref: DatabaseQuery<T>): DocumentData<T>;
|
|
115
|
-
/** Get the data for a document from a result for that document. */
|
|
116
|
-
export declare function getQueryResult<T extends Data>(entries: Entries<T>, ref: DatabaseQuery<T>): DocumentResult<T>;
|
|
117
|
-
/** A document reference within a specific database. */
|
|
118
|
-
export declare class DatabaseDocument<T extends Data = Data> implements Observable<Result<T>>, Validatable<T> {
|
|
119
|
-
readonly db: Database;
|
|
120
|
-
readonly validator: Validator<T>;
|
|
121
|
-
readonly collection: string;
|
|
122
|
-
readonly id: string;
|
|
123
|
-
constructor(db: Database, validator: Validator<T>, collection: string, id: string);
|
|
124
|
-
/** Create a query on this document's collection. */
|
|
125
|
-
query(filters?: FilterProps<T>, sorts?: SortKeys<T>, limit?: number | null): DatabaseQuery<T>;
|
|
126
|
-
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
|
127
|
-
get optional(): DatabaseQuery<T>;
|
|
128
|
-
/**
|
|
129
|
-
* Does this document exist?
|
|
130
|
-
* @return `true` if a document exists or `false` otherwise (possibly promised).
|
|
131
|
-
*/
|
|
132
|
-
get exists(): boolean | PromiseLike<boolean>;
|
|
133
|
-
/**
|
|
134
|
-
* Get the result of this document.
|
|
135
|
-
* @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
|
|
136
|
-
*/
|
|
137
|
-
get result(): DocumentResult<T> | PromiseLike<DocumentResult<T>>;
|
|
138
|
-
/**
|
|
139
|
-
* Get the data of this document.
|
|
140
|
-
* - Useful for destructuring, e.g. `{ name, title } = await documentThatMustExist.asyncData`
|
|
141
|
-
*
|
|
142
|
-
* @return Document's data (possibly promised).
|
|
143
|
-
* @throws RequiredError if the document's result was undefined.
|
|
144
|
-
*/
|
|
145
|
-
get data(): DocumentData<T> | PromiseLike<DocumentData<T>>;
|
|
146
|
-
/**
|
|
147
|
-
* Subscribe to the result of this document (indefinitely).
|
|
148
|
-
* - `next()` is called once with the initial result, and again any time the result changes.
|
|
149
|
-
*
|
|
150
|
-
* @param next Observer with `next`, `error`, or `complete` methods or a `next()` dispatcher.
|
|
151
|
-
* @return Function that ends the subscription.
|
|
152
|
-
*/
|
|
153
|
-
subscribe(next: Observer<Result<T>> | Dispatcher<[Result<T>]>): Unsubscriber;
|
|
154
|
-
/** Set the complete data of this document. */
|
|
155
|
-
set(data: T): void | PromiseLike<void>;
|
|
156
|
-
/** Update this document. */
|
|
157
|
-
update(updates: DataUpdate<T> | PropUpdates<T>): void | PromiseLike<void>;
|
|
158
|
-
/** Delete this document. */
|
|
159
|
-
delete(): void | PromiseLike<void>;
|
|
160
|
-
/** Validate data for this query reference. */
|
|
161
|
-
validate(unsafeData: Data): T;
|
|
162
|
-
toString(): string;
|
|
163
|
-
}
|
|
164
|
-
/** Database data embeds the corresponding `Document` instance and string ID into the data. */
|
|
165
|
-
export declare type DocumentData<T extends Data> = T & {
|
|
166
|
-
id: string;
|
|
167
|
-
doc: DatabaseDocument<T>;
|
|
168
|
-
};
|
|
169
|
-
/** Get the data for a document from a result for that document. */
|
|
170
|
-
export declare function getDocumentData<T extends Data>(result: Result<T>, ref: DatabaseDocument<T>): DocumentData<T>;
|
|
171
|
-
/** Database result embeds the corresponding `Document` instance and string ID into the result. */
|
|
172
|
-
export declare type DocumentResult<T extends Data> = DocumentData<T> | null;
|
|
173
|
-
/** Get the data for a document from a result for that document. */
|
|
174
|
-
export declare function getDocumentResult<T extends Data>(result: Result<T>, ref: DatabaseDocument<T>): DocumentResult<T>;
|
package/db/Database.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { Feedback, InvalidFeedback } from "../feedback/index.js";
|
|
4
|
-
import { Filters, Sorts, Query, Filter } from "../query/index.js";
|
|
5
|
-
import { DocumentRequiredError, DocumentValidationError, QueryRequiredError, QueryValidationError } from "./errors.js";
|
|
1
|
+
import { Filters, Sorts } from "../query/index.js";
|
|
2
|
+
import { DocumentReference, QueryReference } from "./Reference.js";
|
|
6
3
|
/**
|
|
7
4
|
* Combines a database model and a provider.
|
|
8
5
|
*
|
|
@@ -18,11 +15,11 @@ export class Database {
|
|
|
18
15
|
}
|
|
19
16
|
/** Create a query on a collection in this model. */
|
|
20
17
|
query(collection, filters, sorts, limit) {
|
|
21
|
-
return new
|
|
18
|
+
return new QueryReference(this, this.validators[collection], collection, filters && Filters.on(filters), sorts && Sorts.on(sorts), limit);
|
|
22
19
|
}
|
|
23
20
|
/** Reference a document in a collection in this model. */
|
|
24
21
|
doc(collection, id) {
|
|
25
|
-
return new
|
|
22
|
+
return new DocumentReference(this, this.validators[collection], collection, id);
|
|
26
23
|
}
|
|
27
24
|
/**
|
|
28
25
|
* Create a new document with a random ID.
|
|
@@ -36,233 +33,3 @@ export class Database {
|
|
|
36
33
|
return this.query(collection).add(data);
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
|
-
/** A documents reference within a specific database. */
|
|
40
|
-
export class DatabaseQuery extends Query {
|
|
41
|
-
constructor(db, validator, collection, filters, sorts, limit) {
|
|
42
|
-
super(filters, sorts, limit);
|
|
43
|
-
this.db = db;
|
|
44
|
-
this.validator = validator;
|
|
45
|
-
this.collection = collection;
|
|
46
|
-
}
|
|
47
|
-
/** Reference a document in this query's collection. */
|
|
48
|
-
doc(id) {
|
|
49
|
-
return new DatabaseDocument(this.db, this.validator, this.collection, id);
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Create a new document with a random ID.
|
|
53
|
-
* - Created document is guaranteed to have a unique ID.
|
|
54
|
-
*
|
|
55
|
-
* @param data Complete data to set the document to.
|
|
56
|
-
* @return String ID for the created document (possibly promised).
|
|
57
|
-
*/
|
|
58
|
-
add(data) {
|
|
59
|
-
return this.db.provider.add(this, data);
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Get an iterable that yields the results of this entry.
|
|
63
|
-
* @return Map containing the results.
|
|
64
|
-
*/
|
|
65
|
-
get entries() {
|
|
66
|
-
return this.db.provider.getQuery(this);
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Get an iterable that yields the results of this entry.
|
|
70
|
-
* @return Map containing the results.
|
|
71
|
-
*/
|
|
72
|
-
get results() {
|
|
73
|
-
return callAsync(getMap, this.db.provider.getQuery(this));
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Count the number of results of this set of documents.
|
|
77
|
-
* @return Number of documents matching the query (possibly promised).
|
|
78
|
-
*/
|
|
79
|
-
get count() {
|
|
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.db.provider.getQuery(this.max(1)));
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Get an entry for the first document matched by this query or `undefined` if this query has no results.
|
|
91
|
-
*
|
|
92
|
-
* @return Entry in `[id, data]` format for the first document.
|
|
93
|
-
* @throws RequiredError if there were no results for this query.
|
|
94
|
-
*/
|
|
95
|
-
get result() {
|
|
96
|
-
return callAsync(getQueryResult, this.db.provider.getQuery(this.max(1)), this);
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Get an entry for the first document matched by this query.
|
|
100
|
-
*
|
|
101
|
-
* @return Entry in `[id, data]` format for the first document.
|
|
102
|
-
* @throws RequiredError if there were no results for this query.
|
|
103
|
-
*/
|
|
104
|
-
get data() {
|
|
105
|
-
return callAsync(getQueryData, this.db.provider.getQuery(this.max(1)), this);
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Subscribe to all matching documents.
|
|
109
|
-
* - `next()` is called once with the initial results, and again any time the results change.
|
|
110
|
-
*
|
|
111
|
-
* @param next Observer with `next`, `error`, or `complete` methods or a `next()` dispatcher.
|
|
112
|
-
* @return Function that ends the subscription.
|
|
113
|
-
*/
|
|
114
|
-
subscribe(next) {
|
|
115
|
-
return this.db.provider.subscribeQuery(this, new ResultsObserver(typeof next === "function" ? { next } : next));
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Set all matching documents to the same exact value.
|
|
119
|
-
*
|
|
120
|
-
* @param data Complete data to set the document to.
|
|
121
|
-
* @return Nothing (possibly promised).
|
|
122
|
-
*/
|
|
123
|
-
set(data) {
|
|
124
|
-
return this.db.provider.setQuery(this, data);
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Update all matching documents with the same partial value.
|
|
128
|
-
*
|
|
129
|
-
* @param updates `Update` instance or set of updates to apply to every matching document.
|
|
130
|
-
* @return Nothing (possibly promised).
|
|
131
|
-
*/
|
|
132
|
-
update(updates) {
|
|
133
|
-
return this.db.provider.updateQuery(this, updates instanceof DataUpdate ? updates : new DataUpdate(updates));
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Delete all matching documents.
|
|
137
|
-
* @return Nothing (possibly promised).
|
|
138
|
-
*/
|
|
139
|
-
delete() {
|
|
140
|
-
return this.db.provider.deleteQuery(this);
|
|
141
|
-
}
|
|
142
|
-
/** Iterate over the resuls (will throw `Promise` if the results are asynchronous). */
|
|
143
|
-
[Symbol.iterator]() {
|
|
144
|
-
return throwAsync(this.entries)[Symbol.iterator]();
|
|
145
|
-
}
|
|
146
|
-
/** Validate a set of results for this query reference. */
|
|
147
|
-
*validate(unsafeEntries) {
|
|
148
|
-
let invalid = false;
|
|
149
|
-
const details = {};
|
|
150
|
-
for (const [id, unsafeValue] of unsafeEntries) {
|
|
151
|
-
try {
|
|
152
|
-
yield [id, validate(unsafeValue, this.validator)];
|
|
153
|
-
}
|
|
154
|
-
catch (thrown) {
|
|
155
|
-
if (!(thrown instanceof Feedback))
|
|
156
|
-
throw thrown;
|
|
157
|
-
invalid = true;
|
|
158
|
-
details[id] = thrown;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
if (invalid)
|
|
162
|
-
throw new QueryValidationError(this, new InvalidFeedback("Invalid results", details));
|
|
163
|
-
}
|
|
164
|
-
// Override to include the collection name.
|
|
165
|
-
toString() {
|
|
166
|
-
return `${this.collection}?${super.toString()}`;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
/** Get the data for a document from a result for that document. */
|
|
170
|
-
export function getQueryData(entries, ref) {
|
|
171
|
-
const data = getQueryResult(entries, ref);
|
|
172
|
-
if (data)
|
|
173
|
-
return data;
|
|
174
|
-
throw new QueryRequiredError(ref);
|
|
175
|
-
}
|
|
176
|
-
/** Get the data for a document from a result for that document. */
|
|
177
|
-
export function getQueryResult(entries, ref) {
|
|
178
|
-
const first = getFirstItem(entries);
|
|
179
|
-
if (first)
|
|
180
|
-
return getDocumentData(first[1], ref.doc(first[0]));
|
|
181
|
-
return null;
|
|
182
|
-
}
|
|
183
|
-
/** A document reference within a specific database. */
|
|
184
|
-
export class DatabaseDocument {
|
|
185
|
-
constructor(db, validator, collection, id) {
|
|
186
|
-
this.db = db;
|
|
187
|
-
this.validator = validator;
|
|
188
|
-
this.collection = collection;
|
|
189
|
-
this.id = id;
|
|
190
|
-
}
|
|
191
|
-
/** Create a query on this document's collection. */
|
|
192
|
-
query(filters, sorts, limit) {
|
|
193
|
-
return new DatabaseQuery(this.db, this.validator, this.collection, filters && Filters.on(filters), sorts && Sorts.on(sorts), limit);
|
|
194
|
-
}
|
|
195
|
-
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
|
196
|
-
get optional() {
|
|
197
|
-
return new DatabaseQuery(this.db, this.validator, this.collection, new Filters(new Filter("id", "IS", this.id)));
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Does this document exist?
|
|
201
|
-
* @return `true` if a document exists or `false` otherwise (possibly promised).
|
|
202
|
-
*/
|
|
203
|
-
get exists() {
|
|
204
|
-
return callAsync(Boolean, this.db.provider.get(this));
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* Get the result of this document.
|
|
208
|
-
* @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
|
|
209
|
-
*/
|
|
210
|
-
get result() {
|
|
211
|
-
return callAsync(getDocumentResult, this.db.provider.get(this), this);
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Get the data of this document.
|
|
215
|
-
* - Useful for destructuring, e.g. `{ name, title } = await documentThatMustExist.asyncData`
|
|
216
|
-
*
|
|
217
|
-
* @return Document's data (possibly promised).
|
|
218
|
-
* @throws RequiredError if the document's result was undefined.
|
|
219
|
-
*/
|
|
220
|
-
get data() {
|
|
221
|
-
return callAsync(getDocumentData, this.db.provider.get(this), this);
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Subscribe to the result of this document (indefinitely).
|
|
225
|
-
* - `next()` is called once with the initial result, and again any time the result changes.
|
|
226
|
-
*
|
|
227
|
-
* @param next Observer with `next`, `error`, or `complete` methods or a `next()` dispatcher.
|
|
228
|
-
* @return Function that ends the subscription.
|
|
229
|
-
*/
|
|
230
|
-
subscribe(next) {
|
|
231
|
-
return this.db.provider.subscribe(this, typeof next === "function" ? { next } : next);
|
|
232
|
-
}
|
|
233
|
-
/** Set the complete data of this document. */
|
|
234
|
-
set(data) {
|
|
235
|
-
return this.db.provider.set(this, data);
|
|
236
|
-
}
|
|
237
|
-
/** Update this document. */
|
|
238
|
-
update(updates) {
|
|
239
|
-
return this.db.provider.update(this, updates instanceof DataUpdate ? updates : new DataUpdate(updates));
|
|
240
|
-
}
|
|
241
|
-
/** Delete this document. */
|
|
242
|
-
delete() {
|
|
243
|
-
return this.db.provider.delete(this);
|
|
244
|
-
}
|
|
245
|
-
/** Validate data for this query reference. */
|
|
246
|
-
validate(unsafeData) {
|
|
247
|
-
try {
|
|
248
|
-
return validate(unsafeData, this.validator);
|
|
249
|
-
}
|
|
250
|
-
catch (thrown) {
|
|
251
|
-
throw thrown instanceof Feedback ? new DocumentValidationError(this, thrown) : thrown;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
// Implement toString()
|
|
255
|
-
toString() {
|
|
256
|
-
return `${this.collection}/${this.id}`;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
/** Get the data for a document from a result for that document. */
|
|
260
|
-
export function getDocumentData(result, ref) {
|
|
261
|
-
if (result)
|
|
262
|
-
return { ...result, id: ref.id, doc: ref };
|
|
263
|
-
throw new DocumentRequiredError(ref);
|
|
264
|
-
}
|
|
265
|
-
/** Get the data for a document from a result for that document. */
|
|
266
|
-
export function getDocumentResult(result, ref) {
|
|
267
|
-
return result ? getDocumentData(result, ref) : null;
|
|
268
|
-
}
|
package/db/Operation.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PropUpdates, Update } from "../update/index.js";
|
|
2
2
|
import { ImmutableArray, Nullish, Data, Key } from "../util/index.js";
|
|
3
|
-
import type { Database
|
|
3
|
+
import type { Database } from "./Database.js";
|
|
4
|
+
import type { DocumentReference, QueryReference } from "./Reference.js";
|
|
4
5
|
/** Represent a write operation on a database. */
|
|
5
6
|
export declare abstract class Operation {
|
|
6
7
|
/** Run this operation and return the result operation. */
|
|
@@ -21,9 +22,9 @@ export declare class Operations extends Operation {
|
|
|
21
22
|
/** Represent a add operation made to a collection in a database. */
|
|
22
23
|
export declare class AddOperation<T extends Data> extends Operation {
|
|
23
24
|
/** Create a new add operation on a collection. */
|
|
24
|
-
static on<X extends Data>({ collection }:
|
|
25
|
+
static on<X extends Data>({ collection }: DocumentReference<X> | QueryReference<X>, data: X): AddOperation<X>;
|
|
25
26
|
/** Run a new add operation on a collection and return the result operation. */
|
|
26
|
-
static run<X extends Data>({ collection, db }:
|
|
27
|
+
static run<X extends Data>({ collection, db }: DocumentReference<X> | QueryReference<X>, data: X): Promise<SetOperation<X>>;
|
|
27
28
|
readonly collection: string;
|
|
28
29
|
readonly data: T;
|
|
29
30
|
constructor(collection: string, data: T);
|
|
@@ -34,9 +35,9 @@ export declare class AddOperation<T extends Data> extends Operation {
|
|
|
34
35
|
/** Represent a set operation made to a single document in a database. */
|
|
35
36
|
export declare class SetOperation<T extends Data> extends Operation {
|
|
36
37
|
/** Create a new add operation on a collection. */
|
|
37
|
-
static on<X extends Data>({ collection, id }:
|
|
38
|
+
static on<X extends Data>({ collection, id }: DocumentReference<X>, data: X): SetOperation<X>;
|
|
38
39
|
/** Run a new set operation on a collection and return the result operation. */
|
|
39
|
-
static run<X extends Data>({ collection, id, db }:
|
|
40
|
+
static run<X extends Data>({ collection, id, db }: DocumentReference<X>, data: X): Promise<SetOperation<X>>;
|
|
40
41
|
readonly collection: string;
|
|
41
42
|
readonly id: string;
|
|
42
43
|
readonly data: T;
|
|
@@ -48,9 +49,9 @@ export declare class SetOperation<T extends Data> extends Operation {
|
|
|
48
49
|
/** Represent an update operation made to a single document in a database. */
|
|
49
50
|
export declare class UpdateOperation<T extends Data> extends Operation {
|
|
50
51
|
/** Create a new update operation on a document. */
|
|
51
|
-
static on<X extends Data>({ collection, id }:
|
|
52
|
+
static on<X extends Data>({ collection, id }: DocumentReference<X>, updates: PropUpdates<X>): UpdateOperation<X>;
|
|
52
53
|
/** Run a new set operation on a collection and return the result operation. */
|
|
53
|
-
static run<X extends Data>({ collection, id, db }:
|
|
54
|
+
static run<X extends Data>({ collection, id, db }: DocumentReference<X>, updates: PropUpdates<X>): Promise<UpdateOperation<X>>;
|
|
54
55
|
readonly collection: string;
|
|
55
56
|
readonly id: string;
|
|
56
57
|
readonly updates: PropUpdates<T>;
|
|
@@ -62,9 +63,9 @@ export declare class UpdateOperation<T extends Data> extends Operation {
|
|
|
62
63
|
/** Represent a delete operation made to a single document in a database. */
|
|
63
64
|
export declare class DeleteOperation extends Operation {
|
|
64
65
|
/** Create a new delete operation on a document. */
|
|
65
|
-
static on<X extends Data>({ collection, id }:
|
|
66
|
+
static on<X extends Data>({ collection, id }: DocumentReference<X>): DeleteOperation;
|
|
66
67
|
/** Run a new delete operation on a document. */
|
|
67
|
-
static run<X extends Data>({ collection, id, db }:
|
|
68
|
+
static run<X extends Data>({ collection, id, db }: DocumentReference<X>): Promise<DeleteOperation>;
|
|
68
69
|
readonly collection: string;
|
|
69
70
|
readonly id: string;
|
|
70
71
|
constructor(collection: string, id: string);
|
package/db/PaginationState.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Results, Entry, Entries, Data } from "../util/index.js";
|
|
2
2
|
import { BooleanState, State } from "../stream/index.js";
|
|
3
|
-
import {
|
|
3
|
+
import { QueryReference } from "./Reference.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.
|
|
@@ -10,10 +10,10 @@ export declare class PaginationState<T extends Data> extends State<Results<T>> i
|
|
|
10
10
|
/** Whether this pagination is currently loading results. */
|
|
11
11
|
readonly busy: BooleanState;
|
|
12
12
|
/** Reference to the query this pagination is paginating. */
|
|
13
|
-
readonly ref:
|
|
13
|
+
readonly ref: QueryReference<T>;
|
|
14
14
|
/** Limit set on this pagination's query. */
|
|
15
15
|
readonly limit: number;
|
|
16
|
-
constructor(ref:
|
|
16
|
+
constructor(ref: QueryReference<T>);
|
|
17
17
|
/**
|
|
18
18
|
* Load more results after the end.
|
|
19
19
|
* - Promise that needs to be handled.
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { Entry, Observable, Observer, Result, Unsubscriber, Results, Validatable, Validator, Data, Entries, Dispatcher } from "../util/index.js";
|
|
2
|
+
import { DataUpdate, PropUpdates } from "../update/index.js";
|
|
3
|
+
import { Filters, Sorts, Query, FilterProps, SortKeys } from "../query/index.js";
|
|
4
|
+
import { DocumentData, DocumentResult } from "./helpers.js";
|
|
5
|
+
import type { Database } from "./Database.js";
|
|
6
|
+
/** A refence to a location in a database. */
|
|
7
|
+
export interface Reference {
|
|
8
|
+
readonly db: Database;
|
|
9
|
+
toString(): string;
|
|
10
|
+
}
|
|
11
|
+
/** A query reference within a specific database. */
|
|
12
|
+
export declare class QueryReference<T extends Data = Data> extends Query<T> implements Observable<Results<T>>, Validatable<Entries<T>>, Iterable<Entry<T>>, Reference {
|
|
13
|
+
readonly db: Database;
|
|
14
|
+
readonly validator: Validator<T>;
|
|
15
|
+
readonly collection: string;
|
|
16
|
+
constructor(db: Database, validator: Validator<T>, collection: string, filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null);
|
|
17
|
+
/** Reference a document in this query's collection. */
|
|
18
|
+
doc(id: string): DocumentReference<T>;
|
|
19
|
+
/**
|
|
20
|
+
* Create a new document with a random ID.
|
|
21
|
+
* - Created document is guaranteed to have a unique ID.
|
|
22
|
+
*
|
|
23
|
+
* @param data Complete data to set the document to.
|
|
24
|
+
* @return String ID for the created document (possibly promised).
|
|
25
|
+
*/
|
|
26
|
+
add(data: T): string | PromiseLike<string>;
|
|
27
|
+
/**
|
|
28
|
+
* Get an iterable that yields the results of this entry.
|
|
29
|
+
* @return Map containing the results.
|
|
30
|
+
*/
|
|
31
|
+
get entries(): Entries<T> | PromiseLike<Entries<T>>;
|
|
32
|
+
/**
|
|
33
|
+
* Get an iterable that yields the results of this entry.
|
|
34
|
+
* @return Map containing the results.
|
|
35
|
+
*/
|
|
36
|
+
get results(): Results<T> | PromiseLike<Results<T>>;
|
|
37
|
+
/**
|
|
38
|
+
* Count the number of results of this set of documents.
|
|
39
|
+
* @return Number of documents matching the query (possibly promised).
|
|
40
|
+
*/
|
|
41
|
+
get count(): number | PromiseLike<number>;
|
|
42
|
+
/**
|
|
43
|
+
* Does at least one document exist for this query?
|
|
44
|
+
* @return `true` if a document exists or `false` otherwise (possibly promised).
|
|
45
|
+
*/
|
|
46
|
+
get exists(): boolean | PromiseLike<boolean>;
|
|
47
|
+
/**
|
|
48
|
+
* Get an entry for the first document matched by this query or `undefined` if this query has no results.
|
|
49
|
+
*
|
|
50
|
+
* @return Entry in `[id, data]` format for the first document.
|
|
51
|
+
* @throws RequiredError if there were no results for this query.
|
|
52
|
+
*/
|
|
53
|
+
get result(): DocumentResult<T> | PromiseLike<DocumentResult<T>>;
|
|
54
|
+
/**
|
|
55
|
+
* Get an entry for the first document matched by this query.
|
|
56
|
+
*
|
|
57
|
+
* @return Entry in `[id, data]` format for the first document.
|
|
58
|
+
* @throws RequiredError if there were no results for this query.
|
|
59
|
+
*/
|
|
60
|
+
get data(): DocumentData<T> | PromiseLike<DocumentData<T>>;
|
|
61
|
+
/**
|
|
62
|
+
* Subscribe to all matching documents.
|
|
63
|
+
* - `next()` is called once with the initial results, and again any time the results change.
|
|
64
|
+
*
|
|
65
|
+
* @param next Observer with `next`, `error`, or `complete` methods or a `next()` dispatcher.
|
|
66
|
+
* @return Function that ends the subscription.
|
|
67
|
+
*/
|
|
68
|
+
subscribe(next: Observer<Results<T>> | Dispatcher<[Results<T>]>): Unsubscriber;
|
|
69
|
+
/**
|
|
70
|
+
* Set all matching documents to the same exact value.
|
|
71
|
+
*
|
|
72
|
+
* @param data Complete data to set the document to.
|
|
73
|
+
* @return Nothing (possibly promised).
|
|
74
|
+
*/
|
|
75
|
+
set(data: T): number | PromiseLike<number>;
|
|
76
|
+
/**
|
|
77
|
+
* Update all matching documents with the same partial value.
|
|
78
|
+
*
|
|
79
|
+
* @param updates `Update` instance or set of updates to apply to every matching document.
|
|
80
|
+
* @return Nothing (possibly promised).
|
|
81
|
+
*/
|
|
82
|
+
update(updates: DataUpdate<T> | PropUpdates<T>): number | PromiseLike<number>;
|
|
83
|
+
/**
|
|
84
|
+
* Delete all matching documents.
|
|
85
|
+
* @return Nothing (possibly promised).
|
|
86
|
+
*/
|
|
87
|
+
delete(): number | PromiseLike<number>;
|
|
88
|
+
/** Iterate over the resuls (will throw `Promise` if the results are asynchronous). */
|
|
89
|
+
[Symbol.iterator](): Iterator<Entry<T>, void>;
|
|
90
|
+
/** Validate a set of results for this query reference. */
|
|
91
|
+
validate(unsafeEntries: Entries): Entries<T>;
|
|
92
|
+
toString(): string;
|
|
93
|
+
}
|
|
94
|
+
/** A document reference within a specific database. */
|
|
95
|
+
export declare class DocumentReference<T extends Data = Data> implements Observable<Result<T>>, Validatable<T>, Reference {
|
|
96
|
+
readonly db: Database;
|
|
97
|
+
readonly validator: Validator<T>;
|
|
98
|
+
readonly collection: string;
|
|
99
|
+
readonly id: string;
|
|
100
|
+
constructor(db: Database, validator: Validator<T>, collection: string, id: string);
|
|
101
|
+
/** Create a query on this document's collection. */
|
|
102
|
+
query(filters?: FilterProps<T>, sorts?: SortKeys<T>, limit?: number | null): QueryReference<T>;
|
|
103
|
+
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
|
104
|
+
get optional(): QueryReference<T>;
|
|
105
|
+
/**
|
|
106
|
+
* Does this document exist?
|
|
107
|
+
* @return `true` if a document exists or `false` otherwise (possibly promised).
|
|
108
|
+
*/
|
|
109
|
+
get exists(): boolean | PromiseLike<boolean>;
|
|
110
|
+
/**
|
|
111
|
+
* Get the result of this document.
|
|
112
|
+
* @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
|
|
113
|
+
*/
|
|
114
|
+
get result(): DocumentResult<T> | PromiseLike<DocumentResult<T>>;
|
|
115
|
+
/**
|
|
116
|
+
* Get the data of this document.
|
|
117
|
+
* - Useful for destructuring, e.g. `{ name, title } = await documentThatMustExist.asyncData`
|
|
118
|
+
*
|
|
119
|
+
* @return Document's data (possibly promised).
|
|
120
|
+
* @throws RequiredError if the document's result was undefined.
|
|
121
|
+
*/
|
|
122
|
+
get data(): DocumentData<T> | PromiseLike<DocumentData<T>>;
|
|
123
|
+
/**
|
|
124
|
+
* Subscribe to the result of this document (indefinitely).
|
|
125
|
+
* - `next()` is called once with the initial result, and again any time the result changes.
|
|
126
|
+
*
|
|
127
|
+
* @param next Observer with `next`, `error`, or `complete` methods or a `next()` dispatcher.
|
|
128
|
+
* @return Function that ends the subscription.
|
|
129
|
+
*/
|
|
130
|
+
subscribe(next: Observer<DocumentResult<T>> | Dispatcher<[DocumentResult<T>]>): Unsubscriber;
|
|
131
|
+
/** Set the complete data of this document. */
|
|
132
|
+
set(data: T): void | PromiseLike<void>;
|
|
133
|
+
/** Update this document. */
|
|
134
|
+
update(updates: DataUpdate<T> | PropUpdates<T>): void | PromiseLike<void>;
|
|
135
|
+
/** Delete this document. */
|
|
136
|
+
delete(): void | PromiseLike<void>;
|
|
137
|
+
/** Validate data for this query reference. */
|
|
138
|
+
validate(unsafeData: Data): T;
|
|
139
|
+
toString(): string;
|
|
140
|
+
}
|