@fireproof/core 0.3.11 → 0.3.13

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 (52) hide show
  1. package/README.md +1 -1
  2. package/dist/bundle.js +2168 -0
  3. package/dist/src/blockstore.d.ts +115 -0
  4. package/dist/src/blockstore.d.ts.map +1 -0
  5. package/dist/src/clock.d.ts +98 -0
  6. package/dist/src/clock.d.ts.map +1 -0
  7. package/dist/src/crypto.d.ts +18 -0
  8. package/dist/src/crypto.d.ts.map +1 -0
  9. package/dist/src/db-index.d.ts +116 -0
  10. package/dist/src/db-index.d.ts.map +1 -0
  11. package/dist/src/fireproof.d.ts +167 -0
  12. package/dist/src/fireproof.d.ts.map +1 -0
  13. package/dist/src/hydrator.d.ts +6 -0
  14. package/dist/src/hydrator.d.ts.map +1 -0
  15. package/dist/src/index.d.ts +6 -0
  16. package/dist/src/index.d.ts.map +1 -0
  17. package/dist/src/listener.d.ts +36 -0
  18. package/dist/src/listener.d.ts.map +1 -0
  19. package/dist/src/prolly.d.ts +83 -0
  20. package/dist/src/prolly.d.ts.map +1 -0
  21. package/dist/src/sha1.d.ts +9 -0
  22. package/dist/src/sha1.d.ts.map +1 -0
  23. package/dist/src/valet.d.ts +34 -0
  24. package/dist/src/valet.d.ts.map +1 -0
  25. package/dist/tsconfig.tsbuildinfo +1 -0
  26. package/package.json +39 -5
  27. package/src/blockstore.js +24 -23
  28. package/src/clock.js +4 -3
  29. package/src/crypto.js +1 -0
  30. package/src/db-index.js +23 -18
  31. package/src/fireproof.js +31 -26
  32. package/src/hydrator.js +3 -3
  33. package/src/index.js +6 -0
  34. package/src/listener.js +9 -8
  35. package/src/prolly.js +12 -25
  36. package/src/sha1.js +2 -1
  37. package/src/valet.js +22 -20
  38. package/hooks/use-fireproof.js +0 -135
  39. package/index.js +0 -6
  40. package/scripts/keygen.js +0 -3
  41. package/test/block.js +0 -65
  42. package/test/clock.test.js +0 -694
  43. package/test/db-index.test.js +0 -261
  44. package/test/fireproof.test.js +0 -493
  45. package/test/fulltext.test.js +0 -66
  46. package/test/helpers.js +0 -45
  47. package/test/hydrator.test.js +0 -81
  48. package/test/listener.test.js +0 -102
  49. package/test/prolly.test.js +0 -190
  50. package/test/proofs.test.js +0 -53
  51. package/test/reproduce-fixture-bug.test.js +0 -65
  52. package/test/valet.test.js +0 -59
@@ -1,493 +0,0 @@
1
- import { describe, it, beforeEach } from 'mocha'
2
- import assert from 'node:assert'
3
- import Blockstore from '../src/blockstore.js'
4
- import Fireproof from '../src/fireproof.js'
5
- import Hydrator from '../src/hydrator.js'
6
- // import * as codec from '@ipld/dag-cbor'
7
-
8
- let database, resp0
9
-
10
- // const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
11
-
12
- describe('Fireproof', () => {
13
- beforeEach(async () => {
14
- database = Fireproof.storage('helloName')
15
- resp0 = await database.put({
16
- _id: '1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c',
17
- name: 'alice',
18
- age: 42
19
- })
20
- })
21
- it('takes an optional name', () => {
22
- assert.equal(database.name, 'helloName')
23
- const km = database.blocks.valet.getKeyMaterial()
24
- if (process.env.NO_ENCRYPT) { assert.equal(km, null) } else { assert.equal(km.length, 64) }
25
- const x = database.blocks.valet.idb
26
- const keyId = database.blocks.valet.keyId
27
- assert.equal(x.name.toString(), `fp.${keyId}.helloName.valet`)
28
- })
29
- it('only put and get document', async () => {
30
- assert(resp0.id, 'should have id')
31
- assert.equal(resp0.id, '1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
32
- const avalue = await database.get('1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
33
- assert.equal(avalue.name, 'alice')
34
- assert.equal(avalue.age, 42)
35
- assert.equal(avalue._id, '1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
36
- })
37
- it('mvcc put and get document with _clock that matches', async () => {
38
- assert(resp0.clock, 'should have clock')
39
- assert.equal(resp0.clock[0].toString(), 'bafyreiadhnnxgaeeqdxujfew6zxr4lnjyskkrg26cdjvk7tivy6dt4xmsm')
40
- const theDoc = await database.get('1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
41
- theDoc._clock = database.clock
42
- const put2 = await database.put(theDoc)
43
- assert.equal(put2.id, '1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
44
- assert.equal(put2.clock.length, 1)
45
- assert.equal(put2.clock[0].toString(), 'bafyreib2kck2fv73lgahfcd5imarslgxcmachbxxavhtwahx5ppjfts4qe')
46
- })
47
- it('get should return an object instance that is not the same as the one in the db', async () => {
48
- const theDoc = await database.get('1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
49
- const theDoc2 = await database.get('1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
50
- assert.notEqual(theDoc, theDoc2)
51
- theDoc.name = 'really alice'
52
- assert.equal(theDoc.name, 'really alice')
53
- assert.equal(theDoc2.name, 'alice')
54
- })
55
- it('get with mvcc option', async () => {
56
- const theDoc = await database.get('1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c', { mvcc: true })
57
- assert(theDoc._clock, 'should have _clock')
58
- assert.equal(theDoc._clock[0].toString(), 'bafyreiadhnnxgaeeqdxujfew6zxr4lnjyskkrg26cdjvk7tivy6dt4xmsm')
59
- })
60
- it('get with mvcc option where someone else changed another document first', async () => {
61
- const theDoc = await database.get('1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c', { mvcc: true })
62
- const put2 = await database.put({ something: 'else' })
63
- assert(put2.clock, 'should have clock')
64
- assert.notEqual(put2.clock.toString(), resp0.clock.toString())
65
- assert.equal(theDoc._clock.toString(), resp0.clock.toString())
66
- theDoc.name = 'somone else'
67
- const put3works = await database.put(theDoc)
68
- assert(put3works.clock, 'should have id')
69
- })
70
- it('get from an old snapshot with mvcc option', async () => {
71
- const ogClock = resp0.clock
72
- const theDoc = await database.get('1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
73
- theDoc.name = 'not alice'
74
- const put2 = await database.put(theDoc)
75
- assert.equal(put2.id, '1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
76
- assert.notEqual(put2.clock.toString(), ogClock.toString())
77
- const theDoc2 = await database.get('1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c', { clock: ogClock })
78
- assert.equal(theDoc2.name, 'alice')
79
- })
80
- it('put and get document with _clock that does not match b/c the doc changed', async () => {
81
- const theDoc = await database.get('1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c', { mvcc: true })
82
- theDoc.name = 'not alice'
83
- const put2 = await database.put(theDoc)
84
- assert.equal(put2.id, '1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
85
- assert.notEqual(put2.clock.toString(), theDoc._clock.toString())
86
-
87
- const err = await database.put(theDoc).catch((err) => err)
88
- assert.match(err.message, /MVCC conflict/)
89
- })
90
- it('put and get document with _clock that does not match b/c a different doc changed should succeed', async () => {
91
- const theDoc = await database.get('1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c', { mvcc: true })
92
- assert.equal(theDoc.name, 'alice')
93
-
94
- const putAnotherDoc = await database.put({ nothing: 'to see here' })
95
- assert.notEqual(putAnotherDoc.clock.toString(), theDoc._clock.toString())
96
-
97
- const ok = await database.put({ name: "isn't alice", ...theDoc })
98
- assert.equal(ok.id, '1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
99
- })
100
- it('put and get document with _clock that does not match b/c the doc was deleted', async () => {
101
- const theDoc = await database.get('1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c', { mvcc: true })
102
- assert.equal(theDoc.name, 'alice')
103
- const del = await database.del(theDoc)
104
- assert(del.id)
105
- const err = await database.put(theDoc).catch((err) => err)
106
- console.log('err', err)
107
- assert.match(err.message, /MVCC conflict/)
108
- })
109
- it('allDocuments', async () => {
110
- await database.put({ name: 'bob' })
111
- const allDocs = await database.allDocuments()
112
- assert.equal(allDocs.rows.length, 2)
113
- })
114
- it('has a factory for making new instances with default settings', async () => {
115
- // TODO if you pass it an email it asks the local keyring, and if no key, does the email validation thing
116
- const db = await Fireproof.storage({ email: 'jchris@gmail.com' })
117
- assert(db instanceof Fireproof)
118
- })
119
- it('an empty database has no documents', async () => {
120
- const db = Fireproof.storage()
121
- assert(db instanceof Fireproof)
122
- const e = await db.get('8c5c0c5c0c5c').catch((err) => err)
123
- assert.equal(e.message, 'Not found')
124
- const changes = await db.changesSince()
125
- assert.equal(changes.rows.length, 0)
126
- })
127
- it('delete on an empty database', async () => {
128
- const db = Fireproof.storage()
129
- assert(db instanceof Fireproof)
130
- const e = await db.del('8c5c0c5c0c5c').catch((err) => err)
131
- assert.equal(e.id, '8c5c0c5c0c5c')
132
- const changes = await db.changesSince()
133
- assert.equal(changes.rows.length, 0)
134
- })
135
- it('update existing document', async () => {
136
- // const alice = await database.get('1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
137
- // assert.equal(alice.name, 'alice')
138
-
139
- const dogKey = 'aster-3c3a-4b5e-9c1c-8c5c0c5c0c5c'
140
- const value = {
141
- _id: dogKey,
142
- name: 'aster',
143
- age: 2
144
- }
145
- const response = await database.put(value)
146
- assert(response.id, 'should have id')
147
- assert.equal(response.id, dogKey)
148
- assert.equal(value._id, dogKey)
149
- const snapshot = Hydrator.snapshot(database)
150
-
151
- const avalue = await database.get(dogKey)
152
- assert.equal(avalue.name, value.name)
153
- assert.equal(avalue.age, value.age)
154
- assert.equal(avalue._id, dogKey)
155
-
156
- avalue.age = 3
157
- const response2 = await database.put(avalue)
158
- assert(response2.id, 'should have id')
159
- assert.equal(response2.id, dogKey)
160
-
161
- const bvalue = await database.get(dogKey)
162
- assert.equal(bvalue.name, value.name)
163
- assert.equal(bvalue.age, 3)
164
- assert.equal(bvalue._id, dogKey)
165
-
166
- const snapdoc = await snapshot.get(dogKey)
167
- // console.log('snapdoc', snapdoc)
168
- // assert(snapdoc.id, 'should have id')
169
- assert.equal(snapdoc._id, dogKey)
170
- assert.equal(snapdoc.age, 2)
171
- })
172
- it("update document with validation function that doesn't allow it", async () => {
173
- const validationDatabase = new Fireproof(new Blockstore(), [], {
174
- validateChange: (newDoc, oldDoc, authCtx) => {
175
- if (newDoc.name === 'bob') {
176
- throw new Error('no bobs allowed')
177
- }
178
- }
179
- })
180
- const validResp = await validationDatabase.put({
181
- _id: '111-alice',
182
- name: 'alice',
183
- age: 42
184
- })
185
- const getResp = await validationDatabase.get(validResp.id)
186
- assert.equal(getResp.name, 'alice')
187
-
188
- let e = await validationDatabase
189
- .put({
190
- _id: '222-bob',
191
- name: 'bob',
192
- age: 11
193
- })
194
- .catch((e) => e)
195
- assert.equal(e.message, 'no bobs allowed')
196
-
197
- e = await validationDatabase.get('222-bob').catch((e) => e)
198
- assert.equal(e.message, 'Not found')
199
- })
200
-
201
- it('get missing document', async () => {
202
- const e = await database.get('missing').catch((e) => e)
203
- assert.equal(e.message, 'Not found')
204
- })
205
- it('delete the only document', async () => {
206
- const id = '1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c'
207
- const found = await database.get(id)
208
- assert.equal(found._id, id)
209
- const deleted = await database.del(id)
210
- assert.equal(deleted.id, id)
211
- const e = await database
212
- .get(id)
213
- .then((doc) => assert.equal('should be deleted', JSON.stringify(doc)))
214
- .catch((e) => {
215
- if (e.message !== 'Not found') {
216
- throw e
217
- }
218
- return e
219
- })
220
- assert.equal(e.message, 'Not found')
221
- })
222
-
223
- it('delete not last document', async () => {
224
- const resp1 = await database.put({
225
- _id: 'second',
226
- name: 'bob',
227
- age: 39
228
- })
229
-
230
- // const id = '1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c'
231
- const id = resp1.id
232
- const found = await database.get(id)
233
- assert.equal(found._id, id)
234
- const deleted = await database.del(id)
235
- assert.equal(deleted.id, id)
236
- const e = await database
237
- .get(id)
238
- .then((doc) => assert.equal('should be deleted', JSON.stringify(doc)))
239
- .catch((e) => {
240
- if (e.message !== 'Not found') {
241
- throw e
242
- }
243
- return e
244
- })
245
- assert.equal(e.message, 'Not found')
246
- })
247
-
248
- it("delete a document with validation function that doesn't allow it", async () => {
249
- const validationDatabase = new Fireproof(new Blockstore(), [], {
250
- validateChange: (newDoc, oldDoc, authCtx) => {
251
- if (oldDoc.name === 'bob') {
252
- throw new Error('no changing bob')
253
- }
254
- }
255
- })
256
- const validResp = await validationDatabase.put({
257
- _id: '222-bob',
258
- name: 'bob',
259
- age: 11
260
- })
261
- const getResp = await validationDatabase.get(validResp.id)
262
- assert.equal(getResp.name, 'bob')
263
-
264
- const e = await validationDatabase
265
- .put({
266
- _id: '222-bob',
267
- name: 'bob',
268
- age: 12
269
- })
270
- .catch((e) => e)
271
- assert.equal(e.message, 'no changing bob')
272
-
273
- let prevBob = await validationDatabase.get('222-bob')
274
- assert.equal(prevBob.name, 'bob')
275
- assert.equal(prevBob.age, 11)
276
-
277
- const e2 = await validationDatabase.del('222-bob').catch((e) => e)
278
- assert.equal(e2.message, 'no changing bob')
279
-
280
- prevBob = await validationDatabase.get('222-bob')
281
- assert.equal(prevBob.name, 'bob')
282
- assert.equal(prevBob.age, 11)
283
- })
284
-
285
- it('provides docs since tiny', async () => {
286
- const result = await database.changesSince()
287
- assert.equal(result.rows.length, 1)
288
- assert.equal(result.rows[0].key, '1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
289
-
290
- // console.log('result', result)
291
-
292
- // const result2 = await database.changesSince(result.clock)
293
- // console.log('result2', result2)
294
- // assert.equal(result2.rows.length, 0)
295
-
296
- const bKey = 'befbef-3c3a-4b5e-9c1c-bbbbbb'
297
- const bvalue = {
298
- _id: bKey,
299
- name: 'bob',
300
- age: 44
301
- }
302
- const response = await database.put(bvalue)
303
- assert(response.id, 'should have id')
304
- assert.equal(response.id, bKey)
305
-
306
- const res3 = await database.changesSince()
307
- assert.equal(res3.rows.length, 2)
308
-
309
- const res4 = await database.changesSince(result.clock)
310
- assert.equal(res4.rows.length, 1)
311
- })
312
-
313
- it('provides docs since', async () => {
314
- const result = await database.changesSince()
315
- assert.equal(result.rows.length, 1)
316
- assert.equal(result.rows[0].key, '1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
317
-
318
- const result2 = await database.changesSince(result.clock)
319
- assert.equal(result2.rows.length, 0)
320
-
321
- const bKey = 'befbef-3c3a-4b5e-9c1c-bbbbbb'
322
- const bvalue = {
323
- _id: bKey,
324
- name: 'bob',
325
- age: 44
326
- }
327
- const response = await database.put(bvalue)
328
- assert(response.id, 'should have id')
329
- assert.equal(response.id, bKey)
330
-
331
- const res3 = await database.changesSince(result2.clock)
332
- assert.equal(res3.rows.length, 1)
333
-
334
- const res4 = await database.changesSince(res3.clock)
335
- assert.equal(res4.rows.length, 0)
336
- assert.equal(res4.clock[0], res3.clock[0])
337
- assert.equal(res4.clock.length, res3.clock.length)
338
-
339
- const cKey = 'cefecef-3c3a-4b5e-9c1c-bbbbbb'
340
- const value = {
341
- _id: cKey,
342
- name: 'carol',
343
- age: 44
344
- }
345
- const response2 = await database.put(value)
346
- assert(response2.id, 'should have id')
347
- assert.equal(response2.id, cKey)
348
-
349
- const res5 = await database.changesSince(res4.clock)
350
-
351
- assert.equal(res5.rows.length, 1)
352
-
353
- const res6 = await database.changesSince(result2.clock)
354
- assert.equal(res6.rows.length, 2)
355
-
356
- const resultAll = await database.changesSince()
357
- assert.equal(resultAll.rows.length, 3)
358
- assert.equal(resultAll.rows[0].key, '1ef3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c')
359
-
360
- const res7 = await database.changesSince(resultAll.clock)
361
- assert.equal(res7.rows.length, 0)
362
-
363
- const valueCupdate = {
364
- _id: cKey,
365
- name: 'carol update',
366
- age: 45
367
- }
368
- const responseUpdate = await database.put(valueCupdate)
369
- assert(responseUpdate.id)
370
-
371
- const res8 = await database.changesSince(resultAll.clock)
372
- assert.equal(res8.rows.length, 1)
373
-
374
- const res9 = await database.changesSince(res8.clock)
375
- assert.equal(res9.rows.length, 0)
376
- })
377
-
378
- it('docs since repeated changes', async () => {
379
- assert.equal((await database.changesSince()).rows.length, 1)
380
- let resp, doc, changes
381
- for (let index = 0; index < 30; index++) {
382
- const id = '1' + (301 - index).toString()
383
- // console.log(`Putting id: ${id}, index: ${index}`)
384
- resp = await database.put({ index, _id: id }).catch(e => {
385
- assert.fail(`put failed on _id: ${id}, error: ${e.message}`)
386
- })
387
- assert(resp.id, `Failed to obtain resp.id for _id: ${id}`)
388
-
389
- // console.log(`vis for update id: ${id}, index:`, index)
390
- // for await (const line of database.vis()) {
391
- // console.log(line)
392
- // }
393
-
394
- doc = await database.get(resp.id).catch(e => {
395
- console.log('failed', e)
396
- assert.fail(`get failed on _id: ${id}, error: ${e.message}`)
397
- })
398
-
399
- assert.equal(doc.index, index, `doc.index is not equal to index for _id: ${id}`)
400
- changes = await database.changesSince().catch(async e => {
401
- assert.fail(`changesSince failed on _id: ${id}, error: ${e.message}`)
402
- })
403
- changes.rows.forEach(row => {
404
- for (const key in row) {
405
- const value = row[key]
406
- assert(!/^bafy/.test(value), `Unexpected "bafy..." value found at index ${index} in row ${JSON.stringify(row)}`)
407
- }
408
- })
409
-
410
- database.blocks.clearCommittedCache() // clear cache to force re-reading from encrypted store
411
-
412
- doc = await database.get(resp.id).catch(e => {
413
- console.log('failed', e)
414
- assert.fail(`get failed on _id: ${id}, error: ${e.message}`)
415
- })
416
-
417
- assert.equal(doc.index, index, `doc.index is not equal to index for _id: ${id}`)
418
- changes = await database.changesSince().catch(async e => {
419
- assert.fail(`changesSince failed on _id: ${id}, error: ${e.message}`)
420
- })
421
- changes.rows.forEach(row => {
422
- for (const key in row) {
423
- const value = row[key]
424
- assert(!/^bafy/.test(value), `Unexpected "bafy..." value found at index ${index} in row ${JSON.stringify(row)}`)
425
- }
426
- })
427
-
428
- // console.log('changes: ', index, changes.rows.length, JSON.stringify(changes.rows))
429
- assert.equal(changes.rows.length, index + 2, `failed on ${index}, with ${changes.rows.length} ${id}`)
430
- }
431
- }).timeout(30000)
432
-
433
- it('concurrent transactions', async () => {
434
- assert.equal((await database.changesSince()).rows.length, 1)
435
- const promises = []
436
- let putYes = 0
437
- for (let index = 0; index < 20; index++) {
438
- const id = 'a' + (300 - index).toString()
439
- promises.push(database.put({ index, _id: id }).catch(e => {
440
- assert.equal(e.message, 'put failed on _id: ' + id)
441
- }).then(r => {
442
- if (r.id) {
443
- putYes++
444
- return database.get(r.id).catch(e => {
445
- // assert.equal(e.message, 'get failed on _id: ' + r.id)
446
- }).then(d => {
447
- // assert.equal(d.index, index)
448
- return r.id
449
- })
450
- }
451
- }))
452
- promises.push(database.changesSince().catch(e => {
453
- assert.equal(e.message, 'changesSince failed')
454
- }).then(c => {
455
- assert(c.rows.length > 0)
456
- return c.rows.length
457
- }))
458
- }
459
- const got = await Promise.all(promises)
460
- assert.equal(got.length, putYes * 2)
461
- // console.log('putYes', putYes)
462
- // await sleep(1000)
463
- assert.equal((await database.changesSince()).rows.length, 2)
464
- }).timeout(20000)
465
- it('serialize database', async () => {
466
- await database.put({ _id: 'rehy', name: 'drate' })
467
- assert.equal((await database.changesSince()).rows.length, 2)
468
- const serialized = JSON.parse(JSON.stringify(database))
469
- assert.equal(serialized.name, 'helloName')
470
- assert.equal(serialized.clock.length, 1)
471
- })
472
- it('clocked changes in order', async () => {
473
- await database.put({ _id: '2' })
474
- await database.put({ _id: 'three' })
475
- await database.put({ _id: '4' })
476
- const changes = await database.changesSince(resp0.clock)
477
- assert.equal(changes.rows.length, 3)
478
- assert.equal(changes.rows[0].key, '2')
479
- assert.equal(changes.rows[1].key, 'three')
480
- assert.equal(changes.rows[2].key, '4')
481
- })
482
- it.skip('changes in order', async () => {
483
- await database.put({ _id: '2' })
484
- await database.put({ _id: 'three' })
485
- await database.put({ _id: '4' })
486
- const changes = await database.changesSince()
487
- assert.equal(changes.rows.length, 4)
488
- assert.equal(changes.rows[0].key, resp0.id)
489
- assert.equal(changes.rows[1].key, '2')
490
- assert.equal(changes.rows[2].key, 'three')
491
- assert.equal(changes.rows[3].key, '4')
492
- })
493
- })
@@ -1,66 +0,0 @@
1
- import { describe, it, beforeEach } from 'mocha'
2
- import assert from 'node:assert'
3
- import Fireproof from '../src/fireproof.js'
4
- import flexsearch from 'flexsearch'
5
- const { Index } = flexsearch
6
- // this is an illustration of how to use the flexsearch library
7
-
8
- let database, flexed
9
-
10
- describe('Fulltext with flexsearch', () => {
11
- beforeEach(async () => {
12
- database = Fireproof.storage()
13
- flexed = withFlexsearch(database) // this is a function that adds the flexsearch library to the database object
14
-
15
- const messages = [
16
- 'Hello World, this is a test',
17
- 'We are testing the flexsearch library',
18
- 'When we test we test',
19
- 'Apples are red',
20
- 'Bananas are yellow',
21
- 'Oranges are orange',
22
- 'Pears are green',
23
- 'Grapes are purple',
24
- 'Strawberries are red',
25
- 'Blueberries are blue',
26
- 'Raspberries are red',
27
- 'Watermelons are green',
28
- 'Pineapples are yellow'
29
- ]
30
- for (let i = 0, len = messages.length; i < len; i++) {
31
- await database.put({
32
- _id: `message-${i}`,
33
- message: messages[i]
34
- })
35
- }
36
- })
37
-
38
- it('search the index', async () => {
39
- const changes = await database.changesSince()
40
- assert.equal(changes.rows.length, 13)
41
- const results = await flexed.search('red')
42
- assert.equal(results.length, 3)
43
- for (let i = 0, len = results.length; i < len; i++) {
44
- const doc = await database.get(results[i])
45
- assert.match(doc.message, /red/)
46
- }
47
- })
48
- // it('add more docs and search again', async () => {})
49
- // it('delete some docs and search again', async () => {})
50
- // it('update some docs and search again', async () => {})
51
- })
52
-
53
- function withFlexsearch (database, flexsearchOptions = {}) {
54
- const index = new Index(flexsearchOptions)
55
- let clock = null
56
- const search = async (query, options) => {
57
- const changes = await database.changesSince(clock)
58
- clock = changes.clock
59
- for (let i = 0; i < changes.rows.length; i++) {
60
- const { key, value } = changes.rows[i]
61
- await index.add(key, value.message)
62
- }
63
- return index.search(query, options)
64
- }
65
- return { search }
66
- }
package/test/helpers.js DELETED
@@ -1,45 +0,0 @@
1
- import crypto from 'node:crypto'
2
- // import assert from 'node:assert'
3
- import * as Link from 'multiformats/link'
4
- import * as raw from 'multiformats/codecs/raw'
5
- import { sha256 } from 'multiformats/hashes/sha2'
6
- import { MemoryBlockstore } from './block.js'
7
-
8
- // console.x = console.log
9
- // console.log = function (...args) {
10
- // // window.mutedLog = window.mutedLog || []
11
- // // window.mutedLog.push(args)
12
- // }
13
-
14
- /** @param {number} size */
15
- export async function randomCID (size) {
16
- const hash = await sha256.digest(await randomBytes(size))
17
- return Link.create(raw.code, hash)
18
- }
19
-
20
- /** @param {number} size */
21
- export async function randomBytes (size) {
22
- const bytes = new Uint8Array(size)
23
- while (size) {
24
- const chunk = new Uint8Array(Math.min(size, 65_536))
25
- crypto.getRandomValues(chunk)
26
- size -= bytes.length
27
- bytes.set(chunk, size)
28
- }
29
- return bytes
30
- }
31
-
32
- export class Blockstore extends MemoryBlockstore {
33
-
34
- }
35
-
36
- let seq = 0
37
- export function seqEventData (tag = '') {
38
- return {
39
- type: 'put',
40
- value: `event${seq++}${tag}`
41
- }
42
- }
43
- export function setSeq (n) {
44
- seq = n
45
- }
@@ -1,81 +0,0 @@
1
- import { describe, it, beforeEach } from 'mocha'
2
- import assert from 'node:assert'
3
- import Fireproof from '../src/fireproof.js'
4
- import DbIndex from '../src/db-index.js'
5
- import Hydrator from '../src/hydrator.js'
6
-
7
- describe('DbIndex query', () => {
8
- let database, index
9
- beforeEach(async () => {
10
- database = Fireproof.storage()
11
- const docs = [
12
- { _id: 'a1s3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c', name: 'alice', age: 40 },
13
- { _id: 'b2s3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c', name: 'bob', age: 40 },
14
- { _id: 'c3s3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c', name: 'carol', age: 43 },
15
- { _id: 'd4s3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c', name: 'dave', age: 48 },
16
- { _id: 'e4s3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c', name: 'emily', age: 4 },
17
- { _id: 'f4s3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c', name: 'frank', age: 7 }
18
- ]
19
- for (const doc of docs) {
20
- const id = doc._id
21
- const response = await database.put(doc)
22
- assert(response)
23
- assert(response.id, 'should have id')
24
- assert.equal(response.id, id)
25
- }
26
- index = new DbIndex(database, function (doc, map) {
27
- map(doc.age, doc.name)
28
- }, null, { name: 'names_by_age' })
29
- })
30
- it('serialize database with index', async () => {
31
- await database.put({ _id: 'rehy', name: 'drate', age: 1 })
32
- assert.equal((await database.changesSince()).rows.length, 7)
33
- const result = await index.query({ range: [0, 54] })
34
- assert.equal(result.rows[0].value, 'drate')
35
- const serialized = database.toJSON()
36
- // console.log('serialized', serialized)
37
- assert.equal(serialized.name, 'global')
38
- if (database.blocks.valet.keyId !== 'null') {
39
- assert.equal(serialized.key.length, 64)
40
- }
41
- assert.equal(serialized.clock.length, 1)
42
- assert.equal(serialized.clock[0].constructor.name, 'String')
43
- assert.equal(serialized.indexes.length, 1)
44
- assert.equal(serialized.indexes[0].code, `function (doc, map) {
45
- map(doc.age, doc.name)
46
- }`)
47
- assert.equal(serialized.indexes[0].name, 'names_by_age')
48
-
49
- assert.equal(serialized.indexes[0].clock.byId.constructor.name, 'String')
50
- assert.equal(serialized.indexes[0].clock.byKey.constructor.name, 'String')
51
- assert.equal(serialized.indexes[0].clock.db[0].constructor.name, 'String')
52
- })
53
- it('rehydrate database', async () => {
54
- await database.put({ _id: 'rehy', name: 'drate', age: 1 })
55
- assert.equal((await database.changesSince()).rows.length, 7)
56
- const result = await index.query({ range: [0, 54] })
57
- assert.equal(result.rows[0].value, 'drate')
58
-
59
- const serialized = JSON.parse(JSON.stringify(database))
60
- // console.log('serialized', JSON.stringify(serialized))
61
- // connect it to the same blockstore for testing
62
- const newDb = Hydrator.fromJSON(serialized, database)
63
- assert.equal(newDb.name, 'global')
64
- assert.equal(newDb.clock.length, 1)
65
- assert.equal((await newDb.changesSince()).rows.length, 7)
66
- const newIndex = [...newDb.indexes.values()][0]
67
- assert.equal(newIndex.mapFn, `function (doc, map) {
68
- map(doc.age, doc.name)
69
- }`)
70
- assert.equal(newIndex.indexById.cid, 'bafyreifuz54ugnq77fur47vwv3dwab7p3gpnf5to6hlnbhv5p4kwo7auoi')
71
- // assert.equal(newIndex.indexById.root, null)
72
-
73
- assert.equal(newIndex.indexByKey.cid, 'bafyreicr5rpvsxnqchcwk5rxlmdvd3fah2vexmbsp2dvr4cfdxd2q2ycgu')
74
- // assert.equal(newIndex.indexByKey.root, null)
75
-
76
- assert.equal(newIndex.name, 'names_by_age')
77
-
78
- const newResult = await newIndex.query({ range: [0, 54] })
79
- assert.equal(newResult.rows[0].value, 'drate')
80
- })
81
- })