oto-storage 0.2.0 → 0.3.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 +99 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,6 +49,10 @@ Oto storage uses the JavaScript Proxy API to let you interact with browser stora
|
|
|
49
49
|
|
|
50
50
|
- **Collision Protection**: Automatic key prefixing (namespacing).
|
|
51
51
|
|
|
52
|
+
- **Default Values**: Define fallback values for missing keys with deep merge support.
|
|
53
|
+
|
|
54
|
+
- **TTL / Expiration**: Set automatic expiration for stored values.
|
|
55
|
+
|
|
52
56
|
### 🚀 Quick Start
|
|
53
57
|
|
|
54
58
|
**_1. Define your Schema_**
|
|
@@ -239,6 +243,101 @@ import { oto, version } from "oto-storage";
|
|
|
239
243
|
console.log(`Using oto-storage v${version}`);
|
|
240
244
|
```
|
|
241
245
|
|
|
246
|
+
**Default Values**
|
|
247
|
+
|
|
248
|
+
Provide default values that are returned when keys don't exist in storage. Defaults support deep merging with stored values.
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
interface AppStorage {
|
|
252
|
+
theme: "light" | "dark";
|
|
253
|
+
user: { name: string; role: string; active: boolean };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const storage = oto<AppStorage>({
|
|
257
|
+
prefix: "app-",
|
|
258
|
+
defaults: {
|
|
259
|
+
theme: "light",
|
|
260
|
+
user: { name: "Anonymous", role: "guest", active: false },
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
// Returns default when not set
|
|
265
|
+
console.log(storage.theme); // "light"
|
|
266
|
+
|
|
267
|
+
// Stored values override defaults
|
|
268
|
+
storage.theme = "dark";
|
|
269
|
+
console.log(storage.theme); // "dark"
|
|
270
|
+
|
|
271
|
+
// Deep merge - partial updates preserve defaults
|
|
272
|
+
storage.user = { name: "Alice" }; // Only set name
|
|
273
|
+
console.log(storage.user);
|
|
274
|
+
// { name: "Alice", role: "guest", active: false }
|
|
275
|
+
|
|
276
|
+
// Access nested properties - stored value takes precedence over defaults
|
|
277
|
+
console.log(storage.user.name); // "Alice" (stored value)
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
**TTL / Expiration**
|
|
281
|
+
|
|
282
|
+
Automatically expire stored values after a specified time (in milliseconds). Expired keys are automatically deleted on access.
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
interface SessionStorage {
|
|
286
|
+
token: string;
|
|
287
|
+
user: { id: string; name: string };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const storage = oto<SessionStorage>({
|
|
291
|
+
prefix: "session-",
|
|
292
|
+
ttl: 3600000, // 1 hour in milliseconds
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
// Store value - automatically wrapped with expiration
|
|
296
|
+
storage.token = "abc123";
|
|
297
|
+
|
|
298
|
+
// Value is accessible before expiration
|
|
299
|
+
console.log(storage.token); // "abc123"
|
|
300
|
+
|
|
301
|
+
// ... 1 hour later ...
|
|
302
|
+
|
|
303
|
+
// Expired key is auto-deleted and returns undefined
|
|
304
|
+
console.log(storage.token); // undefined
|
|
305
|
+
|
|
306
|
+
// TTL works with nested objects too
|
|
307
|
+
storage.user = { id: "1", name: "Alice" };
|
|
308
|
+
storage.user.name = "Bob"; // Updates are also TTL-protected
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
**Combining Defaults and TTL**
|
|
312
|
+
|
|
313
|
+
Use both features together for powerful patterns like session management:
|
|
314
|
+
|
|
315
|
+
```typescript
|
|
316
|
+
interface AuthStorage {
|
|
317
|
+
token: string | null;
|
|
318
|
+
user: { id: string; name: string } | null;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const auth = oto<AuthStorage>({
|
|
322
|
+
prefix: "auth-",
|
|
323
|
+
ttl: 3600000, // 1 hour
|
|
324
|
+
defaults: {
|
|
325
|
+
token: null,
|
|
326
|
+
user: null,
|
|
327
|
+
},
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// Before login - returns defaults
|
|
331
|
+
console.log(auth.token); // null
|
|
332
|
+
|
|
333
|
+
// After login
|
|
334
|
+
auth.token = "secret-token";
|
|
335
|
+
auth.user = { id: "123", name: "Alice" };
|
|
336
|
+
|
|
337
|
+
// ... after 1 hour (token expires) ...
|
|
338
|
+
console.log(auth.token); // null (back to default)
|
|
339
|
+
```
|
|
340
|
+
|
|
242
341
|
### 🛠️ Architecture Decisions
|
|
243
342
|
|
|
244
343
|
**_Why Proxy?_**
|
package/package.json
CHANGED