📄 src/models/indexdb/common.ts
D-OPEN SOVEREIGN
1/*
2 * Copyright (c) 2026 DataPond D-Library Pty Ltd. ("The Sanctuary")
3 * Non-Profit Public Library — The World Library
4 * 
5 * D-SAFE Certification issued by POND Enterprise.
6 * Published under the D-Open Code Sovereign Licence v1.0 framework
7 * DataPond D-Library All exclusive Right reserved on modifiying the code - CC Attribution to data pond, Non modifiable, Non Commercial.
8 * https://registry.world.bibliotech.com/licence
9 * Source code donated to DataPond D-Library by it's author: data pond.
10 * 
11 * Technical Guardian ("The Shield"): Pond Enterprise Pty Ltd. (ACN 694 747 987)
12 * All liability claims about D-SAFE certification issues coming from the published D-Safe direction must be addressed with Pond Enterprise Pty Ltd.
13 * Code Modification automatically voids the certified liability protection provided by Pond Enterprise.
14 */
15import { proResolver } from "../promises.ts";
16export const version = 4;
17export const dbName = `the_world_library`
18
19let dbDeferred = proResolver<IDBDatabase>()
20export const patchStoreName = `patches`
21export const jsonStoreName = `catalog_json`
22export const keyValStoreName = 'key_value'
23export const pdfDataStoreName = "pdf_data";
24export const deviceStateStoreName = "deviceState";
25export const localRecordsStoreName = "localRecords";
26export const instanceRemapsStoreName = "instanceRemaps";
27export const Db = (): Promise<IDBDatabase> => {
28    if (dbDeferred.isStarted()) {
29        return dbDeferred.pro
30    }
31    dbDeferred.start();
32
33    const request = indexedDB.open(dbName, version);
34    request.onupgradeneeded = function () {
35        const db = request.result;
36        if (!db.objectStoreNames.contains(jsonStoreName))
37            db.createObjectStore(jsonStoreName, { keyPath: "id" });
38        if (!db.objectStoreNames.contains(keyValStoreName))
39            db.createObjectStore(keyValStoreName, { keyPath: "key" });
40        if (!db.objectStoreNames.contains(pdfDataStoreName))
41            db.createObjectStore(pdfDataStoreName, { keyPath: "fileId" });
42        if (!db.objectStoreNames.contains(patchStoreName)) {
43            const patchStore = db.createObjectStore(patchStoreName, { keyPath: ["username", "pk"] });
44            patchStore.createIndex("username", "username", { unique: false });
45        }
46        if (!db.objectStoreNames.contains(deviceStateStoreName)) {
47            db.createObjectStore(deviceStateStoreName);
48        }
49        if (!db.objectStoreNames.contains(localRecordsStoreName)) {
50            const localStore = db.createObjectStore(localRecordsStoreName, { keyPath: ["creatorUsername", "wireId"] });
51            localStore.createIndex("byCreator", "creatorUsername", { unique: false });
52        }
53        if (!db.objectStoreNames.contains(instanceRemapsStoreName)) {
54            db.createObjectStore(instanceRemapsStoreName, { keyPath: ["creatorUsername", "objectId", "langId", "originalIndex"] });
55        }
56        console.log('[IDB] models_v2 schema upgraded to version', db.version);
57    };
58
59    request.onerror = function (event) {
60        dbDeferred.reject(event)
61    };
62    request.onsuccess = function (event: Event) {
63        // @ts-ignore
64        dbDeferred.resolve(event.target.result)
65    };
66    return dbDeferred.pro
67}
68export const ClearDbData = async () => {
69    const db = await Db();
70    const storeNames = Array.from(db.objectStoreNames);
71    if (storeNames.length === 0) return;
72    const tx = db.transaction(storeNames, 'readwrite');
73    storeNames.forEach(store => tx.objectStore(store).clear());
74    return new Promise(resolve => tx.oncomplete = resolve);
75}
76