@fireproof/core 0.6.3-dev2 → 0.6.5

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.
@@ -2,6 +2,7 @@
2
2
  import { visMerkleClock, visMerkleTree, vis, put, get, getAll, eventsSince } from './prolly.js';
3
3
  import { doTransaction, TransactionBlockstore } from './blockstore.js';
4
4
  import charwise from 'charwise';
5
+ import { localSet } from './utils.js';
5
6
  import { CID } from 'multiformats';
6
7
  // TypeScript Types
7
8
  // eslint-disable-next-line no-unused-vars
@@ -30,7 +31,7 @@ export class Database {
30
31
  this.name = name;
31
32
  this.instanceId = `fp.${this.name}.${Math.random().toString(36).substring(2, 7)}`;
32
33
  this.blocks = new TransactionBlockstore(name, config.key);
33
- this.indexBlocks = new TransactionBlockstore(name ? name + '.indexes' : null, config.key);
34
+ this.indexBlocks = new TransactionBlockstore(name + '.indexes', config.key);
34
35
  this.clock = clock;
35
36
  this.config = config;
36
37
  }
@@ -70,7 +71,7 @@ export class Database {
70
71
  }
71
72
  maybeSaveClock() {
72
73
  if (this.name && this.blocks.valet) {
73
- this.blocks.valet.saveHeader(JSON.stringify(this));
74
+ localSet('fp.' + this.name, JSON.stringify(this));
74
75
  }
75
76
  }
76
77
  index(name) {
@@ -291,7 +292,6 @@ export class Database {
291
292
  console.error('failed', event);
292
293
  throw new Error('failed to put at storage layer');
293
294
  }
294
- // await new Promise(resolve => setTimeout(resolve, 10)) // makes concurrent tests work
295
295
  this.applyClock(prevClock, result.head);
296
296
  await this.notifyListeners([decodedEvent]); // this type is odd
297
297
  return {
@@ -127,13 +127,23 @@ export class DbIndex {
127
127
  applyMapFn(mapFn, name) {
128
128
  if (typeof mapFn === 'string') {
129
129
  this.mapFnString = mapFn;
130
+ // make a regex that matches strings that only have letters, numbers, and spaces
131
+ const regex = /^[a-zA-Z0-9 ]+$/;
132
+ // if the string matches the regex, make a function that returns the value at that key
133
+ if (regex.test(mapFn)) {
134
+ this.mapFn = (doc, emit) => {
135
+ if (doc[mapFn])
136
+ emit(doc[mapFn]);
137
+ };
138
+ this.includeDocsDefault = true;
139
+ }
130
140
  }
131
141
  else {
132
142
  this.mapFn = mapFn;
133
143
  this.mapFnString = mapFn.toString();
134
144
  }
135
145
  const matches = /=>\s*(.*)/.exec(this.mapFnString);
136
- this.includeDocsDefault = matches && matches.length > 0;
146
+ this.includeDocsDefault = this.includeDocsDefault || (matches && matches.length > 0);
137
147
  this.name = name || this.makeName();
138
148
  }
139
149
  makeName() {
@@ -1,33 +1,9 @@
1
1
  import * as multiformats from 'multiformats';
2
2
  import { Link, CID } from 'multiformats';
3
3
  import * as multiformats_cid from 'multiformats/cid';
4
- import * as idb from 'idb';
5
4
 
6
5
  type AnyLink = Link<unknown, number, number, 1|0>
7
6
 
8
- declare class Loader {
9
- constructor(name: any, keyId: any, config?: {
10
- dataDir: any;
11
- headerKeyPrefix: string;
12
- });
13
- name: any;
14
- keyId: any;
15
- config: {
16
- dataDir: any;
17
- headerKeyPrefix: string;
18
- };
19
- isBrowser: boolean;
20
- withDB: (dbWorkFun: any) => Promise<any>;
21
- idb: idb.IDBPDatabase<unknown>;
22
- writeCars(cars: any): Promise<void>;
23
- writeCarsIDB(cars: any): Promise<any>;
24
- readCar(carCid: any): Promise<any>;
25
- readCarIDB(carCid: any): Promise<any>;
26
- getHeader(): any;
27
- saveHeader(stringValue: any): Promise<void>;
28
- headerFilename(): any;
29
- }
30
-
31
7
  declare class Valet {
32
8
  constructor(name: string, keyMaterial: any);
33
9
  idb: any;
@@ -46,8 +22,6 @@ declare class Valet {
46
22
  * @type {null|function(string, Uint8Array):Promise<void>}
47
23
  */
48
24
  uploadFunction: null | ((arg0: string, arg1: Uint8Array) => Promise<void>);
49
- loader: Loader;
50
- saveHeader(header: any): Promise<void>;
51
25
  getKeyMaterial(): any;
52
26
  setKeyMaterial(km: any): void;
53
27
  /**
@@ -58,6 +32,7 @@ declare class Valet {
58
32
  * @memberof Valet
59
33
  */
60
34
  writeTransaction(innerBlockstore: InnerBlockstore, cids: Set<string>): Promise<void>;
35
+ withDB: (dbWorkFun: any) => Promise<any>;
61
36
  /**
62
37
  * Iterate over all blocks in the store.
63
38
  *
@@ -80,6 +55,7 @@ declare class Valet {
80
55
  * @param {*} value
81
56
  */
82
57
  parkCar(carCid: string, value: any, cids: any): Promise<void>;
58
+ writeCars(cars: any): Promise<any>;
83
59
  remoteBlockFunction: any;
84
60
  getCarReader(carCid: any): Promise<{
85
61
  root: any;
@@ -422,7 +398,7 @@ declare class DbIndex {
422
398
  applyMapFn(mapFn: any, name: any): void;
423
399
  mapFnString: any;
424
400
  mapFn: any;
425
- includeDocsDefault: boolean;
401
+ includeDocsDefault: any;
426
402
  name: any;
427
403
  makeName(): any;
428
404
  toJSON(): {