universe-code 0.0.8 → 0.0.9
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/dist/indexdb/db.js +26 -24
- package/dist/indexdb/index.js +10 -6
- package/dist/indexdb/store.js +50 -69
- package/package.json +1 -1
package/dist/indexdb/db.js
CHANGED
|
@@ -1,28 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
export class DBManager {
|
|
2
|
+
constructor(dbName, version = 1) {
|
|
3
|
+
this.dbName = dbName;
|
|
4
|
+
this.version = version;
|
|
5
|
+
this.db = null;
|
|
6
|
+
}
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
async connect(stores = []) {
|
|
9
|
+
if (this.db) return this.db;
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
const
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
const request = indexedDB.open(this.dbName, this.version);
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
};
|
|
14
|
+
request.onupgradeneeded = (e) => {
|
|
15
|
+
const db = e.target.result;
|
|
16
|
+
stores.forEach(({ name, keyPath = "id", autoIncrement = false }) => {
|
|
17
|
+
if (!db.objectStoreNames.contains(name)) {
|
|
18
|
+
db.createObjectStore(name, { keyPath, autoIncrement });
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
request.onsuccess = () => {
|
|
24
|
+
this.db = request.result;
|
|
25
|
+
resolve(this.db);
|
|
26
|
+
};
|
|
27
|
+
request.onerror = () => reject(request.error);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
package/dist/indexdb/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { DBManager } from './db.js';
|
|
2
|
+
import { DB } from './store.js';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
// Export everything as a unified object or individual pieces
|
|
5
|
+
export { DBManager, DB };
|
|
6
|
+
|
|
7
|
+
// Default export for easy usage
|
|
8
|
+
export default {
|
|
9
|
+
DBManager,
|
|
10
|
+
...DB
|
|
11
|
+
};
|
package/dist/indexdb/store.js
CHANGED
|
@@ -1,74 +1,55 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generic Transaction Wrapper
|
|
3
|
+
*/
|
|
4
|
+
const perform = (db, storeName, mode, action) => {
|
|
2
5
|
return new Promise((resolve, reject) => {
|
|
3
|
-
const tx = db.transaction(storeName,
|
|
6
|
+
const tx = db.transaction(storeName, mode);
|
|
4
7
|
const store = tx.objectStore(storeName);
|
|
5
|
-
const
|
|
8
|
+
const request = action(store);
|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
|
|
10
|
+
request.onsuccess = () => resolve(request.result);
|
|
11
|
+
request.onerror = () => reject(request.error);
|
|
9
12
|
});
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function put(db, storeName, data) {
|
|
13
|
-
return new Promise((resolve, reject) => {
|
|
14
|
-
const tx = db.transaction(storeName, "readwrite");
|
|
15
|
-
const store = tx.objectStore(storeName);
|
|
16
|
-
const req = store.put(data);
|
|
17
|
-
|
|
18
|
-
req.onsuccess = () => resolve(req.result);
|
|
19
|
-
req.onerror = () => reject(req.error);
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function get(db, storeName, key) {
|
|
24
|
-
return new Promise((resolve, reject) => {
|
|
25
|
-
const tx = db.transaction(storeName, "readonly");
|
|
26
|
-
const store = tx.objectStore(storeName);
|
|
27
|
-
const req = store.get(key);
|
|
28
|
-
|
|
29
|
-
req.onsuccess = () => resolve(req.result);
|
|
30
|
-
req.onerror = () => reject(req.error);
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function getAll(db, storeName) {
|
|
35
|
-
return new Promise((resolve, reject) => {
|
|
36
|
-
const tx = db.transaction(storeName, "readonly");
|
|
37
|
-
const store = tx.objectStore(storeName);
|
|
38
|
-
const req = store.getAll();
|
|
39
|
-
|
|
40
|
-
req.onsuccess = () => resolve(req.result);
|
|
41
|
-
req.onerror = () => reject(req.error);
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function remove(db, storeName, key) {
|
|
46
|
-
return new Promise((resolve, reject) => {
|
|
47
|
-
const tx = db.transaction(storeName, "readwrite");
|
|
48
|
-
const store = tx.objectStore(storeName);
|
|
49
|
-
const req = store.delete(key);
|
|
50
|
-
|
|
51
|
-
req.onsuccess = () => resolve(true);
|
|
52
|
-
req.onerror = () => reject(req.error);
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function clear(db, storeName) {
|
|
57
|
-
return new Promise((resolve, reject) => {
|
|
58
|
-
const tx = db.transaction(storeName, "readwrite");
|
|
59
|
-
const store = tx.objectStore(storeName);
|
|
60
|
-
const req = store.clear();
|
|
61
|
-
|
|
62
|
-
req.onsuccess = () => resolve(true);
|
|
63
|
-
req.onerror = () => reject(req.error);
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
module.exports = {
|
|
68
|
-
add,
|
|
69
|
-
put,
|
|
70
|
-
get,
|
|
71
|
-
getAll,
|
|
72
|
-
remove,
|
|
73
|
-
clear
|
|
74
13
|
};
|
|
14
|
+
|
|
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)),
|
|
19
|
+
|
|
20
|
+
remove: (db, store, key) => perform(db, store, "readwrite", s => s.delete(key)),
|
|
21
|
+
|
|
22
|
+
clear: (db, store) => perform(db, store, "readwrite", s => s.clear()),
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* PRO FEATURE: getWithExpiry
|
|
26
|
+
* Automatically refreshes data from an API if the local version is too old.
|
|
27
|
+
*/
|
|
28
|
+
async getWithExpiry(db, storeName, key, ttl, apiCall) {
|
|
29
|
+
try {
|
|
30
|
+
const cached = await this.get(db, storeName, key);
|
|
31
|
+
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)) {
|
|
35
|
+
return cached.data;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Otherwise, call API
|
|
39
|
+
const freshData = await apiCall();
|
|
40
|
+
|
|
41
|
+
// Save to IndexedDB with new timestamp
|
|
42
|
+
await this.put(db, storeName, {
|
|
43
|
+
id: key,
|
|
44
|
+
data: freshData,
|
|
45
|
+
timestamp: now
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return freshData;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.warn("API/DB Error, returning fallback if available:", error);
|
|
51
|
+
const fallback = await this.get(db, storeName, key);
|
|
52
|
+
return fallback ? fallback.data : null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|