universe-code 0.0.12 → 0.0.13

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.
@@ -1,45 +1,52 @@
1
- /**
2
- * Generic Transaction Wrapper
3
- */
1
+ // Internal helper for transactions
4
2
  const perform = (db, storeName, mode, action) => {
5
3
  return new Promise((resolve, reject) => {
6
4
  const tx = db.transaction(storeName, mode);
7
5
  const store = tx.objectStore(storeName);
8
6
  const request = action(store);
9
-
10
7
  request.onsuccess = () => resolve(request.result);
11
8
  request.onerror = () => reject(request.error);
12
9
  });
13
10
  };
14
11
 
15
- export const DB = {
16
- get: (db, store, key) => perform(db, store, "readonly", s => s.get(key)),
17
-
18
- put: (db, store, data) => perform(db, store, "readwrite", s => s.put(data)),
12
+ export class Store {
13
+ constructor(db, storeName) {
14
+ this.db = db;
15
+ this.storeName = storeName;
16
+ }
17
+
18
+ // Now these methods don't need db or storeName passed in!
19
+ async get(key) {
20
+ return await perform(this.db, this.storeName, "readonly", s => s.get(key));
21
+ }
22
+
23
+ async put(data) {
24
+ return await perform(this.db, this.storeName, "readwrite", s => s.put(data));
25
+ }
19
26
 
20
- remove: (db, store, key) => perform(db, store, "readwrite", s => s.delete(key)),
27
+ async remove(key) {
28
+ return await perform(this.db, this.storeName, "readwrite", s => s.delete(key));
29
+ }
21
30
 
22
- clear: (db, store) => perform(db, store, "readwrite", s => s.clear()),
31
+ async clear() {
32
+ return await perform(this.db, this.storeName, "readwrite", s => s.clear());
33
+ }
23
34
 
24
35
  /**
25
- * PRO FEATURE: getWithExpiry
26
- * Automatically refreshes data from an API if the local version is too old.
36
+ * Smart Fetch with TTL
27
37
  */
28
- async getWithExpiry(db, storeName, key, ttl, apiCall) {
38
+ async fetch(key, ttl, apiCall) {
29
39
  try {
30
- const cached = await this.get(db, storeName, key);
40
+ const cached = await this.get(key);
31
41
  const now = Date.now();
32
-
33
- // If valid data exists and is NOT expired, return it
34
- if (cached && cached.timestamp && (now - cached.timestamp < ttl)) {
42
+
43
+ if (cached?.timestamp && (now - cached.timestamp < ttl)) {
35
44
  return cached.data;
36
45
  }
37
46
 
38
- // Otherwise, call API
39
47
  const freshData = await apiCall();
40
-
41
- // Save to IndexedDB with new timestamp
42
- await this.put(db, storeName, {
48
+
49
+ await this.put({
43
50
  id: key,
44
51
  data: freshData,
45
52
  timestamp: now
@@ -47,9 +54,9 @@ export const DB = {
47
54
 
48
55
  return freshData;
49
56
  } catch (error) {
50
- console.warn("API/DB Error, returning fallback if available:", error);
51
- const fallback = await this.get(db, storeName, key);
57
+ console.warn("Returning fallback data due to error:", error);
58
+ const fallback = await this.get(key);
52
59
  return fallback ? fallback.data : null;
53
60
  }
54
61
  }
55
- };
62
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "universe-code",
3
- "version": "0.0.12",
3
+ "version": "0.0.13",
4
4
  "description": "Universal utility functions for all JS frameworks",
5
5
  "license": "ISC",
6
6
  "type": "module",