-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-api.js
41 lines (37 loc) · 1.61 KB
/
data-api.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
//==============================================================================
// ■ Data-API (data-api.js)
//------------------------------------------------------------------------------
// Data access and mangement.
//==============================================================================
const fs = require("fs");
const { promisify } = require("util");
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const fileName = "./data.json";
//------------------------------------------------------------------------------
// ► Exports
//------------------------------------------------------------------------------
module.exports = { get, add };
//------------------------------------------------------------------------------
// ● Load-Data
//------------------------------------------------------------------------------
async function load() {
const json = await readFile(fileName, "UTF-8");
return JSON.parse(json);
}
//------------------------------------------------------------------------------
// ● Get-Collection
//------------------------------------------------------------------------------
async function get(collectionName) {
const data = await load();
const collection = data[collectionName];
return collection;
}
//------------------------------------------------------------------------------
// ● Add-Item
//------------------------------------------------------------------------------
async function add(collectionName, item) {
const data = await load();
data[collectionName].push(item);
return await writeFile(fileName, JSON.stringify(data, null, 2));
}