shelving 1.40.0 → 1.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/db/Database.d.ts +10 -12
- package/db/Database.js +11 -23
- package/db/index.d.ts +0 -1
- package/db/index.js +0 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/operation/AddOperation.d.ts +15 -0
- package/operation/AddOperation.js +23 -0
- package/operation/DeleteOperation.d.ts +9 -0
- package/operation/DeleteOperation.js +13 -0
- package/operation/Operation.d.ts +6 -0
- package/operation/Operation.js +3 -0
- package/operation/Operations.d.ts +21 -0
- package/operation/Operations.js +33 -0
- package/operation/SetOperation.d.ts +15 -0
- package/operation/SetOperation.js +23 -0
- package/operation/UpdateOperation.d.ts +16 -0
- package/operation/UpdateOperation.js +23 -0
- package/operation/WriteOperation.d.ts +16 -0
- package/operation/WriteOperation.js +25 -0
- package/operation/hydrations.d.ts +13 -0
- package/operation/hydrations.js +14 -0
- package/operation/index.d.ts +8 -0
- package/operation/index.js +8 -0
- package/package.json +1 -1
- 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/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,10 +17,15 @@ 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>> {
|
|
@@ -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
|
*
|
|
@@ -25,15 +24,16 @@ export class Database {
|
|
|
25
24
|
doc(collection, id) {
|
|
26
25
|
return new DatabaseDocument(this.provider, this.validators[collection], collection, id);
|
|
27
26
|
}
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Create a new document with a random ID.
|
|
29
|
+
* - Created document is guaranteed to have a unique ID.
|
|
30
|
+
*
|
|
31
|
+
* @param collection Name of the collection to add the document to.
|
|
32
|
+
* @param data Complete data to set the document to.
|
|
33
|
+
* @return String ID for the created document (possibly promised).
|
|
34
|
+
*/
|
|
35
|
+
add(collection, data) {
|
|
36
|
+
return this.query(collection).add(data);
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
/** A documents reference within a specific database. */
|
|
@@ -235,18 +235,6 @@ export class DatabaseDocument {
|
|
|
235
235
|
delete() {
|
|
236
236
|
return this.provider.delete(this);
|
|
237
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);
|
|
249
|
-
}
|
|
250
238
|
/** Validate data for this query reference. */
|
|
251
239
|
validate(unsafeData) {
|
|
252
240
|
try {
|
package/db/index.d.ts
CHANGED
package/db/index.js
CHANGED
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -15,6 +15,7 @@ export * from "./feedback/index.js";
|
|
|
15
15
|
export * from "./markup/index.js";
|
|
16
16
|
export * from "./stream/index.js";
|
|
17
17
|
export * from "./update/index.js";
|
|
18
|
+
export * from "./operation/index.js";
|
|
18
19
|
export * from "./util/index.js";
|
|
19
20
|
// Integrations.
|
|
20
21
|
// export * from "./react/index.js"; // Not exported.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Database, DatabaseQuery } from "../db/index.js";
|
|
2
|
+
import { Data, Nullish, Key } from "../util/index.js";
|
|
3
|
+
import { SetOperation } from "./SetOperation.js";
|
|
4
|
+
import { WriteOperation } from "./WriteOperation.js";
|
|
5
|
+
/** Represent a add operation made to a collection in a database. */
|
|
6
|
+
export declare class AddOperation<T extends Data> extends WriteOperation {
|
|
7
|
+
readonly collection: string;
|
|
8
|
+
readonly data: T;
|
|
9
|
+
constructor(collection: string, data: T);
|
|
10
|
+
run(db: Database): Promise<SetOperation<T>>;
|
|
11
|
+
/** Set one of the props on this set operation to a different value. */
|
|
12
|
+
with<K extends Key<T>>(key: Nullish<K>, value: T[K]): this;
|
|
13
|
+
}
|
|
14
|
+
/** Create an add operation for a document. */
|
|
15
|
+
export declare const ADD: <T extends Data>({ collection }: DatabaseQuery<T>, data: T) => AddOperation<T>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isNullish } from "../util/index.js";
|
|
2
|
+
import { SetOperation } from "./SetOperation.js";
|
|
3
|
+
import { WriteOperation } from "./WriteOperation.js";
|
|
4
|
+
/** Represent a add operation made to a collection in a database. */
|
|
5
|
+
export class AddOperation extends WriteOperation {
|
|
6
|
+
constructor(collection, data) {
|
|
7
|
+
super();
|
|
8
|
+
this.collection = collection;
|
|
9
|
+
this.data = data;
|
|
10
|
+
}
|
|
11
|
+
async run(db) {
|
|
12
|
+
const id = await db.query(this.collection).add(this.data);
|
|
13
|
+
return new SetOperation(this.collection, id, this.data); // When an add operation is run it returns a set operation so the operation is repeatable.
|
|
14
|
+
}
|
|
15
|
+
/** Set one of the props on this set operation to a different value. */
|
|
16
|
+
with(key, value) {
|
|
17
|
+
if (isNullish(key))
|
|
18
|
+
return this;
|
|
19
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, data: { ...this.data, [key]: value } };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Create an add operation for a document. */
|
|
23
|
+
export const ADD = ({ collection }, data) => new AddOperation(collection, data);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Database } from "../db/Database.js";
|
|
2
|
+
import { WriteOperation } from "./WriteOperation.js";
|
|
3
|
+
/** Represent a delete operation made to a single document in a database. */
|
|
4
|
+
export declare class DeleteOperation extends WriteOperation {
|
|
5
|
+
readonly collection: string;
|
|
6
|
+
readonly id: string;
|
|
7
|
+
constructor(collection: string, id: string);
|
|
8
|
+
run(db: Database): Promise<this>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { WriteOperation } from "./WriteOperation.js";
|
|
2
|
+
/** Represent a delete operation made to a single document in a database. */
|
|
3
|
+
export class DeleteOperation extends WriteOperation {
|
|
4
|
+
constructor(collection, id) {
|
|
5
|
+
super();
|
|
6
|
+
this.collection = collection;
|
|
7
|
+
this.id = id;
|
|
8
|
+
}
|
|
9
|
+
async run(db) {
|
|
10
|
+
await db.doc(this.collection, this.id).delete();
|
|
11
|
+
return this;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Arguments } from "../index.js";
|
|
2
|
+
/** Represent an async operation. */
|
|
3
|
+
export declare abstract class Operation<A extends Arguments = []> {
|
|
4
|
+
/** Run this operation against a database and return the operation that was completed (which may be this same `Operation` instance). */
|
|
5
|
+
abstract run(...args: A): Promise<Operation<A>>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ImmutableArray, Nullish, Arguments } from "../util/index.js";
|
|
2
|
+
import { Operation } from "./Operation.js";
|
|
3
|
+
/** Represent a list of operations that take the same arguments. */
|
|
4
|
+
export declare abstract class Operations<A extends Arguments> extends Operation<A> {
|
|
5
|
+
readonly operations: ImmutableArray<Operation<A>>;
|
|
6
|
+
constructor(...operations: Nullish<Operation<A>>[]);
|
|
7
|
+
/** Return a new instance of this object with an additional operation added. */
|
|
8
|
+
with(operation: Nullish<Operation<A>>): this;
|
|
9
|
+
}
|
|
10
|
+
/** Represent a list of operations run in series. */
|
|
11
|
+
export declare class SeriesOperations<A extends Arguments> extends Operations<A> {
|
|
12
|
+
run(...args: A): Promise<SeriesOperations<A>>;
|
|
13
|
+
}
|
|
14
|
+
/** Create a list of operations to be run. */
|
|
15
|
+
export declare const SERIES: <A extends Arguments>(...operations: Operation<A>[]) => SeriesOperations<A>;
|
|
16
|
+
/** Represent a list of operations run in series. */
|
|
17
|
+
export declare class ParallelOperations<A extends Arguments> extends Operations<A> {
|
|
18
|
+
run(...args: A): Promise<ParallelOperations<A>>;
|
|
19
|
+
}
|
|
20
|
+
/** Create a list of operations to be run. */
|
|
21
|
+
export declare const PARALLEL: <A extends Arguments>(...operations: Operation<A>[]) => ParallelOperations<A>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { callAsyncSeries, callAsyncParallel, isNullish, isNotNullish } from "../util/index.js";
|
|
2
|
+
import { Operation } from "./Operation.js";
|
|
3
|
+
// Run an operation on to a database.
|
|
4
|
+
const _runOperation = (operation, ...args) => operation.run(...args);
|
|
5
|
+
/** Represent a list of operations that take the same arguments. */
|
|
6
|
+
export class Operations extends Operation {
|
|
7
|
+
constructor(...operations) {
|
|
8
|
+
super();
|
|
9
|
+
this.operations = operations.filter(isNotNullish);
|
|
10
|
+
}
|
|
11
|
+
/** Return a new instance of this object with an additional operation added. */
|
|
12
|
+
with(operation) {
|
|
13
|
+
if (isNullish(operation))
|
|
14
|
+
return this;
|
|
15
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, operations: [...this.operations, operation] };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** Represent a list of operations run in series. */
|
|
19
|
+
export class SeriesOperations extends Operations {
|
|
20
|
+
async run(...args) {
|
|
21
|
+
return new SeriesOperations(...(await callAsyncSeries(_runOperation, this.operations, ...args)));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Create a list of operations to be run. */
|
|
25
|
+
export const SERIES = (...operations) => new SeriesOperations(...operations);
|
|
26
|
+
/** Represent a list of operations run in series. */
|
|
27
|
+
export class ParallelOperations extends Operations {
|
|
28
|
+
async run(...args) {
|
|
29
|
+
return new ParallelOperations(...(await callAsyncParallel(_runOperation, this.operations, ...args)));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** Create a list of operations to be run. */
|
|
33
|
+
export const PARALLEL = (...operations) => new ParallelOperations(...operations);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Database, DatabaseDocument } from "../db/index.js";
|
|
2
|
+
import { Data, Key, Nullish } from "../util/index.js";
|
|
3
|
+
import { WriteOperation } from "./WriteOperation.js";
|
|
4
|
+
/** Represent a set operation made to a single document in a database. */
|
|
5
|
+
export declare class SetOperation<T extends Data> extends WriteOperation {
|
|
6
|
+
readonly collection: string;
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly data: T;
|
|
9
|
+
constructor(collection: string, id: string, data: T);
|
|
10
|
+
run(db: Database): Promise<this>;
|
|
11
|
+
/** Set one of the props on this set operation to a different value. */
|
|
12
|
+
with<K extends Key<T>>(key: Nullish<K>, value: T[K]): this;
|
|
13
|
+
}
|
|
14
|
+
/** Create a set operation for a document. */
|
|
15
|
+
export declare const SET: <T extends Data>({ collection, id }: DatabaseDocument<T>, data: T) => SetOperation<T>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isNullish } from "../util/index.js";
|
|
2
|
+
import { WriteOperation } from "./WriteOperation.js";
|
|
3
|
+
/** Represent a set operation made to a single document in a database. */
|
|
4
|
+
export class SetOperation extends WriteOperation {
|
|
5
|
+
constructor(collection, id, data) {
|
|
6
|
+
super();
|
|
7
|
+
this.collection = collection;
|
|
8
|
+
this.id = id;
|
|
9
|
+
this.data = data;
|
|
10
|
+
}
|
|
11
|
+
async run(db) {
|
|
12
|
+
await db.doc(this.collection, this.id).set(this.data);
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
/** Set one of the props on this set operation to a different value. */
|
|
16
|
+
with(key, value) {
|
|
17
|
+
if (isNullish(key))
|
|
18
|
+
return this;
|
|
19
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, data: { ...this.data, [key]: value } };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Create a set operation for a document. */
|
|
23
|
+
export const SET = ({ collection, id }, data) => new SetOperation(collection, id, data);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Database, DatabaseDocument } from "../db/index.js";
|
|
2
|
+
import type { PropUpdates, Update } from "../update/index.js";
|
|
3
|
+
import { Data, Key, Nullish } from "../util/index.js";
|
|
4
|
+
import { WriteOperation } from "./WriteOperation.js";
|
|
5
|
+
/** Represent an update operation made to a single document in a database. */
|
|
6
|
+
export declare class UpdateOperation<T extends Data> extends WriteOperation {
|
|
7
|
+
readonly collection: string;
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly updates: PropUpdates<T>;
|
|
10
|
+
constructor(collection: string, id: string, updates: PropUpdates<T>);
|
|
11
|
+
run(db: Database): Promise<this>;
|
|
12
|
+
/** update one of the props on this set operation to a different value. */
|
|
13
|
+
with<K extends Key<T>>(key: Nullish<K>, value: T[K] | Update<T[K]>): this;
|
|
14
|
+
}
|
|
15
|
+
/** Create an update operation for a document. */
|
|
16
|
+
export declare const UPDATE: <T extends Data>({ collection, id }: DatabaseDocument<T>, updates: PropUpdates<T>) => UpdateOperation<T>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { isNullish } from "../util/index.js";
|
|
2
|
+
import { WriteOperation } from "./WriteOperation.js";
|
|
3
|
+
/** Represent an update operation made to a single document in a database. */
|
|
4
|
+
export class UpdateOperation extends WriteOperation {
|
|
5
|
+
constructor(collection, id, updates) {
|
|
6
|
+
super();
|
|
7
|
+
this.collection = collection;
|
|
8
|
+
this.id = id;
|
|
9
|
+
this.updates = updates;
|
|
10
|
+
}
|
|
11
|
+
async run(db) {
|
|
12
|
+
await db.doc(this.collection, this.id).update(this.updates);
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
/** update one of the props on this set operation to a different value. */
|
|
16
|
+
with(key, value) {
|
|
17
|
+
if (isNullish(key))
|
|
18
|
+
return this;
|
|
19
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, updates: { ...this.updates, [key]: value } };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Create an update operation for a document. */
|
|
23
|
+
export const UPDATE = ({ collection, id }, updates) => new UpdateOperation(collection, id, updates);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Database } from "../db/Database.js";
|
|
2
|
+
import { ImmutableArray, Nullish } from "../util/index.js";
|
|
3
|
+
import { Operation } from "./Operation.js";
|
|
4
|
+
/** Represent a write operation on a database. */
|
|
5
|
+
export declare abstract class WriteOperation extends Operation<[Database]> {
|
|
6
|
+
}
|
|
7
|
+
/** Represent a list of write operations on a database run in series. */
|
|
8
|
+
export declare class WriteOperations extends WriteOperation {
|
|
9
|
+
readonly operations: ImmutableArray<WriteOperation>;
|
|
10
|
+
constructor(...operations: Nullish<WriteOperation>[]);
|
|
11
|
+
run(db: Database): Promise<WriteOperations>;
|
|
12
|
+
/** Return a new instance of this object with an additional operation added. */
|
|
13
|
+
with(operation: Nullish<WriteOperation>): this;
|
|
14
|
+
}
|
|
15
|
+
/** Create a list of operations to be run. */
|
|
16
|
+
export declare const WRITES: (...operations: WriteOperation[]) => WriteOperations;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { callAsyncSeries, isNullish, isNotNullish } from "../util/index.js";
|
|
2
|
+
import { Operation } from "./Operation.js";
|
|
3
|
+
/** Represent a write operation on a database. */
|
|
4
|
+
export class WriteOperation extends Operation {
|
|
5
|
+
}
|
|
6
|
+
/** Represent a list of write operations on a database run in series. */
|
|
7
|
+
export class WriteOperations extends WriteOperation {
|
|
8
|
+
constructor(...operations) {
|
|
9
|
+
super();
|
|
10
|
+
this.operations = operations.filter(isNotNullish);
|
|
11
|
+
}
|
|
12
|
+
async run(db) {
|
|
13
|
+
return new WriteOperations(...(await callAsyncSeries(_write, this.operations, db)));
|
|
14
|
+
}
|
|
15
|
+
/** Return a new instance of this object with an additional operation added. */
|
|
16
|
+
with(operation) {
|
|
17
|
+
if (isNullish(operation))
|
|
18
|
+
return this;
|
|
19
|
+
return { __proto__: Object.getPrototypeOf(this), ...this, operations: [...this.operations, operation] };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
// Run an operation on to a database.
|
|
23
|
+
const _write = (operation, db) => operation.run(db);
|
|
24
|
+
/** Create a list of operations to be run. */
|
|
25
|
+
export const WRITES = (...operations) => new WriteOperations(...operations);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AddOperation } from "./AddOperation.js";
|
|
2
|
+
import { DeleteOperation } from "./DeleteOperation.js";
|
|
3
|
+
import { SetOperation } from "./SetOperation.js";
|
|
4
|
+
import { UpdateOperation } from "./UpdateOperation.js";
|
|
5
|
+
import { WriteOperations } from "./WriteOperation.js";
|
|
6
|
+
/** Set of hydrations for all change classes. */
|
|
7
|
+
export declare const OPERATION_HYDRATIONS: {
|
|
8
|
+
WriteOperations: typeof WriteOperations;
|
|
9
|
+
AddOperation: typeof AddOperation;
|
|
10
|
+
SetOperation: typeof SetOperation;
|
|
11
|
+
UpdateOperation: typeof UpdateOperation;
|
|
12
|
+
DeleteOperation: typeof DeleteOperation;
|
|
13
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AddOperation } from "./AddOperation.js";
|
|
2
|
+
import { DeleteOperation } from "./DeleteOperation.js";
|
|
3
|
+
import { SetOperation } from "./SetOperation.js";
|
|
4
|
+
import { UpdateOperation } from "./UpdateOperation.js";
|
|
5
|
+
import { WriteOperations } from "./WriteOperation.js";
|
|
6
|
+
/** Set of hydrations for all change classes. */
|
|
7
|
+
export const OPERATION_HYDRATIONS = {
|
|
8
|
+
WriteOperations,
|
|
9
|
+
AddOperation,
|
|
10
|
+
SetOperation,
|
|
11
|
+
UpdateOperation,
|
|
12
|
+
DeleteOperation,
|
|
13
|
+
};
|
|
14
|
+
OPERATION_HYDRATIONS;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./Operation.js";
|
|
2
|
+
export * from "./Operations.js";
|
|
3
|
+
export * from "./WriteOperation.js";
|
|
4
|
+
export * from "./AddOperation.js";
|
|
5
|
+
export * from "./DeleteOperation.js";
|
|
6
|
+
export * from "./SetOperation.js";
|
|
7
|
+
export * from "./UpdateOperation.js";
|
|
8
|
+
export * from "./hydrations.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./Operation.js";
|
|
2
|
+
export * from "./Operations.js";
|
|
3
|
+
export * from "./WriteOperation.js";
|
|
4
|
+
export * from "./AddOperation.js";
|
|
5
|
+
export * from "./DeleteOperation.js";
|
|
6
|
+
export * from "./SetOperation.js";
|
|
7
|
+
export * from "./UpdateOperation.js";
|
|
8
|
+
export * from "./hydrations.js";
|
package/package.json
CHANGED
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
|
}
|
package/update/DataUpdate.js
CHANGED
|
@@ -10,7 +10,7 @@ export class DataUpdate extends Update {
|
|
|
10
10
|
return transformProps(existing, this.updates);
|
|
11
11
|
}
|
|
12
12
|
/** Return a new object with the specified additional transform for a prop. */
|
|
13
|
-
|
|
13
|
+
with(key, value) {
|
|
14
14
|
if (isNullish(key))
|
|
15
15
|
return this;
|
|
16
16
|
return { __proto__: Object.getPrototypeOf(this), ...this, updates: { ...this.updates, [key]: value } };
|
package/update/ObjectUpdate.d.ts
CHANGED
|
@@ -5,17 +5,17 @@ export declare type EntryUpdates<T> = ImmutableObject<T | Update<T>>;
|
|
|
5
5
|
/** Update that can be applied to a map-like object to add/remove/update its entries. */
|
|
6
6
|
export declare class ObjectUpdate<T> extends Update<ImmutableObject<T>> implements Iterable<Entry<T | Update<T> | undefined>> {
|
|
7
7
|
/** Return an object update with a specific entry marked for update. */
|
|
8
|
-
static
|
|
8
|
+
static with<X>(key: string | undefined | null, value: X | Update<X>): ObjectUpdate<X>;
|
|
9
9
|
/** Return an object update with a specific entry marked for deletion. */
|
|
10
|
-
static
|
|
10
|
+
static without<X>(key: string | undefined | null): ObjectUpdate<X>;
|
|
11
11
|
readonly updates: EntryUpdates<T>;
|
|
12
12
|
readonly deletes: ImmutableArray<string>;
|
|
13
13
|
constructor(updates?: EntryUpdates<T>, deletes?: ImmutableArray<string>);
|
|
14
14
|
transform(existing: unknown): ImmutableObject<T>;
|
|
15
15
|
/** Return an object update with a specific entry marked for update. */
|
|
16
|
-
|
|
16
|
+
with(key: Nullish<string>, value: T | Update<T>): this;
|
|
17
17
|
/** Return an object update with a specific entry marked for deletion. */
|
|
18
|
-
|
|
18
|
+
without(key: Nullish<string>): this;
|
|
19
19
|
/**
|
|
20
20
|
* Iterate over the changes in this object.
|
|
21
21
|
* - Updates are yielded first, then deletes.
|
package/update/ObjectUpdate.js
CHANGED
|
@@ -8,11 +8,11 @@ export class ObjectUpdate extends Update {
|
|
|
8
8
|
this.deletes = deletes;
|
|
9
9
|
}
|
|
10
10
|
/** Return an object update with a specific entry marked for update. */
|
|
11
|
-
static
|
|
11
|
+
static with(key, value) {
|
|
12
12
|
return new ObjectUpdate(isNullish(key) ? {} : { [key]: value });
|
|
13
13
|
}
|
|
14
14
|
/** Return an object update with a specific entry marked for deletion. */
|
|
15
|
-
static
|
|
15
|
+
static without(key) {
|
|
16
16
|
return new ObjectUpdate({}, isNullish(key) ? [] : [key]);
|
|
17
17
|
}
|
|
18
18
|
transform(existing) {
|
|
@@ -20,13 +20,13 @@ export class ObjectUpdate extends Update {
|
|
|
20
20
|
return transformEntries(existingObject, this.updates, this.deletes);
|
|
21
21
|
}
|
|
22
22
|
/** Return an object update with a specific entry marked for update. */
|
|
23
|
-
|
|
23
|
+
with(key, value) {
|
|
24
24
|
if (isNullish(key))
|
|
25
25
|
return this;
|
|
26
26
|
return { __proto__: Object.getPrototypeOf(this), ...this, sets: { ...this.updates, [key]: value } };
|
|
27
27
|
}
|
|
28
28
|
/** Return an object update with a specific entry marked for deletion. */
|
|
29
|
-
|
|
29
|
+
without(key) {
|
|
30
30
|
if (isNullish(key))
|
|
31
31
|
return this;
|
|
32
32
|
return { __proto__: Object.getPrototypeOf(this), ...this, deletes: [...this.deletes, key] };
|
package/util/async.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import type { ImmutableArray } from "./array.js";
|
|
2
|
+
import type { Arguments, Dispatcher } from "./function.js";
|
|
1
3
|
import { DONE } from "./constants.js";
|
|
2
4
|
import { Handler } from "./error.js";
|
|
3
|
-
import type { Arguments, Dispatcher } from "./function.js";
|
|
4
5
|
/**
|
|
5
6
|
* Throw the value if it's an async (promised) value.
|
|
6
7
|
* @returns Synchronous (not promised) value.
|
|
@@ -9,9 +10,32 @@ import type { Arguments, Dispatcher } from "./function.js";
|
|
|
9
10
|
export declare function throwAsync<T>(asyncValue: T | PromiseLike<T>): T;
|
|
10
11
|
/** Is a value an async (promised) value. */
|
|
11
12
|
export declare const isAsync: <T>(v: T | PromiseLike<T>) => v is PromiseLike<T>;
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Call a callback with an item.
|
|
15
|
+
* - If neither `callback` or `item` are async then the value returned will be synchronous.
|
|
16
|
+
*
|
|
17
|
+
* @param callback The sync or async function to call.
|
|
18
|
+
* @param item The first argument for `callback` (if this value is async it will be awaited before `callback` is called).
|
|
19
|
+
* @param ...args Additional arguments for `callback`
|
|
20
|
+
*/
|
|
21
|
+
export declare const callAsync: <I, O, A extends Arguments = []>(callback: (v: I, ...a: A) => O | PromiseLike<O>, item: I | PromiseLike<I>, ...args: A) => O | PromiseLike<O>;
|
|
22
|
+
export declare const _awaitCallAsync: <I, O, A extends Arguments>(callback: (v: I, ...a: A) => O | PromiseLike<O>, item: PromiseLike<I>, args: A) => Promise<O>;
|
|
23
|
+
/**
|
|
24
|
+
* Call a callback for a set of items in series.
|
|
25
|
+
*
|
|
26
|
+
* @param callback The sync or async function to call for each item.
|
|
27
|
+
* @param items The set of first arguments for `callback` (if this value is async it will be awaited before `callback` is called).
|
|
28
|
+
* @param ...args Additional arguments for `callback`
|
|
29
|
+
*/
|
|
30
|
+
export declare function callAsyncSeries<I, O, A extends Arguments = []>(callback: (item: I, ...a: A) => O | PromiseLike<O>, items: Iterable<I | PromiseLike<I>>, ...args: A): Promise<ImmutableArray<O>>;
|
|
31
|
+
/**
|
|
32
|
+
* Call a callback for a set of items in parallel.
|
|
33
|
+
*
|
|
34
|
+
* @param callback The sync or async function to call for each item.
|
|
35
|
+
* @param items The set of first arguments for `callback` (if this value is async it will be awaited before `callback` is called).
|
|
36
|
+
* @param ...args Additional arguments for `callback`
|
|
37
|
+
*/
|
|
38
|
+
export declare function callAsyncParallel<I, O, A extends Arguments = []>(callback: (item: I, ...a: A) => O | PromiseLike<O>, items: Iterable<I | PromiseLike<I>>, ...args: A): Promise<ImmutableArray<O>>;
|
|
15
39
|
/** Type of `Promise` with its `resolve()` and `reject()` methods exposed publicly. */
|
|
16
40
|
export declare class Deferred<T> extends Promise<T> {
|
|
17
41
|
static get [Symbol.species](): PromiseConstructor;
|
package/util/async.js
CHANGED
|
@@ -11,9 +11,42 @@ export function throwAsync(asyncValue) {
|
|
|
11
11
|
}
|
|
12
12
|
/** Is a value an async (promised) value. */
|
|
13
13
|
export const isAsync = (v) => typeof v === "object" && v !== null && typeof v.then === "function";
|
|
14
|
-
/**
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Call a callback with an item.
|
|
16
|
+
* - If neither `callback` or `item` are async then the value returned will be synchronous.
|
|
17
|
+
*
|
|
18
|
+
* @param callback The sync or async function to call.
|
|
19
|
+
* @param item The first argument for `callback` (if this value is async it will be awaited before `callback` is called).
|
|
20
|
+
* @param ...args Additional arguments for `callback`
|
|
21
|
+
*/
|
|
22
|
+
export const callAsync = (callback, item, ...args) => isAsync(item) ? _awaitCallAsync(callback, item, args) : callback(item, ...args);
|
|
23
|
+
export const _awaitCallAsync = async (callback, item, args) => callback(await item, ...args);
|
|
24
|
+
/**
|
|
25
|
+
* Call a callback for a set of items in series.
|
|
26
|
+
*
|
|
27
|
+
* @param callback The sync or async function to call for each item.
|
|
28
|
+
* @param items The set of first arguments for `callback` (if this value is async it will be awaited before `callback` is called).
|
|
29
|
+
* @param ...args Additional arguments for `callback`
|
|
30
|
+
*/
|
|
31
|
+
export async function callAsyncSeries(callback, items, ...args) {
|
|
32
|
+
const outputs = [];
|
|
33
|
+
for (const item of items)
|
|
34
|
+
outputs.push(await callback(await item, ...args));
|
|
35
|
+
return outputs;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Call a callback for a set of items in parallel.
|
|
39
|
+
*
|
|
40
|
+
* @param callback The sync or async function to call for each item.
|
|
41
|
+
* @param items The set of first arguments for `callback` (if this value is async it will be awaited before `callback` is called).
|
|
42
|
+
* @param ...args Additional arguments for `callback`
|
|
43
|
+
*/
|
|
44
|
+
export async function callAsyncParallel(callback, items, ...args) {
|
|
45
|
+
const outputs = [];
|
|
46
|
+
for (const item of await Promise.all(items))
|
|
47
|
+
outputs.push(callback(item, ...args));
|
|
48
|
+
return Promise.all(outputs);
|
|
49
|
+
}
|
|
17
50
|
// Internal way for us to save `resolve()` and `reject()` from a new Promise used by `Deferred` and `ExtendablePromise`
|
|
18
51
|
let resolve; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
19
52
|
let reject;
|
package/db/Write.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { PropUpdates, Update } from "../update/index.js";
|
|
2
|
-
import { ImmutableArray, Data, Transformable, Key, Nullish } from "../util/index.js";
|
|
3
|
-
import type { Database, DatabaseDocument } from "./Database.js";
|
|
4
|
-
/** Represent a write made to a database. */
|
|
5
|
-
export declare abstract class Write implements Transformable<Database, void | PromiseLike<void>> {
|
|
6
|
-
abstract transform(db: Database): void | PromiseLike<void>;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Represent a list of writes made to a database.
|
|
10
|
-
* - Sets of writes are predictable and repeatable, so unpredictable operations like `create()` and query operations are not supported.
|
|
11
|
-
* - Every write must be applied to a specific database document in a specific collection and are applied in the specified order.
|
|
12
|
-
*/
|
|
13
|
-
export declare class Writes extends Write {
|
|
14
|
-
readonly writes: ImmutableArray<Write>;
|
|
15
|
-
constructor(...writes: Write[]);
|
|
16
|
-
transform(db: Database): Promise<void>;
|
|
17
|
-
}
|
|
18
|
-
/** Represent a write made to a single document in a database. */
|
|
19
|
-
export declare abstract class DocumentWrite<T extends Data> extends Write {
|
|
20
|
-
readonly collection: string;
|
|
21
|
-
readonly id: string;
|
|
22
|
-
constructor({ collection, id }: DatabaseDocument<T>);
|
|
23
|
-
}
|
|
24
|
-
/** Represent a set operation made to a single document in a database. */
|
|
25
|
-
export declare class DocumentSet<T extends Data> extends DocumentWrite<T> {
|
|
26
|
-
readonly data: T;
|
|
27
|
-
constructor(ref: DatabaseDocument<T>, data: T);
|
|
28
|
-
transform(db: Database): Promise<void>;
|
|
29
|
-
/** Set one of the props on this set operation to a different value. */
|
|
30
|
-
set<K extends Key<T>>(key: Nullish<K>, value: T[K]): this;
|
|
31
|
-
}
|
|
32
|
-
/** Represent an update operation made to a single document in a database. */
|
|
33
|
-
export declare class DocumentUpdate<T extends Data> extends DocumentWrite<T> {
|
|
34
|
-
readonly updates: PropUpdates<T>;
|
|
35
|
-
constructor(ref: DatabaseDocument<T>, updates: PropUpdates<T>);
|
|
36
|
-
transform(db: Database): Promise<void>;
|
|
37
|
-
/** update one of the props on this set operation to a different value. */
|
|
38
|
-
update<K extends Key<T>>(key: Nullish<K>, value: T[K] | Update<T[K]>): this;
|
|
39
|
-
}
|
|
40
|
-
/** Represent a delete operation made to a single document in a database. */
|
|
41
|
-
export declare class DocumentDelete<T extends Data> extends DocumentWrite<T> {
|
|
42
|
-
transform(db: Database): Promise<void>;
|
|
43
|
-
}
|
|
44
|
-
/** Set of hydrations for all change classes. */
|
|
45
|
-
export declare const WRITE_HYDRATIONS: {
|
|
46
|
-
Writes: typeof Writes;
|
|
47
|
-
DocumentSet: typeof DocumentSet;
|
|
48
|
-
DocumentUpdate: typeof DocumentUpdate;
|
|
49
|
-
DocumentDelete: typeof DocumentDelete;
|
|
50
|
-
};
|
package/db/Write.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { transform, isNullish } from "../util/index.js";
|
|
2
|
-
/** Represent a write made to a database. */
|
|
3
|
-
export class Write {
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Represent a list of writes made to a database.
|
|
7
|
-
* - Sets of writes are predictable and repeatable, so unpredictable operations like `create()` and query operations are not supported.
|
|
8
|
-
* - Every write must be applied to a specific database document in a specific collection and are applied in the specified order.
|
|
9
|
-
*/
|
|
10
|
-
export class Writes extends Write {
|
|
11
|
-
constructor(...writes) {
|
|
12
|
-
super();
|
|
13
|
-
this.writes = writes;
|
|
14
|
-
}
|
|
15
|
-
async transform(db) {
|
|
16
|
-
for (const writes of this.writes)
|
|
17
|
-
await transform(db, writes);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
/** Represent a write made to a single document in a database. */
|
|
21
|
-
export class DocumentWrite extends Write {
|
|
22
|
-
constructor({ collection, id }) {
|
|
23
|
-
super();
|
|
24
|
-
this.collection = collection;
|
|
25
|
-
this.id = id;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
/** Represent a set operation made to a single document in a database. */
|
|
29
|
-
export class DocumentSet extends DocumentWrite {
|
|
30
|
-
constructor(ref, data) {
|
|
31
|
-
super(ref);
|
|
32
|
-
this.data = data;
|
|
33
|
-
}
|
|
34
|
-
async transform(db) {
|
|
35
|
-
await db.doc(this.collection, this.id).set(this.data);
|
|
36
|
-
}
|
|
37
|
-
/** Set one of the props on this set operation to a different value. */
|
|
38
|
-
set(key, value) {
|
|
39
|
-
if (isNullish(key))
|
|
40
|
-
return this;
|
|
41
|
-
return { __proto__: Object.getPrototypeOf(this), ...this, data: { ...this.data, [key]: value } };
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
/** Represent an update operation made to a single document in a database. */
|
|
45
|
-
export class DocumentUpdate extends DocumentWrite {
|
|
46
|
-
constructor(ref, updates) {
|
|
47
|
-
super(ref);
|
|
48
|
-
this.updates = updates;
|
|
49
|
-
}
|
|
50
|
-
async transform(db) {
|
|
51
|
-
await db.doc(this.collection, this.id).update(this.updates);
|
|
52
|
-
}
|
|
53
|
-
/** update one of the props on this set operation to a different value. */
|
|
54
|
-
update(key, value) {
|
|
55
|
-
if (isNullish(key))
|
|
56
|
-
return this;
|
|
57
|
-
return { __proto__: Object.getPrototypeOf(this), ...this, updates: { ...this.updates, [key]: value } };
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
/** Represent a delete operation made to a single document in a database. */
|
|
61
|
-
export class DocumentDelete extends DocumentWrite {
|
|
62
|
-
async transform(db) {
|
|
63
|
-
await db.doc(this.collection, this.id).delete();
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
/** Set of hydrations for all change classes. */
|
|
67
|
-
export const WRITE_HYDRATIONS = {
|
|
68
|
-
Writes,
|
|
69
|
-
DocumentSet,
|
|
70
|
-
DocumentUpdate,
|
|
71
|
-
DocumentDelete,
|
|
72
|
-
};
|
|
73
|
-
WRITE_HYDRATIONS;
|