shelving 1.20.0 → 1.20.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/Change.d.ts CHANGED
@@ -1,36 +1,34 @@
1
1
  import { Transforms, Transform } from "../transform/index.js";
2
- import { Datas, ImmutableArray, Key } from "../util/index.js";
3
- import type { Database, DatabaseDocument } from "./Database.js";
2
+ import { ImmutableArray, Data } from "../util/index.js";
3
+ import type { Database, DataDocument } from "./Database.js";
4
4
  /** A single change that can be made to a database. */
5
- export declare abstract class Change<T extends Datas> {
5
+ export declare abstract class Change {
6
6
  /** Apply this change to a database. */
7
- abstract apply(db: Database<T>): void | PromiseLike<void>;
7
+ abstract apply(db: Database): void | PromiseLike<void>;
8
8
  }
9
9
  /** A change that writes a document in a database. */
10
- export declare class Write<C extends Key<D>, D extends Datas> extends Change<D> {
11
- static on<Y extends Key<X>, X extends Datas>({ collection, id }: DatabaseDocument<Y, X>, value: X[Y] | Transform<X[Y]> | undefined): Write<Y, X>;
12
- readonly collection: C;
10
+ export declare class Write<T extends Data> extends Change {
11
+ readonly collection: string;
13
12
  readonly id: string;
14
- readonly value: D[C] | Transform<D[C]> | undefined;
15
- constructor(collection: C, id: string, value: D[C] | Transform<D[C]> | undefined);
16
- apply(db: Database<D>): void | PromiseLike<void>;
13
+ readonly value: Data | Transform<Data> | undefined;
14
+ constructor({ collection, id }: DataDocument<T>, value: T | Transform<T> | undefined);
15
+ apply(db: Database): void | PromiseLike<void>;
17
16
  }
18
17
  /**
19
18
  * Set of writes that can be applied to documents in a database.
20
19
  * - Sets of changes are predictable and repeatable, so unpredictable operations like `create()` and query operations are not supported.
21
20
  * - Every change must be applied to a specific database document in a specific collection.
22
21
  */
23
- export declare class Writes<D extends Datas> extends Change<D> {
24
- static on<X extends Datas>(db: Database<X>, ...changes: ImmutableArray<Change<X>>): Writes<X>;
25
- readonly changes: ImmutableArray<Change<D>>;
26
- constructor(...changes: ImmutableArray<Change<D>>);
22
+ export declare class Writes extends Change {
23
+ readonly changes: ImmutableArray<Change>;
24
+ constructor(...changes: Change[]);
27
25
  /** Return a new `Changes` instance with an additional `Write` instance in its changes list. */
28
- set<C extends Key<D>>({ collection, id }: DatabaseDocument<C, D>, data: D[C]): this;
26
+ set<T extends Data>(ref: DataDocument<T>, data: T): this;
29
27
  /** Return a new `Changes` instance with an additional `Write` instance in its changes list. */
30
- delete<C extends Key<D>>({ collection, id }: DatabaseDocument<C, D>): this;
28
+ delete<T extends Data>(ref: DataDocument<T>): this;
31
29
  /** Return a new `Changes` instance with an additional `Write` instance in its changes list. */
32
- update<C extends Key<D>>({ collection, id }: DatabaseDocument<C, D>, transforms: Transforms<D[C]> | Transform<D[C]>): this;
33
- apply(db: Database<D>): Promise<void> | undefined;
30
+ update<T extends Data>(ref: DataDocument<T>, transforms: Transforms<T> | Transform<T>): this;
31
+ apply(db: Database): Promise<void> | undefined;
34
32
  }
35
33
  /** Set of hydrations for all change classes. */
36
34
  export declare const CHANGE_HYDRATIONS: {
package/db/Change.js CHANGED
@@ -5,15 +5,12 @@ export class Change {
5
5
  }
6
6
  /** A change that writes a document in a database. */
7
7
  export class Write extends Change {
8
- constructor(collection, id, value) {
8
+ constructor({ collection, id }, value) {
9
9
  super();
10
10
  this.collection = collection;
11
11
  this.id = id;
12
12
  this.value = value;
13
13
  }
14
- static on({ collection, id }, value) {
15
- return new Write(collection, id, value);
16
- }
17
14
  apply(db) {
18
15
  return db.doc(this.collection, this.id).write(this.value);
19
16
  }
@@ -28,23 +25,20 @@ export class Writes extends Change {
28
25
  super();
29
26
  this.changes = changes;
30
27
  }
31
- static on(db, ...changes) {
32
- return new Writes(...changes);
33
- }
34
28
  /** Return a new `Changes` instance with an additional `Write` instance in its changes list. */
35
- set({ collection, id }, data) {
36
- return { __proto__: Object.getPrototypeOf(this), ...this, changes: [...this.changes, new Write(collection, id, data)] };
29
+ set(ref, data) {
30
+ return { __proto__: Object.getPrototypeOf(this), ...this, changes: [...this.changes, new Write(ref, data)] };
37
31
  }
38
32
  /** Return a new `Changes` instance with an additional `Write` instance in its changes list. */
39
- delete({ collection, id }) {
40
- return { __proto__: Object.getPrototypeOf(this), ...this, changes: [...this.changes, new Write(collection, id, undefined)] };
33
+ delete(ref) {
34
+ return { __proto__: Object.getPrototypeOf(this), ...this, changes: [...this.changes, new Write(ref, undefined)] };
41
35
  }
42
36
  /** Return a new `Changes` instance with an additional `Write` instance in its changes list. */
43
- update({ collection, id }, transforms) {
37
+ update(ref, transforms) {
44
38
  return {
45
39
  __proto__: Object.getPrototypeOf(this),
46
40
  ...this,
47
- changes: [...this.changes, new Write(collection, id, transforms instanceof Transform ? transforms : new DataTransform(transforms))],
41
+ changes: [...this.changes, new Write(ref, transforms instanceof Transform ? transforms : new DataTransform(transforms))],
48
42
  };
49
43
  }
50
44
  apply(db) {
package/db/Database.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Dispatcher, Entry, Observable, Observer, Result, Unsubscriber, Datas, ResultsMap, Validatable, Validator, Validators, Key, Data, Results } from "../util/index.js";
1
+ import { Dispatcher, Entry, Observable, Observer, Result, Unsubscriber, ResultsMap, Validatable, Validator, Key, Data, Results, Datas, Validators, ValidatorType } from "../util/index.js";
2
2
  import { Transform, Transforms } from "../transform/index.js";
3
3
  import type { Provider } from "../provider/Provider.js";
4
4
  import { Filters, Sorts, Query } from "../query/index.js";
@@ -9,23 +9,23 @@ import { Filters, Sorts, Query } from "../query/index.js";
9
9
  * @param collections Set of loci describing collections at the root level of the database.
10
10
  * @param provider Provider that allows data to be read/written.
11
11
  */
12
- export declare class Database<D extends Datas> {
13
- readonly validators: Validators<D>;
14
- readonly provider: Provider<D>;
15
- constructor(validators: Validators<D>, provider: Provider<D>);
12
+ export declare class Database<V extends Validators<Datas> = Validators<Datas>> {
13
+ readonly validators: V;
14
+ readonly provider: Provider;
15
+ constructor(validators: V, provider: Provider);
16
16
  /** Create a query on a collection in this model. */
17
- query<C extends Key<D>>(collection: C, filters?: Filters<D[C]>, sorts?: Sorts<D[C]>, limit?: number | null): DatabaseQuery<C, D>;
17
+ query<K extends Key<V>>(collection: K, filters?: Filters<ValidatorType<V[K]>>, sorts?: Sorts<ValidatorType<V[K]>>, limit?: number | null): DataQuery<ValidatorType<V[K]>>;
18
18
  /** Reference a document in a collection in this model. */
19
- doc<C extends Key<D>>(collection: C, id: string): DatabaseDocument<C, D>;
19
+ doc<K extends Key<V>>(collection: K, id: string): DataDocument<ValidatorType<V[K]>>;
20
20
  }
21
21
  /** A documents reference within a specific database. */
22
- export declare class DatabaseQuery<C extends Key<D>, D extends Datas> extends Query<D[C]> implements Observable<Results<D[C]>>, Validatable<Results<D[C]>>, Iterable<Entry<D[C]>> {
23
- readonly db: Database<D>;
24
- readonly validator: Validator<D[C]>;
25
- readonly collection: C;
26
- constructor(db: Database<D>, collection: C, filters?: Filters<D[C]>, sorts?: Sorts<D[C]>, limit?: number | null);
22
+ export declare class DataQuery<T extends Data = Data> extends Query<T> implements Observable<Results<T>>, Validatable<Results<T>>, Iterable<Entry<T>> {
23
+ readonly provider: Provider;
24
+ readonly validator: Validator<T>;
25
+ readonly collection: string;
26
+ constructor(provider: Provider, validator: Validator<T>, collection: string, filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null);
27
27
  /** Reference a document in this query's collection. */
28
- doc(id: string): DatabaseDocument<C, D>;
28
+ doc(id: string): DataDocument<T>;
29
29
  /**
30
30
  * Create a new document with a random ID.
31
31
  * - Created document is guaranteed to have a unique ID.
@@ -33,17 +33,17 @@ export declare class DatabaseQuery<C extends Key<D>, D extends Datas> extends Qu
33
33
  * @param data Complete data to set the document to.
34
34
  * @return String ID for the created document (possibly promised).
35
35
  */
36
- add(data: D[C]): string | PromiseLike<string>;
36
+ add(data: T): string | PromiseLike<string>;
37
37
  /**
38
38
  * Get an iterable that yields the results of this entry.
39
39
  * @return Map containing the results.
40
40
  */
41
- get results(): Results<D[C]> | PromiseLike<Results<D[C]>>;
41
+ get results(): Results<T> | PromiseLike<Results<T>>;
42
42
  /**
43
43
  * Get an iterable that yields the results of this entry.
44
44
  * @return Map containing the results.
45
45
  */
46
- get resultsMap(): ResultsMap<D[C]> | PromiseLike<ResultsMap<D[C]>>;
46
+ get resultsMap(): ResultsMap<T> | PromiseLike<ResultsMap<T>>;
47
47
  /**
48
48
  * Count the number of results of this set of documents.
49
49
  * @return Number of documents in the collection (possibly promised).
@@ -53,7 +53,7 @@ export declare class DatabaseQuery<C extends Key<D>, D extends Datas> extends Qu
53
53
  * Get an entry for the first document matching this query.
54
54
  * @return Entry in `[id, data]` format for the first document, or `undefined` if there are no matching documents (possibly promised).
55
55
  */
56
- get first(): Entry<D[C]> | undefined | PromiseLike<Entry<D[C]> | undefined>;
56
+ get first(): Entry<T> | undefined | PromiseLike<Entry<T> | undefined>;
57
57
  /**
58
58
  * Subscribe to all matching documents.
59
59
  * - `next()` is called once with the initial results, and again any time the results change.
@@ -65,7 +65,7 @@ export declare class DatabaseQuery<C extends Key<D>, D extends Datas> extends Qu
65
65
  *
66
66
  * @return Function that ends the subscription.
67
67
  */
68
- subscribe(next: Observer<Results<D[C]>> | Dispatcher<Results<D[C]>>, error?: Dispatcher<Error | unknown>, complete?: Dispatcher<void>): Unsubscriber;
68
+ subscribe(next: Observer<Results<T>> | Dispatcher<Results<T>>, error?: Dispatcher<Error | unknown>, complete?: Dispatcher<void>): Unsubscriber;
69
69
  /**
70
70
  * Subscribe to all matching documents.
71
71
  * - `next()` is called once with the initial results, and again any time the results change.
@@ -77,14 +77,14 @@ export declare class DatabaseQuery<C extends Key<D>, D extends Datas> extends Qu
77
77
  *
78
78
  * @return Function that ends the subscription.
79
79
  */
80
- subscribeMap(next: Observer<ResultsMap<D[C]>> | Dispatcher<ResultsMap<D[C]>>, error?: Dispatcher<Error | unknown>, complete?: Dispatcher<void>): Unsubscriber;
80
+ subscribeMap(next: Observer<ResultsMap<T>> | Dispatcher<ResultsMap<T>>, error?: Dispatcher<Error | unknown>, complete?: Dispatcher<void>): Unsubscriber;
81
81
  /**
82
82
  * Set all matching documents to the same exact value.
83
83
  *
84
84
  * @param data Complete data to set the document to.
85
85
  * @return Nothing (possibly promised).
86
86
  */
87
- set(data: D[C]): void | PromiseLike<void>;
87
+ set(data: T): void | PromiseLike<void>;
88
88
  /**
89
89
  * Update all matching documents with the same partial value.
90
90
  *
@@ -93,7 +93,7 @@ export declare class DatabaseQuery<C extends Key<D>, D extends Datas> extends Qu
93
93
  *
94
94
  * @return Nothing (possibly promised).
95
95
  */
96
- update(transform: Transform<D[C]> | Transforms<D[C]>): void | PromiseLike<void>;
96
+ update(transform: Transform<T> | Transforms<T>): void | PromiseLike<void>;
97
97
  /**
98
98
  * Delete all matching documents.
99
99
  * @return Nothing (possibly promised).
@@ -103,24 +103,24 @@ export declare class DatabaseQuery<C extends Key<D>, D extends Datas> extends Qu
103
103
  * Combine `set()`, `update()`, `delete()` into a single method.
104
104
  * @return Nothing (possibly promised).
105
105
  */
106
- write(value: Result<D[C]> | Transform<D[C]>): void | PromiseLike<void>;
106
+ write(value: Result<T> | Transform<T>): void | PromiseLike<void>;
107
107
  /** Iterate over the resuls (will throw `Promise` if the results are asynchronous). */
108
- [Symbol.iterator](): Iterator<Entry<D[C]>, void>;
108
+ [Symbol.iterator](): Iterator<Entry<T>, void>;
109
109
  /** Validate a set of results for this query reference. */
110
- validate(unsafeEntries: Results): Results<D[C]>;
110
+ validate(unsafeEntries: Results): Results<T>;
111
111
  toString(): string;
112
112
  }
113
113
  /** A document reference within a specific database. */
114
- export declare class DatabaseDocument<C extends Key<D>, D extends Datas> implements Observable<Result<D[C]>>, Validatable<D[C]> {
115
- readonly db: Database<D>;
116
- readonly validator: Validator<D[C]>;
117
- readonly collection: C;
114
+ export declare class DataDocument<T extends Data = Data> implements Observable<Result<T>>, Validatable<T> {
115
+ readonly provider: Provider;
116
+ readonly validator: Validator<T>;
117
+ readonly collection: string;
118
118
  readonly id: string;
119
- constructor(db: Database<D>, collection: C, id: string);
119
+ constructor(provider: Provider, validator: Validator<T>, collection: string, id: string);
120
120
  /** Create a query on this document's collection. */
121
- query(filters?: Filters<D[C]>, sorts?: Sorts<D[C]>, limit?: number | null): DatabaseQuery<C, D>;
121
+ query(filters?: Filters<T>, sorts?: Sorts<T>, limit?: number | null): DataQuery<T>;
122
122
  /** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
123
- get optional(): DatabaseQuery<C, D>;
123
+ get optional(): DataQuery<T>;
124
124
  /**
125
125
  * Does this document exist?
126
126
  *
@@ -132,7 +132,7 @@ export declare class DatabaseDocument<C extends Key<D>, D extends Datas> impleme
132
132
  *
133
133
  * @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
134
134
  */
135
- get result(): Result<D[C]> | PromiseLike<Result<D[C]>>;
135
+ get result(): Result<T> | PromiseLike<Result<T>>;
136
136
  /**
137
137
  * Get the data of this document.
138
138
  * - Useful for destructuring, e.g. `{ name, title } = await documentThatMustExist.asyncData`
@@ -140,7 +140,7 @@ export declare class DatabaseDocument<C extends Key<D>, D extends Datas> impleme
140
140
  * @return Document's data (possibly promised).
141
141
  * @throws RequiredError if the document's result was undefined.
142
142
  */
143
- get data(): D[C] | PromiseLike<D[C]>;
143
+ get data(): T | PromiseLike<T>;
144
144
  /**
145
145
  * Subscribe to the result of this document (indefinitely).
146
146
  * - `next()` is called once with the initial result, and again any time the result changes.
@@ -152,7 +152,7 @@ export declare class DatabaseDocument<C extends Key<D>, D extends Datas> impleme
152
152
  *
153
153
  * @return Function that ends the subscription.
154
154
  */
155
- subscribe(next: Observer<Result<D[C]>> | Dispatcher<Result<D[C]>>, error?: Dispatcher<Error | unknown>, complete?: Dispatcher<void>): Unsubscriber;
155
+ subscribe(next: Observer<Result<T>> | Dispatcher<Result<T>>, error?: Dispatcher<Error | unknown>, complete?: Dispatcher<void>): Unsubscriber;
156
156
  /**
157
157
  * Set the complete data of this document.
158
158
  *
@@ -160,7 +160,7 @@ export declare class DatabaseDocument<C extends Key<D>, D extends Datas> impleme
160
160
  *
161
161
  * @return Nothing (possibly promised).
162
162
  */
163
- set(data: D[C]): void | PromiseLike<void>;
163
+ set(data: T): void | PromiseLike<void>;
164
164
  /**
165
165
  * Update this document with partial data.
166
166
  * - If the document exists, merge the partial data into it.
@@ -172,7 +172,7 @@ export declare class DatabaseDocument<C extends Key<D>, D extends Datas> impleme
172
172
  * @return Nothing (possibly promised).
173
173
  * @throws Error If the document does not exist (ideally a `RequiredError` but may be provider-specific).
174
174
  */
175
- update(transforms: Transform<D[C]> | Transforms<D[C]>): void | PromiseLike<void>;
175
+ update(transforms: Transform<T> | Transforms<T>): void | PromiseLike<void>;
176
176
  /**
177
177
  * Delete this document.
178
178
  * - Will not throw an error if the document doesn't exist.
@@ -183,18 +183,10 @@ export declare class DatabaseDocument<C extends Key<D>, D extends Datas> impleme
183
183
  /**
184
184
  * Combine `set()`, `update()`, `delete()` into a single method.
185
185
  */
186
- write(value: Result<D[C]> | Transform<D[C]>): void | PromiseLike<void>;
186
+ write(value: Result<T> | Transform<T>): void | PromiseLike<void>;
187
187
  /** Validate data for this query reference. */
188
- validate(unsafeData: Data): D[C];
188
+ validate(unsafeData: Data): T;
189
189
  toString(): string;
190
190
  }
191
191
  /** Get the data for a document from a result for that document. */
192
- export declare function getDocumentData<C extends Key<D>, D extends Datas>(result: Result<D[C]>, ref: DatabaseDocument<C, D>): D[C];
193
- /** Database query with a collection name. */
194
- export declare type CollectionQuery<C extends string, T extends Data> = DatabaseQuery<C, {
195
- [K in C]: T;
196
- }>;
197
- /** Database document with a collection name. */
198
- export declare type CollectionDocument<C extends string, T extends Data> = DatabaseDocument<C, {
199
- [K in C]: T;
200
- }>;
192
+ export declare function getDocumentData<T extends Data>(result: Result<T>, ref: DataDocument<T>): T;
package/db/Database.js CHANGED
@@ -10,6 +10,7 @@ import { DocumentRequiredError, DocumentValidationError, QueryValidationError }
10
10
  * @param collections Set of loci describing collections at the root level of the database.
11
11
  * @param provider Provider that allows data to be read/written.
12
12
  */
13
+ // Note: typing this with `Validators` rather than raw `Datas` works better for inference — type for props in each collection tends to get lost.
13
14
  export class Database {
14
15
  constructor(validators, provider) {
15
16
  this.validators = validators;
@@ -17,24 +18,24 @@ export class Database {
17
18
  }
18
19
  /** Create a query on a collection in this model. */
19
20
  query(collection, filters, sorts, limit) {
20
- return new DatabaseQuery(this, collection, filters, sorts, limit);
21
+ return new DataQuery(this.provider, this.validators[collection], collection, filters, sorts, limit);
21
22
  }
22
23
  /** Reference a document in a collection in this model. */
23
24
  doc(collection, id) {
24
- return new DatabaseDocument(this, collection, id);
25
+ return new DataDocument(this.provider, this.validators[collection], collection, id);
25
26
  }
26
27
  }
27
28
  /** A documents reference within a specific database. */
28
- export class DatabaseQuery extends Query {
29
- constructor(db, collection, filters, sorts, limit) {
29
+ export class DataQuery extends Query {
30
+ constructor(provider, validator, collection, filters, sorts, limit) {
30
31
  super(filters, sorts, limit);
31
- this.db = db;
32
- this.validator = db.validators[collection];
32
+ this.provider = provider;
33
+ this.validator = validator;
33
34
  this.collection = collection;
34
35
  }
35
36
  /** Reference a document in this query's collection. */
36
37
  doc(id) {
37
- return new DatabaseDocument(this.db, this.collection, id);
38
+ return new DataDocument(this.provider, this.validator, this.collection, id);
38
39
  }
39
40
  /**
40
41
  * Create a new document with a random ID.
@@ -44,21 +45,21 @@ export class DatabaseQuery extends Query {
44
45
  * @return String ID for the created document (possibly promised).
45
46
  */
46
47
  add(data) {
47
- return this.db.provider.add(this, data);
48
+ return this.provider.add(this, data);
48
49
  }
49
50
  /**
50
51
  * Get an iterable that yields the results of this entry.
51
52
  * @return Map containing the results.
52
53
  */
53
54
  get results() {
54
- return this.db.provider.getQuery(this);
55
+ return this.provider.getQuery(this);
55
56
  }
56
57
  /**
57
58
  * Get an iterable that yields the results of this entry.
58
59
  * @return Map containing the results.
59
60
  */
60
61
  get resultsMap() {
61
- return callAsync(toMap, this.db.provider.getQuery(this));
62
+ return callAsync(toMap, this.provider.getQuery(this));
62
63
  }
63
64
  /**
64
65
  * Count the number of results of this set of documents.
@@ -86,7 +87,7 @@ export class DatabaseQuery extends Query {
86
87
  * @return Function that ends the subscription.
87
88
  */
88
89
  subscribe(next, error, complete) {
89
- return this.db.provider.subscribeQuery(this, createObserver(next, error, complete));
90
+ return this.provider.subscribeQuery(this, createObserver(next, error, complete));
90
91
  }
91
92
  /**
92
93
  * Subscribe to all matching documents.
@@ -100,7 +101,7 @@ export class DatabaseQuery extends Query {
100
101
  * @return Function that ends the subscription.
101
102
  */
102
103
  subscribeMap(next, error, complete) {
103
- return this.db.provider.subscribeQuery(this, new DeriveObserver(toMap, createObserver(next, error, complete)));
104
+ return this.provider.subscribeQuery(this, new DeriveObserver(toMap, createObserver(next, error, complete)));
104
105
  }
105
106
  /**
106
107
  * Set all matching documents to the same exact value.
@@ -134,7 +135,7 @@ export class DatabaseQuery extends Query {
134
135
  * @return Nothing (possibly promised).
135
136
  */
136
137
  write(value) {
137
- return this.db.provider.writeQuery(this, value);
138
+ return this.provider.writeQuery(this, value);
138
139
  }
139
140
  /** Iterate over the resuls (will throw `Promise` if the results are asynchronous). */
140
141
  [Symbol.iterator]() {
@@ -164,20 +165,20 @@ export class DatabaseQuery extends Query {
164
165
  }
165
166
  }
166
167
  /** A document reference within a specific database. */
167
- export class DatabaseDocument {
168
- constructor(db, collection, id) {
169
- this.db = db;
170
- this.validator = db.validators[collection];
168
+ export class DataDocument {
169
+ constructor(provider, validator, collection, id) {
170
+ this.provider = provider;
171
+ this.validator = validator;
171
172
  this.collection = collection;
172
173
  this.id = id;
173
174
  }
174
175
  /** Create a query on this document's collection. */
175
176
  query(filters, sorts, limit) {
176
- return new DatabaseQuery(this.db, this.collection, filters, sorts, limit);
177
+ return new DataQuery(this.provider, this.validator, this.collection, filters, sorts, limit);
177
178
  }
178
179
  /** Get an 'optional' reference to this document (uses a `ModelQuery` with an `id` filter). */
179
180
  get optional() {
180
- return new DatabaseQuery(this.db, this.collection, new Filters(new EqualFilter("id", this.id)));
181
+ return new DataQuery(this.provider, this.validator, this.collection, new Filters(new EqualFilter("id", this.id)));
181
182
  }
182
183
  /**
183
184
  * Does this document exist?
@@ -185,7 +186,7 @@ export class DatabaseDocument {
185
186
  * @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
186
187
  */
187
188
  get exists() {
188
- return callAsync(Boolean, this.db.provider.get(this));
189
+ return callAsync(Boolean, this.provider.get(this));
189
190
  }
190
191
  /**
191
192
  * Get the result of this document.
@@ -193,7 +194,7 @@ export class DatabaseDocument {
193
194
  * @return Document's data, or `undefined` if the document doesn't exist (possibly promised).
194
195
  */
195
196
  get result() {
196
- return this.db.provider.get(this);
197
+ return this.provider.get(this);
197
198
  }
198
199
  /**
199
200
  * Get the data of this document.
@@ -203,7 +204,7 @@ export class DatabaseDocument {
203
204
  * @throws RequiredError if the document's result was undefined.
204
205
  */
205
206
  get data() {
206
- return callAsync(getDocumentData, this.db.provider.get(this), this);
207
+ return callAsync(getDocumentData, this.provider.get(this), this);
207
208
  }
208
209
  /**
209
210
  * Subscribe to the result of this document (indefinitely).
@@ -217,7 +218,7 @@ export class DatabaseDocument {
217
218
  * @return Function that ends the subscription.
218
219
  */
219
220
  subscribe(next, error, complete) {
220
- return this.db.provider.subscribe(this, createObserver(next, error, complete));
221
+ return this.provider.subscribe(this, createObserver(next, error, complete));
221
222
  }
222
223
  /**
223
224
  * Set the complete data of this document.
@@ -256,7 +257,7 @@ export class DatabaseDocument {
256
257
  * Combine `set()`, `update()`, `delete()` into a single method.
257
258
  */
258
259
  write(value) {
259
- return this.db.provider.write(this, value);
260
+ return this.provider.write(this, value);
260
261
  }
261
262
  /** Validate data for this query reference. */
262
263
  validate(unsafeData) {
@@ -1,19 +1,19 @@
1
- import { Datas, Key, ResultsMap, Entry, Results } from "../util/index.js";
1
+ import { ResultsMap, Entry, Results, Data } from "../util/index.js";
2
2
  import { State } from "../stream/index.js";
3
- import { DatabaseQuery } from "./Database.js";
3
+ import { DataQuery } 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<C extends Key<D>, D extends Datas> extends State<ResultsMap<D[C]>> implements Iterable<Entry<D[C]>> {
10
- readonly ref: DatabaseQuery<C, D>;
9
+ export declare class Pagination<T extends Data> extends State<ResultsMap<T>> implements Iterable<Entry<T>> {
10
+ readonly ref: DataQuery<T>;
11
11
  readonly limit: number;
12
12
  readonly startLoading: boolean;
13
13
  readonly startDone: boolean;
14
14
  readonly endLoading: boolean;
15
15
  readonly endDone: boolean;
16
- constructor(ref: DatabaseQuery<C, D>);
16
+ constructor(ref: DataQuery<T>);
17
17
  /**
18
18
  * Load more results before the start.
19
19
  * - Promise that needs to be handled.
@@ -28,7 +28,7 @@ export declare class Pagination<C extends Key<D>, D extends Datas> extends State
28
28
  * Merge more results into this pagination.
29
29
  * @return The change in the number of results.
30
30
  */
31
- merge(more: Results<D[C]>): void;
31
+ merge(more: Results<T>): void;
32
32
  /** Iterate over the entries of the values currently in the pagination. */
33
- [Symbol.iterator](): Iterator<Entry<D[C]>>;
33
+ [Symbol.iterator](): Iterator<Entry<T>>;
34
34
  }
package/db/errors.d.ts CHANGED
@@ -1,19 +1,19 @@
1
- import type { Datas, Key } from "../util/index.js";
1
+ import type { Data } from "../util/index.js";
2
2
  import { RequiredError, ValidationError } from "../error/index.js";
3
- import { Feedback } from "../feedback/index.js";
4
- import type { DatabaseDocument, DatabaseQuery } from "./Database.js";
3
+ import type { Feedback } from "../feedback/index.js";
4
+ import type { DataDocument, DataQuery } from "./Database.js";
5
5
  /** Thrown if a document doesn't exist. */
6
- export declare class DocumentRequiredError<D extends Datas, C extends Key<D>> extends RequiredError {
7
- ref: DatabaseDocument<C, D>;
8
- constructor(ref: DatabaseDocument<C, D>);
6
+ export declare class DocumentRequiredError<T extends Data> extends RequiredError {
7
+ ref: DataDocument<T>;
8
+ constructor(ref: DataDocument<T>);
9
9
  }
10
10
  /** Thrown if a document can't validate. */
11
- export declare class DocumentValidationError<D extends Datas, C extends Key<D>> extends ValidationError {
12
- ref: DatabaseDocument<C, D>;
13
- constructor(ref: DatabaseDocument<C, D>, feedback: Feedback);
11
+ export declare class DocumentValidationError<T extends Data> extends ValidationError {
12
+ ref: DataDocument<T>;
13
+ constructor(ref: DataDocument<T>, feedback: Feedback);
14
14
  }
15
15
  /** Thrown if a query can't validate a set of results. */
16
- export declare class QueryValidationError<D extends Datas, C extends Key<D>> extends ValidationError {
17
- ref: DatabaseQuery<C, D>;
18
- constructor(ref: DatabaseQuery<C, D>, feedback: Feedback);
16
+ export declare class QueryValidationError<T extends Data> extends ValidationError {
17
+ ref: DataQuery<T>;
18
+ constructor(ref: DataQuery<T>, feedback: Feedback);
19
19
  }
@@ -1,19 +1,19 @@
1
1
  import type { Firestore } from "firebase/firestore";
2
- import { Results, Provider, DatabaseDocument, DatabaseQuery, Result, Observer, Transform, AsynchronousProvider, Datas, Key } from "../../index.js";
2
+ import { Results, Provider, DataDocument, DataQuery, Result, Observer, Transform, AsynchronousProvider, Data } from "../../index.js";
3
3
  /**
4
4
  * Firestore client database provider.
5
5
  * - Works with the Firebase JS SDK.
6
6
  * - Supports offline mode.
7
7
  * - Supports realtime subscriptions.
8
8
  */
9
- export declare class FirestoreClientProvider<D extends Datas> extends Provider<D> implements AsynchronousProvider<D> {
9
+ export declare class FirestoreClientProvider extends Provider implements AsynchronousProvider {
10
10
  readonly firestore: Firestore;
11
11
  constructor(firestore: Firestore);
12
- get<C extends Key<D>>(ref: DatabaseDocument<C, D>): Promise<Result<D[C]>>;
13
- subscribe<C extends Key<D>>(ref: DatabaseDocument<C, D>, observer: Observer<Result<D[C]>>): () => void;
14
- add<C extends Key<D>>(ref: DatabaseQuery<C, D>, data: D[C]): Promise<string>;
15
- write<C extends Key<D>>(ref: DatabaseDocument<C, D>, value: D[C] | Transform<D[C]> | undefined): Promise<void>;
16
- getQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>): Promise<Results<D[C]>>;
17
- subscribeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, observer: Observer<Results<D[C]>>): () => void;
18
- writeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, value: D[C] | Transform<D[C]> | undefined): Promise<void>;
12
+ get<T extends Data>(ref: DataDocument<T>): Promise<Result<T>>;
13
+ subscribe<T extends Data>(ref: DataDocument<T>, observer: Observer<Result<T>>): () => void;
14
+ add<T extends Data>(ref: DataQuery<T>, data: T): Promise<string>;
15
+ write<T extends Data>(ref: DataDocument<T>, value: T | Transform<T> | undefined): Promise<void>;
16
+ getQuery<T extends Data>(ref: DataQuery<T>): Promise<Results<T>>;
17
+ subscribeQuery<T extends Data>(ref: DataQuery<T>, observer: Observer<Results<T>>): () => void;
18
+ writeQuery<T extends Data>(ref: DataQuery<T>, value: T | Transform<T> | undefined): Promise<void>;
19
19
  }
@@ -1,19 +1,19 @@
1
1
  import type { Firestore } from "firebase/firestore/lite";
2
- import { Provider, DatabaseDocument, DatabaseQuery, Result, Transform, AsynchronousProvider, Datas, Key, Results } from "../../index.js";
2
+ import { Provider, DataDocument, DataQuery, Result, Transform, AsynchronousProvider, Data, Results } from "../../index.js";
3
3
  /**
4
4
  * Firestore Lite client database provider.
5
5
  * - Works with the Firebase JS SDK.
6
6
  * - Does not support offline mode.
7
7
  * - Does not support realtime subscriptions.
8
8
  */
9
- export declare class FirestoreClientProvider<D extends Datas> extends Provider<D> implements AsynchronousProvider<D> {
9
+ export declare class FirestoreClientProvider extends Provider implements AsynchronousProvider {
10
10
  readonly firestore: Firestore;
11
11
  constructor(firestore: Firestore);
12
- get<C extends Key<D>>(ref: DatabaseDocument<C, D>): Promise<Result<D[C]>>;
12
+ get<T extends Data>(ref: DataDocument<T>): Promise<Result<T>>;
13
13
  subscribe(): () => void;
14
- add<C extends Key<D>>(ref: DatabaseQuery<C, D>, data: D[C]): Promise<string>;
15
- write<C extends Key<D>>(ref: DatabaseDocument<C, D>, value: D[C] | Transform<D[C]> | undefined): Promise<void>;
16
- getQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>): Promise<Results<D[C]>>;
14
+ add<T extends Data>(ref: DataQuery<T>, data: T): Promise<string>;
15
+ write<T extends Data>(ref: DataDocument<T>, value: T | Transform<T> | undefined): Promise<void>;
16
+ getQuery<T extends Data>(ref: DataQuery<T>): Promise<Results<T>>;
17
17
  subscribeQuery(): () => void;
18
- writeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, value: D[C] | Transform<D[C]> | undefined): Promise<void>;
18
+ writeQuery<T extends Data>(ref: DataQuery<T>, value: T | Transform<T> | undefined): Promise<void>;
19
19
  }
@@ -1,17 +1,17 @@
1
1
  import { Firestore } from "@google-cloud/firestore";
2
- import { Provider, DatabaseDocument, DatabaseQuery, Observer, Result, Transform, AsynchronousProvider, Datas, Key, Entry, Results } from "../../index.js";
2
+ import { Provider, DataDocument, DataQuery, Observer, Result, Transform, Data, AsynchronousProvider, Entry, Results } from "../../index.js";
3
3
  /**
4
4
  * Firestore server database provider.
5
5
  * - Works with the Firebase Admin SDK for Node.JS
6
6
  */
7
- export declare class FirestoreServerProvider<D extends Datas> extends Provider<D> implements AsynchronousProvider<D> {
7
+ export declare class FirestoreServerProvider extends Provider implements AsynchronousProvider {
8
8
  readonly firestore: Firestore;
9
9
  constructor(firestore?: Firestore);
10
- get<C extends Key<D>>(ref: DatabaseDocument<C, D>): Promise<Result<D[C]>>;
11
- subscribe<C extends Key<D>>(ref: DatabaseDocument<C, D>, observer: Observer<Result<D[C]>>): () => void;
12
- add<C extends Key<D>>(ref: DatabaseQuery<C, D>, data: D[C]): Promise<string>;
13
- write<C extends Key<D>>(ref: DatabaseDocument<C, D>, value: D[C] | Transform<D[C]> | undefined): Promise<void>;
14
- getQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>): Promise<Iterable<Entry<D[C]>>>;
15
- subscribeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, observer: Observer<Results<D[C]>>): () => void;
16
- writeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, value: D[C] | Transform<D[C]> | undefined): Promise<void>;
10
+ get<T extends Data>(ref: DataDocument<T>): Promise<Result<T>>;
11
+ subscribe<T extends Data>(ref: DataDocument<T>, observer: Observer<Result<T>>): () => void;
12
+ add<T extends Data>(ref: DataQuery<T>, data: T): Promise<string>;
13
+ write<T extends Data>(ref: DataDocument<T>, value: T | Transform<T> | undefined): Promise<void>;
14
+ getQuery<T extends Data>(ref: DataQuery<T>): Promise<Iterable<Entry<T>>>;
15
+ subscribeQuery<T extends Data>(ref: DataQuery<T>, observer: Observer<Results<T>>): () => void;
16
+ writeQuery<T extends Data>(ref: DataQuery<T>, value: T | Transform<T> | undefined): Promise<void>;
17
17
  }
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.20.0",
14
+ "version": "1.20.1",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
@@ -61,18 +61,18 @@
61
61
  "@types/jest": "^27.0.3",
62
62
  "@types/react": "^17.0.37",
63
63
  "@types/react-dom": "^17.0.11",
64
- "@typescript-eslint/eslint-plugin": "^5.4.0",
65
- "@typescript-eslint/parser": "^5.4.0",
64
+ "@typescript-eslint/eslint-plugin": "^5.5.0",
65
+ "@typescript-eslint/parser": "^5.5.0",
66
+ "eslint": "^8.4.0",
66
67
  "eslint-config-prettier": "^8.3.0",
67
68
  "eslint-plugin-import": "^2.25.3",
68
69
  "eslint-plugin-prettier": "^4.0.0",
69
- "eslint": "^8.3.0",
70
- "firebase": "^9.5.0",
70
+ "firebase": "^9.6.0",
71
+ "jest": "^27.4.3",
71
72
  "jest-ts-webcompat-resolver": "^1.0.0",
72
- "jest": "^27.3.1",
73
- "prettier": "^2.5.0",
74
- "react-dom": "^17.0.2",
73
+ "prettier": "^2.5.1",
75
74
  "react": "^17.0.2",
75
+ "react-dom": "^17.0.2",
76
76
  "ts-jest": "^27.0.7",
77
77
  "typescript": "^4.5.2"
78
78
  },
@@ -1,5 +1,5 @@
1
- import { MutableObject, Result, Observer, Unsubscriber, Observable, Datas, Key, Results, ResultsMap } from "../util/index.js";
2
- import type { DatabaseDocument, DatabaseQuery } from "../db/index.js";
1
+ import { MutableObject, Result, Observer, Unsubscriber, Observable, Results, ResultsMap, Data } from "../util/index.js";
2
+ import type { DataDocument, DataQuery } from "../db/index.js";
3
3
  import { ThroughProvider } from "./ThroughProvider.js";
4
4
  /**
5
5
  * Provider that batches multiple database reads from a source provider together, for efficiency.
@@ -10,17 +10,17 @@ import { ThroughProvider } from "./ThroughProvider.js";
10
10
  *
11
11
  * Basically makes any provider under it more efficient.
12
12
  */
13
- export declare class BatchProvider<D extends Datas> extends ThroughProvider<D> {
13
+ export declare class BatchProvider extends ThroughProvider {
14
14
  /** List of currently ongoing get requests. */
15
15
  protected readonly _gets: MutableObject<Promise<any>>;
16
16
  /** List of currently ongoing subscription streams. */
17
17
  protected readonly _subs: MutableObject<Observable<any>>;
18
- get<C extends Key<D>>(ref: DatabaseDocument<C, D>): Result<D[C]> | Promise<Result<D[C]>>;
18
+ get<T extends Data>(ref: DataDocument<T>): Result<T> | Promise<Result<T>>;
19
19
  /** Await a result and delete it from get requests when done. */
20
20
  private _awaitDocument;
21
- subscribe<C extends Key<D>>(ref: DatabaseDocument<C, D>, observer: Observer<Result<D[C]>>): Unsubscriber;
22
- getQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>): Results<D[C]> | Promise<Results<D[C]>>;
21
+ subscribe<T extends Data>(ref: DataDocument<T>, observer: Observer<Result<T>>): Unsubscriber;
22
+ getQuery<T extends Data>(ref: DataQuery<T>): Results<T> | Promise<Results<T>>;
23
23
  /** Await a set of results and delete from get requests when done. */
24
24
  private _awaitDocuments;
25
- subscribeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, observer: Observer<ResultsMap<D[C]>>): Unsubscriber;
25
+ subscribeQuery<T extends Data>(ref: DataQuery<T>, observer: Observer<ResultsMap<T>>): Unsubscriber;
26
26
  }
@@ -1,31 +1,31 @@
1
- import type { DatabaseDocument, DatabaseQuery } from "../db/index.js";
1
+ import type { DataDocument, DataQuery } from "../db/index.js";
2
2
  import { Transform } from "../transform/index.js";
3
- import { Result, Unsubscriber, Observer, Datas, Key, Results } from "../util/index.js";
3
+ import { Result, Unsubscriber, Observer, Results, Data } from "../util/index.js";
4
4
  import type { Provider, AsynchronousProvider } from "./Provider.js";
5
5
  import { MemoryProvider } from "./MemoryProvider.js";
6
6
  import { ThroughProvider } from "./ThroughProvider.js";
7
7
  /** Keep a copy of received data in a local cache. */
8
- export declare class CacheProvider<D extends Datas> extends ThroughProvider<D> implements AsynchronousProvider<D> {
8
+ export declare class CacheProvider extends ThroughProvider implements AsynchronousProvider {
9
9
  /** The local cache provider. */
10
- readonly cache: MemoryProvider<D>;
10
+ readonly cache: MemoryProvider;
11
11
  /** Last-known-correct time for data, indexed by key to power `getCachedAge()` etc. */
12
12
  private _times;
13
- constructor(source: Provider<D>, cache?: MemoryProvider<D>);
13
+ constructor(source: Provider, cache?: MemoryProvider);
14
14
  /** Is a given document or query in the cache? */
15
- isCached<C extends Key<D>>(ref: DatabaseDocument<C, D> | DatabaseQuery<C, D>): boolean;
15
+ isCached<T extends Data>(ref: DataDocument<T> | DataQuery<T>): boolean;
16
16
  /** Get the cache age for a given document or query reference. */
17
- getCachedAge<C extends Key<D>>(ref: DatabaseDocument<C, D> | DatabaseQuery<C, D>): number;
17
+ getCachedAge<T extends Data>(ref: DataDocument<T> | DataQuery<T>): number;
18
18
  /** Cache an individual document result. */
19
19
  private _cacheResult;
20
- get<C extends Key<D>>(ref: DatabaseDocument<C, D>): Promise<Result<D[C]>>;
21
- subscribe<C extends Key<D>>(ref: DatabaseDocument<C, D>, observer: Observer<Result<D[C]>>): Unsubscriber;
22
- add<C extends Key<D>>(ref: DatabaseQuery<C, D>, data: D[C]): Promise<string>;
23
- write<C extends Key<D>>(ref: DatabaseDocument<C, D>, value: D[C] | Transform<D[C]> | undefined): Promise<void>;
20
+ get<T extends Data>(ref: DataDocument<T>): Promise<Result<T>>;
21
+ subscribe<T extends Data>(ref: DataDocument<T>, observer: Observer<Result<T>>): Unsubscriber;
22
+ add<T extends Data>(ref: DataQuery<T>, data: T): Promise<string>;
23
+ write<T extends Data>(ref: DataDocument<T>, value: T | Transform<T> | undefined): Promise<void>;
24
24
  /** Cache a set of document results. */
25
25
  private _cacheResults;
26
- getQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>): Promise<Results<D[C]>>;
27
- subscribeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, observer: Observer<Results<D[C]>>): Unsubscriber;
28
- writeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, value: D[C] | Transform<D[C]> | undefined): Promise<void>;
26
+ getQuery<T extends Data>(ref: DataQuery<T>): Promise<Results<T>>;
27
+ subscribeQuery<T extends Data>(ref: DataQuery<T>, observer: Observer<Results<T>>): Unsubscriber;
28
+ writeQuery<T extends Data>(ref: DataQuery<T>, value: T | Transform<T> | undefined): Promise<void>;
29
29
  /** Reset this provider and clear all data. */
30
30
  reset(): void;
31
31
  }
@@ -1,23 +1,23 @@
1
- import { Result, Results, Unsubscriber, Observer, Datas, Key } from "../util/index.js";
1
+ import { Data, Result, Results, Unsubscriber, Observer } from "../util/index.js";
2
2
  import { Transform } from "../transform/index.js";
3
- import { DatabaseQuery, DatabaseDocument } from "../db/index.js";
3
+ import { DataQuery, DataDocument } from "../db/index.js";
4
4
  import { Provider, SynchronousProvider } from "./Provider.js";
5
5
  /**
6
6
  * Fast in-memory store for data.
7
7
  * - Extremely fast (ideal for caching!), but does not persist data after the browser window is closed.
8
8
  * - `get()` etc return the exact same instance of an object that's passed into `set()`
9
9
  */
10
- export declare class MemoryProvider<D extends Datas> extends Provider<D> implements SynchronousProvider<D> {
10
+ export declare class MemoryProvider extends Provider implements SynchronousProvider {
11
11
  /** List of tables in `{ path: Table }` format. */
12
12
  private _tables;
13
13
  private _table;
14
- get<C extends Key<D>>(ref: DatabaseDocument<C, D>): Result<D[C]>;
15
- subscribe<C extends Key<D>>(ref: DatabaseDocument<C, D>, observer: Observer<Result<D[C]>>): Unsubscriber;
16
- add<C extends Key<D>>(ref: DatabaseQuery<C, D>, data: D[C]): string;
17
- write<C extends Key<D>>(ref: DatabaseDocument<C, D>, value: D[C] | Transform<D[C]> | undefined): void;
18
- getQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>): Results<D[C]>;
19
- subscribeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, observer: Observer<Results<D[C]>>): Unsubscriber;
20
- writeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, value: D[C] | Transform<D[C]> | undefined): void;
14
+ get<T extends Data>(ref: DataDocument<T>): Result<T>;
15
+ subscribe<T extends Data>(ref: DataDocument<T>, observer: Observer<Result<T>>): Unsubscriber;
16
+ add<T extends Data>(ref: DataQuery<T>, data: T): string;
17
+ write<T extends Data>(ref: DataDocument<T>, value: T | Transform<T> | undefined): void;
18
+ getQuery<T extends Data>(ref: DataQuery<T>): Results<T>;
19
+ subscribeQuery<T extends Data>(ref: DataQuery<T>, observer: Observer<Results<T>>): Unsubscriber;
20
+ writeQuery<T extends Data>(ref: DataQuery<T>, value: T | Transform<T> | undefined): void;
21
21
  /** Reset this provider and clear all data. */
22
22
  reset(): void;
23
23
  }
@@ -11,7 +11,7 @@ export class MemoryProvider extends Provider {
11
11
  constructor() {
12
12
  super(...arguments);
13
13
  /** List of tables in `{ path: Table }` format. */
14
- this._tables = {};
14
+ this._tables = {}; // eslint-disable-line @typescript-eslint/no-explicit-any
15
15
  }
16
16
  // Get a named collection (or create a new one).
17
17
  _table({ collection }) {
@@ -1,15 +1,15 @@
1
- import type { Observer, Unsubscriber, Result, Datas, Key, Results } from "../util/index.js";
2
- import type { DatabaseDocument, DatabaseQuery } from "../db/Database.js";
1
+ import type { Observer, Unsubscriber, Result, Results, Data } from "../util/index.js";
2
+ import type { DataDocument, DataQuery } from "../db/Database.js";
3
3
  import type { Transform } from "../transform/index.js";
4
4
  /** Provides access to data (e.g. IndexedDB, Firebase, or in-memory cache providers). */
5
- export declare abstract class Provider<D extends Datas = Datas> {
5
+ export declare abstract class Provider {
6
6
  /**
7
7
  * Get the result of a document.
8
8
  *
9
9
  * @param ref Document reference specifying which document to get.
10
10
  * @return The document object, or `undefined` if it doesn't exist.
11
11
  */
12
- abstract get<C extends Key<D>>(ref: DatabaseDocument<C, D>): Result<D[C]> | PromiseLike<Result<D[C]>>;
12
+ abstract get<T extends Data>(ref: DataDocument<T>): Result<T> | PromiseLike<Result<T>>;
13
13
  /**
14
14
  * Subscribe to the result of a document.
15
15
  * - `next()` is called once with the initial result, and again any time the result changes.
@@ -19,7 +19,7 @@ export declare abstract class Provider<D extends Datas = Datas> {
19
19
  *
20
20
  * @return Function that ends the subscription.
21
21
  */
22
- abstract subscribe<C extends Key<D>>(ref: DatabaseDocument<C, D>, observer: Observer<Result<D[C]>>): Unsubscriber;
22
+ abstract subscribe<T extends Data>(ref: DataDocument<T>, observer: Observer<Result<T>>): Unsubscriber;
23
23
  /**
24
24
  * Create a new document with a random ID.
25
25
  * - Created document is guaranteed to have a unique ID.
@@ -29,7 +29,7 @@ export declare abstract class Provider<D extends Datas = Datas> {
29
29
  *
30
30
  * @return String ID for the created document (possibly promised).
31
31
  */
32
- abstract add<C extends Key<D>>(ref: DatabaseQuery<C, D>, data: D[C]): string | PromiseLike<string>;
32
+ abstract add<T extends Data>(ref: DataQuery<T>, data: T): string | PromiseLike<string>;
33
33
  /**
34
34
  * Write to a document.
35
35
  * - If the document exists, set the value of it.
@@ -44,14 +44,14 @@ export declare abstract class Provider<D extends Datas = Datas> {
44
44
  * @return Nothing (possibly promised).
45
45
  * @throws Error If a `Transform` was provided but the document does not exist (ideally a `RequiredError` but may be provider-specific).
46
46
  */
47
- abstract write<C extends Key<D>>(ref: DatabaseDocument<C, D>, value: D[C] | Transform<D[C]> | undefined): void | PromiseLike<void>;
47
+ abstract write<T extends Data>(ref: DataDocument<T>, value: T | Transform<T> | undefined): void | PromiseLike<void>;
48
48
  /**
49
49
  * Get all matching documents.
50
50
  *
51
51
  * @param ref Documents reference specifying which collection to get documents from.
52
52
  * @return Set of results in `id: data` format.
53
53
  */
54
- abstract getQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>): Results<D[C]> | PromiseLike<Results<D[C]>>;
54
+ abstract getQuery<T extends Data>(ref: DataQuery<T>): Results<T> | PromiseLike<Results<T>>;
55
55
  /**
56
56
  * Subscribe to all matching documents.
57
57
  * - `next()` is called once with the initial results, and again any time the results change.
@@ -61,7 +61,7 @@ export declare abstract class Provider<D extends Datas = Datas> {
61
61
  *
62
62
  * @return Function that ends the subscription.
63
63
  */
64
- abstract subscribeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, observer: Observer<Results<D[C]>>): Unsubscriber;
64
+ abstract subscribeQuery<T extends Data>(ref: DataQuery<T>, observer: Observer<Results<T>>): Unsubscriber;
65
65
  /**
66
66
  * Write to all matching documents.
67
67
  *
@@ -73,21 +73,21 @@ export declare abstract class Provider<D extends Datas = Datas> {
73
73
  *
74
74
  * @return Nothing (possibly promised).
75
75
  */
76
- abstract writeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, value: D[C] | Transform<D[C]> | undefined): void | PromiseLike<void>;
76
+ abstract writeQuery<T extends Data>(ref: DataQuery<T>, value: T | Transform<T> | undefined): void | PromiseLike<void>;
77
77
  }
78
78
  /** Provider with a fully synchronous interface */
79
- export interface SynchronousProvider<D extends Datas> extends Provider<D> {
80
- get<C extends Key<D>>(ref: DatabaseDocument<C, D>): Result<D[C]>;
81
- add<C extends Key<D>>(ref: DatabaseQuery<C, D>, data: D[C]): string;
82
- write<C extends Key<D>>(ref: DatabaseDocument<C, D>, value: D[C] | Transform<D[C]> | undefined): void;
83
- getQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>): Results<D[C]>;
84
- writeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, value: D[C] | Transform<D[C]> | undefined): void;
79
+ export interface SynchronousProvider extends Provider {
80
+ get<T extends Data>(ref: DataDocument<T>): Result<T>;
81
+ add<T extends Data>(ref: DataQuery<T>, data: T): string;
82
+ write<T extends Data>(ref: DataDocument<T>, value: T | Transform<T> | undefined): void;
83
+ getQuery<T extends Data>(ref: DataQuery<T>): Results<T>;
84
+ writeQuery<T extends Data>(ref: DataQuery<T>, value: T | Transform<T> | undefined): void;
85
85
  }
86
86
  /** Provider with a fully asynchronous interface */
87
- export interface AsynchronousProvider<D extends Datas> extends Provider<D> {
88
- get<C extends Key<D>>(ref: DatabaseDocument<C, D>): PromiseLike<Result<D[C]>>;
89
- add<C extends Key<D>>(ref: DatabaseQuery<C, D>, data: D[C]): PromiseLike<string>;
90
- write<C extends Key<D>>(ref: DatabaseDocument<C, D>, value: D[C] | Transform<D[C]> | undefined): PromiseLike<void>;
91
- getQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>): PromiseLike<Results<D[C]>>;
92
- writeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, value: D[C] | Transform<D[C]> | undefined): PromiseLike<void>;
87
+ export interface AsynchronousProvider extends Provider {
88
+ get<T extends Data>(ref: DataDocument<T>): PromiseLike<Result<T>>;
89
+ add<T extends Data>(ref: DataQuery<T>, data: T): PromiseLike<string>;
90
+ write<T extends Data>(ref: DataDocument<T>, value: T | Transform<T> | undefined): PromiseLike<void>;
91
+ getQuery<T extends Data>(ref: DataQuery<T>): PromiseLike<Results<T>>;
92
+ writeQuery<T extends Data>(ref: DataQuery<T>, value: T | Transform<T> | undefined): PromiseLike<void>;
93
93
  }
@@ -1,20 +1,20 @@
1
- import type { Result, Results, Unsubscriber, Observer, Class, Datas, Key } from "../util/index.js";
2
- import type { DatabaseDocument, DatabaseQuery } from "../db/index.js";
1
+ import type { Result, Results, Unsubscriber, Observer, Class, Data } from "../util/index.js";
2
+ import type { DataDocument, DataQuery } from "../db/index.js";
3
3
  import type { Transform } from "../transform/index.js";
4
4
  import { Provider } from "./Provider.js";
5
5
  /**
6
6
  * Pass all reads and writes through to a source provider.
7
7
  */
8
- export declare class ThroughProvider<D extends Datas> extends Provider<D> {
9
- readonly source: Provider<D>;
10
- constructor(source: Provider<D>);
11
- get<C extends Key<D>>(ref: DatabaseDocument<C, D>): Result<D[C]> | PromiseLike<Result<D[C]>>;
12
- subscribe<C extends Key<D>>(ref: DatabaseDocument<C, D>, observer: Observer<Result<D[C]>>): Unsubscriber;
13
- add<C extends Key<D>>(ref: DatabaseQuery<C, D>, data: D[C]): string | PromiseLike<string>;
14
- write<C extends Key<D>>(ref: DatabaseDocument<C, D>, value: D[C] | Transform<D[C]> | undefined): void | PromiseLike<void>;
15
- getQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>): Results<D[C]> | PromiseLike<Results<D[C]>>;
16
- subscribeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, observer: Observer<Results<D[C]>>): Unsubscriber;
17
- writeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, value: D[C] | Transform<D[C]> | undefined): void | PromiseLike<void>;
8
+ export declare class ThroughProvider extends Provider {
9
+ readonly source: Provider;
10
+ constructor(source: Provider);
11
+ get<T extends Data>(ref: DataDocument<T>): Result<T> | PromiseLike<Result<T>>;
12
+ subscribe<T extends Data>(ref: DataDocument<T>, observer: Observer<Result<T>>): Unsubscriber;
13
+ add<T extends Data>(ref: DataQuery<T>, data: T): string | PromiseLike<string>;
14
+ write<T extends Data>(ref: DataDocument<T>, value: T | Transform<T> | undefined): void | PromiseLike<void>;
15
+ getQuery<T extends Data>(ref: DataQuery<T>): Results<T> | PromiseLike<Results<T>>;
16
+ subscribeQuery<T extends Data>(ref: DataQuery<T>, observer: Observer<Results<T>>): Unsubscriber;
17
+ writeQuery<T extends Data>(ref: DataQuery<T>, value: T | Transform<T> | undefined): void | PromiseLike<void>;
18
18
  }
19
19
  /** Find a specific source provider in a database's provider stack. */
20
- export declare function findSourceProvider<D extends Datas, P extends Provider<D>>(provider: Provider<D>, type: Class<P>): P;
20
+ export declare function findSourceProvider<P extends Provider>(provider: Provider, type: Class<P>): P;
@@ -1,14 +1,14 @@
1
- import type { DatabaseDocument, DatabaseQuery } from "../db/index.js";
2
- import { Result, Unsubscriber, Observer, Datas, Key, Results } from "../util/index.js";
1
+ import type { DataDocument, DataQuery } from "../db/index.js";
2
+ import { Data, Result, Unsubscriber, Observer, Results } from "../util/index.js";
3
3
  import { Transform } from "../transform/index.js";
4
4
  import { ThroughProvider } from "./ThroughProvider.js";
5
5
  /** Validates any values that are read from or written to a source provider. */
6
- export declare class ValidationProvider<D extends Datas> extends ThroughProvider<D> {
7
- get<C extends Key<D>>(ref: DatabaseDocument<C, D>): Result<D[C]> | PromiseLike<Result<D[C]>>;
8
- subscribe<C extends Key<D>>(ref: DatabaseDocument<C, D>, observer: Observer<Result>): Unsubscriber;
9
- add<C extends Key<D>>(ref: DatabaseQuery<C, D>, data: D[C]): string | PromiseLike<string>;
10
- write<C extends Key<D>>(ref: DatabaseDocument<C, D>, value: D[C] | Transform<D[C]> | undefined): void | PromiseLike<void>;
11
- getQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>): Results<D[C]> | PromiseLike<Results<D[C]>>;
12
- subscribeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, observer: Observer<Results<D[C]>>): Unsubscriber;
13
- writeQuery<C extends Key<D>>(ref: DatabaseQuery<C, D>, value: D[C] | Transform<D[C]> | undefined): void | PromiseLike<void>;
6
+ export declare class ValidationProvider extends ThroughProvider {
7
+ get<T extends Data>(ref: DataDocument<T>): Result<T> | PromiseLike<Result<T>>;
8
+ subscribe<T extends Data>(ref: DataDocument<T>, observer: Observer<Result>): Unsubscriber;
9
+ add<T extends Data>(ref: DataQuery<T>, data: T): string | PromiseLike<string>;
10
+ write<T extends Data>(ref: DataDocument<T>, value: T | Transform<T> | undefined): void | PromiseLike<void>;
11
+ getQuery<T extends Data>(ref: DataQuery<T>): Results<T> | PromiseLike<Results<T>>;
12
+ subscribeQuery<T extends Data>(ref: DataQuery<T>, observer: Observer<Results<T>>): Unsubscriber;
13
+ writeQuery<T extends Data>(ref: DataQuery<T>, value: T | Transform<T> | undefined): void | PromiseLike<void>;
14
14
  }
@@ -1,4 +1,4 @@
1
- import { DatabaseDocument, Result, Datas, Key } from "../index.js";
1
+ import { DataDocument, Result, Data } from "../index.js";
2
2
  /**
3
3
  * Use the cached result of a document in a React component (or a `Promise` to indicate the result is still loading).
4
4
  * - Requires database to use `CacheProvider` and will error if this does not exist.
@@ -13,8 +13,8 @@ import { DatabaseDocument, Result, Datas, Key } from "../index.js";
13
13
  * @trhows `Error` if a `CacheProvider` is not part of the database's provider chain.
14
14
  * @throws `Error` if there was a problem retrieving the result.
15
15
  */
16
- export declare function useAsyncDocument<D extends Datas, C extends Key<D>>(ref: DatabaseDocument<C, D>, maxAge?: number | true): Result<D[C]> | PromiseLike<Result<D[C]>>;
17
- export declare function useAsyncDocument<D extends Datas, C extends Key<D>>(ref: DatabaseDocument<C, D> | undefined, maxAge?: number | true): Result<D[C]> | PromiseLike<Result<D[C]>> | undefined;
16
+ export declare function useAsyncDocument<T extends Data>(ref: DataDocument<T>, maxAge?: number | true): Result<T> | PromiseLike<Result<T>>;
17
+ export declare function useAsyncDocument<T extends Data>(ref: DataDocument<T> | undefined, maxAge?: number | true): Result<T> | PromiseLike<Result<T>> | undefined;
18
18
  /**
19
19
  * Use the cached data of a document in a React component.
20
20
  * - Requires database to use `CacheProvider` and will error if this does not exist.
@@ -30,5 +30,5 @@ export declare function useAsyncDocument<D extends Datas, C extends Key<D>>(ref:
30
30
  * @trhows `Error` if a `CacheProvider` is not part of the database's provider chain.
31
31
  * @throws `Error` if there was a problem retrieving the result.
32
32
  */
33
- export declare function useResult<D extends Datas, C extends Key<D>>(ref: DatabaseDocument<C, D>, maxAge?: number | true): Result<D[C]>;
34
- export declare function useResult<D extends Datas, C extends Key<D>>(ref: DatabaseDocument<C, D> | undefined, maxAge?: number | true): Result<D[C]> | undefined;
33
+ export declare function useResult<T extends Data>(ref: DataDocument<T>, maxAge?: number | true): Result<T>;
34
+ export declare function useResult<T extends Data>(ref: DataDocument<T> | undefined, maxAge?: number | true): Result<T> | undefined;
@@ -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.db.provider, CacheProvider);
33
+ const provider = findSourceProvider(ref.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.db.provider, CacheProvider);
39
+ const provider = findSourceProvider(ref.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.
@@ -1,4 +1,4 @@
1
- import { DatabaseDocument, Datas, Key } from "../index.js";
1
+ import { DataDocument, Data } from "../index.js";
2
2
  /**
3
3
  * Use the cached data of a document in a React component (or a `Promise` to indicate the data is still loading).
4
4
  * - Requires database to use `CacheProvider` and will error if this does not exist.
@@ -14,8 +14,8 @@ import { DatabaseDocument, Datas, Key } from "../index.js";
14
14
  * @trhows `Error` if a `CacheProvider` is not part of the database's provider chain.
15
15
  * @throws `Error` if there was a problem retrieving the data.
16
16
  */
17
- export declare function useAsyncDocumentData<D extends Datas, C extends Key<D>>(ref: DatabaseDocument<C, D>, maxAge?: number | true): D[C] | Promise<D[C]>;
18
- export declare function useAsyncDocumentData<D extends Datas, C extends Key<D>>(ref: DatabaseDocument<C, D> | undefined, maxAge?: number | true): D[C] | PromiseLike<D[C]> | undefined;
17
+ export declare function useAsyncDocumentData<T extends Data>(ref: DataDocument<T>, maxAge?: number | true): T | Promise<T>;
18
+ export declare function useAsyncDocumentData<T extends Data>(ref: DataDocument<T> | undefined, maxAge?: number | true): T | PromiseLike<T> | undefined;
19
19
  /**
20
20
  * Use the cached data of a document in a React component.
21
21
  * - Requires database to use `CacheProvider` and will error if this does not exist.
@@ -32,5 +32,5 @@ export declare function useAsyncDocumentData<D extends Datas, C extends Key<D>>(
32
32
  * @trhows `Error` if a `CacheProvider` is not part of the database's provider chain.
33
33
  * @throws `Error` if there was a problem retrieving the data.
34
34
  */
35
- export declare function useDocumentData<D extends Datas, C extends Key<D>>(ref: DatabaseDocument<C, D>, maxAge?: number | true): D[C];
36
- export declare function useDocumentData<D extends Datas, C extends Key<D>>(ref: DatabaseDocument<C, D> | undefined, maxAge?: number | true): D[C] | undefined;
35
+ export declare function useDocumentData<T extends Data>(ref: DataDocument<T>, maxAge?: number | true): T;
36
+ export declare function useDocumentData<T extends Data>(ref: DataDocument<T> | undefined, maxAge?: number | true): T | undefined;
@@ -1,6 +1,6 @@
1
- import { DatabaseQuery, ResultsMap, Pagination, Datas, Key } from "../index.js";
1
+ import { DataQuery, ResultsMap, Pagination, Data } from "../index.js";
2
2
  /**
3
3
  * Use a `Pagination` for a collection.
4
4
  * - Doesn't persist the state, so if the component or anything beneath it throws the currently paginated results will be lost.
5
5
  */
6
- export declare function usePagination<D extends Datas, C extends Key<D>>(ref: DatabaseQuery<C, D>, initial?: ResultsMap<D[C]>): Pagination<C, D>;
6
+ export declare function usePagination<T extends Data>(ref: DataQuery<T>, initial?: ResultsMap<T>): Pagination<T>;
@@ -1,4 +1,4 @@
1
- import { DatabaseQuery, Datas, Key, ResultsMap } from "../index.js";
1
+ import { DataQuery, ResultsMap, Data } from "../index.js";
2
2
  /**
3
3
  * Use the cached result of a document in a React component (or a `Promise` to indicate the result is still loading).
4
4
  * - Requires database to use `CacheProvider` and will error if this does not exist.
@@ -14,8 +14,8 @@ import { DatabaseQuery, Datas, Key, ResultsMap } from "../index.js";
14
14
  * @trhows `Error` if a `CacheProvider` is not part of the database's provider chain.
15
15
  * @throws `Error` if there was a problem retrieving the results.
16
16
  */
17
- export declare function useAsyncQuery<D extends Datas, C extends Key<D>>(ref: DatabaseQuery<C, D>, maxAge?: number | true): ResultsMap<D[C]> | PromiseLike<ResultsMap<D[C]>>;
18
- export declare function useAsyncQuery<D extends Datas, C extends Key<D>>(ref: DatabaseQuery<C, D> | undefined, maxAge?: number | true): ResultsMap<D[C]> | PromiseLike<ResultsMap<D[C]>> | undefined;
17
+ export declare function useAsyncQuery<T extends Data>(ref: DataQuery<T>, maxAge?: number | true): ResultsMap<T> | PromiseLike<ResultsMap<T>>;
18
+ export declare function useAsyncQuery<T extends Data>(ref: DataQuery<T> | undefined, maxAge?: number | true): ResultsMap<T> | PromiseLike<ResultsMap<T>> | undefined;
19
19
  /**
20
20
  * Use the cached results of a set of documents in a React component.
21
21
  * - Requires database to use `CacheProvider` and will error if this does not exist.
@@ -31,5 +31,5 @@ export declare function useAsyncQuery<D extends Datas, C extends Key<D>>(ref: Da
31
31
  * @trhows `Error` if a `CacheProvider` is not part of the database's provider chain.
32
32
  * @throws `Error` if there was a problem retrieving the results.
33
33
  */
34
- export declare function useResults<D extends Datas, C extends Key<D>>(ref: DatabaseQuery<C, D>, maxAge?: number | true): ResultsMap<D[C]>;
35
- export declare function useResults<D extends Datas, C extends Key<D>>(ref: DatabaseQuery<C, D> | undefined, maxAge?: number | true): ResultsMap<D[C]> | undefined;
34
+ export declare function useResults<T extends Data>(ref: DataQuery<T>, maxAge?: number | true): ResultsMap<T>;
35
+ export declare function useResults<T extends Data>(ref: DataQuery<T> | undefined, maxAge?: number | true): ResultsMap<T> | undefined;
package/react/useQuery.js CHANGED
@@ -30,13 +30,13 @@ export function useAsyncQuery(ref, maxAge = 1000) {
30
30
  function getCachedResults(ref) {
31
31
  if (!ref)
32
32
  return undefined;
33
- const provider = findSourceProvider(ref.db.provider, CacheProvider);
33
+ const provider = findSourceProvider(ref.provider, CacheProvider);
34
34
  return provider.isCached(ref) ? toMap(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.db.provider, CacheProvider);
39
+ const provider = findSourceProvider(ref.provider, CacheProvider);
40
40
  const observer = new DeriveObserver(toMap, { next, error });
41
41
  const stopCache = provider.cache.subscribeQuery(ref, observer);
42
42
  if (maxAge === true) {
@@ -24,12 +24,14 @@ export declare type ValidatorType<X extends AnyValidator> = X extends Validator<
24
24
  export declare const isValidator: <T extends AnyValidator>(v: unknown) => v is T;
25
25
  /** A set of named validators in `{ name: Validator }` format. */
26
26
  export declare type Validators<T extends Data> = {
27
- readonly [K in keyof T]: Validator<T[K]>;
27
+ [K in keyof T]: Validator<T[K]>;
28
28
  };
29
29
  /** Any observer (useful for `extends AnyValidators` clauses). */
30
30
  export declare type AnyValidators = Validators<any>;
31
31
  /** Extract the type from a set of validators. */
32
- export declare type ValidatorsType<O extends AnyValidators> = O extends Validators<infer T> ? T : never;
32
+ export declare type ValidatorsType<T extends AnyValidators> = {
33
+ [K in keyof T]: ValidatorType<T[K]>;
34
+ };
33
35
  /** Validate an unknown value with a validator. */
34
36
  export declare function validate<T>(unsafeValue: unknown, validator: Validator<T>): T;
35
37
  /**