This service should be overwritten to configure IndexedDB.
Overwrite the mapTable
property & add versionX
properties to fit your application.
Properties
-
data
Cleanup a json object. This will convert array-like structures to actual arrays for saving. It will strip out meta properties etc.
Parameters:
-
data
Object
Returns:
, relationships: {}}}
-
val
-
noneValue
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)
.
Parameters:
-
val
Mixed -
noneValue
0 | 1The value to use if val is null/undefined.
Returns:
-
type
-
item
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.
Returns:
currentVersion
Number publicIncrement 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:
// 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.
mapTable
Object protectedThe 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:
- Always convert your IDs to strings. You can use the provided
this._toString(val)
function for this. - Always clean up your json. You can use the provided
this._cleanObject(item)
for this. - 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:
{
task: '++id,isRead,status,[isRead+status]'
}
should look something like this:
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
return {
id: this._toString(item.id),
json: this._cleanObject(item)
};