@esentri/true-object-store
v0.3.2
Published
[![Build Status](https://travis-ci.org/esentri/js-true-object-store.svg?branch=master)](https://travis-ci.org/esentri/js-true-object-store) [![Coverage Status](https://coveralls.io/repos/github/esentri/js-true-object-store/badge.svg?branch=master)](https:
Downloads
21
Keywords
Readme
TrueObjectStore
This project wraps IndexedDB so objects can be stored and loaded as objects (and not as data structures).
In addition promises are used for a better coding flow.
Install
npm install @esentri/true-object-store
yarn add @esentri/true-object-store
Usage
class TestClass {
constructor(key) {
this.key = key
}
printKey() {
console.log('my key: ', key)
}
}
let trueObjectStore = new TrueObjectStoreBuilder().
.name('myObjectStoreName')
.parameters({keyPath: 'key'})
.deserializer(Deserializer.simple(TestClass))
.build()
let simpleIndexedDB = new SimpleIndexedDBBuilder()
.name('testDB1')
.dbVersion(1)
.objectStores([trueObjectStore])
.build()
simpleIndexedDB.open().then(() => {
trueObjectStore.save(new TestClass('test')).then(() => {
trueObjectStore.load('test').then(loadedObject => {
loadedObject.printKey()
})
})
})
let trueObjectStore = new TrueObjectStoreBuilder().
.name('myObjectStoreName')
.parameters({keyPath: 'key'})
.database(myDatabase)
.deserializer(Deserializer.simple(TestClass))
.build()
// if the store is new you need to call <onUpgradeNeeded>
// NOTE: the database needs to be open for this call
trueObjectStore.onUpgradeNeeded()
trueObjectStore.save(new TestClass('test')).then(() => {
trueObjectStore.load('test').then(loadedObject => {
loadedObject.printKey()
})
})
Limitations
Currently, you cannot use a get function as a key. E.g. the following class
class TestClass {
constructor(key) {
this._key = key
}
get key() {
return this._key
}
}
will fail if you choose {keyPath: 'key'}'
. You need to select a real field. In this case
{keyPath: '_key'}
.
Projects used
- @esentri/de-serialize
- License: MIT
- for (de-)serializing from/to objects/datastructures
- fake-indexeddb
- License: Apache 2.0
- for testing against the IndexedDB API
- Typescript Library Starter
- License: MIT
- as a template for the setup
- JEST
- License: MIT
- Colors
- License: MIT
- Commitizen
- License: MIT
- Definitley Typed
- License: MIT
- Coveralls
- License: BSD-2-Clause
- Cross-env
- License: MIT
- cz-conventional-changelog
- License: MIT
- Husky
- License: MIT
- lint-staged
- License: MIT
- lodash.camelcase
- License: MIT
- Prompt
- License: MIT
- replace-in-file
- License: MIT
- rimraf
- License: ISC
- rollup
- License: MIT
- semantic-release
- License: MIT
- tslint
- License: Apache-2.0
- typedoc
- License: Apache-2.0
- typescript
- License: Apache-2.0
- validate-commit-msg
- License: MIT
License
MIT License
Copyright (c) 2018 esentri AG
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.