-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataUtils.js
54 lines (49 loc) · 1.87 KB
/
dataUtils.js
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
import fs from "fs/promises";
const SUPPORTED_DATASTORE_VERSION = 2;
/**
* Load dataStore from hard disk or return default value if dataStore doesn't exist.
* @param {string} name - Name of dataStore to load.
* @param {object | object[]} defaultValue - Default value to return if dataStore doesn't exit.
* @returns {object | object[]} dataStore object or array.
*/
export async function loadDataStore(name, defaultValue) {
try {
const dataStore = JSON.parse(await fs.readFile(`datastore/${name}.json`, {encoding: "utf-8"}));
if(dataStore.dataStoreManifest.version == SUPPORTED_DATASTORE_VERSION) {
return dataStore;
} else {
throw new Error(`DataStore is incompatible! Version: ${dataStore.dataStoreManifest.version}; Expected: ${SUPPORTED_DATASTORE_VERSION}`)
}
}catch(e) {
return {
dataStoreManifest: {
version: 2
},
content: defaultValue
};
}
}
/**
* Save dataStore to hard disk.
* @param {string} name - Name of dataStore to save.
* @param {object | object[]} content - Content of dataStore.
*/
export async function saveDataStore(name, content) {
await fs.writeFile(`datastore/${name}.json`, JSON.stringify(content), {encoding: "utf-8"});
}
/**
* Get date string formatted as YYYY-MM-DD.
* @param {Date} [date = new Date()] - Date object, defaults to *new Date()*.
* @returns {string} Date string formatted as YYYY-MM-DD.
*/
export function getDateStr(date = new Date()) {
return date.toISOString().substring(0,10);
}
/**
* Import dataStore from datastore_import to datastore. Used to import data to docker container.
* @param {string} name - Name of dataStore to import.
* @returns {Promise<void>} File copy promise.
*/
export function importDataStore(name) {
return fs.copyFile("datastore_import/"+name+".json", "datastore/"+name+".json");
}