npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jsonql/store

v1.0.0

Published

A super simple in-memory key value store also provide a event hook for you to hook into the storage of your choice

Downloads

15

Readme

@jsonql/store

A super simple in-memory store with an event hook

What that mean is, whenever you call this store, you can hook into it by listen to the event, and do your thing; such as return value from the store of your own choice, save value to your store etc.

The event hook is using @to1source/event

Example

const { 
  JsonqlStore, 
  SET_EVT   
} from '@jsonql/store' 

const cache = new JsonqlStore()

cache.set('key', 'some value')

// listen to the event 

cache.$on(SET_EVT, function(value) {
  // here you will get the value just set 
  // which is 'some value' 
})

API

This module exports the follow:

  • JsonqlStore
  • HAS_EVT
  • SET_EVT
  • GET_EVT
  • DELETE_EVT
  • TAKE_EVT
  • ALL_EVT
  • SIZE_EVT
  • CLEAR_EVT
  • AVAILABLE_EVTS

JsonqlStore

The main class interface


const store = new JsonqlStore()

// or if you want to take a look inside 
// example using debug 

const logger = require('debug')('your-key')

const store = new JsonqlStore({ logger })

Init options

logger: allow you to pass a logging function
// store.js 
const debug = require('debug')('a-key-to-id-your-logger')
const { JsonqlStore } = require('@jsonql/store')

const store = new JsonqlStore({ logger })

Then when you run it like:

$ DEBUG=a-key-to-id-your-logger node ./store.js 

You will see a lot of output from your console, that show what is going on internally.

eventOff: turn off the event listener

You can turn off the event listener

const { JsonqlStore, SET_EVT } = require('@jsonql/store')
const store = new JsonqlStore({ eventOff: true})

// if you try to listen to the event 
const isEventOn = store.on(SET_EVT, function somecallback() {})

In the above example, the on will do nothing, isEventOn is undefined

For more about the it's parent the @to1source/event, please check their documentation.

store.on(eventName, callback)

Allow you to listen to the event happening inside, it's better using the exported constants as event name

const { JsonqlStore, SET_EVT } = require('@jsonql/store')
const store = new JsonqlStore()

store.set('some-key', 'some-value')

// the magic is we don't care about when you start listening
store.on(SET_EVT, function(value) {
  // you will get back the key and value 
  // also you can add as many event listener as you need to
})

store.has(key) HAS_EVT

Check if if certain key existed in the store


const check = store.has(key)
// listen to this event
store.on(HAS_EVT, function(value) {
  // you will get back the key and the check value
})

store.set(key,value) SET_EVT

Store data id by key (key can be anything)

store.set('some-key', 'some-value')
store.on(SET_EVT, function(result) {
  // get back the key and value in an array
})

store.get(key) GET_EVT

Get the data id by key

const data = store.get('some-key')
store.on(GET_EVT, function(result) {
  // get back the key and value in an array 
})

store.delete(key) DELETE_EVT

Delete data id by key

const result = store.delete('some-key')
store.on(DELETE_EVT, function(result) {
  // get back the key and value in an array
})

store.take(key) TAKE_EVT

Get the data id by key, then remove it from the store

const result = store.take('some-key')
store.on(TAKE_EVT, function(result) {
  // get back the key and value in an array
})
const existed = store.has('some-key')
// existed is false

store.all(arr=true) ALL_EVT

Return everything from the store as an array, if you pass arr=false then return the raw Map object

const result = store.all()
// it's an array so you can perform your ops here
result.map(x => console.log(x))

store.size() SIZE_EVT (alias JsonqlStore.length)

Return the size (how many) of the store

store.set('a', 1)
store.set('b', 2)
store.set('c', 3)

const ctn = store.size() // it will be 3 

store.length === ctn // same as above 

store.clear() CLEAR_EVT

Clear out everything from the store

store.clear()
const check = store.all()
store.length === 0

Joel Chu (c) 2020

NEWBRAN LTD

TO1SOURCE