use-idb-storage 0.0.1

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 ADDED
@@ -0,0 +1,6 @@
1
+ # sohanemon/idb
2
+
3
+ ![npm version](https://img.shields.io/npm/v/@sohanemon/idb)
4
+ ![npm downloads](https://img.shields.io/npm/dm/@sohanemon/idb)
5
+ ![License](https://img.shields.io/npm/l/@sohanemon/idb)
6
+ ![Tests](https://github.com/sohanemon/idb/actions/workflows/test.yml/badge.svg)
@@ -0,0 +1,56 @@
1
+ //#region src/config.d.ts
2
+ /**
3
+ * Configure global defaults for IDBStorage.
4
+ */
5
+ declare function configureIDBStorage(config: {
6
+ database?: string;
7
+ store?: string;
8
+ }): void;
9
+ //#endregion
10
+ //#region src/types.d.ts
11
+ /**
12
+ * Configuration options for IDBStorage.
13
+ */
14
+ interface IDBStorageOptions<T extends Record<string, any>> {
15
+ /** The key for the stored value */
16
+ key: string;
17
+ /** The default value if no value is found in IndexedDB */
18
+ defaultValue: T;
19
+ /** The name of the IndexedDB database (optional, defaults to global config) */
20
+ database?: string;
21
+ /** The name of the object store (optional, defaults to global config) */
22
+ store?: string;
23
+ }
24
+ //#endregion
25
+ //#region src/hook.d.ts
26
+ /**
27
+ * Hook to persist state in IndexedDB with a clean object-based API.
28
+ *
29
+ * @param options - Configuration object containing key, defaultValue, and optional database/store names
30
+ * @returns A tuple of the stored value, an async updater function, and a remove function.
31
+ *
32
+ * @example
33
+ * ```tsx
34
+ * // Configure global defaults (optional)
35
+ * configureIDBStorage({
36
+ * database: 'myApp',
37
+ * store: 'data'
38
+ * });
39
+ *
40
+ * const [userData, setUserData, removeUserData] = useIDBStorage({
41
+ * key: 'currentUser',
42
+ * defaultValue: { name: '', email: '' },
43
+ * // database: 'myApp', // optional, uses global default
44
+ * // store: 'users' // optional, uses global default
45
+ * });
46
+ *
47
+ * // Update data
48
+ * await setUserData({ name: 'John', email: 'john@example.com' });
49
+ *
50
+ * // Remove data
51
+ * await removeUserData();
52
+ * ```
53
+ */
54
+ declare function useIDBStorage<T extends Record<string, any>>(options: IDBStorageOptions<T>): [T, (value: T | ((prevState: T) => T)) => Promise<void>, () => Promise<void>];
55
+ //#endregion
56
+ export { configureIDBStorage, useIDBStorage };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ import*as e from"react";let t={database:`sohanemon-idb`,store:`default`};function n(e){t={...t,...e}}function r(){return{...t}}const i=(e,t,n=1)=>new Promise((r,i)=>{let a=indexedDB.open(e,n);a.onerror=()=>i(a.error),a.onsuccess=()=>r(a.result),a.onupgradeneeded=e=>{let n=e.target.result;n.objectStoreNames.contains(t)||n.createObjectStore(t)}});function a(e,t,n){return new Promise((r,i)=>{let a=e.transaction([t],`readonly`).objectStore(t).get(n);a.onerror=()=>i(a.error),a.onsuccess=()=>r(a.result)})}const o=(e,t,n,r)=>new Promise((i,a)=>{let o=e.transaction([t],`readwrite`).objectStore(t).put(r,n);o.onerror=()=>a(o.error),o.onsuccess=()=>i()}),s=(e,t,n)=>new Promise((r,i)=>{let a=e.transaction([t],`readwrite`).objectStore(t).delete(n);a.onerror=()=>i(a.error),a.onsuccess=()=>r()});function c(t){let{key:n,defaultValue:c,database:l=r().database,store:u=r().store}=t,[d,f]=e.useState(c),[p,m]=e.useState(null);return e.useEffect(()=>{let e=!0;return(async()=>{try{let t=await i(l,u);e&&m(t)}catch(e){console.error(`Failed to open IndexedDB:`,e)}})(),()=>{e=!1}},[l,u]),e.useEffect(()=>{p&&(async()=>{try{let e=await a(p,u,n);e!==void 0&&f(e)}catch(e){console.error(`Failed to load value from IndexedDB:`,e)}})()},[p,u,n]),[d,e.useCallback(async e=>{if(!p)return;let t=typeof e==`function`?e(d):e;f(t);try{await o(p,u,n,t)}catch(e){console.error(`Failed to save value to IndexedDB:`,e)}},[p,u,n,d]),e.useCallback(async()=>{if(p){f(c);try{await s(p,u,n)}catch(e){console.error(`Failed to remove value from IndexedDB:`,e)}}},[p,u,n,c])]}export{n as configureIDBStorage,c as useIDBStorage};
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "use-idb-storage",
3
+ "version": "0.0.1",
4
+ "author": "Sohan Emon <sohanemon@outlook.com>",
5
+ "description": "",
6
+ "type": "module",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "source": "./src/index.ts",
11
+ "types": "./dist/index.d.ts",
12
+ "main": "./dist/index.js",
13
+ "module": "./dist/index.js",
14
+ "scripts": {
15
+ "build": "tsdown",
16
+ "dev": "tsdown --watch",
17
+ "test": "vitest",
18
+ "test:run": "vitest run",
19
+ "test:list": "vitest list",
20
+ "test:log": "vitest run --reporter verbose",
21
+ "test:ui": "vitest --ui",
22
+ "check": "biome check .",
23
+ "fix": "biome check --diagnostic-level=error --write .",
24
+ "lint": "biome lint .",
25
+ "lint:fix": "biome lint --write --unsafe .",
26
+ "format": "biome format .",
27
+ "format:write": "biome format . --write",
28
+ "typecheck": "tsgo --noEmit",
29
+ "release": "tsgo --noEmit && npm run build && npm publish"
30
+ },
31
+ "keywords": [
32
+ "use-indexeddb",
33
+ "indexeddb",
34
+ "idb",
35
+ "localstorage"
36
+ ],
37
+ "license": "ISC",
38
+ "homepage": "https://sohanjs.web.app/idb",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/sohanemon/idb.git"
42
+ },
43
+ "bugs": {
44
+ "url": "https://github.com/sohanemon/idb/issues"
45
+ },
46
+ "devDependencies": {
47
+ "@biomejs/biome": "2.3.8",
48
+ "@testing-library/jest-dom": "^6.9.1",
49
+ "@testing-library/react": "^16.3.0",
50
+ "@tsconfig/strictest": "^2.0.8",
51
+ "@types/node": "^24.10.1",
52
+ "@types/react": "^19.2.5",
53
+ "@types/react-dom": "^19.2.3",
54
+ "@vitejs/plugin-react": "^5.1.1",
55
+ "@vitest/ui": "^4.0.14",
56
+ "jsdom": "^27.2.0",
57
+ "tsdown": "^0.16.4",
58
+ "typescript": "^5.9.3",
59
+ "vite": "npm:rolldown-vite@^7.2.5",
60
+ "vitest": "^4.0.14"
61
+ },
62
+ "peerDependencies": {
63
+ "react": "^19.2.0",
64
+ "react-dom": "^19.2.0"
65
+ },
66
+ "publishConfig": {
67
+ "access": "public"
68
+ },
69
+ "exports": {
70
+ ".": "./dist/index.js",
71
+ "./package.json": "./package.json"
72
+ }
73
+ }