universe-code 0.0.75 → 0.0.77
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,19 +1,30 @@
|
|
|
1
1
|
import { connectDB } from "./idbStore.js";
|
|
2
|
+
|
|
2
3
|
let config = null;
|
|
3
4
|
|
|
5
|
+
// 🔹 promise that resolves once IndexedDB is configured
|
|
6
|
+
let resolveReady;
|
|
7
|
+
export const configReady = new Promise((res) => (resolveReady = res));
|
|
8
|
+
|
|
4
9
|
export const configureIdb = (options) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
10
|
+
if (config) return;
|
|
11
|
+
|
|
12
|
+
config = options;
|
|
13
|
+
|
|
14
|
+
// notify others that config is ready
|
|
15
|
+
resolveReady?.();
|
|
16
|
+
|
|
17
|
+
// optional: eager connection
|
|
18
|
+
if (typeof window !== "undefined" && typeof indexedDB !== "undefined") {
|
|
9
19
|
connectDB();
|
|
20
|
+
}
|
|
10
21
|
};
|
|
11
22
|
|
|
12
23
|
export const getConfig = () => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
};
|
|
24
|
+
if (!config) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
"IndexedDB not configured. Call configureIdb() before using the store."
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
return config;
|
|
30
|
+
};
|
|
@@ -1,39 +1,62 @@
|
|
|
1
1
|
import { DBManager, DB } from "universe-code";
|
|
2
|
-
import { getConfig } from "./config.js";
|
|
2
|
+
import { getConfig, configReady } from "./config.js";
|
|
3
3
|
|
|
4
4
|
let db = null;
|
|
5
5
|
let store = null;
|
|
6
6
|
let manager = null;
|
|
7
7
|
|
|
8
|
-
//
|
|
8
|
+
// 🔹 Helper: only run in browser
|
|
9
|
+
const isBrowser = () =>
|
|
10
|
+
typeof window !== "undefined" && typeof indexedDB !== "undefined";
|
|
11
|
+
|
|
12
|
+
// ✅ connect to IndexedDB
|
|
9
13
|
export const connectDB = async () => {
|
|
10
|
-
|
|
14
|
+
// 🚫 Ignore during SSR / Node
|
|
15
|
+
if (!isBrowser()) return null;
|
|
16
|
+
|
|
17
|
+
// 🔥 wait for configureIdb() to be called
|
|
18
|
+
await configReady;
|
|
19
|
+
|
|
20
|
+
const { dbName, dbVersion, storeName } = getConfig();
|
|
11
21
|
|
|
12
22
|
if (!manager) {
|
|
13
23
|
manager = new DBManager(dbName, dbVersion);
|
|
14
24
|
}
|
|
15
25
|
|
|
16
26
|
if (!db) {
|
|
17
|
-
db = await manager.connect([{ name: storeName }]);
|
|
27
|
+
db = await manager.connect([{ name: storeName }]);
|
|
18
28
|
}
|
|
19
29
|
|
|
20
30
|
return db;
|
|
21
31
|
};
|
|
22
32
|
|
|
33
|
+
// ✅ Return usable store object
|
|
23
34
|
export const getIdbStore = async () => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
// 🚫 Ignore during SSR / Node
|
|
36
|
+
if (!isBrowser()) return null;
|
|
37
|
+
|
|
38
|
+
// already created? return cached store
|
|
39
|
+
if (store) return store;
|
|
40
|
+
|
|
41
|
+
// 🔥 wait for configureIdb()
|
|
42
|
+
await configReady;
|
|
43
|
+
|
|
44
|
+
const { storeName } = getConfig();
|
|
45
|
+
|
|
46
|
+
const database = await connectDB();
|
|
47
|
+
|
|
48
|
+
store = {
|
|
49
|
+
get: (key) => DB.get(database, storeName, key),
|
|
50
|
+
|
|
51
|
+
getWithExpiry: (key, ttl, api) =>
|
|
52
|
+
DB.getWithExpiry(database, storeName, key, ttl, api),
|
|
53
|
+
|
|
54
|
+
put: (data) => DB.put(database, storeName, data),
|
|
55
|
+
|
|
56
|
+
remove: (key) => DB.remove(database, storeName, key),
|
|
57
|
+
|
|
58
|
+
clear: () => DB.clear(database, storeName),
|
|
59
|
+
};
|
|
37
60
|
|
|
38
61
|
return store;
|
|
39
|
-
};
|
|
62
|
+
};
|