shelving 1.91.2 → 1.92.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/Change.d.ts +19 -20
- package/db/Change.js +22 -21
- package/db/CollectionReference.d.ts +103 -0
- package/db/CollectionReference.js +127 -0
- package/db/Database.d.ts +21 -23
- package/db/Database.js +18 -18
- package/db/{Item.d.ts → ItemReference.d.ts} +13 -14
- package/db/{Item.js → ItemReference.js} +14 -17
- package/db/ItemState.d.ts +3 -3
- package/db/{Query.d.ts → QueryReference.d.ts} +25 -33
- package/db/QueryReference.js +101 -0
- package/db/QueryState.d.ts +10 -10
- package/db/QueryState.js +20 -18
- package/db/index.d.ts +3 -3
- package/db/index.js +3 -3
- package/firestore/client/FirestoreClientProvider.d.ts +13 -13
- package/firestore/client/FirestoreClientProvider.js +55 -84
- package/firestore/lite/FirestoreLiteProvider.d.ts +11 -11
- package/firestore/lite/FirestoreLiteProvider.js +52 -83
- package/firestore/server/FirestoreServerProvider.d.ts +13 -13
- package/firestore/server/FirestoreServerProvider.js +51 -84
- package/index.d.ts +0 -2
- package/index.js +0 -2
- package/package.json +1 -3
- package/provider/CacheProvider.d.ts +7 -7
- package/provider/CacheProvider.js +14 -14
- package/provider/DebugProvider.d.ts +11 -11
- package/provider/DebugProvider.js +28 -29
- package/provider/MemoryProvider.d.ts +17 -17
- package/provider/MemoryProvider.js +41 -43
- package/provider/Provider.d.ts +15 -15
- package/provider/ThroughProvider.d.ts +12 -12
- package/provider/ThroughProvider.js +20 -20
- package/provider/ValidationProvider.d.ts +11 -11
- package/provider/ValidationProvider.js +16 -12
- package/react/useData.d.ts +5 -5
- package/react/useData.js +2 -2
- package/state/DataState.d.ts +4 -8
- package/state/DataState.js +6 -16
- package/state/DictionaryState.d.ts +4 -6
- package/state/DictionaryState.js +4 -9
- package/test/basics.d.ts +1 -1
- package/test/people.d.ts +1 -1
- package/test/util.d.ts +1 -1
- package/test/util.js +1 -1
- package/util/data.d.ts +2 -2
- package/util/equal.js +5 -5
- package/util/hydrate.d.ts +13 -26
- package/util/hydrate.js +44 -60
- package/util/index.d.ts +2 -0
- package/util/index.js +2 -0
- package/util/iterate.d.ts +2 -0
- package/util/match.d.ts +2 -10
- package/util/match.js +4 -8
- package/util/query.d.ts +83 -0
- package/util/query.js +129 -0
- package/util/sort.d.ts +8 -15
- package/util/sort.js +15 -19
- package/util/transform.d.ts +26 -45
- package/util/transform.js +24 -29
- package/util/update.d.ts +22 -0
- package/util/update.js +42 -0
- package/util/validate.js +2 -2
- package/constraint/Constraint.d.ts +0 -7
- package/constraint/Constraint.js +0 -3
- package/constraint/Constraints.d.ts +0 -20
- package/constraint/Constraints.js +0 -34
- package/constraint/Filter.d.ts +0 -34
- package/constraint/Filter.js +0 -89
- package/constraint/Filters.d.ts +0 -27
- package/constraint/Filters.js +0 -41
- package/constraint/Sort.d.ts +0 -18
- package/constraint/Sort.js +0 -33
- package/constraint/Sorts.d.ts +0 -26
- package/constraint/Sorts.js +0 -47
- package/constraint/Statement.d.ts +0 -45
- package/constraint/Statement.js +0 -79
- package/constraint/index.d.ts +0 -7
- package/constraint/index.js +0 -7
- package/db/Collection.d.ts +0 -64
- package/db/Collection.js +0 -83
- package/db/Query.js +0 -94
- package/update/ArrayUpdate.d.ts +0 -17
- package/update/ArrayUpdate.js +0 -31
- package/update/DataUpdate.d.ts +0 -37
- package/update/DataUpdate.js +0 -44
- package/update/Delete.d.ts +0 -10
- package/update/Delete.js +0 -12
- package/update/DictionaryUpdate.d.ts +0 -31
- package/update/DictionaryUpdate.js +0 -62
- package/update/Increment.d.ts +0 -18
- package/update/Increment.js +0 -22
- package/update/Update.d.ts +0 -8
- package/update/Update.js +0 -6
- package/update/hydrations.d.ts +0 -3
- package/update/hydrations.js +0 -13
- package/update/index.d.ts +0 -7
- package/update/index.js +0 -7
package/db/Change.d.ts
CHANGED
|
@@ -1,44 +1,43 @@
|
|
|
1
1
|
import type { AsyncProvider, Provider } from "../provider/Provider.js";
|
|
2
|
-
import type { Updates } from "../update/DataUpdate.js";
|
|
3
2
|
import type { ImmutableArray } from "../util/array.js";
|
|
4
3
|
import type { Data } from "../util/data.js";
|
|
5
4
|
import type { Nullish } from "../util/null.js";
|
|
6
|
-
|
|
7
|
-
export interface Change {
|
|
8
|
-
readonly action: string;
|
|
9
|
-
readonly collection: string;
|
|
10
|
-
}
|
|
5
|
+
import type { Updates } from "../util/update.js";
|
|
11
6
|
/** Add on an item. */
|
|
12
|
-
export interface AddChange<T extends Data>
|
|
13
|
-
readonly action: "
|
|
7
|
+
export interface AddChange<T extends Data> {
|
|
8
|
+
readonly action: "add";
|
|
9
|
+
readonly collection: string;
|
|
14
10
|
readonly data: T;
|
|
15
11
|
}
|
|
16
12
|
/** Set on an item. */
|
|
17
|
-
export interface SetChange<T extends Data
|
|
18
|
-
readonly action: "
|
|
13
|
+
export interface SetChange<T extends Data> {
|
|
14
|
+
readonly action: "set";
|
|
15
|
+
readonly collection: string;
|
|
19
16
|
readonly id: string;
|
|
20
17
|
readonly data: T;
|
|
21
18
|
}
|
|
22
19
|
/** Update change on an item. */
|
|
23
|
-
export interface UpdateChange<T extends Data
|
|
24
|
-
readonly action: "
|
|
20
|
+
export interface UpdateChange<T extends Data> {
|
|
21
|
+
readonly action: "update";
|
|
22
|
+
readonly collection: string;
|
|
25
23
|
readonly id: string;
|
|
26
24
|
readonly updates: Updates<T>;
|
|
27
25
|
}
|
|
28
26
|
/** Delete change on an item. */
|
|
29
|
-
export interface DeleteChange
|
|
30
|
-
readonly action: "
|
|
27
|
+
export interface DeleteChange {
|
|
28
|
+
readonly action: "delete";
|
|
29
|
+
readonly collection: string;
|
|
31
30
|
readonly id: string;
|
|
32
31
|
}
|
|
33
32
|
/** Set, update, or delete change on an item. */
|
|
34
|
-
export type ItemChange<T extends Data
|
|
33
|
+
export type ItemChange<T extends Data> = SetChange<T> | UpdateChange<T> | DeleteChange;
|
|
35
34
|
/** Array of item changes. */
|
|
36
|
-
export type ItemChanges = ImmutableArray<ItemChange
|
|
35
|
+
export type ItemChanges = ImmutableArray<ItemChange<Data>>;
|
|
37
36
|
/** Write change on an item. */
|
|
38
|
-
export type WriteChange<T extends Data
|
|
37
|
+
export type WriteChange<T extends Data> = ItemChange<T> | AddChange<T>;
|
|
39
38
|
/** Array of write changes. */
|
|
40
|
-
export type WriteChanges = ImmutableArray<WriteChange
|
|
39
|
+
export type WriteChanges = ImmutableArray<WriteChange<Data>>;
|
|
41
40
|
/** Apply a set of changes to a synchronous provider. */
|
|
42
|
-
export declare function changeProvider(provider: Provider, ...changes: Nullish<WriteChange
|
|
41
|
+
export declare function changeProvider(provider: Provider, ...changes: Nullish<WriteChange<Data>>[]): ItemChanges;
|
|
43
42
|
/** Apply a set of changes to an asynchronous provider. */
|
|
44
|
-
export declare function changeAsyncProvider(provider: AsyncProvider, ...changes: Nullish<WriteChange
|
|
43
|
+
export declare function changeAsyncProvider(provider: AsyncProvider, ...changes: Nullish<WriteChange<Data>>[]): Promise<ItemChanges>;
|
package/db/Change.js
CHANGED
|
@@ -1,34 +1,35 @@
|
|
|
1
1
|
import { notNullish } from "../util/null.js";
|
|
2
|
-
import {
|
|
2
|
+
import { mapArray, mapItems } from "../util/transform.js";
|
|
3
|
+
import { getItemQuery } from "./ItemReference.js";
|
|
3
4
|
/** Apply a set of changes to a synchronous provider. */
|
|
4
5
|
export function changeProvider(provider, ...changes) {
|
|
5
|
-
return changes.filter(notNullish)
|
|
6
|
+
return mapArray(changes.filter(notNullish), _changeItem, provider);
|
|
6
7
|
}
|
|
7
|
-
function _changeItem(change) {
|
|
8
|
+
function _changeItem(change, provider) {
|
|
8
9
|
const { action, collection } = change;
|
|
9
|
-
if (action === "
|
|
10
|
-
return { action: "
|
|
11
|
-
else if (action === "
|
|
12
|
-
|
|
13
|
-
else if (action === "
|
|
14
|
-
|
|
15
|
-
else if (action === "
|
|
16
|
-
|
|
10
|
+
if (action === "add")
|
|
11
|
+
return { action: "set", collection, id: provider.addItem(collection, change.data), data: change.data };
|
|
12
|
+
else if (action === "set")
|
|
13
|
+
provider.setItem(collection, change.id, change.data);
|
|
14
|
+
else if (action === "update")
|
|
15
|
+
provider.updateQuery(collection, getItemQuery(change.id), change.updates);
|
|
16
|
+
else if (action === "delete")
|
|
17
|
+
provider.deleteItem(collection, change.id);
|
|
17
18
|
return change;
|
|
18
19
|
}
|
|
19
20
|
/** Apply a set of changes to an asynchronous provider. */
|
|
20
21
|
export function changeAsyncProvider(provider, ...changes) {
|
|
21
|
-
return Promise.all(changes.filter(notNullish)
|
|
22
|
+
return Promise.all(mapItems(changes.filter(notNullish), _changeAsyncItem, provider));
|
|
22
23
|
}
|
|
23
|
-
async function _changeAsyncItem(change) {
|
|
24
|
+
async function _changeAsyncItem(change, provider) {
|
|
24
25
|
const { collection, action } = change;
|
|
25
|
-
if (action === "
|
|
26
|
-
return { action: "
|
|
27
|
-
else if (action === "
|
|
28
|
-
await
|
|
29
|
-
else if (action === "
|
|
30
|
-
await
|
|
31
|
-
else if (action === "
|
|
32
|
-
await
|
|
26
|
+
if (action === "add")
|
|
27
|
+
return { action: "set", collection, id: await provider.addItem(collection, change.data), data: change.data };
|
|
28
|
+
else if (action === "set")
|
|
29
|
+
await provider.setItem(collection, change.id, change.data);
|
|
30
|
+
else if (action === "update")
|
|
31
|
+
await provider.updateQuery(collection, getItemQuery(change.id), change.updates);
|
|
32
|
+
else if (action === "delete")
|
|
33
|
+
await provider.deleteItem(collection, change.id);
|
|
33
34
|
return change;
|
|
34
35
|
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { AddChange, DeleteChange, SetChange, UpdateChange } from "./Change.js";
|
|
2
|
+
import type { ItemArray, ItemData, ItemQuery, ItemValue } from "./ItemReference.js";
|
|
3
|
+
import type { AsyncProvider, Provider } from "../provider/Provider.js";
|
|
4
|
+
import type { Data } from "../util/data.js";
|
|
5
|
+
import type { Updates } from "../util/update.js";
|
|
6
|
+
import { AsyncItemReference, ItemReference } from "./ItemReference.js";
|
|
7
|
+
import { AsyncQueryReference, QueryReference } from "./QueryReference.js";
|
|
8
|
+
/** Reference to a collection in a synchronous or asynchronous provider. */
|
|
9
|
+
declare abstract class AbstractCollectionReference<T extends Data = Data> {
|
|
10
|
+
abstract readonly provider: Provider | AsyncProvider;
|
|
11
|
+
readonly collection: string;
|
|
12
|
+
constructor(collection: string);
|
|
13
|
+
/**
|
|
14
|
+
* Get array of entities for this query.
|
|
15
|
+
* @return Array of entities.
|
|
16
|
+
*/
|
|
17
|
+
abstract items: ItemArray<T> | PromiseLike<ItemArray<T>>;
|
|
18
|
+
/**
|
|
19
|
+
* Count the number of results of this set of items.
|
|
20
|
+
* @return Number of items matching the query (possibly promised).
|
|
21
|
+
*/
|
|
22
|
+
abstract count: number | PromiseLike<number>;
|
|
23
|
+
/**
|
|
24
|
+
* Does at least one item exist for this query?
|
|
25
|
+
* @return `true` if a item exists or `false` otherwise (possibly promised).
|
|
26
|
+
*/
|
|
27
|
+
abstract exists: boolean | PromiseLike<boolean>;
|
|
28
|
+
/**
|
|
29
|
+
* Get the first item matched by this query or `null` if this query has no results.
|
|
30
|
+
* @throws RequiredError if there were no results for this query.
|
|
31
|
+
*/
|
|
32
|
+
abstract first: ItemValue<T> | PromiseLike<ItemValue<T>>;
|
|
33
|
+
/**
|
|
34
|
+
* Get the last item matched by this query or `null` if this query has no results.
|
|
35
|
+
* @throws RequiredError if there were no results for this query.
|
|
36
|
+
*/
|
|
37
|
+
abstract last: ItemValue<T> | PromiseLike<ItemValue<T>>;
|
|
38
|
+
/**
|
|
39
|
+
* Get the first item matched by this query.
|
|
40
|
+
* @throws RequiredError if there were no results for this query.
|
|
41
|
+
*/
|
|
42
|
+
abstract data: ItemData<T> | PromiseLike<ItemData<T>>;
|
|
43
|
+
/** Create a query on this item's collection. */
|
|
44
|
+
abstract query(query?: ItemQuery<T>): QueryReference<T> | AsyncQueryReference<T>;
|
|
45
|
+
/** Create a query on this item's collection. */
|
|
46
|
+
abstract item(id: string): ItemReference<T> | AsyncItemReference<T>;
|
|
47
|
+
/** Get an item from this collection. */
|
|
48
|
+
abstract get(id: string): ItemValue<T> | Promise<ItemValue<T>>;
|
|
49
|
+
/** Add an item to this collection. */
|
|
50
|
+
abstract add(data: T): string | Promise<string>;
|
|
51
|
+
/** Set a document in this collection. */
|
|
52
|
+
abstract set(id: string, data: T): void | Promise<void>;
|
|
53
|
+
/** Update a document in this collection. */
|
|
54
|
+
abstract update(id: string, updates: Updates<T>): void | Promise<void>;
|
|
55
|
+
/** Delete a document in this collection. */
|
|
56
|
+
abstract delete(id: string): void | Promise<void>;
|
|
57
|
+
/** Get an add change for this collection. */
|
|
58
|
+
getAdd(data: T): AddChange<T>;
|
|
59
|
+
/** Get a set change for this collection. */
|
|
60
|
+
getSet(id: string, data: T): SetChange<T>;
|
|
61
|
+
/** Get an update change for this collection. */
|
|
62
|
+
getUpdate(id: string, updates: Updates<T>): UpdateChange<T>;
|
|
63
|
+
/** Get a delete change for this collection. */
|
|
64
|
+
getDelete(id: string): DeleteChange;
|
|
65
|
+
toString(): string;
|
|
66
|
+
}
|
|
67
|
+
/** Reference to a collection in a synchronous provider. */
|
|
68
|
+
export declare class CollectionReference<T extends Data = Data> extends AbstractCollectionReference<T> {
|
|
69
|
+
readonly provider: Provider;
|
|
70
|
+
constructor(provider: Provider, collection: string);
|
|
71
|
+
get items(): ItemArray<T>;
|
|
72
|
+
get count(): number;
|
|
73
|
+
get exists(): boolean;
|
|
74
|
+
get first(): ItemValue<T>;
|
|
75
|
+
get last(): ItemValue<T>;
|
|
76
|
+
get data(): ItemData<T>;
|
|
77
|
+
query(constraints?: ItemQuery<T>): QueryReference<T>;
|
|
78
|
+
item(id: string): ItemReference<T>;
|
|
79
|
+
get(id: string): ItemValue<T>;
|
|
80
|
+
add(data: T): string;
|
|
81
|
+
set(id: string, data: T): void;
|
|
82
|
+
update(id: string, updates: Updates<T>): void;
|
|
83
|
+
delete(id: string): void;
|
|
84
|
+
}
|
|
85
|
+
/** Reference to a collection in an asynchronous provider. */
|
|
86
|
+
export declare class AsyncCollectionReference<T extends Data = Data> extends AbstractCollectionReference<T> {
|
|
87
|
+
readonly provider: AsyncProvider;
|
|
88
|
+
constructor(provider: AsyncProvider, collection: string);
|
|
89
|
+
get items(): Promise<ItemArray<T>>;
|
|
90
|
+
get count(): Promise<number>;
|
|
91
|
+
get exists(): Promise<boolean>;
|
|
92
|
+
get first(): Promise<ItemValue<T>>;
|
|
93
|
+
get last(): Promise<ItemValue<T>>;
|
|
94
|
+
get data(): Promise<ItemData<T>>;
|
|
95
|
+
query(query?: ItemQuery<T>): AsyncQueryReference<T>;
|
|
96
|
+
item(id: string): AsyncItemReference<T>;
|
|
97
|
+
get(id: string): Promise<ItemValue<T>>;
|
|
98
|
+
add(data: T): Promise<string>;
|
|
99
|
+
set(id: string, data: T): Promise<void>;
|
|
100
|
+
update(id: string, updates: Updates<T>): Promise<void>;
|
|
101
|
+
delete(id: string): Promise<void>;
|
|
102
|
+
}
|
|
103
|
+
export {};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { countArray, getFirstItem, getOptionalFirstItem, getOptionalLastItem, isArrayLength } from "../util/array.js";
|
|
2
|
+
import { AsyncItemReference, ItemReference } from "./ItemReference.js";
|
|
3
|
+
import { AsyncQueryReference, QueryReference } from "./QueryReference.js";
|
|
4
|
+
/** Reference to a collection in a synchronous or asynchronous provider. */
|
|
5
|
+
class AbstractCollectionReference {
|
|
6
|
+
constructor(collection) {
|
|
7
|
+
this.collection = collection;
|
|
8
|
+
}
|
|
9
|
+
/** Get an add change for this collection. */
|
|
10
|
+
getAdd(data) {
|
|
11
|
+
return { action: "add", collection: this.collection, data };
|
|
12
|
+
}
|
|
13
|
+
/** Get a set change for this collection. */
|
|
14
|
+
getSet(id, data) {
|
|
15
|
+
return { action: "set", collection: this.collection, id, data };
|
|
16
|
+
}
|
|
17
|
+
/** Get an update change for this collection. */
|
|
18
|
+
getUpdate(id, updates) {
|
|
19
|
+
return { action: "update", collection: this.collection, id, updates };
|
|
20
|
+
}
|
|
21
|
+
/** Get a delete change for this collection. */
|
|
22
|
+
getDelete(id) {
|
|
23
|
+
return { action: "delete", collection: this.collection, id };
|
|
24
|
+
}
|
|
25
|
+
// Implement toString()
|
|
26
|
+
toString() {
|
|
27
|
+
return this.collection;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** Reference to a collection in a synchronous provider. */
|
|
31
|
+
export class CollectionReference extends AbstractCollectionReference {
|
|
32
|
+
constructor(provider, collection) {
|
|
33
|
+
super(collection);
|
|
34
|
+
this.provider = provider;
|
|
35
|
+
}
|
|
36
|
+
get items() {
|
|
37
|
+
return _getItemArray(this);
|
|
38
|
+
}
|
|
39
|
+
get count() {
|
|
40
|
+
return this.items.length;
|
|
41
|
+
}
|
|
42
|
+
get exists() {
|
|
43
|
+
return !!_getSingleItemArray(this).length;
|
|
44
|
+
}
|
|
45
|
+
get first() {
|
|
46
|
+
return getOptionalFirstItem(_getSingleItemArray(this));
|
|
47
|
+
}
|
|
48
|
+
get last() {
|
|
49
|
+
return getOptionalLastItem(this.items);
|
|
50
|
+
}
|
|
51
|
+
get data() {
|
|
52
|
+
return getFirstItem(_getSingleItemArray(this));
|
|
53
|
+
}
|
|
54
|
+
query(constraints) {
|
|
55
|
+
return new QueryReference(this.provider, this.collection, constraints);
|
|
56
|
+
}
|
|
57
|
+
item(id) {
|
|
58
|
+
return new ItemReference(this.provider, this.collection, id);
|
|
59
|
+
}
|
|
60
|
+
get(id) {
|
|
61
|
+
return this.provider.getItem(this.collection, id);
|
|
62
|
+
}
|
|
63
|
+
add(data) {
|
|
64
|
+
return this.provider.addItem(this.collection, data);
|
|
65
|
+
}
|
|
66
|
+
set(id, data) {
|
|
67
|
+
return this.provider.setItem(this.collection, id, data);
|
|
68
|
+
}
|
|
69
|
+
update(id, updates) {
|
|
70
|
+
return this.provider.updateItem(this.collection, id, updates);
|
|
71
|
+
}
|
|
72
|
+
delete(id) {
|
|
73
|
+
return this.provider.deleteItem(this.collection, id);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Reference to a collection in an asynchronous provider. */
|
|
77
|
+
export class AsyncCollectionReference extends AbstractCollectionReference {
|
|
78
|
+
constructor(provider, collection) {
|
|
79
|
+
super(collection);
|
|
80
|
+
this.provider = provider;
|
|
81
|
+
}
|
|
82
|
+
get items() {
|
|
83
|
+
return _getItemArray(this);
|
|
84
|
+
}
|
|
85
|
+
get count() {
|
|
86
|
+
return this.items.then(countArray);
|
|
87
|
+
}
|
|
88
|
+
get exists() {
|
|
89
|
+
return _getSingleItemArray(this).then(isArrayLength);
|
|
90
|
+
}
|
|
91
|
+
get first() {
|
|
92
|
+
return _getSingleItemArray(this).then(getOptionalFirstItem);
|
|
93
|
+
}
|
|
94
|
+
get last() {
|
|
95
|
+
return this.items.then(getOptionalLastItem);
|
|
96
|
+
}
|
|
97
|
+
get data() {
|
|
98
|
+
return _getSingleItemArray(this).then(getFirstItem);
|
|
99
|
+
}
|
|
100
|
+
query(query) {
|
|
101
|
+
return new AsyncQueryReference(this.provider, this.collection, query);
|
|
102
|
+
}
|
|
103
|
+
item(id) {
|
|
104
|
+
return new AsyncItemReference(this.provider, this.collection, id);
|
|
105
|
+
}
|
|
106
|
+
get(id) {
|
|
107
|
+
return this.provider.getItem(this.collection, id);
|
|
108
|
+
}
|
|
109
|
+
add(data) {
|
|
110
|
+
return this.provider.addItem(this.collection, data);
|
|
111
|
+
}
|
|
112
|
+
set(id, data) {
|
|
113
|
+
return this.provider.setItem(this.collection, id, data);
|
|
114
|
+
}
|
|
115
|
+
update(id, updates) {
|
|
116
|
+
return this.provider.updateItem(this.collection, id, updates);
|
|
117
|
+
}
|
|
118
|
+
delete(id) {
|
|
119
|
+
return this.provider.deleteItem(this.collection, id);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function _getItemArray({ provider, collection }) {
|
|
123
|
+
return provider.getQuery(collection, {});
|
|
124
|
+
}
|
|
125
|
+
function _getSingleItemArray({ provider, collection }) {
|
|
126
|
+
return provider.getQuery(collection, { $limit: 1 });
|
|
127
|
+
}
|
package/db/Database.d.ts
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
import type { AddChange, DeleteChange, ItemChanges, SetChange, UpdateChange, WriteChange } from "./Change.js";
|
|
2
|
-
import type {
|
|
3
|
-
import type { PossibleSorts } from "../constraint/Sorts.js";
|
|
4
|
-
import type { ItemData, ItemValue } from "../db/Item.js";
|
|
2
|
+
import type { ItemQuery, ItemValue } from "./ItemReference.js";
|
|
5
3
|
import type { AsyncProvider, Provider } from "../provider/Provider.js";
|
|
6
|
-
import type {
|
|
7
|
-
import type { DataKey, Datas } from "../util/data.js";
|
|
4
|
+
import type { Data, DataKey, Datas } from "../util/data.js";
|
|
8
5
|
import type { Nullish } from "../util/null.js";
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
6
|
+
import type { Updates } from "../util/update.js";
|
|
7
|
+
import { AsyncCollectionReference, CollectionReference } from "./CollectionReference.js";
|
|
8
|
+
import { AsyncItemReference, ItemReference } from "./ItemReference.js";
|
|
9
|
+
import { AsyncQueryReference, QueryReference } from "./QueryReference.js";
|
|
12
10
|
/** Database with a synchronous or asynchronous provider. */
|
|
13
|
-
declare abstract class
|
|
11
|
+
declare abstract class AbstractDatabase<T extends Datas> {
|
|
14
12
|
abstract readonly provider: Provider | AsyncProvider;
|
|
15
13
|
/** Create a query on a collection in this database. */
|
|
16
|
-
abstract collection<K extends DataKey<T>>(collection: K):
|
|
14
|
+
abstract collection<K extends DataKey<T>>(collection: K): CollectionReference<T[K]> | AsyncCollectionReference<T[K]>;
|
|
17
15
|
/** Create a query on a collection in this database. */
|
|
18
|
-
abstract query<K extends DataKey<T>>(collection: K,
|
|
16
|
+
abstract query<K extends DataKey<T>>(collection: K, query?: ItemQuery<T>): QueryReference<T[K]> | AsyncQueryReference<T[K]>;
|
|
19
17
|
/** Reference an item in a collection in this database. */
|
|
20
|
-
abstract item<K extends DataKey<T>>(collection: K, id: string):
|
|
18
|
+
abstract item<K extends DataKey<T>>(collection: K, id: string): ItemReference<T[K]> | AsyncItemReference<T[K]>;
|
|
21
19
|
/** Run a set of changes in this database. */
|
|
22
|
-
abstract change(...changes: Nullish<WriteChange
|
|
20
|
+
abstract change(...changes: Nullish<WriteChange<Data>>[]): ItemChanges | Promise<ItemChanges>;
|
|
23
21
|
/** Get a document from a collection in this database. */
|
|
24
22
|
abstract get<K extends DataKey<T>>(collection: K, id: string): ItemValue<T[K]> | Promise<ItemValue<T[K]>>;
|
|
25
23
|
/** Add a document to a collection in this database. */
|
|
@@ -40,13 +38,13 @@ declare abstract class BaseDatabase<T extends Datas> {
|
|
|
40
38
|
getDelete<K extends DataKey<T>>(collection: K, id: string): DeleteChange;
|
|
41
39
|
}
|
|
42
40
|
/** Database with a synchronous provider. */
|
|
43
|
-
export declare class Database<T extends Datas = Datas> extends
|
|
41
|
+
export declare class Database<T extends Datas = Datas> extends AbstractDatabase<T> {
|
|
44
42
|
readonly provider: Provider;
|
|
45
43
|
constructor(provider: Provider);
|
|
46
|
-
collection<K extends DataKey<T>>(collection: K):
|
|
47
|
-
query<K extends DataKey<T>>(collection: K,
|
|
48
|
-
item<K extends DataKey<T>>(collection: K, id: string):
|
|
49
|
-
change(...changes: Nullish<WriteChange
|
|
44
|
+
collection<K extends DataKey<T>>(collection: K): CollectionReference<T[K]>;
|
|
45
|
+
query<K extends DataKey<T>>(collection: K, query?: ItemQuery<T>): QueryReference<T[K]>;
|
|
46
|
+
item<K extends DataKey<T>>(collection: K, id: string): ItemReference<T[K]>;
|
|
47
|
+
change(...changes: Nullish<WriteChange<Data>>[]): ItemChanges;
|
|
50
48
|
get<K extends DataKey<T>>(collection: K, id: string): ItemValue<T[K]>;
|
|
51
49
|
add<K extends DataKey<T>>(collection: K, data: T[K]): string;
|
|
52
50
|
set<K extends DataKey<T>>(collection: K, id: string, data: T[K]): void;
|
|
@@ -54,13 +52,13 @@ export declare class Database<T extends Datas = Datas> extends BaseDatabase<T> {
|
|
|
54
52
|
delete<K extends DataKey<T>>(collection: K, id: string): void;
|
|
55
53
|
}
|
|
56
54
|
/** Database with a synchronous provider. */
|
|
57
|
-
export declare class AsyncDatabase<T extends Datas = Datas> extends
|
|
55
|
+
export declare class AsyncDatabase<T extends Datas = Datas> extends AbstractDatabase<T> {
|
|
58
56
|
readonly provider: AsyncProvider;
|
|
59
57
|
constructor(provider: AsyncProvider);
|
|
60
|
-
collection<K extends DataKey<T>>(collection: K):
|
|
61
|
-
query<K extends DataKey<T>>(collection: K,
|
|
62
|
-
item<K extends DataKey<T>>(collection: K, id: string):
|
|
63
|
-
change(...changes: Nullish<WriteChange
|
|
58
|
+
collection<K extends DataKey<T>>(collection: K): AsyncCollectionReference<T[K]>;
|
|
59
|
+
query<K extends DataKey<T>>(collection: K, query?: ItemQuery<T>): AsyncQueryReference<T[K]>;
|
|
60
|
+
item<K extends DataKey<T>>(collection: K, id: string): AsyncItemReference<T[K]>;
|
|
61
|
+
change(...changes: Nullish<WriteChange<Data>>[]): Promise<ItemChanges>;
|
|
64
62
|
get<K extends DataKey<T>>(collection: K, id: string): Promise<ItemValue<T[K]>>;
|
|
65
63
|
add<K extends DataKey<T>>(collection: K, data: T[K]): Promise<string>;
|
|
66
64
|
set<K extends DataKey<T>>(collection: K, id: string, data: T[K]): Promise<void>;
|
package/db/Database.js
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
1
|
import { changeAsyncProvider, changeProvider } from "./Change.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { AsyncCollectionReference, CollectionReference } from "./CollectionReference.js";
|
|
3
|
+
import { AsyncItemReference, ItemReference } from "./ItemReference.js";
|
|
4
|
+
import { AsyncQueryReference, QueryReference } from "./QueryReference.js";
|
|
5
5
|
/** Database with a synchronous or asynchronous provider. */
|
|
6
|
-
class
|
|
6
|
+
class AbstractDatabase {
|
|
7
7
|
/** Get an add change for a collection in this database. */
|
|
8
8
|
getAdd(collection, data) {
|
|
9
|
-
return { action: "
|
|
9
|
+
return { action: "add", collection, data };
|
|
10
10
|
}
|
|
11
11
|
/** Get a set change for a collection in this database. */
|
|
12
12
|
getSet(collection, id, data) {
|
|
13
|
-
return { action: "
|
|
13
|
+
return { action: "set", collection, id, data };
|
|
14
14
|
}
|
|
15
15
|
/** Get an update change for a collection in this database. */
|
|
16
16
|
getUpdate(collection, id, updates) {
|
|
17
|
-
return { action: "
|
|
17
|
+
return { action: "update", collection, id, updates };
|
|
18
18
|
}
|
|
19
19
|
/** Get a delete change for a collection in this database. */
|
|
20
20
|
getDelete(collection, id) {
|
|
21
|
-
return { action: "
|
|
21
|
+
return { action: "delete", collection, id };
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
/** Database with a synchronous provider. */
|
|
25
|
-
export class Database extends
|
|
25
|
+
export class Database extends AbstractDatabase {
|
|
26
26
|
constructor(provider) {
|
|
27
27
|
super();
|
|
28
28
|
this.provider = provider;
|
|
29
29
|
}
|
|
30
30
|
collection(collection) {
|
|
31
|
-
return new
|
|
31
|
+
return new CollectionReference(this.provider, collection);
|
|
32
32
|
}
|
|
33
|
-
query(collection,
|
|
34
|
-
return new
|
|
33
|
+
query(collection, query) {
|
|
34
|
+
return new QueryReference(this.provider, collection, query);
|
|
35
35
|
}
|
|
36
36
|
item(collection, id) {
|
|
37
|
-
return new
|
|
37
|
+
return new ItemReference(this.provider, collection, id);
|
|
38
38
|
}
|
|
39
39
|
change(...changes) {
|
|
40
40
|
return changeProvider(this.provider, ...changes);
|
|
@@ -56,19 +56,19 @@ export class Database extends BaseDatabase {
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
/** Database with a synchronous provider. */
|
|
59
|
-
export class AsyncDatabase extends
|
|
59
|
+
export class AsyncDatabase extends AbstractDatabase {
|
|
60
60
|
constructor(provider) {
|
|
61
61
|
super();
|
|
62
62
|
this.provider = provider;
|
|
63
63
|
}
|
|
64
64
|
collection(collection) {
|
|
65
|
-
return new
|
|
65
|
+
return new AsyncCollectionReference(this.provider, collection);
|
|
66
66
|
}
|
|
67
|
-
query(collection,
|
|
68
|
-
return new
|
|
67
|
+
query(collection, query) {
|
|
68
|
+
return new AsyncQueryReference(this.provider, collection, query);
|
|
69
69
|
}
|
|
70
70
|
item(collection, id) {
|
|
71
|
-
return new
|
|
71
|
+
return new AsyncItemReference(this.provider, collection, id);
|
|
72
72
|
}
|
|
73
73
|
change(...changes) {
|
|
74
74
|
return changeAsyncProvider(this.provider, ...changes);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { DeleteChange, SetChange, UpdateChange } from "./Change.js";
|
|
2
2
|
import type { AsyncProvider, Provider } from "../provider/Provider.js";
|
|
3
|
-
import type { Updates } from "../update/DataUpdate.js";
|
|
4
3
|
import type { ImmutableArray } from "../util/array.js";
|
|
5
4
|
import type { Data } from "../util/data.js";
|
|
6
5
|
import type { Dispatch, Handler, Stop } from "../util/function.js";
|
|
7
|
-
import {
|
|
6
|
+
import type { Query } from "../util/query.js";
|
|
7
|
+
import type { Updates } from "../util/update.js";
|
|
8
8
|
/** Item data with a string ID that uniquely identifies it. */
|
|
9
9
|
export type ItemData<T extends Data = Data> = T & {
|
|
10
10
|
id: string;
|
|
@@ -18,14 +18,17 @@ export declare function getIDs<T extends Data>(entities: Iterable<ItemData<T>>):
|
|
|
18
18
|
/** An array of item data. */
|
|
19
19
|
export type ItemArray<T extends Data = Data> = ImmutableArray<ItemData<T>>;
|
|
20
20
|
/** A set of query constraints for item data. */
|
|
21
|
-
export type
|
|
22
|
-
/** Get
|
|
23
|
-
export declare const
|
|
21
|
+
export type ItemQuery<T extends Data = Data> = Query<ItemData<T>>;
|
|
22
|
+
/** Get query that targets a single database item by its ID. */
|
|
23
|
+
export declare const getItemQuery: (id: string) => Query<{
|
|
24
|
+
id: string;
|
|
25
|
+
}>;
|
|
24
26
|
/** Reference to an item in a synchronous or asynchronous database. */
|
|
25
|
-
declare abstract class
|
|
27
|
+
declare abstract class AbstractItemReference<T extends Data = Data> implements AsyncIterable<ItemValue<T>> {
|
|
26
28
|
abstract readonly provider: Provider | AsyncProvider;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
readonly collection: string;
|
|
30
|
+
readonly id: string;
|
|
31
|
+
constructor(collection: string, id: string);
|
|
29
32
|
/**
|
|
30
33
|
* Does this item exist?
|
|
31
34
|
* @return `true` if an item exists or `false` otherwise (possibly promised).
|
|
@@ -62,10 +65,8 @@ declare abstract class BaseItem<T extends Data = Data> implements AsyncIterable<
|
|
|
62
65
|
[Symbol.asyncIterator](): AsyncIterator<ItemValue<T>>;
|
|
63
66
|
}
|
|
64
67
|
/** Reference to an item in a synchronous database. */
|
|
65
|
-
export declare class
|
|
68
|
+
export declare class ItemReference<T extends Data = Data> extends AbstractItemReference<T> {
|
|
66
69
|
readonly provider: Provider;
|
|
67
|
-
readonly collection: string;
|
|
68
|
-
readonly id: string;
|
|
69
70
|
constructor(provider: Provider, collection: string, id: string);
|
|
70
71
|
get exists(): boolean;
|
|
71
72
|
get value(): ItemValue<T>;
|
|
@@ -75,10 +76,8 @@ export declare class Item<T extends Data = Data> extends BaseItem<T> {
|
|
|
75
76
|
delete(): void;
|
|
76
77
|
}
|
|
77
78
|
/** Reference to an item in an asynchronous database. */
|
|
78
|
-
export declare class
|
|
79
|
+
export declare class AsyncItemReference<T extends Data = Data> extends AbstractItemReference<T> {
|
|
79
80
|
readonly provider: AsyncProvider;
|
|
80
|
-
readonly collection: string;
|
|
81
|
-
readonly id: string;
|
|
82
81
|
constructor(provider: AsyncProvider, collection: string, id: string);
|
|
83
82
|
get exists(): Promise<boolean>;
|
|
84
83
|
get value(): Promise<ItemValue<T>>;
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { Filter } from "../constraint/Filter.js";
|
|
2
|
-
import { Filters } from "../constraint/Filters.js";
|
|
3
|
-
import { Statement } from "../constraint/Statement.js";
|
|
4
1
|
import { getData } from "../util/data.js";
|
|
5
2
|
import { runSequence } from "../util/sequence.js";
|
|
6
3
|
/** Get the ID from item data. */
|
|
@@ -10,21 +7,25 @@ export function* getIDs(entities) {
|
|
|
10
7
|
for (const { id } of entities)
|
|
11
8
|
yield id;
|
|
12
9
|
}
|
|
13
|
-
/** Get
|
|
14
|
-
export const
|
|
10
|
+
/** Get query that targets a single database item by its ID. */
|
|
11
|
+
export const getItemQuery = (id) => ({ id, $limit: 1 });
|
|
15
12
|
/** Reference to an item in a synchronous or asynchronous database. */
|
|
16
|
-
class
|
|
13
|
+
class AbstractItemReference {
|
|
14
|
+
constructor(collection, id) {
|
|
15
|
+
this.collection = collection;
|
|
16
|
+
this.id = id;
|
|
17
|
+
}
|
|
17
18
|
/** Get a set change for this item. */
|
|
18
19
|
getSet(data) {
|
|
19
|
-
return { action: "
|
|
20
|
+
return { action: "set", collection: this.collection, id: this.id, data };
|
|
20
21
|
}
|
|
21
22
|
/** Get an update change for this item. */
|
|
22
23
|
getUpdate(updates) {
|
|
23
|
-
return { action: "
|
|
24
|
+
return { action: "update", collection: this.collection, id: this.id, updates };
|
|
24
25
|
}
|
|
25
26
|
/** Get a delete change for this item. */
|
|
26
27
|
getDelete() {
|
|
27
|
-
return { action: "
|
|
28
|
+
return { action: "delete", collection: this.collection, id: this.id };
|
|
28
29
|
}
|
|
29
30
|
// Implement toString()
|
|
30
31
|
toString() {
|
|
@@ -40,12 +41,10 @@ class BaseItem {
|
|
|
40
41
|
}
|
|
41
42
|
}
|
|
42
43
|
/** Reference to an item in a synchronous database. */
|
|
43
|
-
export class
|
|
44
|
+
export class ItemReference extends AbstractItemReference {
|
|
44
45
|
constructor(provider, collection, id) {
|
|
45
|
-
super();
|
|
46
|
+
super(collection, id);
|
|
46
47
|
this.provider = provider;
|
|
47
|
-
this.collection = collection;
|
|
48
|
-
this.id = id;
|
|
49
48
|
}
|
|
50
49
|
get exists() {
|
|
51
50
|
return !!this.provider.getItem(this.collection, this.id);
|
|
@@ -67,12 +66,10 @@ export class Item extends BaseItem {
|
|
|
67
66
|
}
|
|
68
67
|
}
|
|
69
68
|
/** Reference to an item in an asynchronous database. */
|
|
70
|
-
export class
|
|
69
|
+
export class AsyncItemReference extends AbstractItemReference {
|
|
71
70
|
constructor(provider, collection, id) {
|
|
72
|
-
super();
|
|
71
|
+
super(collection, id);
|
|
73
72
|
this.provider = provider;
|
|
74
|
-
this.collection = collection;
|
|
75
|
-
this.id = id;
|
|
76
73
|
}
|
|
77
74
|
get exists() {
|
|
78
75
|
return this.provider.getItem(this.collection, this.id).then(Boolean);
|