Skip to content

Commit ae1abff

Browse files
authored
Merge pull request #7 from BaileyBrightman/develop
Develop
2 parents 1c8f4e0 + a58451b commit ae1abff

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,72 @@ import { SavedData } from 'springroll-container';
486486
// Firstly, construct the SavedData object. This is only needed for IndexedDB work
487487
savedData = new SavedData('dbName');
488488

489+
// Then, open a connection to the database. All changes to the structure of the database should be passed in here
489490
```
491+
Additions is an optional parameter expecting a JSON object with any additions to the databases structure namely new [stores](https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/createObjectStore) and [indexes](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/createIndex). These are placed inside of an array
492+
493+
Deletions is an optional parameter used to delete any [indexes](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/deleteIndex) or [stores](https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/deleteObjectStore)
494+
495+
``` javascript
496+
497+
let additions = {
498+
stores: [{
499+
storeName: 'storeOne',
500+
// optionally define a keyPath and/or set autoIncrement to true or false
501+
options: { keyPath: "taskTitle" }
502+
},
503+
{
504+
storeName: 'storeTwo'
505+
}],
506+
indexes: [{
507+
indexName: 'newIndex',
508+
keyPath: 'key',
509+
// Any objectParameters for the Index
510+
options: {
511+
unique: false
512+
}
513+
}]
514+
};
515+
516+
// Deletions is an optional parameter used to delete any indexes or stores
517+
let deletions = {
518+
stores: ['storeOne', 'storeTwo'],
519+
indexes: ['newIndex']
520+
};
521+
522+
// Optionally pass in the new database version. Set to true to increment the database version.
523+
// Leave this parameter out or pass in false to connect without making any changes to the structure of the database
524+
let dbVersion = 1
525+
526+
// The name of the database to connect to
527+
let dbName = 'dbName';
528+
529+
// Finally, pass these parameters in to establish a connection with the database
530+
savedData.onOpenDb(dbName, dbVersion, additions, deletions);
531+
```
532+
533+
There are other methods currently supported to interact with the database. These allow you to [Add a record](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/add), [Deleting a record](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/delete), [Reading](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/get), [reading all records](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/getAll) Each will return a success, or on failure, an error message
534+
535+
``` javascript
536+
537+
//Delete a record by the key in a specific store
538+
savedData.IDBRemove('storeName', 'key');
539+
540+
// add a record to a store. The record can be any type of object accepted by indexedDB
541+
savedData.IDBAdd('storeName', 'record');
542+
543+
// returns the record with the given key from the store with the given storeName
544+
savedData.IDBRead('storeName', 'key');
545+
546+
// Finally, close the connection to the database
547+
savedData.closeDb();
548+
549+
// Return all records from a database or optionally a specified amount defined by the second parameter
550+
savedData.IDBReadAll('storeName');
551+
savedData.IDBReadAll('storeName', 5);
552+
553+
554+
490555
All other methods will work the same as the documentation [here](https://github.com/SpringRoll/SpringRoll/tree/main/src/state#userdata);
491556

492557

src/plugins/UserDataPlugin.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ export class UserDataPlugin extends BasePlugin {
5757
* Handler for the userDataRemove event
5858
* @method onUserDataRemove
5959
* @private
60-
* @param {string} data The name of the record to be removed
60+
* @param {string} name The name of the record to be removed
6161
* @param {string} type The type of listener for bellhop to send to
6262
*/
63-
onUserDataRemove({ data, type }) {
64-
SavedDataHandler.remove(data, () => {
63+
onUserDataRemove({ name, type }) {
64+
SavedDataHandler.remove(name, () => {
6565
this.client.send(type);
6666
});
6767
}
@@ -70,20 +70,20 @@ export class UserDataPlugin extends BasePlugin {
7070
* Handler for the userDataRead event
7171
* @method onUserDataRead
7272
* @private
73-
* @param {string} data The name of the record to be removed
73+
* @param {string} name The name of the record to be removed
7474
* @param {string} type The type of listener for bellhop to send to
7575
*/
76-
onUserDataRead({ data, type }) {
77-
SavedDataHandler.read(data, value => this.client.send(type, value));
76+
onUserDataRead({ name, type }) {
77+
SavedDataHandler.read(name, value => this.client.send(type, value));
7878
}
7979

8080
/**
8181
* Handler for the userDataWrite event
8282
* @method onUserDataWrite
8383
* @private
8484
* @param {string} type The type of listener for bellhop to send to
85-
* @param {string} data The name for the record. This is what is used to read or remove the record
86-
* @param {object | string} value The data object with the data and value for the record
85+
* @param {string} data.name The name for the record. This is what is used to read or remove the record
86+
* @param {object | string} data.value The data object with the data and value for the record
8787
*/
8888
onUserDataWrite({type, data: { name, value } }) {
8989

0 commit comments

Comments
 (0)