shelving 1.40.0 → 1.44.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 +14 -16
- package/db/Database.js +37 -49
- package/db/Operation.d.ts +82 -0
- package/db/Operation.js +132 -0
- package/db/index.d.ts +1 -1
- package/db/index.js +1 -1
- package/package.json +6 -6
- package/react/useDocument.js +2 -2
- package/react/useQuery.js +3 -3
- package/update/ArrayUpdate.d.ts +4 -4
- package/update/ArrayUpdate.js +4 -4
- package/update/DataUpdate.d.ts +1 -1
- package/update/DataUpdate.js +1 -1
- package/update/ObjectUpdate.d.ts +4 -4
- package/update/ObjectUpdate.js +4 -4
- package/util/async.d.ts +28 -4
- package/util/async.js +36 -3
- package/util/date.d.ts +49 -18
- package/util/date.js +92 -85
- package/util/number.d.ts +56 -9
- package/util/number.js +76 -13
- package/util/string.d.ts +6 -0
- package/util/string.js +6 -0
- package/util/units.d.ts +37 -9
- package/util/units.js +60 -89
- package/db/Write.d.ts +0 -50
- package/db/Write.js +0 -73
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
|
*
|
|
@@ -18,17 +17,22 @@ export declare class Database<V extends Validators<Datas> = Validators<Datas>> {
|
|
|
18
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
19
|
doc<K extends Key<V>>(collection: K, id: string): DatabaseDocument<ValidatorType<V[K]>>;
|
|
21
|
-
/**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
31
|
export declare class DatabaseQuery<T extends Data = Data> extends Query<T> implements Observable<Results<T>>, Validatable<Entries<T>>, Iterable<Entry<T>> {
|
|
28
|
-
readonly
|
|
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
37
|
doc(id: string): DatabaseDocument<T>;
|
|
34
38
|
/**
|
|
@@ -110,11 +114,11 @@ export declare class DatabaseQuery<T extends Data = Data> extends Query<T> imple
|
|
|
110
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
116
|
export declare class DatabaseDocument<T extends Data = Data> implements Observable<Result<T>>, Validatable<T> {
|
|
113
|
-
readonly
|
|
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
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). */
|
|
@@ -151,12 +155,6 @@ export declare class DatabaseDocument<T extends Data = Data> implements Observab
|
|
|
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: 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;
|
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 DatabaseQuery(this
|
|
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 DatabaseDocument(this
|
|
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
40
|
export class DatabaseQuery extends Query {
|
|
41
|
-
constructor(
|
|
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 DatabaseDocument(this.
|
|
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 DatabaseQuery 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 DatabaseQuery 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 DatabaseQuery 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 DatabaseQuery 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 DatabaseQuery 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 DatabaseQuery 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 DatabaseQuery 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]() {
|
|
@@ -175,33 +175,33 @@ export function getQueryData(entries, ref) {
|
|
|
175
175
|
}
|
|
176
176
|
/** A document reference within a specific database. */
|
|
177
177
|
export class DatabaseDocument {
|
|
178
|
-
constructor(
|
|
179
|
-
this.
|
|
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 DatabaseQuery(this.
|
|
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 DatabaseQuery(this.
|
|
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 DatabaseDocument {
|
|
|
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 DatabaseDocument {
|
|
|
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/index.d.ts
CHANGED
package/db/index.js
CHANGED
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"state-management",
|
|
12
12
|
"query-builder"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.44.0",
|
|
15
15
|
"repository": "https://github.com/dhoulb/shelving",
|
|
16
16
|
"author": "Dave Houlbrooke <dave@shax.com>",
|
|
17
17
|
"license": "0BSD",
|
|
@@ -63,14 +63,14 @@
|
|
|
63
63
|
"@types/jest": "^27.4.0",
|
|
64
64
|
"@types/react": "^17.0.38",
|
|
65
65
|
"@types/react-dom": "^17.0.11",
|
|
66
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
67
|
-
"@typescript-eslint/parser": "^5.
|
|
66
|
+
"@typescript-eslint/eslint-plugin": "^5.9.0",
|
|
67
|
+
"@typescript-eslint/parser": "^5.9.0",
|
|
68
68
|
"eslint": "^8.6.0",
|
|
69
69
|
"eslint-config-prettier": "^8.3.0",
|
|
70
|
-
"eslint-plugin-import": "^2.25.
|
|
70
|
+
"eslint-plugin-import": "^2.25.4",
|
|
71
71
|
"eslint-plugin-prettier": "^4.0.0",
|
|
72
|
-
"firebase": "^9.6.
|
|
73
|
-
"jest": "^27.4.
|
|
72
|
+
"firebase": "^9.6.2",
|
|
73
|
+
"jest": "^27.4.7",
|
|
74
74
|
"jest-ts-webcompat-resolver": "^1.0.0",
|
|
75
75
|
"prettier": "^2.5.1",
|
|
76
76
|
"react": "^17.0.2",
|
package/react/useDocument.js
CHANGED
|
@@ -30,13 +30,13 @@ export function useAsyncDocument(ref, maxAge = 1000) {
|
|
|
30
30
|
function getCachedResult(ref) {
|
|
31
31
|
if (!ref)
|
|
32
32
|
return undefined;
|
|
33
|
-
const provider = findSourceProvider(ref.provider, CacheProvider);
|
|
33
|
+
const provider = findSourceProvider(ref.db.provider, CacheProvider);
|
|
34
34
|
return provider.isCached(ref) ? provider.cache.get(ref) : NOVALUE;
|
|
35
35
|
}
|
|
36
36
|
/** Effect that subscribes a component to the cache for a reference. */
|
|
37
37
|
function subscribeEffect(ref, maxAge, next, error) {
|
|
38
38
|
if (ref) {
|
|
39
|
-
const provider = findSourceProvider(ref.provider, CacheProvider);
|
|
39
|
+
const provider = findSourceProvider(ref.db.provider, CacheProvider);
|
|
40
40
|
const stopCache = provider.cache.subscribe(ref, { next, error });
|
|
41
41
|
if (maxAge === true) {
|
|
42
42
|
// If `maxAge` is true subscribe to the source for as long as this component is attached.
|
package/react/useQuery.js
CHANGED
|
@@ -30,18 +30,18 @@ export function useAsyncQuery(ref, maxAge = 1000) {
|
|
|
30
30
|
function getCachedResults(ref) {
|
|
31
31
|
if (!ref)
|
|
32
32
|
return undefined;
|
|
33
|
-
const provider = findSourceProvider(ref.provider, CacheProvider);
|
|
33
|
+
const provider = findSourceProvider(ref.db.provider, CacheProvider);
|
|
34
34
|
return provider.isCached(ref) ? getMap(provider.cache.getQuery(ref)) : NOVALUE;
|
|
35
35
|
}
|
|
36
36
|
/** Effect that subscribes a component to the cache for a reference. */
|
|
37
37
|
function subscribeEffect(ref, maxAge, next, error) {
|
|
38
38
|
if (ref) {
|
|
39
|
-
const provider = findSourceProvider(ref.provider, CacheProvider);
|
|
39
|
+
const provider = findSourceProvider(ref.db.provider, CacheProvider);
|
|
40
40
|
const observer = new ResultsObserver({ next, error });
|
|
41
41
|
const stopCache = provider.cache.subscribeQuery(ref, observer);
|
|
42
42
|
if (maxAge === true) {
|
|
43
43
|
// If `maxAge` is true subscribe to the source for as long as this component is attached.
|
|
44
|
-
const stopSource = ref.provider.subscribeQuery(ref, observer);
|
|
44
|
+
const stopSource = ref.db.provider.subscribeQuery(ref, observer);
|
|
45
45
|
return () => {
|
|
46
46
|
stopCache();
|
|
47
47
|
stopSource();
|
package/update/ArrayUpdate.d.ts
CHANGED
|
@@ -3,15 +3,15 @@ import { Update } from "./Update.js";
|
|
|
3
3
|
/** Update that can be applied to an array to add/remove items. */
|
|
4
4
|
export declare class ArrayUpdate<T> extends Update<ImmutableArray<T>> {
|
|
5
5
|
/** Return an array update with an item marked for addition. */
|
|
6
|
-
static
|
|
6
|
+
static with<X>(...adds: X[]): ArrayUpdate<X>;
|
|
7
7
|
/** Return an array update with an item marked for deletion. */
|
|
8
|
-
static
|
|
8
|
+
static without<X>(...deletes: X[]): ArrayUpdate<X>;
|
|
9
9
|
readonly adds: ImmutableArray<T>;
|
|
10
10
|
readonly deletes: ImmutableArray<T>;
|
|
11
11
|
constructor(adds?: ImmutableArray<T>, deletes?: ImmutableArray<T>);
|
|
12
12
|
transform(existing: unknown): ImmutableArray<T>;
|
|
13
13
|
/** Return an array update with an additional item marked for addition. */
|
|
14
|
-
|
|
14
|
+
with(...adds: T[]): this;
|
|
15
15
|
/** Return an array update with an additional item marked for deletion. */
|
|
16
|
-
|
|
16
|
+
without(...deletes: T[]): this;
|
|
17
17
|
}
|
package/update/ArrayUpdate.js
CHANGED
|
@@ -8,11 +8,11 @@ export class ArrayUpdate extends Update {
|
|
|
8
8
|
this.deletes = deletes;
|
|
9
9
|
}
|
|
10
10
|
/** Return an array update with an item marked for addition. */
|
|
11
|
-
static
|
|
11
|
+
static with(...adds) {
|
|
12
12
|
return new ArrayUpdate(adds);
|
|
13
13
|
}
|
|
14
14
|
/** Return an array update with an item marked for deletion. */
|
|
15
|
-
static
|
|
15
|
+
static without(...deletes) {
|
|
16
16
|
return new ArrayUpdate([], deletes);
|
|
17
17
|
}
|
|
18
18
|
transform(existing) {
|
|
@@ -20,11 +20,11 @@ export class ArrayUpdate extends Update {
|
|
|
20
20
|
return withoutItems(withItems(existingArray, this.adds), this.deletes);
|
|
21
21
|
}
|
|
22
22
|
/** Return an array update with an additional item marked for addition. */
|
|
23
|
-
|
|
23
|
+
with(...adds) {
|
|
24
24
|
return { __proto__: Object.getPrototypeOf(this), ...this, adds: [...this.adds, ...adds] };
|
|
25
25
|
}
|
|
26
26
|
/** Return an array update with an additional item marked for deletion. */
|
|
27
|
-
|
|
27
|
+
without(...deletes) {
|
|
28
28
|
return { __proto__: Object.getPrototypeOf(this), ...this, deletes: [...this.deletes, ...deletes] };
|
|
29
29
|
}
|
|
30
30
|
}
|
package/update/DataUpdate.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export declare class DataUpdate<T extends Data> extends Update<T> implements Ite
|
|
|
16
16
|
constructor(props: PropUpdates<T>);
|
|
17
17
|
transform(existing: T): T;
|
|
18
18
|
/** Return a new object with the specified additional transform for a prop. */
|
|
19
|
-
|
|
19
|
+
with<K extends Key<T>>(key: Nullish<K>, value: T[K] | Update<T[K]>): this;
|
|
20
20
|
/** Iterate over the transforms in this object. */
|
|
21
21
|
[Symbol.iterator](): Iterator<Prop<PropUpdates<T>>, void>;
|
|
22
22
|
}
|