-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-store.js
161 lines (143 loc) · 4.83 KB
/
db-store.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import PouchDB from 'pouchdb';
import Upsert from "pouchdb-upsert";
import Find from 'pouchdb-find';
import Memory from "pouchdb-adapter-memory";
PouchDB.plugin(Upsert);
PouchDB.plugin(Find);
PouchDB.plugin(Memory);
export default class DBStore{
// each database can be sync with other general databases (see config stores.js)
PARAMS = {
live:true,
retry:true,
batch_size:100,
batches_limit:5,
since:"now"
}
// @todo support to format
constructor(name,adapter = "memory",format){
if(!name ){
throw new Error(`Error: missing mandatory name param`);
}
this.DB = new PouchDB(name,{adapter});
// to do manages the synch and modalities of PouchDB
// this._connectDB(params,this.DB);
}
async read(){
if(!this.DB){return Promise.reject("DB must be initialized first!")}
return new Promise((resolve,reject)=> {
this.DB.allDocs({
include_docs:true
}).then(
response => {
resolve(response.rows.map(e=>e.doc))
}
).catch(err => reject(err));
});
}
async save(doc){
if(!doc){
return Promise.reject(`ERROR, nothing to save`);
}
// bulk save
if(Array.isArray(doc)){return this._saveBulk(doc)}
// save
return this._save(doc);
}
async cleanUp(params={}){
// console.log("1",params.id && !this.store[section].id && this.store[section].id !== params.id);
if( params.id && this.id !== params.id ){
return Promise.resolve("nothing to do");
}
// console.log("2",params.simulation && !this.store[section].simulation && this.store[section].simulation !== params.simulation);
if( params.simulation && this.simulation !== params.simulation ){
return Promise.resolve("nothing to do");
}
// console.log("3");
return Promise.allSettled([
this.DB.destroy(),
this._disconnectDB()
]);
}
async _save(doc){
return new Promise((resolve,reject)=>{
// console.log("saving here 2",store);
if(doc.id){
return this.DB.upsert(doc.id,(doc)=>doc).then( response => {
// console.log("saved 3?",response);
resolve(response);
} )
.catch( err => {
// console.error("4",err);
reject(err);
} );
}
// fallback no id provided
this.DB.post(doc)
.then( response => {
// console.log("saved 3?",response);
resolve(response);
} )
.catch( err => {
// console.error("4",err);
reject(err);
} );
});
}
async _saveBulk(docs){
// console.log(docs);
return new Promise((resolve,reject)=>{
// console.log("saving here 2",store);
this.DB.bulkDocs(docs)
.then(res=>{
// console.log("bulk result?",res);
resolve(res)
})
.catch(err=>reject(err));
});
}
_connectDB(params,db){
let path = null, operation = null;
if(params.to){
path = params.to;
operation = "to";
} else if(params.from){
path = params.from;
operation = "from";
} else if(params.sync){
path = params.sync;
operation = "sync";
}
// check if db exists or created it
if(path) {
path = path.concat(this._dbName(params));
let linkedDB = new PouchDB(path);
let repParams = params.repParams ? params.repParams : {};
// console.log("-----",db.name,operation,path);
// connect
let handler = null;
switch (operation){
case "to":
handler = db.replicate.to(linkedDB,repParams);
break;
case "from":
handler = db.replicate.from(linkedDB,repParams);
break;
default:
handler = db.sync(linkedDB,repParams);
}
// save handler to cancel replication later
this.replicationHandler = handler;
}
}
async _disconnectDB(){
return new Promise((resolve,reject)=>{
if(this.replicationHandler){
this.replicationHandler.cancel()
.on("complete", (res)=>resolve(res))
.on("error",(err)=>reject(err))
}
resolve("nothing to do");
})
}
}