0.4.2 • Published 4 years ago

sequelize-store v0.4.2

Weekly downloads
232
License
MIT
Repository
github
Last release
4 years ago

Sequelize Store

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

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 1. Retrieve the store's object 1. Set / retrieve values as you like 1. 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