idb-refined 0.0.1 → 0.0.2

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 CHANGED
@@ -14,21 +14,15 @@ npm install idb-refined
14
14
 
15
15
  | Function | Purpose |
16
16
  |----------|---------|
17
- | **initDb(name, options?)** | Open or create a DB. Version is auto-detected when you pass a declarative `schema`; optional manual `version` + `upgrade`. Returns idb’s `IDBPDatabase`. |
18
- | **cleanOldEntries(db, storeName, options)** | Delete entries where `dateKey` < `before` (timestamp ms). Options: `{ dateKey, before }`. Requires an index on `dateKey`. |
19
- | **cleanWhenTooLarge(db, storeName, options)** | Evict oldest entries (by `dateKey`) until count ≤ `maxCount`. Options: `{ dateKey, maxCount }`. Returns count deleted. |
20
- | **putWithEviction(db, storeName, value, options)** | Put value, then if store count > `maxCount` evict oldest by `dateKey`. Options: `{ key?, dateKey, maxCount }`. |
17
+ | **initDb(name, options?)** | Open or create a DB. Version is auto-detected when you pass a declarative `schema`; or use manual `version` and `upgrade`. Returns idb’s `IDBPDatabase`. |
18
+ | **cleanOldEntries(db, storeName, options)** | Delete entries where `dateKey` < `before` (timestamp ms). Requires an index on `dateKey`. |
19
+ | **cleanWhenTooLarge(db, storeName, options)** | Evict oldest entries (by `dateKey`) until count ≤ `maxCount`. Returns count deleted. Requires an index on `dateKey`. `maxCount` is optional. |
20
+ | **putWithEviction(db, storeName, value, options)** | Put value, then if store count > `maxCount` evict oldest by `dateKey`. Options may include `expiresAt` (ms), `ttlSeconds` (seconds), and optional `maxCount`. |
21
21
  | **deleteByKey(db, storeName, key)** | Delete a single entry by key. |
22
22
  | **clearStore(db, storeName)** | Delete all entries in a store. |
23
23
  | **deleteDB(name)** | Re-export of idb’s `deleteDB`. |
24
24
 
25
- ## Use cases
26
-
27
- - **Cache with TTL:** initDb, put items with `expiresAt`, call `cleanOldEntries` on startup or interval.
28
- - **Cache with max size:** Use `putWithEviction` or call `cleanWhenTooLarge` after adding.
29
- - **Cache with TTL + max size:** `putWithEviction` + periodic `cleanOldEntries` for expired.
30
- - **Log / event buffer:** `putWithEviction` with `dateKey: 'createdAt'` and `maxCount`.
31
- - **Simple key-value:** initDb, `db.get` / `db.put` / `deleteByKey`; optional cleanup when too large.
25
+ For parameter details, behavior, and use-case explanations, see **[Advanced documentation](docs/advanced.md)**.
32
26
 
33
27
  ## Examples
34
28
 
@@ -45,57 +39,35 @@ const db = await initDb("my-cache", {
45
39
  },
46
40
  });
47
41
 
48
- // Add item; if store has more than 1000 entries, evict oldest
49
- await putWithEviction(
50
- db,
51
- "cache",
52
- { id: "k1", data: "v1", expiresAt: Date.now() + 3600 },
53
- { dateKey: "expiresAt", maxCount: 1000 }
54
- );
55
-
56
- // Periodic: remove expired
57
- await cleanOldEntries(db, "cache", {
42
+ await putWithEviction(db, "cache", { id: "k1", data: "v1" }, {
58
43
  dateKey: "expiresAt",
59
- before: Date.now(),
44
+ maxCount: 1000,
45
+ ttlSeconds: 3600,
60
46
  });
61
47
 
62
- // Delete one
48
+ await cleanOldEntries(db, "cache", { dateKey: "expiresAt", before: Date.now() });
63
49
  await deleteByKey(db, "cache", "k1");
64
50
  ```
65
51
 
66
- ### Log buffer (cap size)
52
+ ### Log buffer
67
53
 
68
54
  ```ts
69
- await putWithEviction(
70
- db,
71
- "logs",
72
- { id: generateId(), message, createdAt: Date.now() },
73
- { dateKey: "createdAt", maxCount: 5000 }
74
- );
55
+ await putWithEviction(db, "logs", { id: generateId(), message }, {
56
+ dateKey: "createdAt",
57
+ maxCount: 5000,
58
+ });
75
59
  ```
76
60
 
77
- ### Manual cleanup when too large
61
+ ### Manual eviction
78
62
 
79
63
  ```ts
80
- const deleted = await cleanWhenTooLarge(db, "cache", {
81
- dateKey: "expiresAt",
82
- maxCount: 500,
83
- });
64
+ const deleted = await cleanWhenTooLarge(db, "cache", { dateKey: "expiresAt", maxCount: 500 });
84
65
  console.log(`Evicted ${deleted} entries`);
85
66
  ```
86
67
 
87
68
  ## Requirements
88
69
 
89
- - Stores that use `cleanOldEntries`, `cleanWhenTooLarge`, or `putWithEviction` must have an **index** on the date field (e.g. `expiresAt`, `createdAt`). Define it in your schema:
90
-
91
- ```ts
92
- schema: {
93
- stores: {
94
- cache: { keyPath: "id", indexes: ["expiresAt"] },
95
- },
96
- }
97
- ```
98
-
70
+ - Stores used with `cleanOldEntries`, `cleanWhenTooLarge`, or `putWithEviction` must have an **index** on the date field (e.g. `expiresAt`, `createdAt`). Define it in your schema.
99
71
  - Eviction is **count-based** only (no byte-size or quota check).
100
72
 
101
73
  ## Releasing
@@ -3,9 +3,14 @@ export interface PutWithEvictionOptions {
3
3
  key?: IDBValidKey;
4
4
  dateKey: string;
5
5
  maxCount: number;
6
+ /** Absolute expiry timestamp (ms). If set, used for the dateKey field on the value. */
7
+ expiresAt?: number;
8
+ /** Relative expiry in seconds. Used when expiresAt is not set; then dateKey = now + ttlSeconds * 1000. */
9
+ ttlSeconds?: number;
6
10
  }
7
11
  /**
8
12
  * Put a value in the store, then if count > maxCount evict oldest entries (by dateKey).
13
+ * Optionally set the dateKey field from expiresAt (absolute ms) or ttlSeconds (relative seconds).
9
14
  * Value must include the keyPath field, or pass options.key.
10
15
  */
11
16
  export declare function putWithEviction<T = unknown>(db: IDBPDatabase<unknown>, storeName: string, value: T, options: PutWithEvictionOptions): Promise<void>;
@@ -1,10 +1,21 @@
1
1
  import { cleanWhenTooLarge } from "./cleanWhenTooLarge.js";
2
+ const DEFAULT_TTL_SECONDS = 3600;
2
3
  /**
3
4
  * Put a value in the store, then if count > maxCount evict oldest entries (by dateKey).
5
+ * Optionally set the dateKey field from expiresAt (absolute ms) or ttlSeconds (relative seconds).
4
6
  * Value must include the keyPath field, or pass options.key.
5
7
  */
6
8
  export async function putWithEviction(db, storeName, value, options) {
7
- const { key, dateKey, maxCount } = options;
9
+ const { key, dateKey, maxCount, expiresAt, ttlSeconds } = options;
10
+ if (typeof value === "object" && value !== null) {
11
+ const valueRecord = value;
12
+ if (expiresAt !== undefined) {
13
+ valueRecord[dateKey] = expiresAt;
14
+ }
15
+ else {
16
+ valueRecord[dateKey] = Date.now() + (ttlSeconds ?? DEFAULT_TTL_SECONDS) * 1000;
17
+ }
18
+ }
8
19
  await db.put(storeName, value, key);
9
20
  const count = await db.count(storeName);
10
21
  if (count > maxCount) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "idb-refined",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Thin TypeScript IndexedDB helper: initDb (auto-version), clean by date/size, add with eviction, delete helpers",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -16,6 +16,16 @@
16
16
  "files": [
17
17
  "dist"
18
18
  ],
19
+ "scripts": {
20
+ "build": "tsc -p tsconfig.build.json",
21
+ "lint": "eslint src",
22
+ "lint:fix": "eslint src --fix",
23
+ "format": "prettier --write \"src/**/*.ts\"",
24
+ "format:check": "prettier --check \"src/**/*.ts\"",
25
+ "test": "vitest run",
26
+ "test:watch": "vitest",
27
+ "prepare": "husky"
28
+ },
19
29
  "lint-staged": {
20
30
  "src/**/*.ts": [
21
31
  "eslint --fix",
@@ -46,14 +56,5 @@
46
56
  "browser",
47
57
  "storage"
48
58
  ],
49
- "license": "MIT",
50
- "scripts": {
51
- "build": "tsc -p tsconfig.build.json",
52
- "lint": "eslint src",
53
- "lint:fix": "eslint src --fix",
54
- "format": "prettier --write \"src/**/*.ts\"",
55
- "format:check": "prettier --check \"src/**/*.ts\"",
56
- "test": "vitest run",
57
- "test:watch": "vitest"
58
- }
59
- }
59
+ "license": "MIT"
60
+ }