@fireproof/core 0.3.12 → 0.3.14

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.
@@ -1,6 +1,321 @@
1
- import { Fireproof } from './fireproof.js';
2
- import { DbIndex as Index } from './db-index.js';
3
- import { Listener } from './listener.js';
4
- import { Hydrator } from './hydrator.js';
5
- export { Fireproof, Index, Listener, Hydrator };
6
- //# sourceMappingURL=index.d.ts.map
1
+ /**
2
+ * @class Fireproof
3
+ * @classdesc Fireproof stores data in IndexedDB and provides a Merkle clock.
4
+ * This is the main class for saving and loading JSON and other documents with the database. You can find additional examples and
5
+ * usage guides in the repository README.
6
+ *
7
+ * @param {import('./blockstore.js').TransactionBlockstore} blocks - The block storage instance to use documents and indexes
8
+ * @param {CID[]} clock - The Merkle clock head to use for the Fireproof instance.
9
+ * @param {object} [config] - Optional configuration options for the Fireproof instance.
10
+ * @param {object} [authCtx] - Optional authorization context object to use for any authentication checks.
11
+ *
12
+ */
13
+ declare class Fireproof {
14
+ /**
15
+ * @function storage
16
+ * @memberof Fireproof
17
+ * Creates a new Fireproof instance with default storage settings
18
+ * Most apps should use this and not worry about the details.
19
+ * @static
20
+ * @returns {Fireproof} - a new Fireproof instance
21
+ */
22
+ static storage: (name?: string) => Fireproof;
23
+ constructor(blocks: any, clock: any, config: any, authCtx?: {});
24
+ listeners: Set<any>;
25
+ name: any;
26
+ instanceId: string;
27
+ blocks: any;
28
+ clock: any;
29
+ config: any;
30
+ authCtx: {};
31
+ indexes: Map<any, any>;
32
+ /**
33
+ * Renders the Fireproof instance as a JSON object.
34
+ * @returns {Object} - The JSON representation of the Fireproof instance. Includes clock heads for the database and its indexes.
35
+ * @memberof Fireproof
36
+ * @instance
37
+ */
38
+ toJSON(): any;
39
+ /**
40
+ * Returns the Merkle clock heads for the Fireproof instance.
41
+ * @returns {string[]} - The Merkle clock heads for the Fireproof instance.
42
+ * @memberof Fireproof
43
+ * @instance
44
+ */
45
+ clockToJSON(): string[];
46
+ hydrate({ clock, name, key }: {
47
+ clock: any;
48
+ name: any;
49
+ key: any;
50
+ }): void;
51
+ indexBlocks: any;
52
+ /**
53
+ * Triggers a notification to all listeners
54
+ * of the Fireproof instance so they can repaint UI, etc.
55
+ * @returns {Promise<void>}
56
+ * @memberof Fireproof
57
+ * @instance
58
+ */
59
+ notifyReset(): Promise<void>;
60
+ notifyExternal(source?: string): Promise<void>;
61
+ /**
62
+ * Returns the changes made to the Fireproof instance since the specified event.
63
+ * @function changesSince
64
+ * @param {CID[]} [event] - The clock head to retrieve changes since. If null or undefined, retrieves all changes.
65
+ * @returns {Promise<{rows : Object[], clock: CID[], proof: {}}>} An object containing the rows and the head of the instance's clock.
66
+ * @memberof Fireproof
67
+ * @instance
68
+ */
69
+ changesSince(event?: CID[]): Promise<{
70
+ rows: any[];
71
+ clock: CID[];
72
+ proof: {};
73
+ }>;
74
+ allDocuments(): Promise<{
75
+ rows: {
76
+ key: any;
77
+ value: any;
78
+ }[];
79
+ clock: string[];
80
+ proof: any[];
81
+ }>;
82
+ /**
83
+ * Runs validation on the specified document using the Fireproof instance's configuration. Throws an error if the document is invalid.
84
+ *
85
+ * @param {Object} doc - The document to validate.
86
+ * @returns {Promise<void>}
87
+ * @throws {Error} - Throws an error if the document is invalid.
88
+ * @memberof Fireproof
89
+ * @instance
90
+ */
91
+ runValidation(doc: any): Promise<void>;
92
+ /**
93
+ * Retrieves the document with the specified ID from the database
94
+ *
95
+ * @param {string} key - the ID of the document to retrieve
96
+ * @param {Object} [opts] - options
97
+ * @returns {Promise<{_id: string}>} - the document with the specified ID
98
+ * @memberof Fireproof
99
+ * @instance
100
+ */
101
+ get(key: string, opts?: any): Promise<{
102
+ _id: string;
103
+ }>;
104
+ /**
105
+ * Adds a new document to the database, or updates an existing document. Returns the ID of the document and the new clock head.
106
+ *
107
+ * @param {Object} doc - the document to be added
108
+ * @param {string} doc._id - the document ID. If not provided, a random ID will be generated.
109
+ * @param {CID[]} doc._clock - the document ID. If not provided, a random ID will be generated.
110
+ * @param {Proof} doc._proof - CIDs referenced by the update
111
+ * @returns {Promise<{ id: string, clock: CID[] }>} - The result of adding the document to the database
112
+ * @memberof Fireproof
113
+ * @instance
114
+ */
115
+ put({ _id, _proof, ...doc }: {
116
+ _id: string;
117
+ _clock: CID[];
118
+ _proof: Proof;
119
+ }): Promise<{
120
+ id: string;
121
+ clock: CID[];
122
+ }>;
123
+ /**
124
+ * Deletes a document from the database
125
+ * @param {string | any} docOrId - the document ID
126
+ * @returns {Promise<{ id: string, clock: CID[] }>} - The result of deleting the document from the database
127
+ * @memberof Fireproof
128
+ * @instance
129
+ */
130
+ del(docOrId: string | any): Promise<{
131
+ id: string;
132
+ clock: CID[];
133
+ }>;
134
+ /**
135
+ * Updates the underlying storage with the specified event.
136
+ * @private
137
+ * @param {{del?: true, key : string, value?: any}} decodedEvent - the event to add
138
+ * @returns {Promise<{ proof:{}, id: string, clock: CID[] }>} - The result of adding the event to storage
139
+ */
140
+ private putToProllyTree;
141
+ vis(): AsyncGenerator<any, {
142
+ cids: any;
143
+ result: any;
144
+ vis?: undefined;
145
+ } | {
146
+ vis: string;
147
+ cids: any;
148
+ result?: undefined;
149
+ }, unknown>;
150
+ visTree(): Promise<{
151
+ cids: any;
152
+ result: any;
153
+ vis?: undefined;
154
+ } | {
155
+ vis: string;
156
+ cids: any;
157
+ result?: undefined;
158
+ }>;
159
+ visClock(): Promise<{
160
+ vis: string;
161
+ }>;
162
+ /**
163
+ * Registers a Listener to be called when the Fireproof instance's clock is updated.
164
+ * Recieves live changes from the database after they are committed.
165
+ * @param {Function} listener - The listener to be called when the clock is updated.
166
+ * @returns {Function} - A function that can be called to unregister the listener.
167
+ * @memberof Fireproof
168
+ */
169
+ registerListener(listener: Function): Function;
170
+ notifyListeners(changes: any): Promise<void>;
171
+ setCarUploader(carUploaderFn: any): void;
172
+ setRemoteBlockReader(remoteBlockReaderFn: any): void;
173
+ }
174
+ declare class Proof {
175
+ }
176
+
177
+ /**
178
+ * Represents an DbIndex for a Fireproof database.
179
+ *
180
+ * @class DbIndex
181
+ * @classdesc An DbIndex can be used to order and filter the documents in a Fireproof database.
182
+ *
183
+ * @param {Fireproof} database - The Fireproof database instance to DbIndex.
184
+ * @param {Function} mapFn - The map function to apply to each entry in the database.
185
+ *
186
+ */
187
+ declare class DbIndex {
188
+ static registerWithDatabase(inIndex: any, database: any): void;
189
+ static fromJSON(database: any, { code, clock, name }: {
190
+ code: any;
191
+ clock: any;
192
+ name: any;
193
+ }): DbIndex;
194
+ constructor(database: any, mapFn: any, clock: any, opts?: {});
195
+ database: any;
196
+ mapFnString: any;
197
+ mapFn: any;
198
+ name: any;
199
+ indexById: {
200
+ root: any;
201
+ cid: any;
202
+ };
203
+ indexByKey: {
204
+ root: any;
205
+ cid: any;
206
+ };
207
+ dbHead: any;
208
+ instanceId: string;
209
+ updateIndexPromise: Promise<void>;
210
+ makeName(): any;
211
+ toJSON(): {
212
+ name: any;
213
+ code: any;
214
+ clock: {
215
+ db: any;
216
+ byId: any;
217
+ byKey: any;
218
+ };
219
+ };
220
+ /**
221
+ * JSDoc for Query type.
222
+ * @typedef {Object} DbQuery
223
+ * @property {string[]} [range] - The range to query.
224
+ * @memberof DbIndex
225
+ */
226
+ /**
227
+ * Query object can have {range}
228
+ * @param {DbQuery} query - the query range to use
229
+ * @returns {Promise<{proof: {}, rows: Array<{id: string, key: string, value: any}>}>}
230
+ * @memberof DbIndex
231
+ * @instance
232
+ */
233
+ query(query: {
234
+ /**
235
+ * - The range to query.
236
+ */
237
+ range?: string[];
238
+ }, update?: boolean): Promise<{
239
+ proof: {};
240
+ rows: Array<{
241
+ id: string;
242
+ key: string;
243
+ value: any;
244
+ }>;
245
+ }>;
246
+ /**
247
+ * Update the DbIndex with the latest changes
248
+ * @private
249
+ * @returns {Promise<void>}
250
+ */
251
+ private updateIndex;
252
+ innerUpdateIndex(inBlocks: any): Promise<void>;
253
+ }
254
+ /**
255
+ * JDoc for the result row type.
256
+ */
257
+ type ChangeEvent = {
258
+ /**
259
+ * - The key of the document.
260
+ */
261
+ key: string;
262
+ /**
263
+ * - The new value of the document.
264
+ */
265
+ value: any;
266
+ /**
267
+ * - Is the row deleted?
268
+ */
269
+ del?: boolean;
270
+ };
271
+
272
+ /**
273
+ * A Fireproof database Listener allows you to react to events in the database.
274
+ *
275
+ * @class Listener
276
+ * @classdesc An listener attaches to a Fireproof database and runs a routing function on each change, sending the results to subscribers.
277
+ *
278
+ * @param {import('./fireproof.js').Fireproof} database - The Fireproof database instance to index.
279
+ * @param {Function} routingFn - The routing function to apply to each entry in the database.
280
+ */
281
+ declare class Listener {
282
+ /**
283
+ * @param {import('./fireproof.js').Fireproof} database
284
+ * @param {(_: any, emit: any) => void} routingFn
285
+ */
286
+ constructor(database: Fireproof, routingFn: (_: any, emit: any) => void);
287
+ subcribers: Map<any, any>;
288
+ doStopListening: any;
289
+ database: Fireproof;
290
+ /**
291
+ * The map function to apply to each entry in the database.
292
+ * @type {Function}
293
+ */
294
+ routingFn: Function;
295
+ dbHead: any;
296
+ /**
297
+ * Subscribe to a topic emitted by the event function.
298
+ * @param {string} topic - The topic to subscribe to.
299
+ * @param {Function} subscriber - The function to call when the topic is emitted.
300
+ * @returns {Function} A function to unsubscribe from the topic.
301
+ * @memberof Listener
302
+ * @instance
303
+ * @param {any} since
304
+ */
305
+ on(topic: string, subscriber: Function, since: any): Function;
306
+ /**
307
+ * @typedef {import('./db-index').ChangeEvent} ChangeEvent
308
+ */
309
+ /**
310
+ * @param {ChangeEvent[]} changes
311
+ */
312
+ onChanges(changes: ChangeEvent[]): void;
313
+ }
314
+
315
+ declare class Hydrator {
316
+ static fromJSON(json: any, database: any): any;
317
+ static snapshot(database: any, clock: any): any;
318
+ static zoom(database: any, clock: any): Promise<any>;
319
+ }
320
+
321
+ export { Fireproof, Hydrator, DbIndex as Index, Listener };