@fireproof/core 0.20.0-dev-preview-05 → 0.20.0-dev-preview-10

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.
@@ -4,18 +4,17 @@ import { CID } from "multiformats/cid";
4
4
 
5
5
  import {
6
6
  ConfigOpts,
7
- Ledger,
8
- LedgerFactory,
9
7
  DocResponse,
10
8
  DocWithId,
11
9
  Index,
12
10
  IndexRows,
13
11
  MapFn,
14
12
  bs,
15
- fireproof,
16
13
  index,
17
- isLedger,
18
14
  ensureSuperThis,
15
+ fireproof,
16
+ Database,
17
+ isDatabase,
19
18
  } from "@fireproof/core";
20
19
 
21
20
  export function carLogIncludesGroup(list: bs.AnyLink[], cid: CID) {
@@ -38,7 +37,7 @@ describe("dreamcode", function () {
38
37
  let ok: DocResponse;
39
38
  let doc: DocWithId<Doc>;
40
39
  let result: IndexRows<string, Doc>;
41
- let db: Ledger;
40
+ let db: Database;
42
41
  const sthis = ensureSuperThis();
43
42
  afterEach(async function () {
44
43
  await db.close();
@@ -46,7 +45,7 @@ describe("dreamcode", function () {
46
45
  });
47
46
  beforeEach(async function () {
48
47
  await sthis.start();
49
- db = fireproof("test-db");
48
+ db = fireproof.DB("test-db");
50
49
  ok = await db.put({ _id: "test-1", text: "fireproof", dream: true });
51
50
  doc = await db.get(ok.id);
52
51
  result = await db.query("text", { range: ["a", "z"] });
@@ -77,7 +76,7 @@ describe("public API", function () {
77
76
  interface Doc {
78
77
  foo: string;
79
78
  }
80
- let db: Ledger;
79
+ let db: Database;
81
80
  let ok: DocResponse;
82
81
  let doc: DocWithId<Doc>;
83
82
  let query: IndexRows<string, Doc>;
@@ -90,7 +89,7 @@ describe("public API", function () {
90
89
 
91
90
  beforeEach(async function () {
92
91
  await sthis.start();
93
- db = fireproof("test-api");
92
+ db = fireproof.DB("test-api");
94
93
  // index = index(db, 'test-index', (doc) => doc.foo)
95
94
  ok = await db.put({ _id: "test", foo: "bar" });
96
95
  doc = await db.get("test");
@@ -98,7 +97,7 @@ describe("public API", function () {
98
97
  });
99
98
  it("should be a ledger instance", function () {
100
99
  expect(db).toBeTruthy();
101
- expect(isLedger(db)).toBeTruthy();
100
+ expect(isDatabase(db)).toBeTruthy();
102
101
  });
103
102
  it("should put", function () {
104
103
  expect(ok).toBeTruthy();
@@ -119,7 +118,7 @@ describe("basic ledger", function () {
119
118
  interface Doc {
120
119
  foo: string;
121
120
  }
122
- let db: Ledger<Doc>;
121
+ let db: Database;
123
122
  const sthis = ensureSuperThis();
124
123
  afterEach(async function () {
125
124
  await db.close();
@@ -127,7 +126,7 @@ describe("basic ledger", function () {
127
126
  });
128
127
  beforeEach(async function () {
129
128
  await sthis.start();
130
- db = LedgerFactory("test-basic");
129
+ db = fireproof.DB("test-basic");
131
130
  });
132
131
  it("can put with id", async function () {
133
132
  const ok = await db.put({ _id: "test", foo: "bar" });
@@ -192,7 +191,7 @@ describe("basic ledger", function () {
192
191
  });
193
192
 
194
193
  describe("benchmarking with compaction", function () {
195
- let db: Ledger;
194
+ let db: Database;
196
195
  const sthis = ensureSuperThis();
197
196
  afterEach(async function () {
198
197
  await db.close();
@@ -201,14 +200,14 @@ describe("benchmarking with compaction", function () {
201
200
  beforeEach(async function () {
202
201
  // erase the existing test data
203
202
  await sthis.start();
204
- db = LedgerFactory("test-benchmark-compaction", { autoCompact: 3 });
203
+ db = fireproof.DB("test-benchmark-compaction", { autoCompact: 3 });
205
204
  });
206
205
  it.skip("insert during compaction", async function () {
207
206
  const ok = await db.put({ _id: "test", foo: "fast" });
208
207
  expect(ok).toBeTruthy();
209
208
  expect(ok.id).toBe("test");
210
- expect(db.crdt.clock.head).toBeTruthy();
211
- expect(db.crdt.clock.head.length).toBe(1);
209
+ expect(db.ledger.crdt.clock.head).toBeTruthy();
210
+ expect(db.ledger.crdt.clock.head.length).toBe(1);
212
211
 
213
212
  const numDocs = 20;
214
213
  const batchSize = 5;
@@ -229,7 +228,7 @@ describe("benchmarking with compaction", function () {
229
228
  }),
230
229
  );
231
230
  }
232
- const blocks = db.crdt.blockstore as bs.EncryptedBlockstore;
231
+ const blocks = db.ledger.crdt.blockstore as bs.EncryptedBlockstore;
233
232
  const loader = blocks.loader;
234
233
  expect(loader).toBeTruthy();
235
234
 
@@ -246,8 +245,7 @@ describe("benchmarking with compaction", function () {
246
245
  });
247
246
 
248
247
  describe("benchmarking a ledger", function () {
249
- /** @type {Ledger} */
250
- let db: Ledger;
248
+ let db: Database;
251
249
  const sthis = ensureSuperThis();
252
250
  afterEach(async function () {
253
251
  await db.close();
@@ -256,8 +254,8 @@ describe("benchmarking a ledger", function () {
256
254
  beforeEach(async function () {
257
255
  await sthis.start();
258
256
  // erase the existing test data
259
- db = LedgerFactory("test-benchmark", { autoCompact: 100000, public: true });
260
- // db = LedgerFactory(null, {autoCompact: 100000})
257
+ db = fireproof.DB("test-benchmark", { autoCompact: 100000, public: true });
258
+ // db = fireproof.DB(null, {autoCompact: 100000})
261
259
  });
262
260
 
263
261
  // run benchmarking tests
@@ -270,8 +268,8 @@ describe("benchmarking a ledger", function () {
270
268
  expect(ok).toBeTruthy();
271
269
  expect(ok.id).toBe("test");
272
270
 
273
- expect(db.crdt.clock.head).toBeTruthy();
274
- expect(db.crdt.clock.head.length).toBe(1);
271
+ expect(db.ledger.crdt.clock.head).toBeTruthy();
272
+ expect(db.ledger.crdt.clock.head.length).toBe(1);
275
273
 
276
274
  const numDocs = 2500;
277
275
  const batchSize = 500;
@@ -307,7 +305,7 @@ describe("benchmarking a ledger", function () {
307
305
  // equals(allDocsResult2.rows.length, numDocs+1)
308
306
 
309
307
  // console.time("open new DB");
310
- const newDb = LedgerFactory("test-benchmark", { autoCompact: 100000, public: true });
308
+ const newDb = fireproof.DB("test-benchmark", { autoCompact: 100000, public: true });
311
309
  const doc = await newDb.get<{ foo: string }>("test");
312
310
  expect(doc.foo).toBe("fast");
313
311
  // console.timeEnd("open new DB");
@@ -337,7 +335,7 @@ describe("benchmarking a ledger", function () {
337
335
  await db.put({ _id: "compacted-test", foo: "bar" });
338
336
 
339
337
  // console.log('car log length', db._crdt.blockstore.loader.carLog.length)
340
- const blocks = db.crdt.blockstore as bs.EncryptedBlockstore;
338
+ const blocks = db.ledger.crdt.blockstore as bs.EncryptedBlockstore;
341
339
  const loader = blocks.loader;
342
340
  expect(loader).toBeTruthy();
343
341
  expect(loader.carLog.length).toBe(2);
@@ -349,10 +347,10 @@ describe("benchmarking a ledger", function () {
349
347
  await sleep(100);
350
348
 
351
349
  // console.time("compacted reopen again");
352
- const newDb2 = LedgerFactory("test-benchmark", { autoCompact: 100000, public: true });
350
+ const newDb2 = fireproof.DB("test-benchmark", { autoCompact: 100000, public: true });
353
351
  const doc21 = await newDb2.get<FooType>("test");
354
352
  expect(doc21.foo).toBe("fast");
355
- const blocks2 = newDb2.crdt.blockstore as bs.EncryptedBlockstore;
353
+ const blocks2 = newDb2.ledger.crdt.blockstore as bs.EncryptedBlockstore;
356
354
  const loader2 = blocks2.loader;
357
355
  expect(loader2).toBeTruthy();
358
356
 
@@ -405,7 +403,7 @@ describe("Reopening a ledger", function () {
405
403
  interface Doc {
406
404
  foo: string;
407
405
  }
408
- let db: Ledger;
406
+ let db: Database;
409
407
  const sthis = ensureSuperThis();
410
408
  afterEach(async function () {
411
409
  await db.close();
@@ -415,13 +413,13 @@ describe("Reopening a ledger", function () {
415
413
  // erase the existing test data
416
414
  await sthis.start();
417
415
 
418
- db = LedgerFactory("test-reopen", { autoCompact: 100000 });
416
+ db = fireproof.DB("test-reopen", { autoCompact: 100000 });
419
417
  const ok = await db.put({ _id: "test", foo: "bar" });
420
418
  expect(ok).toBeTruthy();
421
419
  expect(ok.id).toBe("test");
422
420
 
423
- expect(db.crdt.clock.head).toBeDefined();
424
- expect(db.crdt.clock.head.length).toBe(1);
421
+ expect(db.ledger.crdt.clock.head).toBeDefined();
422
+ expect(db.ledger.crdt.clock.head.length).toBe(1);
425
423
  });
426
424
 
427
425
  it("should persist data", async function () {
@@ -430,18 +428,18 @@ describe("Reopening a ledger", function () {
430
428
  });
431
429
 
432
430
  it("should have the same data on reopen", async function () {
433
- const db2 = LedgerFactory("test-reopen");
431
+ const db2 = fireproof.DB("test-reopen");
434
432
  const doc = await db2.get<FooType>("test");
435
433
  expect(doc.foo).toBe("bar");
436
- expect(db2.crdt.clock.head).toBeDefined();
437
- expect(db2.crdt.clock.head.length).toBe(1);
438
- expect(db2.crdt.clock.head).toEqual(db.crdt.clock.head);
434
+ expect(db2.ledger.crdt.clock.head).toBeDefined();
435
+ expect(db2.ledger.crdt.clock.head.length).toBe(1);
436
+ expect(db2.ledger.crdt.clock.head).toEqual(db.ledger.crdt.clock.head);
439
437
  await db2.close();
440
438
  });
441
439
 
442
440
  it("should have a car in the car log", async function () {
443
- await db.crdt.ready();
444
- const blocks = db.crdt.blockstore as bs.EncryptedBlockstore;
441
+ await db.ledger.crdt.ready();
442
+ const blocks = db.ledger.crdt.blockstore as bs.EncryptedBlockstore;
445
443
  const loader = blocks.loader;
446
444
  expect(loader).toBeDefined();
447
445
  expect(loader.carLog).toBeDefined();
@@ -449,9 +447,9 @@ describe("Reopening a ledger", function () {
449
447
  });
450
448
 
451
449
  it("should have carlog after reopen", async function () {
452
- const db2 = LedgerFactory("test-reopen");
453
- await db2.crdt.ready();
454
- const blocks = db2.crdt.blockstore as bs.EncryptedBlockstore;
450
+ const db2 = fireproof.DB("test-reopen");
451
+ await db2.ledger.crdt.ready();
452
+ const blocks = db2.ledger.crdt.blockstore as bs.EncryptedBlockstore;
455
453
  const loader = blocks.loader;
456
454
  expect(loader).toBeDefined();
457
455
  expect(loader.carLog).toBeDefined();
@@ -462,10 +460,10 @@ describe("Reopening a ledger", function () {
462
460
  it("faster, should have the same data on reopen after reopen and update", async function () {
463
461
  for (let i = 0; i < 4; i++) {
464
462
  // console.log('iteration', i)
465
- const db = LedgerFactory("test-reopen");
463
+ const db = fireproof.DB("test-reopen");
466
464
  // assert(db._crdt.xready());
467
- await db.crdt.ready();
468
- const blocks = db.crdt.blockstore as bs.EncryptedBlockstore;
465
+ await db.ready();
466
+ const blocks = db.ledger.crdt.blockstore as bs.EncryptedBlockstore;
469
467
  const loader = blocks.loader;
470
468
  expect(loader.carLog.length).toBe(i + 1);
471
469
  const ok = await db.put({ _id: `test${i}`, fire: "proof".repeat(50 * 1024) });
@@ -481,11 +479,11 @@ describe("Reopening a ledger", function () {
481
479
  for (let i = 0; i < 200; i++) {
482
480
  // console.log("iteration", i);
483
481
  // console.time("db open");
484
- const db = LedgerFactory("test-reopen", { autoCompact: 1000 }); // try with 10
482
+ const db = fireproof.DB("test-reopen", { autoCompact: 1000 }); // try with 10
485
483
  // assert(db._crdt.ready);
486
- await db.crdt.ready();
484
+ await db.ready();
487
485
  // console.timeEnd("db open");
488
- const blocks = db.crdt.blockstore as bs.EncryptedBlockstore;
486
+ const blocks = db.ledger.crdt.blockstore as bs.EncryptedBlockstore;
489
487
  const loader = blocks.loader;
490
488
  expect(loader).toBeDefined();
491
489
  expect(loader.carLog.length).toBe(i + 1);
@@ -507,7 +505,7 @@ describe("Reopening a ledger with indexes", function () {
507
505
  interface Doc {
508
506
  foo: string;
509
507
  }
510
- let db: Ledger;
508
+ let db: Database;
511
509
  let idx: Index<string, Doc>;
512
510
  let didMap: boolean;
513
511
  let mapFn: MapFn<Doc>;
@@ -518,7 +516,7 @@ describe("Reopening a ledger with indexes", function () {
518
516
  });
519
517
  beforeEach(async function () {
520
518
  await sthis.start();
521
- db = fireproof("test-reopen-idx");
519
+ db = fireproof.DB("test-reopen-idx");
522
520
  const ok = await db.put({ _id: "test", foo: "bar" });
523
521
  expect(ok.id).toBe("test");
524
522
 
@@ -563,12 +561,12 @@ describe("Reopening a ledger with indexes", function () {
563
561
  });
564
562
 
565
563
  it("should have the same data on reopen", async function () {
566
- const db2 = fireproof("test-reopen-idx");
564
+ const db2 = fireproof.DB("test-reopen-idx");
567
565
  const doc = await db2.get<FooType>("test");
568
566
  expect(doc.foo).toBe("bar");
569
- expect(db2.crdt.clock.head).toBeTruthy();
570
- expect(db2.crdt.clock.head.length).toBe(1);
571
- expect(db2.crdt.clock.head).toEqual(db.crdt.clock.head);
567
+ expect(db2.ledger.crdt.clock.head).toBeTruthy();
568
+ expect(db2.ledger.crdt.clock.head.length).toBe(1);
569
+ expect(db2.ledger.crdt.clock.head).toEqual(db.ledger.crdt.clock.head);
572
570
  });
573
571
 
574
572
  it("should have the same data on reopen after a query", async function () {
@@ -578,12 +576,12 @@ describe("Reopening a ledger with indexes", function () {
578
576
  expect(r0.rows.length).toBe(1);
579
577
  expect(r0.rows[0].key).toBe("bar");
580
578
 
581
- const db2 = fireproof("test-reopen-idx");
579
+ const db2 = fireproof.DB("test-reopen-idx");
582
580
  const doc = await db2.get<FooType>("test");
583
581
  expect(doc.foo).toBe("bar");
584
- expect(db2.crdt.clock.head).toBeTruthy();
585
- expect(db2.crdt.clock.head.length).toBe(1);
586
- expect(db2.crdt.clock.head).toEqual(db.crdt.clock.head);
582
+ expect(db2.ledger.crdt.clock.head).toBeTruthy();
583
+ expect(db2.ledger.crdt.clock.head.length).toBe(1);
584
+ expect(db2.ledger.crdt.clock.head).toEqual(db.ledger.crdt.clock.head);
587
585
  });
588
586
 
589
587
  // it('should query the same data on reopen', async function () {
@@ -613,16 +611,16 @@ describe("basic js verify", function () {
613
611
  await sthis.start();
614
612
  });
615
613
  it("should include cids in arrays", async function () {
616
- const db = fireproof("test-verify");
614
+ const db = fireproof.DB("test-verify");
617
615
  const ok = await db.put({ _id: "test", foo: ["bar", "bam"] });
618
616
  expect(ok.id).toBe("test");
619
617
  const ok2 = await db.put({ _id: "test2", foo: ["bar", "bam"] });
620
618
  expect(ok2.id).toBe("test2");
621
- const blocks = db.crdt.blockstore as bs.EncryptedBlockstore;
619
+ const blocks = db.ledger.crdt.blockstore as bs.EncryptedBlockstore;
622
620
  const loader = blocks.loader;
623
621
  expect(loader).toBeTruthy();
624
622
  const cid = loader.carLog[0][0];
625
- const cid2 = db.crdt.clock.head[0];
623
+ const cid2 = db.ledger.crdt.clock.head[0];
626
624
  expect(cid).not.toBe(cid2);
627
625
  expect(cid).not.toBe(cid2);
628
626
  const cidList = [cid, cid2];
@@ -635,8 +633,8 @@ describe("basic js verify", function () {
635
633
  });
636
634
 
637
635
  describe("same workload twice, same CID", function () {
638
- let dbA: Ledger;
639
- let dbB: Ledger;
636
+ let dbA: Database;
637
+ let dbB: Database;
640
638
  let headA: string;
641
639
  let headB: string;
642
640
 
@@ -666,22 +664,22 @@ describe("same workload twice, same CID", function () {
666
664
  let ok: DocResponse;
667
665
  await sthis.start();
668
666
  // todo this fails because the test setup doesn't properly configure both ledgers to use the same key
669
- dbA = fireproof("test-dual-workload-a", configA);
667
+ dbA = fireproof.DB("test-dual-workload-a", configA);
670
668
  for (const doc of docs) {
671
669
  ok = await dbA.put(doc);
672
670
  expect(ok).toBeTruthy();
673
671
  expect(ok.id).toBeTruthy();
674
672
  }
675
- headA = dbA.crdt.clock.head.toString();
673
+ headA = dbA.ledger.crdt.clock.head.toString();
676
674
 
677
675
  // todo this fails because the test setup doesn't properly configure both ledgers to use the same key
678
- dbB = fireproof("test-dual-workload-b", configB);
676
+ dbB = fireproof.DB("test-dual-workload-b", configB);
679
677
  for (const doc of docs) {
680
678
  ok = await dbB.put(doc);
681
679
  expect(ok).toBeTruthy();
682
680
  expect(ok.id).toBeTruthy();
683
681
  }
684
- headB = dbB.crdt.clock.head.toString();
682
+ headB = dbB.ledger.crdt.clock.head.toString();
685
683
  });
686
684
  it("should have head A and B", async function () {
687
685
  expect(headA).toBeTruthy();
@@ -690,12 +688,12 @@ describe("same workload twice, same CID", function () {
690
688
  expect(headA.length).toBeGreaterThan(10);
691
689
  });
692
690
  it("should have same car log", async function () {
693
- const logA = dbA.crdt.blockstore.loader?.carLog;
691
+ const logA = dbA.ledger.crdt.blockstore.loader?.carLog;
694
692
  expect(logA).toBeTruthy();
695
693
  assert(logA);
696
694
  expect(logA.length).toBe(docs.length);
697
695
 
698
- const logB = dbB.crdt.blockstore.loader?.carLog;
696
+ const logB = dbB.ledger.crdt.blockstore.loader?.carLog;
699
697
  expect(logB).toBeTruthy();
700
698
  assert(logB);
701
699
  expect(logB.length).toBe(docs.length);
@@ -712,12 +710,12 @@ describe("same workload twice, same CID", function () {
712
710
  await dbA.compact();
713
711
  await dbB.compact();
714
712
 
715
- const cmpLogA = dbA.crdt.blockstore.loader?.carLog;
713
+ const cmpLogA = dbA.ledger.crdt.blockstore.loader?.carLog;
716
714
  expect(cmpLogA).toBeTruthy();
717
715
  assert(cmpLogA);
718
716
  expect(cmpLogA.length).toBe(1);
719
717
 
720
- const cmpLogB = dbB.crdt.blockstore.loader?.carLog;
718
+ const cmpLogB = dbB.ledger.crdt.blockstore.loader?.carLog;
721
719
  expect(cmpLogB).toBeTruthy();
722
720
  assert(cmpLogB);
723
721
  expect(cmpLogB.length).toBe(1);
@@ -1,9 +1,9 @@
1
- import { fireproof, Ledger, DocResponse, DocWithId, index, isLedger } from "@fireproof/core";
1
+ import { fireproof, DocResponse, DocWithId, index, Database, isDatabase } from "@fireproof/core";
2
2
  import { mockSuperThis } from "../helpers.js";
3
3
 
4
4
  describe("Hello World Test", function () {
5
5
  it("should pass the hello world test", function () {
6
- const result = fireproof("hello"); // call to your library function
6
+ const result = fireproof.DB("hello"); // call to your library function
7
7
  expect(result.name).toBe("hello");
8
8
  });
9
9
  });
@@ -12,7 +12,7 @@ describe("hello public API", () => {
12
12
  interface TestDoc {
13
13
  foo: string;
14
14
  }
15
- let db: Ledger;
15
+ let db: Database;
16
16
  let ok: DocResponse;
17
17
  let doc: DocWithId<TestDoc>;
18
18
  // let idx: Index<string, TestDoc>;
@@ -23,14 +23,14 @@ describe("hello public API", () => {
23
23
  });
24
24
  beforeEach(async () => {
25
25
  await sthis.start();
26
- db = fireproof("test-public-api");
26
+ db = fireproof.DB("test-public-api");
27
27
  index<string, TestDoc>(db, "test-index", (doc) => doc.foo);
28
28
  ok = await db.put({ _id: "test", foo: "bar" });
29
29
  doc = await db.get("test");
30
30
  });
31
31
  it("should have a ledger", function () {
32
32
  expect(db).toBeTruthy();
33
- expect(isLedger(db)).toBeTruthy();
33
+ expect(isDatabase(db)).toBeTruthy();
34
34
  });
35
35
  it("should put", function () {
36
36
  expect(ok).toBeTruthy();
@@ -41,20 +41,20 @@ describe("hello public API", () => {
41
41
  });
42
42
  it("should get when you open it again", async () => {
43
43
  await db.close();
44
- db = fireproof("test-public-api");
44
+ db = fireproof.DB("test-public-api");
45
45
  doc = await db.get("test");
46
46
  expect(doc.foo).toBe("bar");
47
47
  });
48
48
  });
49
49
 
50
50
  describe("Simplified Reopening a ledger", function () {
51
- let db: Ledger;
51
+ let db: Database;
52
52
  afterEach(async function () {
53
53
  await db.close();
54
54
  await db.destroy();
55
55
  });
56
56
  beforeEach(async function () {
57
- db = fireproof("test-reopen-simple");
57
+ db = fireproof.DB("test-reopen-simple");
58
58
  const ok = await db.put({ _id: "test", foo: "bar" });
59
59
  expect(ok).toBeTruthy();
60
60
  expect(ok.id).toBe("test");
@@ -66,7 +66,7 @@ describe("Simplified Reopening a ledger", function () {
66
66
  });
67
67
 
68
68
  it("should have the same data on reopen", async function () {
69
- const db2 = fireproof("test-reopen-simple");
69
+ const db2 = fireproof.DB("test-reopen-simple");
70
70
  const doc = await db2.get<{ foo: string }>("test");
71
71
  expect(doc.foo).toBe("bar");
72
72
  await db2.close();