universe-code 0.0.15 → 0.0.17
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 +9 -1
- package/dist/indexdb/db.js +2 -4
- package/dist/indexdb/index.js +11 -2
- package/dist/indexdb/store.js +29 -36
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1 +1,9 @@
|
|
|
1
|
-
An opinionated npm package that enforces coding discipline by standardizing structure, patterns, and optimized best practices across all projects.
|
|
1
|
+
An opinionated npm package that enforces coding discipline by standardizing structure, patterns, and optimized best practices across all projects.
|
|
2
|
+
|
|
3
|
+
## Documentation
|
|
4
|
+
|
|
5
|
+
📦 IndexedDB Angular
|
|
6
|
+
👉 [View Docs](https://github.com/connectwithfalco/universe-code/tree/main/doc/angular/indexdb)
|
|
7
|
+
|
|
8
|
+
📦 IndexedDB React
|
|
9
|
+
👉 [View Docs](https://github.com/connectwithfalco/universe-code/tree/main/doc/react/indexdb)
|
package/dist/indexdb/db.js
CHANGED
|
@@ -11,9 +11,8 @@ export class DBManager {
|
|
|
11
11
|
return new Promise((resolve, reject) => {
|
|
12
12
|
const request = indexedDB.open(this.dbName, this.version);
|
|
13
13
|
|
|
14
|
-
request.onupgradeneeded = (
|
|
15
|
-
const db = target.result;
|
|
16
|
-
|
|
14
|
+
request.onupgradeneeded = (e) => {
|
|
15
|
+
const db = e.target.result;
|
|
17
16
|
stores.forEach(({ name, keyPath = "id", autoIncrement = false }) => {
|
|
18
17
|
if (!db.objectStoreNames.contains(name)) {
|
|
19
18
|
db.createObjectStore(name, { keyPath, autoIncrement });
|
|
@@ -25,7 +24,6 @@ export class DBManager {
|
|
|
25
24
|
this.db = request.result;
|
|
26
25
|
resolve(this.db);
|
|
27
26
|
};
|
|
28
|
-
|
|
29
27
|
request.onerror = () => reject(request.error);
|
|
30
28
|
});
|
|
31
29
|
}
|
package/dist/indexdb/index.js
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { DBManager } from './db.js';
|
|
2
|
+
import { DB } from './store.js';
|
|
3
|
+
|
|
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,6 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Generic Transaction Wrapper
|
|
3
|
+
*/
|
|
4
|
+
const perform = (db, storeName, mode, action) => {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
4
6
|
const tx = db.transaction(storeName, mode);
|
|
5
7
|
const store = tx.objectStore(storeName);
|
|
6
8
|
const request = action(store);
|
|
@@ -8,55 +10,46 @@ const perform = (db, storeName, mode, action) =>
|
|
|
8
10
|
request.onsuccess = () => resolve(request.result);
|
|
9
11
|
request.onerror = () => reject(request.error);
|
|
10
12
|
});
|
|
13
|
+
};
|
|
11
14
|
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
this.db = db;
|
|
15
|
-
this.storeName = storeName;
|
|
16
|
-
}
|
|
15
|
+
export const DB = {
|
|
16
|
+
get: (db, store, key) => perform(db, store, "readonly", s => s.get(key)),
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
return perform(this.db, this.storeName, "readonly", s => s.get(key));
|
|
20
|
-
}
|
|
18
|
+
put: (db, store, data) => perform(db, store, "readwrite", s => s.put(data)),
|
|
21
19
|
|
|
22
|
-
|
|
23
|
-
return perform(this.db, this.storeName, "readwrite", s => s.put(data));
|
|
24
|
-
}
|
|
20
|
+
remove: (db, store, key) => perform(db, store, "readwrite", s => s.delete(key)),
|
|
25
21
|
|
|
26
|
-
|
|
27
|
-
return perform(this.db, this.storeName, "readwrite", s => s.delete(key));
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
clear() {
|
|
31
|
-
return perform(this.db, this.storeName, "readwrite", s => s.clear());
|
|
32
|
-
}
|
|
22
|
+
clear: (db, store) => perform(db, store, "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
27
|
*/
|
|
37
|
-
async
|
|
38
|
-
const now = Date.now();
|
|
39
|
-
|
|
28
|
+
async getWithExpiry(db, storeName, key, ttl, apiCall) {
|
|
40
29
|
try {
|
|
41
|
-
const cached = await this.get(key);
|
|
30
|
+
const cached = await this.get(db, storeName, key);
|
|
31
|
+
const now = Date.now();
|
|
42
32
|
|
|
43
|
-
|
|
33
|
+
// If valid data exists and is NOT expired, return it
|
|
34
|
+
if (cached && cached.timestamp && (now - cached.timestamp < ttl)) {
|
|
44
35
|
return cached.data;
|
|
45
36
|
}
|
|
46
37
|
|
|
47
|
-
|
|
38
|
+
// Otherwise, call API
|
|
39
|
+
const freshData = await apiCall();
|
|
48
40
|
|
|
49
|
-
|
|
41
|
+
// Save to IndexedDB with new timestamp
|
|
42
|
+
await this.put(db, storeName, {
|
|
50
43
|
id: key,
|
|
51
|
-
data,
|
|
44
|
+
data: freshData,
|
|
52
45
|
timestamp: now
|
|
53
46
|
});
|
|
54
47
|
|
|
55
|
-
return
|
|
56
|
-
} catch (
|
|
57
|
-
console.warn("
|
|
58
|
-
const fallback = await this.get(key);
|
|
59
|
-
return fallback
|
|
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;
|
|
60
53
|
}
|
|
61
54
|
}
|
|
62
|
-
}
|
|
55
|
+
};
|