offdex 1.0.6 → 2.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 offdex contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,59 +1,68 @@
1
- # Offdex
2
-
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
-
5
- ## Why use Offdex
6
- - Zero schema/versioning overhead: one object store keyed by `id`, no migrations to manage.
7
- - Works everywhere IndexedDB works: tabs, workers, and other browser runtimes share the same underlying database.
8
- - Offline by default: IndexedDB persists across reloads and disconnects.
9
- - Typed surface: ships with TypeScript definitions for easy adoption.
10
-
11
- ## Install
12
- ```bash
13
- npm install offdex
14
- ```
15
-
16
- ## Quick start
17
- ```js
18
- import { storage } from "offdex";
19
-
1
+ # Offdex
2
+
3
+ Keyed object storage for the browser. Offdex wraps IndexedDB with a tiny, unbiased API so you can drop in `{ key: "<string>", ...data }` objects and have them shared across tabs, workers, and sessions without thinking about schema versions.
4
+
5
+ ## Why use Offdex
6
+
7
+ - Zero schema/versioning overhead: one object store keyed by `key`, no migrations to manage.
8
+ - Minimal wrapper: no hooks or proxies in the storage API, just data in and data out.
9
+ - Works everywhere IndexedDB works: tabs, workers, and other browser runtimes share the same underlying database.
10
+ - Offline by default: IndexedDB persists across reloads and disconnects.
11
+ - Typed surface: ships with TypeScript definitions for easy adoption.
12
+
13
+ ## Install
14
+
15
+ ```bash
16
+ npm install offdex
17
+ ```
18
+
19
+ ## Quick start
20
+
21
+ ```js
22
+ import { storage } from "offdex";
23
+
20
24
  const profile = {
21
- id: crypto.randomUUID(), // UUIDv4 string
25
+ key: "profile:ada",
22
26
  name: "Ada Lovelace",
23
- role: "analyst"
27
+ role: "analyst",
24
28
  };
25
-
26
- await storage.put(profile);
27
-
28
- const again = await storage.get(profile.id);
29
- // -> { id: "", name: "Ada Lovelace", role: "analyst" }
30
-
31
- await storage.delete(profile.id);
32
- ```
33
-
34
- ## API
35
- ### `storage`
36
- - Ready-to-use singleton instance shared across every import in the same origin. Uses the `offdex` database and `objects` store under the hood.
37
-
38
- ### `class ObjectStore`
39
- - `constructor()` — opens (or creates) the `offdex` database with the `objects` store. Use this only if you need a separate instance.
40
- - `put(object: { id: UUIDv4 } & Record<string, unknown>): Promise<void>` — upserts an object keyed by `id`.
41
- - `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.
42
- - `delete(id: UUIDv4): Promise<void>` removes an object by `id`.
43
- - `getAllMatches(queryOrFilter: StorageQuery | (object) => boolean, onSet?, onDelete?): Promise<object[]>` returns objects that pass a query or predicate (with the same optional callbacks as `get`).
44
- - `deleteAllMatches(queryOrFilter: StorageQuery | (object) => boolean): Promise<void>` deletes objects that pass a query or predicate.
45
-
46
- ### Other exports
47
- - `StorageQuery` helper for simple equality-based queries.
48
- - `ObservableValue`observable wrapper around a single value.
49
- - `ObservableObject` — wraps an object in observable values keyed by its properties.
50
-
51
- ### Types
52
- - `UUIDv4` — template literal type for UUID strings.
53
- - `StoredObject` — `{ id: UUIDv4 } & Record<string, unknown>`.
54
- - `OnSetHandler`, `OnDeleteHandler`callback shapes used by `get`/`getAllMatches`.
55
-
56
- ## Notes
57
- - Runs in any environment that exposes `indexedDB` (secure contexts in modern browsers).
58
- - Data is shared per origin; open multiple tabs or workers and you will see the same store.
59
- - There is no schema migration system; keep your stored objects backward compatible or manage migrations externally if you need them.
29
+
30
+ await storage.put(profile);
31
+
32
+ const again = await storage.get(profile.key);
33
+ // -> { key: "profile:ada", name: "Ada Lovelace", role: "analyst" }
34
+
35
+ await storage.delete(profile.key);
36
+ ```
37
+
38
+ ## API
39
+
40
+ ### `storage`
41
+
42
+ - Ready-to-use singleton instance shared across every import in the same origin. Uses the `offdex` database and `objects` store under the hood.
43
+
44
+ ### `class ObjectStore`
45
+
46
+ - `constructor()` — opens (or creates) the `offdex` database with the `objects` store. Use this only if you need a separate instance.
47
+ - `put(object: { key: string } & Record<string, unknown>): Promise<void>` - upserts an object keyed by `key`.
48
+ - `putAll(objects: { key: string } & Record<string, unknown>[]): Promise<void>` - upserts multiple objects in a single transaction.
49
+ - `get(key: string): Promise<{ key: string } & Record<string, unknown> | undefined>` - fetches by `key`, returning `undefined` when missing.
50
+ - `delete(key: string): Promise<void>` - removes an object by `key`.
51
+ - `getAllMatches(queryOrFilter: StorageQuery | (object) => boolean): Promise<object[]>` - returns objects that pass a query or predicate.
52
+ - `deleteAllMatches(queryOrFilter: StorageQuery | (object) => boolean): Promise<void>` deletes objects that pass a query or predicate.
53
+
54
+ ### Other exports
55
+
56
+ - `StorageQuery` — helper for simple equality-based queries.
57
+ - `ObservableValue` — observable wrapper around a single value.
58
+ - `ObservableObject` — wraps an object in observable values keyed by its properties.
59
+
60
+ ### Types
61
+
62
+ - `StoredObject` - `{ key: string } & Record<string, unknown>`.
63
+
64
+ ## Notes
65
+
66
+ - Runs in any environment that exposes `indexedDB` (secure contexts in modern browsers).
67
+ - Data is shared per origin; open multiple tabs or workers and you will see the same store.
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,7 +1,7 @@
1
1
  {
2
2
  "name": "offdex",
3
- "version": "1.0.6",
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.",
3
+ "version": "2.0.0",
4
+ "description": "Minimal, unbiased IndexedDB object-store wrapper keyed by a string. Shared across browser threads, offline-persistent, simple API.",
5
5
  "keywords": [
6
6
  "indexeddb",
7
7
  "offline",
@@ -10,16 +10,25 @@
10
10
  "web-storage",
11
11
  "object-store",
12
12
  "key-value",
13
- "id",
13
+ "key",
14
14
  "persist",
15
15
  "threads",
16
16
  "workers",
17
- "shared"
17
+ "shared",
18
+ "minimal"
18
19
  ],
19
20
  "license": "MIT",
20
21
  "type": "module",
21
22
  "main": "src/index.js",
22
23
  "types": "src/index.d.ts",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/jortsupetterson/offdex.git"
27
+ },
28
+ "bugs": {
29
+ "url": "https://github.com/jortsupetterson/offdex/issues"
30
+ },
31
+ "homepage": "https://github.com/jortsupetterson/offdex#readme",
23
32
  "exports": {
24
33
  ".": {
25
34
  "types": "./src/index.d.ts",
@@ -29,6 +38,7 @@
29
38
  },
30
39
  "files": [
31
40
  "src",
41
+ "LICENSE",
32
42
  "README.md"
33
43
  ],
34
44
  "sideEffects": false
@@ -12,7 +12,7 @@ export class ObjectStore {
12
12
  request.addEventListener("upgradeneeded", () => {
13
13
  const db = request.result;
14
14
  if (!db.objectStoreNames.contains(ObjectStore.#store)) {
15
- db.createObjectStore(ObjectStore.#store, { keyPath: "id" });
15
+ db.createObjectStore(ObjectStore.#store, { keyPath: "key" });
16
16
  }
17
17
  });
18
18
 
@@ -48,19 +48,16 @@ export class ObjectStore {
48
48
  }
49
49
 
50
50
  /**
51
- * @param {import("../types").UUIDv4} id
52
- * @param {(propertyName: string, oldValue: any, newValue: any) => void | boolean} [onSetEvent]
53
- * @param {(propertyName: string, deletedValue: any) => void | boolean} [onDeleteEvent]
51
+ * @param {string} key
54
52
  * @returns {Promise<import("../types").StoredObject | undefined>}
55
53
  */
56
- async get(id, onSetEvent, onDeleteEvent) {
54
+ async get(key) {
57
55
  const db = await this.instance;
58
- const objectStoreInstance = this;
59
56
 
60
57
  return new Promise((resolve, reject) => {
61
58
  const transaction = db.transaction(ObjectStore.#store, "readonly");
62
59
  const store = transaction.objectStore(ObjectStore.#store);
63
- const request = store.get(id);
60
+ const request = store.get(key);
64
61
 
65
62
  transaction.oncomplete = () => {
66
63
  if (!request.result) {
@@ -68,32 +65,7 @@ export class ObjectStore {
68
65
  return;
69
66
  }
70
67
 
71
- const target = request.result;
72
-
73
- const handler = {
74
- set(targetObject, propertyName, newValue) {
75
- const oldValue = targetObject[propertyName];
76
- if (onSetEvent) {
77
- const shouldUpdate = onSetEvent(propertyName, oldValue, newValue);
78
- if (shouldUpdate === false) return true;
79
- }
80
- targetObject[propertyName] = newValue;
81
- void objectStoreInstance.put(targetObject);
82
- return true;
83
- },
84
- deleteProperty(targetObject, propertyName) {
85
- const deletedValue = targetObject[propertyName];
86
- if (onDeleteEvent) {
87
- const shouldDelete = onDeleteEvent(propertyName, deletedValue);
88
- if (shouldDelete === false) return true;
89
- }
90
- delete targetObject[propertyName];
91
- void objectStoreInstance.put(targetObject);
92
- return true;
93
- },
94
- };
95
-
96
- resolve(new Proxy(target, handler));
68
+ resolve(request.result);
97
69
  };
98
70
 
99
71
  transaction.onerror = () =>
@@ -102,28 +74,46 @@ export class ObjectStore {
102
74
  }
103
75
 
104
76
  /**
105
- * @param {import("../types").UUIDv4} id
77
+ * @param {string} key
106
78
  * @returns {Promise<void>}
107
79
  */
108
- async delete(id) {
80
+ async delete(key) {
109
81
  const db = await this.instance;
110
82
  return new Promise((resolve, reject) => {
111
83
  const transaction = db.transaction(ObjectStore.#store, "readwrite");
112
84
  const store = transaction.objectStore(ObjectStore.#store);
113
- store.delete(id);
85
+ store.delete(key);
114
86
  transaction.oncomplete = () => resolve();
115
87
  transaction.onerror = () =>
116
88
  reject(new Error(`{offdex} ObjectStore: ${transaction.error}`));
117
89
  });
118
90
  }
119
91
 
92
+ /**
93
+ * @param {import("../types").StoredObject[]} objects
94
+ * @returns {Promise<void>}
95
+ */
96
+ async putAll(objects) {
97
+ const db = await this.instance;
98
+ return new Promise((resolve, reject) => {
99
+ const transaction = db.transaction(ObjectStore.#store, "readwrite");
100
+ const store = transaction.objectStore(ObjectStore.#store);
101
+
102
+ for (const object of objects) {
103
+ store.put(object);
104
+ }
105
+
106
+ transaction.oncomplete = () => resolve();
107
+ transaction.onerror = () =>
108
+ reject(new Error(`{offdex} ObjectStore.putAll: ${transaction.error}`));
109
+ });
110
+ }
111
+
120
112
  /**
121
113
  * @param {import("../StorageQuery/index.js").StorageQuery | ((object: import("../types").StoredObject) => boolean)} queryOrPredicate
122
- * @param {(propertyName: string, oldValue: any, newValue: any) => void | boolean} [onSetEvent]
123
- * @param {(propertyName: string, deletedValue: any) => void | boolean} [onDeleteEvent]
124
114
  * @returns {Promise<import("../types").StoredObject[]>}
125
115
  */
126
- async getAllMatches(queryOrPredicate, onSetEvent, onDeleteEvent) {
116
+ async getAllMatches(queryOrPredicate) {
127
117
  const db = await this.instance;
128
118
  const objectStoreInstance = this;
129
119
 
@@ -146,34 +136,7 @@ export class ObjectStore {
146
136
  const value = cursor.value;
147
137
 
148
138
  if (predicate(value)) {
149
- const handler = {
150
- set(targetObject, propertyName, newValue) {
151
- const oldValue = targetObject[propertyName];
152
- if (onSetEvent) {
153
- const shouldUpdate = onSetEvent(
154
- propertyName,
155
- oldValue,
156
- newValue
157
- );
158
- if (shouldUpdate === false) return true;
159
- }
160
- targetObject[propertyName] = newValue;
161
- void objectStoreInstance.put(targetObject);
162
- return true;
163
- },
164
- deleteProperty(targetObject, propertyName) {
165
- const deletedValue = targetObject[propertyName];
166
- if (onDeleteEvent) {
167
- const shouldDelete = onDeleteEvent(propertyName, deletedValue);
168
- if (shouldDelete === false) return true;
169
- }
170
- delete targetObject[propertyName];
171
- void objectStoreInstance.put(targetObject);
172
- return true;
173
- },
174
- };
175
-
176
- results.push(new Proxy(value, handler));
139
+ results.push(value);
177
140
  }
178
141
 
179
142
  cursor.continue();
package/src/index.d.ts CHANGED
@@ -1,54 +1,36 @@
1
- export type UUIDv4 = `${string}-${string}-${string}-${string}-${string}`;
2
-
3
- export type StoredObject = { id: UUIDv4 } & Record<string, unknown>;
4
-
5
- export type OnSetHandler = (
6
- propertyName: string,
7
- oldValue: unknown,
8
- newValue: unknown
9
- ) => void | boolean;
10
-
11
- export type OnDeleteHandler = (
12
- propertyName: string,
13
- deletedValue: unknown
14
- ) => void | boolean;
15
-
16
- export class StorageQuery {
17
- constructor(args?: Record<string, unknown>);
18
- args: Record<string, unknown>;
19
- matches(object: StoredObject): boolean;
20
- }
21
-
22
- export class ObservableValue<T = unknown> {
23
- constructor(value: T, fallback?: T);
24
- get(): T;
25
- set(newValue: T): void;
26
- reset(): void;
27
- observe(event: "set" | "reset", callback: (value: T) => void): () => void;
28
- }
29
-
30
- export class ObservableObject<T extends StoredObject = StoredObject> {
31
- constructor(object: T);
32
- [key: string]: ObservableValue<unknown>;
33
- }
34
-
1
+ export type StoredObject = { key: string } & Record<string, unknown>;
2
+
3
+ export class StorageQuery {
4
+ constructor(args?: Record<string, unknown>);
5
+ args: Record<string, unknown>;
6
+ matches(object: StoredObject): boolean;
7
+ }
8
+
9
+ export class ObservableValue<T = unknown> {
10
+ constructor(value: T, fallback?: T);
11
+ get(): T;
12
+ set(newValue: T): void;
13
+ reset(): void;
14
+ observe(event: "set" | "reset", callback: (value: T) => void): () => void;
15
+ }
16
+
17
+ export class ObservableObject<T extends StoredObject = StoredObject> {
18
+ constructor(object: T);
19
+ [key: string]: ObservableValue<unknown>;
20
+ }
21
+
35
22
  export class ObjectStore {
36
23
  constructor();
37
24
  put(object: StoredObject): Promise<void>;
38
- get(
39
- id: UUIDv4,
40
- onSetEvent?: OnSetHandler,
41
- onDeleteEvent?: OnDeleteHandler
42
- ): Promise<StoredObject | undefined>;
43
- delete(id: UUIDv4): Promise<void>;
25
+ get(key: string): Promise<StoredObject | undefined>;
26
+ delete(key: string): Promise<void>;
27
+ putAll(objects: StoredObject[]): Promise<void>;
44
28
  getAllMatches(
45
- queryOrFilter: StorageQuery | ((object: StoredObject) => boolean),
46
- onSetEvent?: OnSetHandler,
47
- onDeleteEvent?: OnDeleteHandler
29
+ queryOrFilter: StorageQuery | ((object: StoredObject) => boolean)
48
30
  ): Promise<StoredObject[]>;
49
31
  deleteAllMatches(
50
32
  queryOrFilter: StorageQuery | ((object: StoredObject) => boolean)
51
- ): Promise<void>;
52
- }
53
-
54
- export const storage: ObjectStore;
33
+ ): Promise<void>;
34
+ }
35
+
36
+ export const storage: ObjectStore;
package/src/types.d.ts CHANGED
@@ -1,3 +1 @@
1
- export type UUIDv4 = `${string}-${string}-${string}-${string}-${string}`;
2
-
3
- export type StoredObject = { id: UUIDv4 } & Record<string, unknown>;
1
+ export type StoredObject = { key: string } & Record<string, unknown>;