[Backends] Create if file does not exist
This commit is contained in:
parent
1840577fb5
commit
1ae2da84fe
1 changed files with 12 additions and 6 deletions
|
@ -16,9 +16,15 @@ export const createMMKVBackend = (store: string): StorageBackend => ({
|
||||||
set: (data) => MMKVManager.setItem(store, JSON.stringify(data)),
|
set: (data) => MMKVManager.setItem(store, JSON.stringify(data)),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const createFileBackend = (file: string): StorageBackend => ({
|
export const createFileBackend = (file: string): StorageBackend => {
|
||||||
|
let created: boolean;
|
||||||
|
return {
|
||||||
get: async function() {
|
get: async function() {
|
||||||
return JSON.parse((await DCDFileManager.readFile(`${DCDFileManager.getConstants().DocumentsDirPath}/${file}`, "utf8")) ?? "{}");
|
const path = `${DCDFileManager.getConstants().DocumentsDirPath}/${file}`;
|
||||||
|
if (!created && !await DCDFileManager.fileExists(path))
|
||||||
|
return (created = true, DCDFileManager.writeFile("documents", filePathFixer(file), "{}", "utf8"));
|
||||||
|
return JSON.parse((await DCDFileManager.readFile(path, "utf8")));
|
||||||
},
|
},
|
||||||
set: (data) => void DCDFileManager.writeFile("documents", filePathFixer(file), JSON.stringify(data), "utf8"),
|
set: (data) => void DCDFileManager.writeFile("documents", filePathFixer(file), JSON.stringify(data), "utf8"),
|
||||||
});
|
};
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue