mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-01-07 05:49:17 +00:00
- (Beta) ignore files handling Fixed: - Buttons on lock-detected-dialogue now can be shown in narrow-width devices. Improved: - Some constant has been flattened to be evaluated. - The usage of the deprecated API of obsidian has been reduced. - Now the indexedDB adapter will be enabled while the importing configuration. Misc: - Compiler, framework, and dependencies have been upgraded. - Due to standing for these impacts (especially in esbuild and svelte,) terser has been introduced. Feel free to notify your opinion to me! I do not like to obfuscate the code too.
53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
import { deleteDB, type IDBPDatabase, openDB } from "idb";
|
|
export interface KeyValueDatabase {
|
|
get<T>(key: string): Promise<T>;
|
|
set<T>(key: string, value: T): Promise<IDBValidKey>;
|
|
del(key: string): Promise<void>;
|
|
clear(): Promise<void>;
|
|
keys(query?: IDBValidKey | IDBKeyRange, count?: number): Promise<IDBValidKey[]>;
|
|
close(): void;
|
|
destroy(): void;
|
|
}
|
|
const databaseCache: { [key: string]: IDBPDatabase<any> } = {};
|
|
export const OpenKeyValueDatabase = async (dbKey: string): Promise<KeyValueDatabase> => {
|
|
if (dbKey in databaseCache) {
|
|
databaseCache[dbKey].close();
|
|
delete databaseCache[dbKey];
|
|
}
|
|
const storeKey = dbKey;
|
|
const dbPromise = openDB(dbKey, 1, {
|
|
upgrade(db) {
|
|
db.createObjectStore(storeKey);
|
|
},
|
|
});
|
|
let db: IDBPDatabase<any> = null;
|
|
db = await dbPromise;
|
|
databaseCache[dbKey] = db;
|
|
return {
|
|
get<T>(key: string): Promise<T> {
|
|
return db.get(storeKey, key);
|
|
},
|
|
set<T>(key: string, value: T) {
|
|
return db.put(storeKey, value, key);
|
|
},
|
|
del(key: string) {
|
|
return db.delete(storeKey, key);
|
|
},
|
|
clear() {
|
|
return db.clear(storeKey);
|
|
},
|
|
keys(query?: IDBValidKey | IDBKeyRange, count?: number) {
|
|
return db.getAllKeys(storeKey, query, count);
|
|
},
|
|
close() {
|
|
delete databaseCache[dbKey];
|
|
return db.close();
|
|
},
|
|
async destroy() {
|
|
delete databaseCache[dbKey];
|
|
db.close();
|
|
await deleteDB(dbKey);
|
|
},
|
|
};
|
|
};
|