shelving 1.42.0 → 1.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/db/Database.d.ts +4 -4
- package/db/Database.js +26 -26
- package/db/Operation.d.ts +13 -4
- package/db/Operation.js +18 -2
- package/package.json +1 -1
- package/react/useDocument.js +2 -2
- package/react/useQuery.js +3 -3
package/db/Database.d.ts
CHANGED
|
@@ -29,10 +29,10 @@ export declare class Database<V extends Validators<Datas> = Validators<Datas>> {
|
|
|
29
29
|
}
|
|
30
30
|
/** A documents reference within a specific database. */
|
|
31
31
|
export declare class DatabaseQuery<T extends Data = Data> extends Query<T> implements Observable<Results<T>>, Validatable<Entries<T>>, Iterable<Entry<T>> {
|
|
32
|
-
readonly
|
|
32
|
+
readonly db: Database;
|
|
33
33
|
readonly validator: Validator<T>;
|
|
34
34
|
readonly collection: string;
|
|
35
|
-
constructor(
|
|
35
|
+
constructor(db: Database, validator: Validator<T>, collection: string, filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null);
|
|
36
36
|
/** Reference a document in this query's collection. */
|
|
37
37
|
doc(id: string): DatabaseDocument<T>;
|
|
38
38
|
/**
|
|
@@ -114,11 +114,11 @@ export declare class DatabaseQuery<T extends Data = Data> extends Query<T> imple
|
|
|
114
114
|
export declare function getQueryData<T extends Data>(entries: Entries<T>, ref: DatabaseQuery<T>): Entry<T>;
|
|
115
115
|
/** A document reference within a specific database. */
|
|
116
116
|
export declare class DatabaseDocument<T extends Data = Data> implements Observable<Result<T>>, Validatable<T> {
|
|
117
|
-
readonly
|
|
117
|
+
readonly db: Database;
|
|
118
118
|
readonly validator: Validator<T>;
|
|
119
119
|
readonly collection: string;
|
|
120
120
|
readonly id: string;
|
|
121
|
-
constructor(
|
|
121
|
+
constructor(db: Database, validator: Validator<T>, collection: string, id: string);
|
|
122
122
|
/** Create a query on this document's collection. */
|
|
123
123
|
query(filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null): DatabaseQuery<T>;
|
|
124
124
|
/** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
|
package/db/Database.js
CHANGED
|
@@ -18,11 +18,11 @@ export class Database {
|
|
|
18
18
|
}
|
|
19
19
|
/** Create a query on a collection in this model. */
|
|
20
20
|
query(collection, filters, sorts, limit) {
|
|
21
|
-
return new DatabaseQuery(this
|
|
21
|
+
return new DatabaseQuery(this, this.validators[collection], collection, filters, sorts, limit);
|
|
22
22
|
}
|
|
23
23
|
/** Reference a document in a collection in this model. */
|
|
24
24
|
doc(collection, id) {
|
|
25
|
-
return new DatabaseDocument(this
|
|
25
|
+
return new DatabaseDocument(this, this.validators[collection], collection, id);
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
28
|
* Create a new document with a random ID.
|
|
@@ -38,15 +38,15 @@ export class Database {
|
|
|
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,19 +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);
|
|
236
|
+
return this.db.provider.delete(this);
|
|
237
237
|
}
|
|
238
238
|
/** Validate data for this query reference. */
|
|
239
239
|
validate(unsafeData) {
|
package/db/Operation.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { ImmutableArray, Nullish, Data, Key } from "../util/index.js";
|
|
|
3
3
|
import type { Database, DatabaseDocument, DatabaseQuery } from "./Database.js";
|
|
4
4
|
/** Represent a write operation on a database. */
|
|
5
5
|
export declare abstract class Operation {
|
|
6
|
+
/** Run this operation and return the result operation. */
|
|
6
7
|
abstract run(db: Database): Promise<Operation>;
|
|
7
8
|
}
|
|
8
9
|
/** Represent a list of write operations on a database run in series. */
|
|
@@ -22,7 +23,9 @@ export declare class Operations extends Operation {
|
|
|
22
23
|
/** Represent a add operation made to a collection in a database. */
|
|
23
24
|
export declare class AddOperation<T extends Data> extends Operation {
|
|
24
25
|
/** Create a new add operation on a collection. */
|
|
25
|
-
static on<X extends Data>({ collection }: DatabaseDocument | DatabaseQuery
|
|
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>>;
|
|
26
29
|
readonly collection: string;
|
|
27
30
|
readonly data: T;
|
|
28
31
|
constructor(collection: string, data: T);
|
|
@@ -33,7 +36,9 @@ export declare class AddOperation<T extends Data> extends Operation {
|
|
|
33
36
|
/** Represent a set operation made to a single document in a database. */
|
|
34
37
|
export declare class SetOperation<T extends Data> extends Operation {
|
|
35
38
|
/** Create a new add operation on a collection. */
|
|
36
|
-
static on<X extends Data>({ collection, id }: DatabaseDocument
|
|
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>>;
|
|
37
42
|
readonly collection: string;
|
|
38
43
|
readonly id: string;
|
|
39
44
|
readonly data: T;
|
|
@@ -45,7 +50,9 @@ export declare class SetOperation<T extends Data> extends Operation {
|
|
|
45
50
|
/** Represent an update operation made to a single document in a database. */
|
|
46
51
|
export declare class UpdateOperation<T extends Data> extends Operation {
|
|
47
52
|
/** Create a new update operation on a document. */
|
|
48
|
-
static on<X extends Data>({ collection, id }: DatabaseDocument<X>, updates
|
|
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>>;
|
|
49
56
|
readonly collection: string;
|
|
50
57
|
readonly id: string;
|
|
51
58
|
readonly updates: PropUpdates<T>;
|
|
@@ -56,8 +63,10 @@ export declare class UpdateOperation<T extends Data> extends Operation {
|
|
|
56
63
|
}
|
|
57
64
|
/** Represent a delete operation made to a single document in a database. */
|
|
58
65
|
export declare class DeleteOperation extends Operation {
|
|
59
|
-
/** Create a new
|
|
66
|
+
/** Create a new delete operation on a document. */
|
|
60
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>;
|
|
61
70
|
readonly collection: string;
|
|
62
71
|
readonly id: string;
|
|
63
72
|
constructor(collection: string, id: string);
|
package/db/Operation.js
CHANGED
|
@@ -32,6 +32,10 @@ export class AddOperation extends Operation {
|
|
|
32
32
|
static on({ collection }, data) {
|
|
33
33
|
return new AddOperation(collection, data);
|
|
34
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
|
+
}
|
|
35
39
|
async run(db) {
|
|
36
40
|
const id = await db.query(this.collection).add(this.data);
|
|
37
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.
|
|
@@ -55,6 +59,10 @@ export class SetOperation extends Operation {
|
|
|
55
59
|
static on({ collection, id }, data) {
|
|
56
60
|
return new SetOperation(collection, id, data);
|
|
57
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
|
+
}
|
|
58
66
|
async run(db) {
|
|
59
67
|
await db.doc(this.collection, this.id).set(this.data);
|
|
60
68
|
return this;
|
|
@@ -75,9 +83,13 @@ export class UpdateOperation extends Operation {
|
|
|
75
83
|
this.updates = updates;
|
|
76
84
|
}
|
|
77
85
|
/** Create a new update operation on a document. */
|
|
78
|
-
static on({ collection, id }, updates
|
|
86
|
+
static on({ collection, id }, updates) {
|
|
79
87
|
return new UpdateOperation(collection, id, updates);
|
|
80
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
|
+
}
|
|
81
93
|
async run(db) {
|
|
82
94
|
await db.doc(this.collection, this.id).update(this.updates);
|
|
83
95
|
return this;
|
|
@@ -96,10 +108,14 @@ export class DeleteOperation extends Operation {
|
|
|
96
108
|
this.collection = collection;
|
|
97
109
|
this.id = id;
|
|
98
110
|
}
|
|
99
|
-
/** Create a new
|
|
111
|
+
/** Create a new delete operation on a document. */
|
|
100
112
|
static on({ collection, id }) {
|
|
101
113
|
return new DeleteOperation(collection, id);
|
|
102
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
|
+
}
|
|
103
119
|
async run(db) {
|
|
104
120
|
await db.doc(this.collection, this.id).delete();
|
|
105
121
|
return this;
|
package/package.json
CHANGED
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();
|