offdex 1.0.3 → 1.0.4
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 +7 -7
- package/package.json +1 -1
- package/src/index.d.ts +2 -0
- package/src/index.js +20 -18
package/README.md
CHANGED
|
@@ -15,9 +15,7 @@ npm install offdex
|
|
|
15
15
|
|
|
16
16
|
## Quick start
|
|
17
17
|
```js
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
const storage = new OfflineStorage();
|
|
18
|
+
import { storage } from "offdex";
|
|
21
19
|
|
|
22
20
|
const profile = {
|
|
23
21
|
id: crypto.randomUUID(), // UUIDv4 string
|
|
@@ -34,19 +32,21 @@ await storage.delete(profile.id);
|
|
|
34
32
|
```
|
|
35
33
|
|
|
36
34
|
## API
|
|
35
|
+
### `storage`
|
|
36
|
+
- Ready-to-use singleton instance shared across every import in the same origin.
|
|
37
|
+
- Uses the `offline` database and `objects` store under the hood.
|
|
38
|
+
|
|
37
39
|
### `class OfflineStorage`
|
|
38
|
-
- `constructor()` — opens (or creates) the `offline` database with the `objects` store.
|
|
40
|
+
- `constructor()` — opens (or creates) the `offline` database with the `objects` store. Use this only if you need a separate instance.
|
|
39
41
|
- `put(object: { id: UUIDv4 } & Record<string, unknown>): Promise<void>` — upserts an object keyed by `id`.
|
|
40
42
|
- `get(id: UUIDv4): Promise<{ id: UUIDv4 } & Record<string, unknown> | undefined>` — fetches by `id`, returning `undefined` when missing.
|
|
41
43
|
- `delete(id: UUIDv4): Promise<void>` — removes an object by `id`.
|
|
42
44
|
|
|
43
45
|
### Types
|
|
44
46
|
- `UUIDv4` — template literal type for UUID strings.
|
|
47
|
+
- `StoredObject` — `{ id: UUIDv4 } & Record<string, unknown>`.
|
|
45
48
|
|
|
46
49
|
## Notes
|
|
47
50
|
- Runs in any environment that exposes `indexedDB` (secure contexts in modern browsers).
|
|
48
51
|
- Data is shared per origin; open multiple tabs or workers and you will see the same store.
|
|
49
52
|
- There is no schema migration system; keep your stored objects backward compatible or manage migrations externally if you need them.
|
|
50
|
-
|
|
51
|
-
## Quick manual test
|
|
52
|
-
Open `in-browser-testing.html` in a browser (or serve it locally) to poke at the API from DevTools: the module attaches `OfflineStorage` to `globalThis`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "offdex",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
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/index.d.ts
CHANGED
package/src/index.js
CHANGED
|
@@ -28,13 +28,13 @@ export class OfflineStorage {
|
|
|
28
28
|
|
|
29
29
|
constructor() {
|
|
30
30
|
this.storage = OfflineStorage.#open();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* @param {import("./types").StoredObject} object
|
|
35
|
-
* @returns {Promise<void>}
|
|
36
|
-
*/
|
|
37
|
-
async put(object) {
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param {import("./types").StoredObject} object
|
|
35
|
+
* @returns {Promise<void>}
|
|
36
|
+
*/
|
|
37
|
+
async put(object) {
|
|
38
38
|
const db = await this.storage;
|
|
39
39
|
return new Promise((resolve, reject) => {
|
|
40
40
|
const tx = db.transaction(OfflineStorage.#store, "readwrite");
|
|
@@ -45,12 +45,12 @@ export class OfflineStorage {
|
|
|
45
45
|
tx.onerror = () => reject(new Error(`[OfflineStorage] ${tx.error}`));
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @param {import("./types").UUIDv4} id
|
|
51
|
-
* @returns {Promise<import("./types").StoredObject | undefined>}
|
|
52
|
-
*/
|
|
53
|
-
async get(id) {
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {import("./types").UUIDv4} id
|
|
51
|
+
* @returns {Promise<import("./types").StoredObject | undefined>}
|
|
52
|
+
*/
|
|
53
|
+
async get(id) {
|
|
54
54
|
const db = await this.storage;
|
|
55
55
|
return new Promise((resolve, reject) => {
|
|
56
56
|
const tx = db.transaction(OfflineStorage.#store, "readonly");
|
|
@@ -63,11 +63,11 @@ export class OfflineStorage {
|
|
|
63
63
|
tx.onerror = () => reject(new Error(`[OfflineStorage] ${tx.error}`));
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
|
-
/**
|
|
67
|
-
*
|
|
68
|
-
* @param {import("./types").UUIDv4} id
|
|
69
|
-
* @returns {Promise<void>}
|
|
70
|
-
*/
|
|
66
|
+
/**
|
|
67
|
+
*
|
|
68
|
+
* @param {import("./types").UUIDv4} id
|
|
69
|
+
* @returns {Promise<void>}
|
|
70
|
+
*/
|
|
71
71
|
async delete(id) {
|
|
72
72
|
const db = await this.storage;
|
|
73
73
|
return new Promise((resolve, reject) => {
|
|
@@ -79,3 +79,5 @@ export class OfflineStorage {
|
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
|
+
|
|
83
|
+
export const storage = new OfflineStorage();
|