๐Ÿ“„ src/models/indexdb/file_storage.ts
D-OPEN SOVEREIGN

Documentation & Insights

Developer-friendly namespace bundling the file-storage ops. Replaces v1's
`files` import โ€” mechanical swap `files.x` โ†’ `fileStore.x`:
  `fileStore.has(id)` ยท `fileStore.load(id)` ยท `fileStore.store(record)`
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'
16import type {FileRecord} from "../interfaces.ts";
17import {Db} from "./common";
18import {jsonStoreName} from "./common";
19import {DateUtils} from "../dates.ts";
20
21
22const storeName= jsonStoreName;
23
24export const hasFile = (id:string): Promise<boolean> => {
25    const deferred = proResolver<boolean>()
26    Db().then(db => {
27        try {
28            const tx = db.transaction(storeName, "readonly");
29            const req = tx.objectStore(storeName).count(id);
30            req.onsuccess = function (e) {
31                // @ts-ignore
32                const count = e.target.result;
33                if (count > 0) { // key already exist
34                    deferred.resolve(true)
35                } else { // key not exist
36                    deferred.resolve(false)
37                }
38            };
39            req.onerror = function (e) {
40                console.error("worker: error", e.target)
41                deferred.reject(e.target)
42            }
43        } catch(e) {
44            console.error(e);
45            debugger
46        }
47    }).catch(e => {
48        console.error(`[indexdb error:] hasFile(): ${e.message}`)
49        deferred.reject(e)
50    });
51    return deferred.pro
52}
53
54export const loadFile = (id: string) : Promise<FileRecord | undefined> => {
55    const deferred = proResolver<FileRecord | undefined>()
56    Db().then(db => {
57        const tx = db.transaction(storeName, "readonly");
58        let result: FileRecord | undefined = undefined;
59        tx.objectStore(storeName).get(id).addEventListener("success", function (event) {
60            // @ts-ignore
61            result = event.target.result;
62            deferred.resolve(result);
63        });
64        tx.onerror = function (event) {
65            console.error("worker: error", event.target)
66            deferred.reject(event.target)
67        };
68    }).catch(deferred.reject)
69
70    return deferred.pro
71}
72
73export const storeFile = async (record: FileRecord) : Promise<boolean> => {
74    const deferred = proResolver<boolean>()
75    const db = await Db()
76    const has = await hasFile(record.id)
77    const tx = db.transaction(storeName, "readwrite");
78    if (has) {
79        // @ts-ignore
80        tx.objectStore(storeName).put(record).onsuccess = function () {
81            // console.log(`worker: File stored successfully - onsuccess`)
82        };
83    } else {
84        // @ts-ignore
85        tx.objectStore(storeName).add(record).onsuccess = function () {
86            // console.log(`worker: File stored successfully - onsuccess`)
87        };
88    }
89
90    tx.oncomplete = function () {
91        deferred.resolve(true)
92    };
93    tx.onerror = function (event) {
94        console.error("worker: error", event.target)
95        deferred.reject(event.target)
96    };
97    return deferred.pro
98}
99
100/**
101 * Developer-friendly namespace bundling the file-storage ops. Replaces v1's
102 * `files` import โ€” mechanical swap `files.x` โ†’ `fileStore.x`:
103 *   `fileStore.has(id)` ยท `fileStore.load(id)` ยท `fileStore.store(record)`
104 */
105export const fileStore = { has: hasFile, load: loadFile, store: storeFile };
106