-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrapon-db.js
295 lines (281 loc) · 9.14 KB
/
strapon-db.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
export class Database {
constructor(dbName, schema) {
this.schema = schema
this._caches = {}
this._fullyLoaded = {}
this._dbp = new Promise((resolve, reject) => {
let openreq = indexedDB.open(dbName, schema.getVersion())
openreq.onerror = () => reject(openreq.error)
openreq.onsuccess = () => {
schema.createFunctions(this)
resolve(openreq.result)
}
openreq.onupgradeneeded = (event) => {
// First time setup: create an empty object store
schema.upgrade(openreq.result, event.oldVersion)
}
})
}
ready() {
return this._dbp
}
dump() {
let data = {}, promises=[];
return this._dbp.then(db => {
let names = db.objectStoreNames, len = db.objectStoreNames.length;
for (let i=0;i<len;i++) {
let store = names[i];
promises.push(this.getAll(store).then(rows => data[store] = rows))
}
return Promise.all(promises).then(x => data)
});
}
_cacheOf(store) {
if (!this._caches.hasOwnProperty(store)) {
this._caches[store] = {}
}
return this._caches[store]
}
_wrap(store, action, type, ...args) {
return this._dbp.then(db => new Promise((resolve, reject) => {
let transaction = db.transaction(store, type)
let request = transaction.objectStore(store)[action](...args)
transaction.oncomplete = () => resolve(request.result)
transaction.onabort = transaction.onerror = () => reject(transaction.error)
}))
}
put(store, record) {
return this._wrap(store, 'put', 'readwrite', record).then(id => {
record.id = id
this._cacheOf(store)[id] = record
return record
})
}
del(store, record) {
return this._wrap(store, 'delete', 'readwrite', record.id).then(id => {
delete this._cacheOf(store)[record.id]
})
}
get(store, id) {
let record = this._cacheOf(store)[id]
if (record == undefined) {
return this._wrap(store, 'get', undefined, id).then(record => {
this._cacheOf(store)[id] = record
return record
})
} else {
return Promise.resolve(record)
}
}
getAll(store) {
if (this._fullyLoaded[store]) {
return Promise.resolve(Object.values(this._cacheOf(store)))
} else {
return this._wrap(store, 'getAll').then(records => {
let cache = this._cacheOf(store)
this._fullyLoaded[store] = true
records.map(record => cache[record.id] = record)
return records
})
}
}
_criteriaMatch(record, criteria) {
for (let key in criteria) {
if (record[key] !== criteria[key]) {
return false
}
}
return true
}
_fetchOne(store, criteria) {
// UNTESTED
//Todo: add query caching
return this._dbp.then(db => new Promise((resolve, reject) => {
let records = []
let cursorTrans = db.transaction(store).objectStore(store).openCursor()
cursorTrans.onerror = error => c.log(error)
cursorTrans.onsuccess = event => {
var cursor = event.target.result
if (cursor) {
let record = cursor.value
if (this._criteriaMatch(record, criteria)) {
records.push(record)
} else {
cursor.continue()
}
}
else {
resolve(records)
}
}
}))
}
filter(store, criteria) {
//Todo: add query caching
return this._dbp.then(db => new Promise((resolve, reject) => {
let records = []
let cursorTrans = db.transaction(store).objectStore(store).openCursor()
cursorTrans.onerror = error => c.log(error)
cursorTrans.onsuccess = event => {
var cursor = event.target.result
if (cursor) {
let record = cursor.value
if (this._criteriaMatch(record, criteria)) {
records.push(record)
}
cursor.continue();
}
else {
resolve(records)
}
}
}))
}
getParent(childStore, parentStore, child) {
let fkName = this.schema.getFkName(parentStore)
let parentId = child[fkName]
if (parentId == undefined ) {
return Promise.resolve(undefined)
}
return this.get(parentStore, parentId)
}
getLinked(storeName, store1, store2Record) {
}
getChildren(parentStore, childStore, parentRecord) {
//Todo : cache
return this._dbp.then(db => new Promise((resolve, reject) => {
let transaction = db.transaction(childStore)
let request = transaction.objectStore(childStore).index(parentStore).get(parentRecord.id)
transaction.oncomplete = () => resolve(request.result)
transaction.onabort = transaction.onerror = () => reject(transaction.error)
}))
}
setParent(childStore, parentStore, childRecord, parentRecord) {
let fkName = this.schema.getFkName(parentStore)
childRecord[fkName] = parentRecord.id
return this.put(childStore, childRecord)
}
link(store1, store2, store1Record, store2Record) {
let storeName = this.schema.getLinkStoreName(store1, store2);
let record = {}
record[this.schema.getFkName(store1)] = store1Record.id;
record[this.schema.getFkName(store2)] = store2Record.id;
return this.put(storeName, record)
}
}
/*
May not want to load everything in memory, e.g. child objects.
But once a specific query has been called, e.g. getChildren of x, then so long as all other changes are cached.
Todo:
Make a generic backend agnostic CachedDatabase on which we must implement a wrap method
*/
export class Schema {
constructor(defaultConf={keyPath: "id", autoIncrement: true}) {
this.defaultConf = defaultConf
this._versions = []
}
addVersion(fn) {
this._versions.push(fn)
}
getVersion() {
return this._versions.length + 1
}
upgrade(idb, oldVersion) {
let schemaUpgrader = new SchemaUpgrader(this, idb, this.defaultConf)
this._versions.forEach((fn, version) => {
if (version >= oldVersion) {
fn(schemaUpgrader, true)
}
})
}
createFunctions(target) {
let schemaFunctionBuilder = new SchemaFunctionBuilder(this, target)
this._versions.forEach((fn, version) => {
fn(schemaFunctionBuilder, false)
})
}
getFkName(parentStore) {
return `__${parentStore}Id`
}
getLinkStoreName(store1, store2) {
return `m2m__${store1}__${store2}`
}
}
class SchemaFunctionBuilder {
constructor(schema, target) {
this.schema = schema
this.target = target
}
capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
addStore(name) {
let capitalizedName = this.capitalize(name);
['put', 'del', 'get', 'getAll'].forEach(method => {
this.target[method + capitalizedName] = function(arg) {
return this[method](name, arg)
}
})
}
oneToMany(parentStore, childStore) {
let parentCaps = this.capitalize(parentStore);
let childCaps = this.capitalize(childStore);
let pluralChildren = childCaps + 's'; //TODO: allow override in opts.
//Get parent as getChildParent(child)
this.target['get' + childCaps + parentCaps] = function(childRecord) {
return this.getParent(childStore, parentStore, childRecord)
}
//Get children as getParentChildren(parent)
this.target['get' + parentCaps + pluralChildren] = function(parentRecord) {
return this.getChildren(parentStore, childStore, parentRecord)
}
this.target['set' + childCaps + parentCaps] = function(childRecord, parentRecord) {
return this.setParent(childStore, parentStore, childRecord, parentRecord)
}
}
manyToMany(store1, store2) {
let storeName = this.schema.getLinkStoreName(store1, store2);
let store1Caps = this.capitalize(store1);
let store2Caps = this.capitalize(store2);
let pluralStore1 = store1Caps + 's';
let pluralStore2 = store2Caps + 's';
this.target['get' + store1Caps + pluralStore2] = function(store1Record) {
return this.getChildren(store2, storeName, store1Record)
//return this.getLinked(storeName, store2, store1Record) //tagtask(tag)
}
this.target['get' + store2Caps + pluralStore1] = function(store2Record) {
//return this.getLinked(storeName, store1, store2Record)
}
this.target['link' + store1Caps + 'to' + store2Caps] = function(store1Record, store2Record) {
db.link(store1, store2, store1Record, store2Record)
}
this.target['link' + store2Caps + 'to' + store1Caps] = function(store2Record, store1Record) {
db.link(store1, store2, store1Record, store2Record)
}
//TODO: test above, then add unlink
}
}
class SchemaUpgrader {
constructor(schema, idb, defaultConf) {
this.schema = schema
this.idb = idb
this.stores = {}
this.defaultConf = defaultConf
}
addStore(name, conf=this.defaultConf) {
let store = this.idb.createObjectStore(name, conf)
this.stores[name] = store
return store
}
oneToMany(parent, child) {
this.stores[child].createIndex(parent, this.schema.getFkName(parent));
}
manyToMany(store1, store2) {
let store = this.idb.createObjectStore(this.schema.getLinkStoreName(store1, store2), this.defaultConf)
store.createIndex(store1, this.schema.getFkName(store1));
store.createIndex(store2, this.schema.getFkName(store2));
}
}
export function deleteIdb(dbName) {
indexedDB.deleteDatabase(dbName)
}