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