0.3.2 • Published 6 years ago

@esentri/true-object-store v0.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

TrueObjectStore

Build Status Coverage Status

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

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.

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.7

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.0.1

6 years ago