Files
notesnook/apps/mobile/app/common/database/storage.ts

151 lines
3.7 KiB
TypeScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-12-13 14:07:15 +05:00
import { IStorage } from "@notesnook/core";
import { MMKVInstance } from "react-native-mmkv-storage";
2022-02-28 13:48:59 +05:00
import {
decrypt,
decryptMulti,
2022-02-28 13:48:59 +05:00
deriveCryptoKey,
2024-12-13 14:07:15 +05:00
deriveCryptoKeyFallback,
2022-02-28 13:48:59 +05:00
encrypt,
encryptMulti,
2022-02-28 13:48:59 +05:00
generateCryptoKey,
getCryptoKey,
2024-12-13 14:07:15 +05:00
hash
} from "./encryption";
import { MMKV } from "./mmkv";
2022-02-28 13:48:59 +05:00
2022-07-19 13:36:45 +05:00
export class KV {
2024-12-13 14:07:15 +05:00
storage: MMKVInstance;
constructor(storage: MMKVInstance) {
2022-07-19 13:36:45 +05:00
this.storage = storage;
}
2024-12-13 14:07:15 +05:00
async read<T>(key: string, isArray?: boolean) {
if (!key) return undefined;
const data = this.storage.getString(key);
if (!data) return undefined;
2022-07-19 13:36:45 +05:00
try {
2024-12-13 14:07:15 +05:00
return JSON.parse(data) as T;
2022-07-19 13:36:45 +05:00
} catch (e) {
2024-12-13 14:07:15 +05:00
return data as T;
2022-07-19 13:36:45 +05:00
}
2022-02-28 13:48:59 +05:00
}
2024-12-13 14:07:15 +05:00
async write<T>(key: string, data: T) {
this.storage.setString(
key,
typeof data === "string" ? data : JSON.stringify(data)
);
2022-07-19 13:36:45 +05:00
}
2022-02-28 13:48:59 +05:00
2024-12-13 14:07:15 +05:00
async readMulti<T>(keys: string[]) {
2022-07-19 13:36:45 +05:00
if (keys.length <= 0) {
return [];
} else {
2023-08-26 12:02:13 +05:00
try {
2024-12-13 14:07:15 +05:00
const data = await this.storage.getMultipleItemsAsync<any>(
2023-08-26 12:02:13 +05:00
keys.slice(),
2023-08-30 15:48:12 +05:00
"string"
2023-08-26 12:02:13 +05:00
);
return data.map(([key, value]) => {
let obj;
try {
obj = JSON.parse(value);
} catch (e) {
obj = value;
}
return [key, obj];
2024-12-13 14:07:15 +05:00
}) as [string, T][];
2023-08-26 12:02:13 +05:00
} catch (e) {
2024-12-13 14:07:15 +05:00
return [];
2023-08-26 12:02:13 +05:00
}
2022-07-19 13:36:45 +05:00
}
2022-02-28 13:48:59 +05:00
}
2024-12-13 14:07:15 +05:00
async remove(key: string) {
this.storage.removeItem(key);
2022-07-19 13:36:45 +05:00
}
2022-02-28 13:48:59 +05:00
2024-12-13 14:07:15 +05:00
async removeMulti(keys: string[]) {
if (!keys) return;
this.storage.removeItems(keys);
}
2022-07-19 13:36:45 +05:00
async clear() {
2024-12-13 14:07:15 +05:00
this.storage.clearStore();
2022-07-19 13:36:45 +05:00
}
2022-02-28 13:48:59 +05:00
2022-07-19 13:36:45 +05:00
async getAllKeys() {
let keys = (await this.storage.indexer.getKeys()) || [];
keys = keys.filter(
(k) =>
k !== "stringIndex" &&
k !== "boolIndex" &&
k !== "mapIndex" &&
k !== "arrayIndex" &&
k !== "numberIndex" &&
2022-07-19 13:36:45 +05:00
k !== this.storage.instanceID
);
return keys;
}
2023-08-26 12:02:13 +05:00
2024-12-13 14:07:15 +05:00
async writeMulti(items: [string, any][]) {
await this.storage.setMultipleItemsAsync(items, "object");
2023-08-26 12:02:13 +05:00
}
2022-02-28 13:48:59 +05:00
}
2022-07-19 13:36:45 +05:00
const DefaultStorage = new KV(MMKV);
2024-12-13 14:07:15 +05:00
export const Storage: IStorage = {
write<T>(key: string, data: T): Promise<void> {
return DefaultStorage.write(key, data);
},
writeMulti<T>(entries: [string, T][]): Promise<void> {
return DefaultStorage.writeMulti(entries);
},
readMulti<T>(keys: string[]): Promise<[string, T][]> {
return DefaultStorage.readMulti(keys);
},
read<T>(key: string, isArray?: boolean): Promise<T | undefined> {
return DefaultStorage.read(key, isArray);
},
remove(key: string): Promise<void> {
return DefaultStorage.remove(key);
},
removeMulti(keys: string[]): Promise<void> {
return DefaultStorage.removeMulti(keys);
},
clear(): Promise<void> {
return DefaultStorage.clear();
},
getAllKeys(): Promise<string[]> {
return DefaultStorage.getAllKeys();
},
hash,
getCryptoKey,
2022-02-28 13:48:59 +05:00
encrypt,
2024-12-13 14:07:15 +05:00
encryptMulti,
2022-02-28 13:48:59 +05:00
decrypt,
2023-08-26 12:02:13 +05:00
decryptMulti,
2022-02-28 13:48:59 +05:00
deriveCryptoKey,
2023-09-04 13:15:24 +05:00
generateCryptoKey,
2024-12-13 14:07:15 +05:00
deriveCryptoKeyFallback
2022-02-28 13:48:59 +05:00
};