@incoqnito.io/bajadab 1.2.1 → 1.3.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/README.md +192 -153
- package/dist/base.d.ts +50 -3
- package/dist/base.js.map +1 -1
- package/dist/basics.d.ts +44 -3
- package/dist/basics.js +456 -211
- package/dist/basics.js.map +1 -1
- package/dist/fileio.d.ts +7 -0
- package/dist/fileio.js +65 -14
- package/dist/fileio.js.map +1 -1
- package/package.json +2 -2
- package/src/main/base.ts +235 -186
- package/src/main/basics.ts +1058 -787
- package/src/main/fileio.ts +270 -207
package/src/main/base.ts
CHANGED
|
@@ -1,186 +1,235 @@
|
|
|
1
|
-
/** How a missing id is handled on {@link IBCollection.add}. */
|
|
2
|
-
export enum EBAutoIDStrategy {
|
|
3
|
-
/** Reject values without an id. */
|
|
4
|
-
NO_AUTO_ID = "NO_AUTO_ID",
|
|
5
|
-
/** Generate a `uuidv7` and assign it via the configured setter. */
|
|
6
|
-
AUTO_ID_UUID = "AUTO_ID_UUID",
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/** In-memory representation of a collection's data. */
|
|
10
|
-
export enum EBMemStorageStrategy {
|
|
11
|
-
/** Keyed by id; `add()` looks up existing entries by id in O(1). */
|
|
12
|
-
MAP = "MAP",
|
|
13
|
-
// LINKED_LIST = "LINKED_LIST",
|
|
14
|
-
/** Plain list; insertion order is preserved. */
|
|
15
|
-
ARRAY = "ARRAY",
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/** What happens on {@link IBCollection.add} when a value's id already exists. */
|
|
19
|
-
export enum EBDuplicateIDStrategy {
|
|
20
|
-
/** Replace the existing value. */
|
|
21
|
-
OVERWRITE = "OVERWRITE",
|
|
22
|
-
/** Reject the whole call. */
|
|
23
|
-
ERROR = "ERROR",
|
|
24
|
-
/** Keep the existing value, drop the new one. */
|
|
25
|
-
IGNORE = "IGNORE",
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** How {@link IBCollection.delete} removes matched items from an `ARRAY` collection. */
|
|
29
|
-
export enum EBArrayDeletionStrategy {
|
|
30
|
-
/** Build a new array via `filter()`. */
|
|
31
|
-
NEW_ARRAY = "NEW_ARRAY",
|
|
32
|
-
/** Compact the existing array in place. */
|
|
33
|
-
IN_PLACE = "IN_PLACE",
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** When a collection is written to disk after a mutation. */
|
|
37
|
-
export enum EBAutoFlushStrategy {
|
|
38
|
-
/** Only on an explicit {@link IBCollection.flush} call. */
|
|
39
|
-
NO_AUTO_FLUSH = "NO_AUTO_FLUSH",
|
|
40
|
-
/** After every mutating call (`add`/`update`/`delete`). */
|
|
41
|
-
ALWAYS_AUTO_FLUSH = "ALWAYS_AUTO_FLUSH",
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/** What {@link IBCollection.unload} does with unsaved changes. */
|
|
45
|
-
export enum EBDirtyUnloadStrategy {
|
|
46
|
-
/** Reject the unload. */
|
|
47
|
-
ERROR = "ERROR",
|
|
48
|
-
/** Flush first, then unload. */
|
|
49
|
-
FLUSH = "FLUSH",
|
|
50
|
-
/** Discard the unsaved changes silently. */
|
|
51
|
-
IGNORE = "IGNORE",
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** Resolves the id of a value; `undefined` means "no id yet". */
|
|
55
|
-
export type TGetIDFunction = (v: any) => Promise<string | undefined>;
|
|
56
|
-
|
|
57
|
-
/** Assigns an id onto a value. */
|
|
58
|
-
export type TSetIDFunction = (v: any, id: string) => Promise<void>;
|
|
59
|
-
|
|
60
|
-
/** Extracts an index key (or several, for a multi-valued index) from a value; `undefined` means "not indexed". */
|
|
61
|
-
export type TIndexKeyFunction<T = any> = (v: T) => Promise<string | string[] | undefined>;
|
|
62
|
-
|
|
63
|
-
/** Per-collection behavior. Passed to {@link IBDatabase.createCollection}/{@link IBDatabase.getCollection}. */
|
|
64
|
-
export interface IBCollectionConfig {
|
|
65
|
-
autoIDStrategy: EBAutoIDStrategy;
|
|
66
|
-
duplicateIDStrategy: EBDuplicateIDStrategy;
|
|
67
|
-
memStorageStrategy: EBMemStorageStrategy;
|
|
68
|
-
arrayDeletionStrategy: EBArrayDeletionStrategy;
|
|
69
|
-
dirtyUnloadStrategy: EBDirtyUnloadStrategy;
|
|
70
|
-
autoFlushStrategy: EBAutoFlushStrategy;
|
|
71
|
-
/** Registry key for a custom {@link TGetIDFunction}. Omit to use the default (reads `.id`). */
|
|
72
|
-
getID?: string;
|
|
73
|
-
/** Registry key for a custom {@link TSetIDFunction}. Omit to use the default (sets `.id`). */
|
|
74
|
-
setID?: string;
|
|
75
|
-
/** Index name → registry key of a {@link TIndexKeyFunction} for that index. Omit for no indexes. */
|
|
76
|
-
indexes?: { [indexKey: string]: string };
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Database-wide configuration. */
|
|
80
|
-
export interface IBDatabaseConfig {
|
|
81
|
-
/** Used for any collection created without an explicit config. */
|
|
82
|
-
readonly defaultCollectionConfig: IBCollectionConfig;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** Options for {@link IBDatabase.snapshot}. */
|
|
86
|
-
export interface IBSnapshotOptions {
|
|
87
|
-
/** Appended to the version folder's timestamp. Letters, digits, `-` and `_` only. */
|
|
88
|
-
label?: string;
|
|
89
|
-
/** Older version folders under `targetDir` to keep besides the one just created. `0` keeps only the new one. Negative values reject. */
|
|
90
|
-
keepLast?: number;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/** A JSON-file-backed database. Obtain one via `getDatabase()`. */
|
|
94
|
-
export interface IBDatabase {
|
|
95
|
-
readonly config: IBDatabaseConfig;
|
|
96
|
-
/** Creates a new collection. Rejects if one with the same (canonicalized) name already exists. */
|
|
97
|
-
createCollection<T>(collectionName: string, collectionConfig?: IBCollectionConfig): Promise<IBCollection<T>>;
|
|
98
|
-
/** Returns the named collection, creating it with `collectionConfig` (or the database default) if it doesn't exist yet. */
|
|
99
|
-
getCollection<T>(collectionName: string, collectionConfig?: IBCollectionConfig): Promise<IBCollection<T>>;
|
|
100
|
-
/** Whether a collection with this (canonicalized) name exists. */
|
|
101
|
-
hasCollection(collectionName: string): Promise<boolean>;
|
|
102
|
-
/** Deletes a collection and invalidates any handle still held on it. Rejects if it doesn't exist. */
|
|
103
|
-
dropCollection(collectionName: string): Promise<void>;
|
|
104
|
-
/**
|
|
105
|
-
* Flushes every loaded collection, then copies the whole database directory into a new, timestamped
|
|
106
|
-
* version folder under `targetDir` (created if needed). Blocks all other database operations for the
|
|
107
|
-
* duration. See {@link IBSnapshotOptions} for pruning older versions. Returns the new version folder's path.
|
|
108
|
-
*/
|
|
109
|
-
snapshot(targetDir: string, options?: IBSnapshotOptions): Promise<string>;
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
export
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
1
|
+
/** How a missing id is handled on {@link IBCollection.add}. */
|
|
2
|
+
export enum EBAutoIDStrategy {
|
|
3
|
+
/** Reject values without an id. */
|
|
4
|
+
NO_AUTO_ID = "NO_AUTO_ID",
|
|
5
|
+
/** Generate a `uuidv7` and assign it via the configured setter. */
|
|
6
|
+
AUTO_ID_UUID = "AUTO_ID_UUID",
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** In-memory representation of a collection's data. */
|
|
10
|
+
export enum EBMemStorageStrategy {
|
|
11
|
+
/** Keyed by id; `add()` looks up existing entries by id in O(1). */
|
|
12
|
+
MAP = "MAP",
|
|
13
|
+
// LINKED_LIST = "LINKED_LIST",
|
|
14
|
+
/** Plain list; insertion order is preserved. */
|
|
15
|
+
ARRAY = "ARRAY",
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** What happens on {@link IBCollection.add} when a value's id already exists. */
|
|
19
|
+
export enum EBDuplicateIDStrategy {
|
|
20
|
+
/** Replace the existing value. */
|
|
21
|
+
OVERWRITE = "OVERWRITE",
|
|
22
|
+
/** Reject the whole call. */
|
|
23
|
+
ERROR = "ERROR",
|
|
24
|
+
/** Keep the existing value, drop the new one. */
|
|
25
|
+
IGNORE = "IGNORE",
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** How {@link IBCollection.delete} removes matched items from an `ARRAY` collection. */
|
|
29
|
+
export enum EBArrayDeletionStrategy {
|
|
30
|
+
/** Build a new array via `filter()`. */
|
|
31
|
+
NEW_ARRAY = "NEW_ARRAY",
|
|
32
|
+
/** Compact the existing array in place. */
|
|
33
|
+
IN_PLACE = "IN_PLACE",
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** When a collection is written to disk after a mutation. */
|
|
37
|
+
export enum EBAutoFlushStrategy {
|
|
38
|
+
/** Only on an explicit {@link IBCollection.flush} call. */
|
|
39
|
+
NO_AUTO_FLUSH = "NO_AUTO_FLUSH",
|
|
40
|
+
/** After every mutating call (`add`/`update`/`delete`). */
|
|
41
|
+
ALWAYS_AUTO_FLUSH = "ALWAYS_AUTO_FLUSH",
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** What {@link IBCollection.unload} does with unsaved changes. */
|
|
45
|
+
export enum EBDirtyUnloadStrategy {
|
|
46
|
+
/** Reject the unload. */
|
|
47
|
+
ERROR = "ERROR",
|
|
48
|
+
/** Flush first, then unload. */
|
|
49
|
+
FLUSH = "FLUSH",
|
|
50
|
+
/** Discard the unsaved changes silently. */
|
|
51
|
+
IGNORE = "IGNORE",
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Resolves the id of a value; `undefined` means "no id yet". */
|
|
55
|
+
export type TGetIDFunction = (v: any) => Promise<string | undefined>;
|
|
56
|
+
|
|
57
|
+
/** Assigns an id onto a value. */
|
|
58
|
+
export type TSetIDFunction = (v: any, id: string) => Promise<void>;
|
|
59
|
+
|
|
60
|
+
/** Extracts an index key (or several, for a multi-valued index) from a value; `undefined` means "not indexed". */
|
|
61
|
+
export type TIndexKeyFunction<T = any> = (v: T) => Promise<string | string[] | undefined>;
|
|
62
|
+
|
|
63
|
+
/** Per-collection behavior. Passed to {@link IBDatabase.createCollection}/{@link IBDatabase.getCollection}. */
|
|
64
|
+
export interface IBCollectionConfig {
|
|
65
|
+
autoIDStrategy: EBAutoIDStrategy;
|
|
66
|
+
duplicateIDStrategy: EBDuplicateIDStrategy;
|
|
67
|
+
memStorageStrategy: EBMemStorageStrategy;
|
|
68
|
+
arrayDeletionStrategy: EBArrayDeletionStrategy;
|
|
69
|
+
dirtyUnloadStrategy: EBDirtyUnloadStrategy;
|
|
70
|
+
autoFlushStrategy: EBAutoFlushStrategy;
|
|
71
|
+
/** Registry key for a custom {@link TGetIDFunction}. Omit to use the default (reads `.id`). */
|
|
72
|
+
getID?: string;
|
|
73
|
+
/** Registry key for a custom {@link TSetIDFunction}. Omit to use the default (sets `.id`). */
|
|
74
|
+
setID?: string;
|
|
75
|
+
/** Index name → registry key of a {@link TIndexKeyFunction} for that index. Omit for no indexes. */
|
|
76
|
+
indexes?: { [indexKey: string]: string };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Database-wide configuration. */
|
|
80
|
+
export interface IBDatabaseConfig {
|
|
81
|
+
/** Used for any collection created without an explicit config. */
|
|
82
|
+
readonly defaultCollectionConfig: IBCollectionConfig;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Options for {@link IBDatabase.snapshot}. */
|
|
86
|
+
export interface IBSnapshotOptions {
|
|
87
|
+
/** Appended to the version folder's timestamp. Letters, digits, `-` and `_` only. */
|
|
88
|
+
label?: string;
|
|
89
|
+
/** Older version folders under `targetDir` to keep besides the one just created. `0` keeps only the new one. Negative values reject. */
|
|
90
|
+
keepLast?: number;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** A JSON-file-backed database. Obtain one via `getDatabase()`. */
|
|
94
|
+
export interface IBDatabase {
|
|
95
|
+
readonly config: IBDatabaseConfig;
|
|
96
|
+
/** Creates a new collection. Rejects if one with the same (canonicalized) name already exists. */
|
|
97
|
+
createCollection<T>(collectionName: string, collectionConfig?: IBCollectionConfig): Promise<IBCollection<T>>;
|
|
98
|
+
/** Returns the named collection, creating it with `collectionConfig` (or the database default) if it doesn't exist yet. */
|
|
99
|
+
getCollection<T>(collectionName: string, collectionConfig?: IBCollectionConfig): Promise<IBCollection<T>>;
|
|
100
|
+
/** Whether a collection with this (canonicalized) name exists. */
|
|
101
|
+
hasCollection(collectionName: string): Promise<boolean>;
|
|
102
|
+
/** Deletes a collection and invalidates any handle still held on it. Rejects if it doesn't exist. */
|
|
103
|
+
dropCollection(collectionName: string): Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Flushes every loaded collection, then copies the whole database directory into a new, timestamped
|
|
106
|
+
* version folder under `targetDir` (created if needed). Blocks all other database operations for the
|
|
107
|
+
* duration. See {@link IBSnapshotOptions} for pruning older versions. Returns the new version folder's path.
|
|
108
|
+
*/
|
|
109
|
+
snapshot(targetDir: string, options?: IBSnapshotOptions): Promise<string>;
|
|
110
|
+
/**
|
|
111
|
+
* Runs `fn` against isolated, in-memory working copies of `collectionNames`, which must all exist. Reads and
|
|
112
|
+
* writes inside `fn` are visible only within the transaction. If `fn` resolves, every working copy is written
|
|
113
|
+
* to a temp file first - if any of those writes fails, nothing is applied - then all of them replace their
|
|
114
|
+
* files and their collections' live state, and only then do `ADD`/`UPDATE`/`DELETE` handlers fire: once per
|
|
115
|
+
* id the transaction touched, typed by the last operation on it, same as outside a transaction - a value
|
|
116
|
+
* added and later deleted within the same transaction fires nothing. If `fn` throws, nothing is applied,
|
|
117
|
+
* nothing is flushed, and no handler fires.
|
|
118
|
+
* Only collections named in `collectionNames` can be reached from within `fn`; writing to one of them through
|
|
119
|
+
* its regular {@link IBCollection} handle inside `fn` rejects, and so does a nested `transaction()` call.
|
|
120
|
+
* Values mutated in place are shared with the live collection and are neither isolated nor reverted.
|
|
121
|
+
*/
|
|
122
|
+
transaction(collectionNames: string[], fn: (tx: IBTransaction) => Promise<void>): Promise<void>;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** What kind of collection mutation a handler subscribes to via {@link IBCollection.on}. */
|
|
126
|
+
export enum EBModificationType {
|
|
127
|
+
/** A value was stored by `add()` (including an id-based overwrite). */
|
|
128
|
+
ADD = "ADD",
|
|
129
|
+
/** A value was replaced by `update()`. */
|
|
130
|
+
UPDATE = "UPDATE",
|
|
131
|
+
/** A value was removed by `delete()`. */
|
|
132
|
+
DELETE = "DELETE",
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Handler for {@link EBModificationType.ADD}: called with each value stored by `add()`. */
|
|
136
|
+
export type TAddHandler<T> = (added: T) => void | Promise<void>;
|
|
137
|
+
/** Handler for {@link EBModificationType.UPDATE}: called with the previous and the replacement value. */
|
|
138
|
+
export type TUpdateHandler<T> = (previous: T, updated: T) => void | Promise<void>;
|
|
139
|
+
/** Handler for {@link EBModificationType.DELETE}: called with each value removed by `delete()`. */
|
|
140
|
+
export type TDeleteHandler<T> = (deleted: T) => void | Promise<void>;
|
|
141
|
+
|
|
142
|
+
/** Handle returned by {@link IBCollection.on}. */
|
|
143
|
+
export interface IHandlerRegistration {
|
|
144
|
+
/** Unsubscribes the handler. Idempotent. */
|
|
145
|
+
cancel(): Promise<void>;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Whether a handler registered via {@link IBCollection.on} blocks the call that triggered it. */
|
|
149
|
+
export enum EBHandlerMode {
|
|
150
|
+
/** The triggering `add()`/`update()`/`delete()` call doesn't resolve until this handler has run. Default. */
|
|
151
|
+
SYNC = "SYNC",
|
|
152
|
+
/** The handler runs without the triggering call waiting for it. */
|
|
153
|
+
ASYNC = "ASYNC",
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** A single collection of values of type `T`. Obtain one via {@link IBDatabase}. */
|
|
157
|
+
export interface IBCollection<T = any> {
|
|
158
|
+
readonly database: IBDatabase;
|
|
159
|
+
readonly collectionConfig: IBCollectionConfig;
|
|
160
|
+
/** Canonicalized (trimmed, lowercased) name. */
|
|
161
|
+
readonly collectionName: string;
|
|
162
|
+
/** Stable identity used to address this collection's storage, independent of its name. */
|
|
163
|
+
readonly collectionID: string;
|
|
164
|
+
|
|
165
|
+
/** All items, or only those matching `predicate`. A new array, but holding the stored objects themselves - don't mutate them in place. */
|
|
166
|
+
get(predicate?: (v: T) => boolean): Promise<T[]>;
|
|
167
|
+
/** Deletes matching items. Returns the number removed. */
|
|
168
|
+
delete(predicate: (v: T) => boolean): Promise<number>;
|
|
169
|
+
/** Whether any item matches `predicate`. */
|
|
170
|
+
has(predicate: (v: T) => boolean): Promise<boolean>;
|
|
171
|
+
/** Adds values, resolving/assigning ids per the collection's id strategies. Returns the number added. */
|
|
172
|
+
add(...v: T[]): Promise<number>;
|
|
173
|
+
/** Replaces each item matching `predicate` with the (defined) result of `updater`; all-or-nothing if `updater` throws or returns `undefined`. `updater` receives the stored object - return a new one rather than mutating it, and never change its id. Returns the number updated. */
|
|
174
|
+
update(predicate: (v: T) => boolean, updater: (v: T) => Promise<T>): Promise<number>;
|
|
175
|
+
/** Number of items, or only those matching `predicate`. */
|
|
176
|
+
count(predicate?: (v: T) => boolean): Promise<number>;
|
|
177
|
+
/** Items whose {@link IBCollectionConfig.indexes}-configured `indexKey` extracts `key`. A new array, but holding the stored objects themselves. Throws if `indexKey` isn't configured. */
|
|
178
|
+
getByIndex(indexKey: string, key: string): Promise<T[]>;
|
|
179
|
+
/** Subscribes to `add()` mutations. Fires after the value is stored, including any auto-flush to disk. `mode` defaults to {@link EBHandlerMode.SYNC}. A handler's own errors never fail the triggering call; they're logged only. */
|
|
180
|
+
on(modificationType: EBModificationType.ADD, handler: TAddHandler<T>, mode?: EBHandlerMode): Promise<IHandlerRegistration>;
|
|
181
|
+
/** Subscribes to `update()` mutations. Fires after the value is replaced, including any auto-flush to disk. `mode` defaults to {@link EBHandlerMode.SYNC}. A handler's own errors never fail the triggering call; they're logged only. */
|
|
182
|
+
on(modificationType: EBModificationType.UPDATE, handler: TUpdateHandler<T>, mode?: EBHandlerMode): Promise<IHandlerRegistration>;
|
|
183
|
+
/** Subscribes to `delete()` mutations. Fires after the value is removed, including any auto-flush to disk. `mode` defaults to {@link EBHandlerMode.SYNC}. A handler's own errors never fail the triggering call; they're logged only. */
|
|
184
|
+
on(modificationType: EBModificationType.DELETE, handler: TDeleteHandler<T>, mode?: EBHandlerMode): Promise<IHandlerRegistration>;
|
|
185
|
+
|
|
186
|
+
/** Writes the current in-memory state to disk. */
|
|
187
|
+
flush(): Promise<void>;
|
|
188
|
+
/** Discards in-memory state and re-reads it from disk. */
|
|
189
|
+
reload(): Promise<void>;
|
|
190
|
+
/** Evicts the in-memory state (per the dirty-unload strategy if there are unsaved changes). */
|
|
191
|
+
unload(): Promise<void>;
|
|
192
|
+
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Transaction-scoped view of a collection, returned by {@link IBTransaction.getCollection}. Deliberately
|
|
197
|
+
* narrower than {@link IBCollection}: no `flush`/`reload`/`unload`/`on` here - flushing would write the
|
|
198
|
+
* transaction's not-yet-committed state to disk, and `on()` would register a handler against a working copy
|
|
199
|
+
* that either gets discarded (rolled back) or replaced (committed), neither of which is what a caller means
|
|
200
|
+
* by "subscribe to this collection".
|
|
201
|
+
*/
|
|
202
|
+
export interface IBTransactionCollection<T = any> {
|
|
203
|
+
readonly collectionName: string;
|
|
204
|
+
readonly collectionID: string;
|
|
205
|
+
/** Same as {@link IBCollection.get}, scoped to this transaction's in-progress state. */
|
|
206
|
+
get(predicate?: (v: T) => boolean): Promise<T[]>;
|
|
207
|
+
/** Same as {@link IBCollection.has}, scoped to this transaction's in-progress state. */
|
|
208
|
+
has(predicate: (v: T) => boolean): Promise<boolean>;
|
|
209
|
+
/** Same as {@link IBCollection.count}, scoped to this transaction's in-progress state. */
|
|
210
|
+
count(predicate?: (v: T) => boolean): Promise<number>;
|
|
211
|
+
/** Same as {@link IBCollection.getByIndex}, scoped to this transaction's in-progress state. */
|
|
212
|
+
getByIndex(indexKey: string, key: string): Promise<T[]>;
|
|
213
|
+
/** Same as {@link IBCollection.add}, applied only within this transaction until it commits. */
|
|
214
|
+
add(...v: T[]): Promise<number>;
|
|
215
|
+
/** Same as {@link IBCollection.update}, applied only within this transaction until it commits. */
|
|
216
|
+
update(predicate: (v: T) => boolean, updater: (v: T) => Promise<T>): Promise<number>;
|
|
217
|
+
/** Same as {@link IBCollection.delete}, applied only within this transaction until it commits. */
|
|
218
|
+
delete(predicate: (v: T) => boolean): Promise<number>;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Passed into {@link IBDatabase.transaction}'s callback; the only way to reach the collections it declared. */
|
|
222
|
+
export interface IBTransaction {
|
|
223
|
+
/**
|
|
224
|
+
* The transaction-scoped view of `collectionName`. Returns the same instance on repeat calls with the
|
|
225
|
+
* same (canonicalized) name within one transaction. Throws if `collectionName` wasn't declared to
|
|
226
|
+
* {@link IBDatabase.transaction}.
|
|
227
|
+
*/
|
|
228
|
+
getCollection<T = any>(collectionName: string): Promise<IBTransactionCollection<T>>;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Resolves the custom id functions referenced by {@link IBCollectionConfig.getID}/`setID`. */
|
|
232
|
+
export interface IBFunctionRegistry {
|
|
233
|
+
/** Returns the function registered under `key`, or `undefined` if there is none. */
|
|
234
|
+
fetch(key?: string): Function | undefined;
|
|
235
|
+
}
|