Package Exports
- sequelize-store
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sequelize-store) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Sequelize Store
Key Value store backed by Sequelize
Table of Contents
Install
$ npm install sequelize-store
This package requires Sequelize
as peer-dependency, but that should
be already satisfied because most probably you will use this package in projects where it is already present.
In case not just run:
$ npm install sequelize
Usage
There are four steps you have to do:
- Define schema and initialize SequelizeStore
- Retrieve the store's object
- Set / retrieve values as you like
- Profit
import {init, getObject, getEndPromise} from 'sequelize-store'
const sequelize = sequelizeFactory()
// Define schema and init
await init(sequelize, {
adminId: 'int', // Lets say this was already set previously and hence is pesisted in DB
secretToken: { type: 'string', default: 'notSoRandomSecret' },
someCoolObject: 'json',
scope1_id: 'int',
scope1_name: 'string'
})
const store = getObject()
console.log(store.secretToken) // --> 'notSoRandomSecret'
console.log(store.adminId) // --> undefined
store.adminId = 5
console.log(store.adminId) // --> 5
delete store.adminId
console.log(store.adminId) // --> undefined
console.log(store.adminId) // --> 5
// Scopes
store.scope1_id = 10
const scopedStore = getObject('scope1_')
console.log(scopedStore.id) // --> 10
console.log(scopedStore.name) // --> undefined
// Lets await for everything to be committed to database
await getEndPromise()
Schema
Schema is an object that defines the structure of the Store. Supported types are:
bool
, int
, float
, json
, string
.
The Schema has two following formats
{
'key-name': <<type string>>,
otherKeyName: {
type: <<type string>>,
default: 'some default'
}
}
API
init(sequelize: Sequelize, schema: Schema, options?: StoreOptions) -> Promise<void>
Initialize SequelizeStore for usage
Parameters:
sequelize: Sequelize
(required) - Instance of Sequelizeschema: Schema
(required) - Object defining the Schema of the storeoptions
- Store's optionsoptions.tableName: string
- string defining name of the table where the data should be stored
getObject(scope?: string) -> object
Returns the Store objects which is a singleton, so you can call it anywhere (after initialization!)
Parameters:
scope?: string
- The returned object will be scoped to given scoped. That means that all keys will prefixed with the string.
purge() -> Promise<void>
Delete all data in database and the local cache
getEndPromise() -> Promise<void>
Function that returns a Promise that is resolved when the DB processing queue is finished with all the pending transactions. If queue is empty Promise is resolved right away.
Contribute
There are some ways you can make this module better:
- Consult our open issues and take on one of them
- Help our tests reach 100% coverage!