-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (60 loc) · 2.15 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
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
// Store
import STORES from "../configuration/Store/index.js";
import FileStore from './file-store.js';
import DBStore from './db-store.js';
// each instance of Store, create its own database
export default class Store{
constructor(params = {id,simulation,type}){
if(!params.type || !STORES.has(params.type)){
throw new Error(`Error: unknown store ${type}`);
}
if(!params.id&&!params.simulation){
throw new Error(`Error: id or simulation mandatory!`);
}
this.type = params.type;
this.simulation = params.simulation;
this.id = params.id;
this.name = this._generateName(params);
this.mode = STORES.get(params.type).mode;
this.format = STORES.get(params.type).format;
// console.log("store params",this.name,this.type,this.mode,this.format);
switch(this.mode){
case "file":
console.log("instantiating file mode",this.format);
this._DB = new FileStore(this.name,this.format);
break;
default:
this._DB = new DBStore(this.name,this.mode,this.format);
}
}
get DB(){return this._DB;}
async read(params){
if(!this.DB){return Promise.reject("DB must be initialized first!")}
// @todo what from where?
return this.DB.read();
}
async save(doc){
if(!doc){
return Promise.reject(`ERROR, nothing to save`);
}
// @todo array of adapters
// mapping between adapters and output formats
// e.g. FHIR > file, default > db
return this.DB.save(doc);
}
async cleanUp(params={}){
// @todo cleanup of what adapter?
return this.DB.cleanUp(params);
}
_generateName({name = "", id, simulation}){
// console.log("_generateName, params",name,id,simulation);
// console.log("store type",type," with id ",id);
if(simulation){
name = name.toString().concat("-",simulation);
} else if(id){
name = name.toString().concat("-",id);
}
console.log("_generateName, name",name);
return name
}
}