This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataEngine.js
80 lines (70 loc) · 2.21 KB
/
DataEngine.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const { DataFunction, DataFunctionGroup } = require('./DataFunction');
class DataType {
/**
* Used to describe file-based data storage.
* Would be used for things like JSON or YAML
*/
static FILE = 'file';
/**
* Used to describe a database engine.
* Would be used for things like MongoDB, MySQL, PostgreSQL
*/
static DB = 'database';
}
class DataEngine {
#getFunc = DataFunction.NULL;
#putFunc = DataFunction.NULL;
#delFunc = DataFunction.NULL;
#hasFunc = DataFunction.NULL;
/**
* DataEngines implement resource storage operations for ass
* @param {String} name Name of the Data Engine
* @param {DataType} type FILE or DB ('file' or 'database')
* @param {DataFunctionGroup} funcGroup The DataFunctionGroup to get DataFunction's from
*/
constructor(name, type, funcGroup) {
this.name = name;
this.type = type;
this.#getFunc = funcGroup.getFunc;
this.#putFunc = funcGroup.putFunc;
this.#delFunc = funcGroup.delFunc;
this.#hasFunc = funcGroup.hasFunc;
}
/**
* Get resource data from the DataEngine
* @param {String=} resourceId The ID of the resource to get data for. If left unspecified, will return the full set of entries
* @returns {Promise} A Promise containing a JSON Object representing the resource data, OR the full set of entries as a [key,value] array
*/
get(resourceId) {
return this.#getFunc.func.call(null, resourceId);
}
/**
* Adds a resource to the DataEngine
* @param {String} resourceId The ID of the resource to add
* @param {Object} resourceData The data for the resource
* @returns {Promise}
*/
put(resourceId, resourceData) {
return this.#putFunc.func.call(null, resourceId, resourceData);
}
/**
* Deletes a resource from the DataEngine
* @param {String} resourceId The ID of the resource to delete
* @returns {Promise}
*/
del(resourceId) {
return this.#delFunc.func.call(null, resourceId);
}
/**
* Check if a resource exists in the DataEngine
* @param {String} resourceId The ID to check
* @returns {Promise} A Promise containing a boolean, true if the DataEngine DOES have the resource, false if it does not
*/
has(resourceId) {
return this.#hasFunc.func.call(null, resourceId);
}
}
module.exports = {
DataType,
DataEngine,
};