offdex 1.0.6 → 1.0.7
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 +10 -1
- package/package.json +1 -1
- package/src/ObjectStore/index.js +20 -0
package/README.md
CHANGED
|
@@ -3,24 +3,27 @@
|
|
|
3
3
|
ID-first object storage for the browser. Offdex wraps IndexedDB with a tiny, stable API so you can drop in `{ id: <uuid>, ...data }` objects and have them shared across tabs, workers, and sessions without thinking about schema versions.
|
|
4
4
|
|
|
5
5
|
## Why use Offdex
|
|
6
|
+
|
|
6
7
|
- Zero schema/versioning overhead: one object store keyed by `id`, no migrations to manage.
|
|
7
8
|
- Works everywhere IndexedDB works: tabs, workers, and other browser runtimes share the same underlying database.
|
|
8
9
|
- Offline by default: IndexedDB persists across reloads and disconnects.
|
|
9
10
|
- Typed surface: ships with TypeScript definitions for easy adoption.
|
|
10
11
|
|
|
11
12
|
## Install
|
|
13
|
+
|
|
12
14
|
```bash
|
|
13
15
|
npm install offdex
|
|
14
16
|
```
|
|
15
17
|
|
|
16
18
|
## Quick start
|
|
19
|
+
|
|
17
20
|
```js
|
|
18
21
|
import { storage } from "offdex";
|
|
19
22
|
|
|
20
23
|
const profile = {
|
|
21
24
|
id: crypto.randomUUID(), // UUIDv4 string
|
|
22
25
|
name: "Ada Lovelace",
|
|
23
|
-
role: "analyst"
|
|
26
|
+
role: "analyst",
|
|
24
27
|
};
|
|
25
28
|
|
|
26
29
|
await storage.put(profile);
|
|
@@ -32,10 +35,13 @@ await storage.delete(profile.id);
|
|
|
32
35
|
```
|
|
33
36
|
|
|
34
37
|
## API
|
|
38
|
+
|
|
35
39
|
### `storage`
|
|
40
|
+
|
|
36
41
|
- Ready-to-use singleton instance shared across every import in the same origin. Uses the `offdex` database and `objects` store under the hood.
|
|
37
42
|
|
|
38
43
|
### `class ObjectStore`
|
|
44
|
+
|
|
39
45
|
- `constructor()` — opens (or creates) the `offdex` database with the `objects` store. Use this only if you need a separate instance.
|
|
40
46
|
- `put(object: { id: UUIDv4 } & Record<string, unknown>): Promise<void>` — upserts an object keyed by `id`.
|
|
41
47
|
- `get(id: UUIDv4, onSet?, onDelete?): Promise<{ id: UUIDv4 } & Record<string, unknown> | undefined>` — fetches by `id`, returning `undefined` when missing. Optional callbacks run before a property change/delete; return `false` to block the change.
|
|
@@ -44,16 +50,19 @@ await storage.delete(profile.id);
|
|
|
44
50
|
- `deleteAllMatches(queryOrFilter: StorageQuery | (object) => boolean): Promise<void>` — deletes objects that pass a query or predicate.
|
|
45
51
|
|
|
46
52
|
### Other exports
|
|
53
|
+
|
|
47
54
|
- `StorageQuery` — helper for simple equality-based queries.
|
|
48
55
|
- `ObservableValue` — observable wrapper around a single value.
|
|
49
56
|
- `ObservableObject` — wraps an object in observable values keyed by its properties.
|
|
50
57
|
|
|
51
58
|
### Types
|
|
59
|
+
|
|
52
60
|
- `UUIDv4` — template literal type for UUID strings.
|
|
53
61
|
- `StoredObject` — `{ id: UUIDv4 } & Record<string, unknown>`.
|
|
54
62
|
- `OnSetHandler`, `OnDeleteHandler` — callback shapes used by `get`/`getAllMatches`.
|
|
55
63
|
|
|
56
64
|
## Notes
|
|
65
|
+
|
|
57
66
|
- Runs in any environment that exposes `indexedDB` (secure contexts in modern browsers).
|
|
58
67
|
- Data is shared per origin; open multiple tabs or workers and you will see the same store.
|
|
59
68
|
- There is no schema migration system; keep your stored objects backward compatible or manage migrations externally if you need them.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "offdex",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "ID-driven object storage wrapper for IndexedDB with zero schema/versioning overhead. Shared across all browser threads, offline-persistent, large capacity, simple API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"indexeddb",
|
package/src/ObjectStore/index.js
CHANGED
|
@@ -117,6 +117,26 @@ export class ObjectStore {
|
|
|
117
117
|
});
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
+
/**
|
|
121
|
+
* @param {import("../types").StoredObject[]} objects
|
|
122
|
+
* @returns {Promise<void>}
|
|
123
|
+
*/
|
|
124
|
+
async putAll(objects) {
|
|
125
|
+
const db = await this.instance;
|
|
126
|
+
return new Promise((resolve, reject) => {
|
|
127
|
+
const transaction = db.transaction(ObjectStore.#store, "readwrite");
|
|
128
|
+
const store = transaction.objectStore(ObjectStore.#store);
|
|
129
|
+
|
|
130
|
+
for (const object of objects) {
|
|
131
|
+
store.put(object);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
transaction.oncomplete = () => resolve();
|
|
135
|
+
transaction.onerror = () =>
|
|
136
|
+
reject(new Error(`{offdex} ObjectStore.putAll: ${transaction.error}`));
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
120
140
|
/**
|
|
121
141
|
* @param {import("../StorageQuery/index.js").StorageQuery | ((object: import("../types").StoredObject) => boolean)} queryOrPredicate
|
|
122
142
|
* @param {(propertyName: string, oldValue: any, newValue: any) => void | boolean} [onSetEvent]
|