-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocaldb.js
More file actions
100 lines (81 loc) · 2.27 KB
/
localdb.js
File metadata and controls
100 lines (81 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// localdb.js — localforage (IndexedDB) wrapper for Secure WebDAV Images v2.0.0
// Stores prevSyncRecords (Entity snapshots) and plugin metadata.
"use strict";
class LocalDB {
constructor() {
this._store = null;
this._ready = false;
}
async ready() {
if (this._ready) return;
// localforage is loaded as a global by the Obsidian plugin environment,
// or via require in Node.js test environment.
const lf = typeof localforage !== "undefined" ? localforage : require("localforage");
this._store = lf.createInstance({ name: "SecureWebdavImages" });
this._ready = true;
}
// ── PrevSyncRecords (Entity snapshots) ──
async getPrevSyncRecord(key) {
await this.ready();
return this._store.getItem(this._prevKey(key));
}
async setPrevSyncRecord(key, entity) {
await this.ready();
return this._store.setItem(this._prevKey(key), entity);
}
async deletePrevSyncRecord(key) {
await this.ready();
return this._store.removeItem(this._prevKey(key));
}
async getAllPrevSyncRecords() {
await this.ready();
const result = new Map();
await this._store.iterate((value, key) => {
if (key.startsWith("prev:")) {
result.set(key.slice(5), value);
}
});
return result;
}
async clearPrevSyncRecords() {
await this.ready();
const keys = await this._prevKeys();
for (const key of keys) {
await this._store.removeItem(key);
}
}
// ── Plugin Metadata ──
async getPluginMeta(key) {
await this.ready();
return this._store.getItem(this._metaKey(key));
}
async setPluginMeta(key, value) {
await this.ready();
return this._store.setItem(this._metaKey(key), value);
}
async deletePluginMeta(key) {
await this.ready();
return this._store.removeItem(this._metaKey(key));
}
// ── Internal helpers ──
_prevKey(key) {
return `prev:${key}`;
}
_metaKey(key) {
return `meta:${key}`;
}
async _prevKeys() {
const keys = [];
await this._store.iterate((_, key) => {
if (key.startsWith("prev:")) {
keys.push(key);
}
});
return keys;
}
}
// Singleton
const localDB = new LocalDB();
if (typeof module !== "undefined" && module.exports) {
module.exports = { LocalDB, localDB };
}