/
indexed-db-configuration.js
270 lines (248 loc) · 7.26 KB
/
indexed-db-configuration.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
import Service from '@ember/service';
import { typeOf as getTypeOf, isNone } from '@ember/utils';
import { A as array } from '@ember/array';
import { assert } from '@ember/debug';
/**
* This service should be overwritten to configure IndexedDB.
* Overwrite the `mapTable` property & add `versionX` properties to fit your application.
*
* @module Services
* @class IndexedDbConfiguration
* @extends Ember.Service
* @public
*/
export default class IndexedDbConfigurationService extends Service {
/**
* Increment this whenever you do a new database version.
* Set it to 1 on your initial version.
*
* For every version, you should provide a `versionX` property.
* Each of these properties should be an object with "stores" and/or "upgrade" properties.
*
* stores should be an object where the keys are dasherized, singular model names (e.g. task-item),
* and the value is a string with the indexedable fields.
* See https://github.com/dfahlander/Dexie.js/wiki/Version.stores() for detailed options.
*
* upgrade is a function that gets a transaction as parameter, which can be used to run database migrations.
* See https://github.com/dfahlander/Dexie.js/wiki/Version.upgrade() for detailed options/examples.
*
* Note that in newer versions of Dexie, you do not need to keep old version definitions anymnore, unless they contain upgrade instructions.
* Instead, each version has to contain the full, current schema (not just the updates to the last version).
*
* An example would be:
*
* ```js
* // You can delete this safely when adding version2, as it does not contain an upgrade
* version1 = {
* stores: {
* 'task': '&id*,isRead',
* 'task-item': '&id'
* }
* },
*
* version2 = {
* stores: {
* 'task': '&id*,isRead',
* 'task-item': '&id,*isNew'
* }
* upgrade: (transaction) => {
* transaction['task-item'].each((taskItem, cursor) => {
taskItem.isNew = 0;
cursor.update(taskItem);
});
* }
* }
* ```
*
* The
* You can also use upgrade/store without using the other option.
*
* @property currentVersion
* @type {Number}
* @public
*/
currentVersion = 0;
/**
* The map functions for the tables.
* This should be an object with one key per table
* where the value is a function that takes an object and returns an object to save in IndexedDB.
*
* This object NEEDS to contain at least the properties id & json.
* It should also contain one property per queryable property on the database table.
*
* There are a few things to note here:
*
* 1. Always convert your IDs to strings. You can use the provided `this._toString(val)` function for this.
* 2. Always clean up your json. You can use the provided `this._cleanObject(item)` for this.
* 3. IndexedDB doesn't work with boolean queries. You need to convert booleans to 1/0 when inserting it into the Database.
* You can use the provided `this._toZeroOne(val)` for this.
*
* For example, the following table config:
*
* ```js
* {
* task: '++id,isRead,status,[isRead+status]'
* }
* ```
*
* should look something like this:
*
* ```js
* return {
* task: (item) => {
* return {
* id: this._toString(item.id),
* json: this._cleanObject(item),
* isRead: this._toZeroOne(item.attributes.isRead),
* status: item.attributes.status
* };
* }
* };
* ```
*
* Note that if you do not specify anything here, it will default to
*
* ```js
* return {
* id: this._toString(item.id),
* json: this._cleanObject(item)
* };
* ```
*
* @property mapTable
* @type {Object}
* @protected
*/
mapTable = {};
/**
* Map a payload to a database table.
* This will use the function provided in mapTable to get a payload to insert into IndexedDB.
* Returns null if no map function is found for the type.
*
* @method mapItem
* @param {String} type The type of object to map
* @param {Object} item The data to map
* @return {Object}
* @public
*/
mapItem(type, item) {
let tables = this.mapTable;
let mapFunc = tables[type];
if (!item) {
return null;
}
if (!mapFunc) {
return {
id: this._toString(item.id),
json: this._cleanObject(item),
};
}
return mapFunc(item);
}
/**
* Setup the database and do all necessary database migrations.
*
* @method setupDatabase
* @param {Dexie} db
* @return {Dexie}
* @public
*/
setupDatabase(db) {
let { currentVersion } = this;
assert(
'You need to override services/indexed-db-configuration.js and provide at least one version.',
currentVersion
);
for (let v = 1; v <= currentVersion; v++) {
let versionName = `version${v}`;
let versionDefinition = this[versionName];
if (!versionDefinition) {
continue;
}
let { stores, upgrade } = versionDefinition;
if (stores && upgrade) {
db.version(v).stores(stores).upgrade(upgrade);
} else if (stores) {
db.version(v).stores(stores);
} else if (upgrade) {
db.version(v).upgrade(upgrade);
}
}
return db;
}
/**
* Cleanup a json object.
* This will convert array-like structures to actual arrays for saving.
* It will strip out meta properties etc.
*
* @method _cleanObject
* @param {Object} data
* @return {{id, type, attributes: {}, relationships: {}}}
* @private
*/
_cleanObject(data) {
if (!data) {
return null;
}
let { id, type, attributes = {}, relationships = {} } = data;
let obj = {
id,
type,
attributes: {},
relationships: {},
};
let isArray = (item) => {
return (
getTypeOf(item) === 'array' ||
(getTypeOf(item) === 'instance' &&
getTypeOf(item.toArray) === 'function')
);
};
Object.keys(attributes).forEach((prop) => {
// Convert array-like structures to real arrays
if (isArray(attributes[prop])) {
obj.attributes[prop] = array(attributes[prop]).toArray();
} else {
obj.attributes[prop] = attributes[prop];
}
});
Object.keys(relationships).forEach((prop) => {
if (isArray(relationships[prop].data)) {
obj.relationships[prop] = {
data: array(relationships[prop].data).toArray(),
};
} else {
obj.relationships[prop] = relationships[prop];
}
});
return obj;
}
/**
* Convert a property to a string.
*
* @method _toString
* @param {Mixed} val
* @return {String}
* @private
*/
_toString(val) {
return `${val}`;
}
/**
* Convert a boolean to 1/0.
* Optionally, you can specify the value that should be used if the given value does not exist in the payload.
* For example, if you want that a given value should be 1 if not found in the payload, use `this._toZeroOne(value, 1)`.
*
* @method _toZeroOne
* @param {Mixed} val
* @param {0|1} noneValue The value to use if val is null/undefined.
* @return {1|0}
* @private
*/
_toZeroOne(val, noneValue = 0) {
if (isNone(val)) {
return noneValue;
}
return val ? 1 : 0;
}
}