tiny-idb 1.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) 2026 Jelodar
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 ADDED
@@ -0,0 +1,72 @@
1
+ # tiny-idb
2
+
3
+ **A minimalist, high-performance IndexedDB wrapper for modern web applications.**
4
+
5
+ `tiny-idb` provides a non-blocking, asynchronous alternative to `localStorage`. It is designed for developers who require the durability and capacity of IndexedDB without the complexity of its native API. By focusing on a single-store, key-value architecture, it eliminates the need for database versioning and boilerplate configuration.
6
+
7
+ [![NPM Version](https://img.shields.io/npm/v/tiny-idb.svg)](https://www.npmjs.com/package/tiny-idb)
8
+ [![License](https://img.shields.io/npm/l/tiny-idb.svg)](LICENSE)
9
+ [![Size](https://img.shields.io/bundlephobia/minzip/tiny-idb)](https://bundlephobia.com/package/tiny-idb)
10
+
11
+ ## Core Advantages
12
+
13
+ ### Architectural Simplicity
14
+ Native IndexedDB requires managing requests, transactions, and version upgrades. `tiny-idb` abstracts these into a predictable, promise-based API. It acts as a drop-in replacement for `localStorage` logic, allowing for an immediate transition to persistent, non-blocking storage.
15
+
16
+ ### Guaranteed Atomicity
17
+ The primary weakness of most storage wrappers is the "read-modify-write" race condition. If two parts of an application attempt to update the same key simultaneously, data loss often occurs. `tiny-idb` addresses this by executing `update`, `push`, and `merge` operations within a single IndexedDB transaction, ensuring that updates are processed sequentially and reliably.
18
+
19
+ ### Resource Efficiency
20
+ At only **596 bytes** (gzipped), the library introduces negligible overhead to your bundle. It is dependency-free and written in modern vanilla JavaScript, ensuring high performance across all environments that support IndexedDB.
21
+
22
+ ### Intelligent Lifecycle Management
23
+ The library handles database connection pooling, tab synchronization, and error recovery automatically. If a database connection is blocked by another tab or fails due to an environmental error, `tiny-idb` gracefully resets and recovers without requiring a page reload.
24
+
25
+ ### Zero-Configuration Portability
26
+ Beyond npm installation, `tiny-idb` is designed for maximum portability. As a single-file, dependency-free module, it can be integrated into any environment by simply dropping `tiny-idb.js` into a project directory. This makes it an ideal solution for rapid prototyping, legacy system upgrades, and environments where complex build pipelines are unavailable.
27
+
28
+ ## Installation
29
+
30
+ ```bash
31
+ npm install tiny-idb
32
+ ```
33
+
34
+ ### CDN Usage
35
+
36
+ ```html
37
+ <script type="module">
38
+ import { tinyIDB } from 'https://unpkg.com/tiny-idb/tiny-idb.min.js';
39
+ </script>
40
+ ```
41
+
42
+ * **Minified Module**: `https://unpkg.com/tiny-idb/tiny-idb.min.js` (596 bytes gzipped)
43
+ * **Source**: `https://unpkg.com/tiny-idb/tiny-idb.js`
44
+
45
+ ## Technical Comparison
46
+
47
+ | Feature | localStorage | tiny-idb |
48
+ |---------|--------------|----------|
49
+ | **Execution** | Synchronous (Blocks UI) | Asynchronous (Non-blocking) |
50
+ | **Storage Limit** | ~5-10MB | Disk-limited |
51
+ | **Data Types** | Strings only | Objects, Blobs, Arrays, Numbers |
52
+ | **Data Integrity** | Basic | ACID Compliant |
53
+ | **Race Condition Safety** | None | Atomic `update`/`push`/`merge` |
54
+
55
+ ## API Reference
56
+
57
+ | Method | Description |
58
+ |--------|-------------|
59
+ | `get(key)` | Retrieves a value; returns `undefined` if not found. |
60
+ | `set(key, value)` | Persists a value to the store. |
61
+ | `remove(key)` | Deletes a specific key. |
62
+ | `clear()` | Removes all data from the store. |
63
+ | `keys()` | Returns an array of all keys. |
64
+ | `values()` | Returns an array of all values. |
65
+ | `count()` | Returns the total number of entries. |
66
+ | `update(key, fn)` | Performs an atomic read-modify-write operation. |
67
+ | `push(key, value)` | Atomically appends a value to an array. |
68
+ | `merge(key, object)` | Atomically shallow-merges an object. |
69
+
70
+ ## License
71
+
72
+ MIT © [Jelodar](https://github.com/Jelodar)
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "tiny-idb",
3
+ "version": "1.0.0",
4
+ "description": "An extremely fast, super simple, and dependency-free IndexedDB wrapper. A drop-in replacement for localStorage with reliability and durability.",
5
+ "main": "tiny-idb.js",
6
+ "module": "tiny-idb.js",
7
+ "type": "module",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/Jelodar/tiny-idb.git"
11
+ },
12
+ "keywords": [
13
+ "indexeddb",
14
+ "storage",
15
+ "localstorage",
16
+ "simple",
17
+ "wrapper",
18
+ "db",
19
+ "browser",
20
+ "persistent",
21
+ "nosql",
22
+ "key-value"
23
+ ],
24
+ "author": "Jelodar",
25
+ "license": "MIT",
26
+ "bugs": {
27
+ "url": "https://github.com/Jelodar/tiny-idb/issues"
28
+ },
29
+ "homepage": "https://github.com/Jelodar/tiny-idb#readme",
30
+ "files": [
31
+ "tiny-idb.js",
32
+ "tiny-idb.min.js",
33
+ "LICENSE",
34
+ "README.md"
35
+ ],
36
+ "devDependencies": {
37
+ "fake-indexeddb": "^6.2.5",
38
+ "terser": "^5.46.1"
39
+ },
40
+ "scripts": {
41
+ "test": "node test.js"
42
+ }
43
+ }
package/tiny-idb.js ADDED
@@ -0,0 +1,55 @@
1
+ /**
2
+ * tiny-idb - A super simple, fast, and dependency-free IndexedDB wrapper.
3
+ * Designed as a drop-in replacement for localStorage with durability and performance.
4
+ *
5
+ * @author Jelodar
6
+ * @license MIT
7
+ */
8
+
9
+ const DB_NAME = 'tiny-idb', STORE_NAME = 's', VERSION = 1;
10
+ let dbPromise;
11
+
12
+ const getDB = () => dbPromise || (dbPromise = new Promise((resolve, reject) => {
13
+ const req = indexedDB.open(DB_NAME, VERSION);
14
+ req.onupgradeneeded = e => e.target.result.createObjectStore(STORE_NAME);
15
+ req.onsuccess = e => {
16
+ const db = e.target.result;
17
+ db.onversionchange = () => { db.close(); dbPromise = null; };
18
+ resolve(db);
19
+ };
20
+ req.onerror = () => { dbPromise = null; reject(req.error); };
21
+ }));
22
+
23
+ const prom = (req) => new Promise((res, rej) => {
24
+ req.onsuccess = () => res(req.result);
25
+ req.onerror = () => rej(req.error);
26
+ });
27
+
28
+ const tx = async (mode, cb) => {
29
+ const db = await getDB();
30
+ return new Promise((resolve, reject) => {
31
+ const t = db.transaction(STORE_NAME, mode);
32
+ let res;
33
+ try { res = cb(t.objectStore(STORE_NAME)); } catch (e) { t.abort(); return reject(e); }
34
+ t.oncomplete = () => resolve(res);
35
+ t.onerror = () => reject(t.error);
36
+ });
37
+ };
38
+
39
+ export const tinyIDB = {
40
+ set: (k, v) => tx('readwrite', s => prom(s.put(v, k))),
41
+ get: k => tx('readonly', s => prom(s.get(k))),
42
+ remove: k => tx('readwrite', s => prom(s.delete(k))),
43
+ clear: () => tx('readwrite', s => prom(s.clear())),
44
+ keys: () => tx('readonly', s => prom(s.getAllKeys())),
45
+ values: () => tx('readonly', s => prom(s.getAll())),
46
+ count: () => tx('readonly', s => prom(s.count())),
47
+ update: async (k, fn) => tx('readwrite', async s => {
48
+ const n = await fn(await prom(s.get(k)));
49
+ return prom(s.put(n, k));
50
+ }),
51
+ push: (k, v) => tinyIDB.update(k, c => [...(Array.isArray(c) ? c : []), v]),
52
+ merge: (k, p) => tinyIDB.update(k, c => ({ ...(c && typeof c === 'object' ? c : {}), ...p }))
53
+ };
54
+
55
+ export default tinyIDB;
@@ -0,0 +1 @@
1
+ const DB_NAME="tiny-idb",STORE_NAME="s",VERSION=1;let dbPromise;const getDB=()=>dbPromise||(dbPromise=new Promise((e,r)=>{const t=indexedDB.open(DB_NAME,1);t.onupgradeneeded=e=>e.target.result.createObjectStore("s"),t.onsuccess=r=>{const t=r.target.result;t.onversionchange=()=>{t.close(),dbPromise=null},e(t)},t.onerror=()=>{dbPromise=null,r(t.error)}})),prom=e=>new Promise((r,t)=>{e.onsuccess=()=>r(e.result),e.onerror=()=>t(e.error)}),tx=async(e,r)=>{const t=await getDB();return new Promise((o,n)=>{const s=t.transaction("s",e);let a;try{a=r(s.objectStore("s"))}catch(e){return s.abort(),n(e)}s.oncomplete=()=>o(a),s.onerror=()=>n(s.error)})};export const tinyIDB={set:(e,r)=>tx("readwrite",t=>prom(t.put(r,e))),get:e=>tx("readonly",r=>prom(r.get(e))),remove:e=>tx("readwrite",r=>prom(r.delete(e))),clear:()=>tx("readwrite",e=>prom(e.clear())),keys:()=>tx("readonly",e=>prom(e.getAllKeys())),values:()=>tx("readonly",e=>prom(e.getAll())),count:()=>tx("readonly",e=>prom(e.count())),update:async(e,r)=>tx("readwrite",async t=>{const o=await r(await prom(t.get(e)));return prom(t.put(o,e))}),push:(e,r)=>tinyIDB.update(e,e=>[...Array.isArray(e)?e:[],r]),merge:(e,r)=>tinyIDB.update(e,e=>({...e&&"object"==typeof e?e:{},...r}))};export default tinyIDB;