JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q40905F
  • License MIT

Key Value store backed by Sequelize

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

CircleCI Dependency Status standard-readme compliant js-standard-style Managed by tAsEgir

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:

  1. Define schema and initialize SequelizeStore
  2. Retrieve the store's object
  3. Set / retrieve values as you like
  4. 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 Sequelize
  • schema: Schema (required) - Object defining the Schema of the store
  • options - Store's options
    • options.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!

License

MIT