@fireproof/core 0.6.5 → 0.6.6

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.
package/README.md CHANGED
@@ -8,8 +8,8 @@
8
8
  </h3>
9
9
 
10
10
  <p align="center">
11
- <a href="https://github.com/jchris/fireproof/actions/workflows/test.yml">
12
- <img src="https://github.com/jchris/fireproof/actions/workflows/test.yml/badge.svg" alt="Test" style="max-width: 100%;">
11
+ <a href="https://github.com/fireproof-storage/fireproof/actions/workflows/test.yml">
12
+ <img src="https://github.com/fireproof-storage/fireproof/actions/workflows/test.yml/badge.svg" alt="Test" style="max-width: 100%;">
13
13
  </a>
14
14
  <a href="https://standardjs.com" rel="nofollow">
15
15
  <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="JavaScript Style Guide" style="max-width: 100%;">
@@ -38,14 +38,14 @@ With Fireproof, you **build first** and connect it to your cloud of choice when
38
38
  ```js
39
39
  const completedTodos = useLiveQuery((doc) => doc.completed, { key: true })
40
40
  ```
41
- This is the README for the core database technology. [The `useFireproof` hook documentation has features and a getting started guide](https://github.com/fireproof-storage/fireproof/blob/main/packages/react/README.md), like `useLiveDocument` and database setup helpers. Don't forget to star 🌟 this repo on the way over.
41
+ This is the README for the core database technology. [The `useFireproof` hook documentation has features and a getting started guide](https://github.com/fireproof-storage/fireproof/blob/main/packages/react/README.md), like `useDocument` and database setup helpers. Don't forget to star 🌟 this repo on the way over.
42
42
 
43
43
  ## AI Assistant Quick Start
44
44
 
45
45
  Because Fireproof is designed around the mantra of build-first, it's ideal for AI-assisted app development as you can get an app up and running before even considering the cloud. If you are using GPT-3.5, GPT-4, or Bard, you can [easily enable the AI to write React apps using Fireproof](https://hackernoon.com/get-chatgpt-to-focus-on-coding-on-the-right-apis-with-gptdoc-strings). Paste this line of code into your prompt, along with a detailed description of the app to build:
46
46
 
47
47
  ```
48
- Fireproof/React/Usage: import { useFireproof } from '@fireproof/react'; function App() { const { useLiveQuery, useLiveDocument, database } = useFireproof(); const result = useLiveQuery(doc => doc.word, { limit: 10 }); const [{ count }, saveCountDocument] = useLiveDocument({_id: 'count', count: 0}); return (<><p>{count} changes</p><input type='text' onChange={() => saveCountDocument({count: count + 1})} onSubmit={e => database.put({word: e.target.value})} /><ul>{result.map(row => (<li key={row.id}>{row.key}</li>))}</ul></>)}
48
+ Fireproof/React/Usage: import { useFireproof } from '@fireproof/react'; function App() { const { useLiveQuery, useDocument, database } = useFireproof(); const result = useLiveQuery(doc => doc.word, { limit: 10 }); const [{ count }, saveCountDocument] = useDocument({_id: 'count', count: 0}); return (<><p>{count} changes</p><input type='text' onChange={() => saveCountDocument({count: count + 1})} onSubmit={e => database.put({word: e.target.value})} /><ul>{result.map(row => (<li key={row.id}>{row.key}</li>))}</ul></>)}
49
49
  ```
50
50
 
51
51
  Here are some examples that worked for us:
@@ -0,0 +1,90 @@
1
+ import randomBytes from 'randombytes';
2
+ import { Database, parseCID } from './database.js';
3
+ import { Listener } from './listener.js';
4
+ import { DbIndex as Index } from './db-index.js';
5
+ // import { TransactionBlockstore } from './blockstore.js'
6
+ import { localGet } from './utils.js';
7
+ import { Sync } from './sync.js';
8
+ // todo remove Listener in 0.7.0
9
+ export { Index, Listener, Database, Sync };
10
+ class Fireproof {
11
+ /**
12
+ * @function storage
13
+ * @memberof Fireproof
14
+ * Creates a new Fireproof instance with default storage settings
15
+ * Most apps should use this and not worry about the details.
16
+ * @static
17
+ * @returns {Database} - a new Fireproof instance
18
+ */
19
+ static storage = (name = null, opts = {}) => {
20
+ if (name) {
21
+ opts.name = name;
22
+ // todo this can come from a registry also eg remote database / config, etc
23
+ const existing = localGet('fp.' + name);
24
+ if (existing) {
25
+ const existingConfig = JSON.parse(existing);
26
+ return Fireproof.fromConfig(name, existingConfig, opts);
27
+ }
28
+ else {
29
+ const instanceKey = randomBytes(32).toString('hex'); // pass null to disable encryption
30
+ opts.key = instanceKey;
31
+ return new Database(name, [], opts);
32
+ }
33
+ }
34
+ else {
35
+ return new Database(null, [], opts);
36
+ }
37
+ };
38
+ static fromConfig(name, existingConfig, opts = {}) {
39
+ opts.key = existingConfig.key;
40
+ const fp = new Database(name, [], opts);
41
+ return Fireproof.fromJSON(existingConfig, fp);
42
+ }
43
+ static fromJSON(json, database) {
44
+ database.hydrate({ car: json.car, indexCar: json.indexCar, clock: json.clock.map(c => parseCID(c)), name: json.name, key: json.key });
45
+ if (json.indexes) {
46
+ for (const { name, code, clock: { byId, byKey, db } } of json.indexes) {
47
+ Index.fromJSON(database, {
48
+ clock: {
49
+ byId: byId ? parseCID(byId) : null,
50
+ byKey: byKey ? parseCID(byKey) : null,
51
+ db: (db && db.length > 0) ? db.map(c => parseCID(c)) : null
52
+ },
53
+ code,
54
+ name
55
+ });
56
+ }
57
+ }
58
+ return database;
59
+ }
60
+ static snapshot(database, clock) {
61
+ const definition = database.toJSON();
62
+ const withBlocks = new Database(database.name);
63
+ withBlocks.blocks = database.blocks;
64
+ if (clock) {
65
+ definition.clock = clock.map(c => parseCID(c));
66
+ definition.indexes.forEach(index => {
67
+ index.clock.byId = null;
68
+ index.clock.byKey = null;
69
+ index.clock.db = null;
70
+ });
71
+ }
72
+ const snappedDb = Fireproof.fromJSON(definition, withBlocks);
73
+ [...database.indexes.values()].forEach(index => {
74
+ snappedDb.indexes.get(index.mapFnString).mapFn = index.mapFn;
75
+ });
76
+ return snappedDb;
77
+ }
78
+ static async zoom(database, clock) {
79
+ ;
80
+ [...database.indexes.values()].forEach(index => {
81
+ index.indexById = { root: null, cid: null };
82
+ index.indexByKey = { root: null, cid: null };
83
+ index.dbHead = null;
84
+ });
85
+ database.clock = clock.map(c => parseCID(c));
86
+ await database.notifyReset(); // hmm... indexes should listen to this? might be more complex than worth it. so far this is the only caller
87
+ return database;
88
+ }
89
+ }
90
+ export { Fireproof };
@@ -1,5 +1,5 @@
1
1
  import { Browser } from './storage/browser.js';
2
- // import { Filesystem } from './storage/filesystem.js'
2
+ import { Filesystem } from './storage/filesystem.js';
3
3
  import { Rest } from './storage/rest.js';
4
4
  const FORCE_IDB = typeof process !== 'undefined' && !!process.env?.FORCE_IDB;
5
5
  /* global window */
@@ -17,8 +17,7 @@ export const Loader = {
17
17
  return new Browser(name, config);
18
18
  }
19
19
  else {
20
- return new Browser(name, config);
21
- // return new Filesystem(name, config)
20
+ return new Filesystem(name, config);
22
21
  }
23
22
  }
24
23
  };