Skip to content

Commit

Permalink
docs: Update docs to use ES6 class syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Jun 23, 2020
1 parent 2a68675 commit f073881
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 71 deletions.
72 changes: 35 additions & 37 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ import IndexedDbConfiguration from './services/indexed-db-configuration';
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default Ember.Route.extend({
indexedDb: service(),
export default class ApplicationRoute extends Route {
@service indexedDb;
beforeModel() {
this._super(...arguments);
return this.indexedDb.setupTask.perform();
}
});
}
```
This returns a promise that is ready once the database is setup. Note that this will reject if IndexedDB is not available - you need to handle this case accordingly.
Expand All @@ -69,16 +69,16 @@ import IndexedDbConfiguration from './services/indexed-db-configuration';
```js
import IndexedDbConfigurationService from 'ember-indexeddb/services/indexed-db-configuration';
export default IndexedDbConfigurationService.extend({
currentVersion: 1,
export default class ExtendedIndexedDbConfigurationService extends IndexedDbConfigurationService {
currentVersion = 1;
version1: {
stores: {
'model-one': '&id,*isNew',
'model-two': '&id,*status,*modelOne,[status+modelOne]'
}
}
});
}
```
Please consult the Dexie Documentation on [details about configuring your database](https://github.com/dfahlander/Dexie.js/wiki/Version.stores()).
Expand All @@ -88,6 +88,8 @@ import IndexedDbConfiguration from './services/indexed-db-configuration';
You can add as many version as you want, and Dexie will handle the upgrading for you. Note that you cannot downgrade a version. There needs to be a `versionX` property per version, starting at 1. So if you have a `currentVersion` of 3, you need to have `version1`, `version2` and `version3` properties.
You do not need to keep old versionX configurations unless they contain an upgrade.
All of these migrations are automatically run when running `this.indexedDb.setup();`.
In addition to the store configuration, you also need to define a `mapTable`.
Expand All @@ -96,25 +98,23 @@ import IndexedDbConfiguration from './services/indexed-db-configuration';
For the above example, this should look something like this:
```js
mapTable: computed(function() {
return {
'model-one': (item) => {
return {
id: this._toString(item.id),
json: this._cleanupObject(item),
isNew: this._toZeroOne(item.isNew)
};
},
'model-two': (item) => {
return {
id: this._toString(item.id),
json: this._cleanupObject(item),
modelOne: this._toString(item.relationships.modelOne?.data?.id),
status: item.attribtues.status
};
}
};
})
mapTable: {
'model-one': (item) => {
return {
id: this._toString(item.id),
json: this._cleanupObject(item),
isNew: this._toZeroOne(item.isNew)
};
},
'model-two': (item) => {
return {
id: this._toString(item.id),
json: this._cleanupObject(item),
modelOne: this._toString(item.relationships.modelOne?.data?.id),
status: item.attribtues.status
};
}
}
```
Things to note here:
Expand Down Expand Up @@ -173,7 +173,7 @@ import IndexedDbConfiguration from './services/indexed-db-configuration';
```js
import IndexedDbAdapter from 'ember-indexeddb/adapters/indexed-db';
export default IndexedDbAdapter.extend();
export default class ApplicationAdapter extends IndexedDbAdapter {}
```
The next step is to setup your database for your Ember Data models.
Expand All @@ -185,18 +185,16 @@ import IndexedDbConfiguration from './services/indexed-db-configuration';
import IndexedDbConfigurationService from 'ember-indexeddb/services/indexed-db-configuration';
import { computed, get } from '@ember/object';
export default IndexedDbConfigurationService.extend({
currentVersion: 1,
export default class ExtendedIndexedDbConfigurationService extends IndexedDbConfigurationService {
currentVersion = 1;
version1: computed(function() {
return {
stores: {
'model-a': '&id',
'model-b': '&id'
}
};
})
});
version1 = {
stores: {
'model-a': '&id',
'model-b': '&id'
}
};
}
```
Now, you can simply use the normal ember-data store with functions like `store.query('item', { isNew: true })`.
Expand Down
13 changes: 9 additions & 4 deletions addon/services/indexed-db-configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,26 @@ export default class IndexedDbConfigurationService extends Service {
*
* 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
* version1: {
* // 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: {
* version2 = {
* stores: {
* 'task': '&id*,isRead',
* 'task-item': '&id,*isNew'
* },
* }
* upgrade: (transaction) => {
* transaction['task-item'].each((taskItem, cursor) => {
taskItem.isNew = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,36 @@ import { computed } from '@ember/object';
* A full example configuration after some time of use could look like this:
*
* ```
* export default IndexedDbConfigurationService.extend({
currentVersion: 2,
version1: computed(function() {
return {
stores: {
'project': '&id',
'todo': '&id'
}
};
}),
version2: computed(function() {
return {
stores: {
'tag': '&id'
}
};
})
});
* export default class ExtendedIndexedDbConfigurationService extends IndexedDbConfigurationService {
currentVersion = 2;
version1: {
stores: {
project: '&id',
todo: '&id'
}
},
version2: {
stores: {
project: '&id',
todo: '&id,title',
tag: '&id'
}
}
}
* ```
*
* For more information, please see https://mydea.github.io/ember-indexeddb/docs/modules/Configuring%20your%20database.html
*/
export default IndexedDbConfigurationService.extend({
currentVersion: 1,
export default class ExtendedIndexedDbConfigurationService extends IndexedDbConfigurationService {
currentVersion = 1;

version1: computed(function () {
return {
stores: {
// Add your tables here, like this: 'item': '&id'
// When using the ember data adapter, add one entry per model, where the key is your model name
// For example, if you have a model named "my-item", add an entry: `'my-item': '&id'
},
};
}),
});
version1 = {
stores: {
// Add your tables here, like this: 'item': '&id'
// When using the ember data adapter, add one entry per model, where the key is your model name
// For example, if you have a model named "my-item", add an entry: `'my-item': '&id'
},
};
}

0 comments on commit f073881

Please sign in to comment.