-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
39 lines (36 loc) · 1.54 KB
/
index.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
//==============================================================================
// ■ 0db (0db/index.js)
//------------------------------------------------------------------------------
// Main entry point.
//==============================================================================
const fileProvider = require("./core/file");
const collectionProvider = require("./core/collection");
const crudProvider = require("./core/crud/");
const { isEmpty } = require("./utils/type");
//------------------------------------------------------------------------------
// ► Exports
//------------------------------------------------------------------------------
module.exports = odb;
//------------------------------------------------------------------------------
// ● Odb-Main
//------------------------------------------------------------------------------
async function odb(filePath = "./db.json") {
const file = fileProvider(filePath);
let dataObj = await file.load();
if (isEmpty(dataObj)) await file.save(dataObj);
const db = (collectionName) => dbApi(collectionName, dataObj, file.save);
db.file = file;
db.dataObj = dataObj;
db.drop = async () => {
dataObj = {};
file.save(dataObj);
};
return db;
}
//------------------------------------------------------------------------------
// ● Db-Api
//------------------------------------------------------------------------------
function dbApi(collectionName, dataObj, save) {
const collection = collectionProvider(collectionName, dataObj, save);
return crudProvider(collection);
}