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

stone-skin

v0.6.0

Published

Isomorphic IndexedDb and Memory data wrapper

Downloads

11

Readme

StoneSkin

Isomorphic IndexedDb and in-memory db wrapper with jsonschema validation.

$ npm install stone-skin --save

Inspired by mWater/minimongo. And based on thin indexedDb wrapper mizchi/idb-wrapper-promisify

Features

  • ActiveRecord like API
  • Universal indexedDb or in-memory object
  • Promisified
  • Runnable in shared-worker and service-worker
  • (optional) validation by jsonschema(tv4)
  • Selectable target
    • IndexedDb(browser)
    • LocalStorageDb(browser)
    • FileDb(node)
    • MemoryDb(universal)

FileDb and LocalStorageDb do just only serialization to json/string. Don't use them with big data.

Example

with babel(>=4.7.8) async/await (babel --experimental)

global.Promise = require('bluebird');

import "babel/polyfill";
import StoneSkin from 'stone-skin';

class ItemStore extends StoneSkin.IndexedDb {
  storeName: 'Item';
  schema: {
    properties: {
      title: {
        type: 'string'
      }
    }
  }
}

let itemStore = new ItemStore();
(async () => {
  await itemStore.ready;
  await itemStore.save({title: 'foo', _id: 'xxx'});
  let item = await itemStore.find('xxx');
  console.log(item);
  let items = await itemStore.all();
  console.log(items);
})();

with coffee

StoneSkin = require('stone-skin/with-tv4')

class Item extends StoneSkin.IndexedDb
  storeName: 'Item'
  schema:
    properties:
      title:
        type: 'string'
      body:
        type: 'string'

item = new Item
item.ready
.then ->
  item.clear()
.then ->
  item.all()
.then (items) ->
  console.log items
.then ->
  item.save {
    _id: 'xxx'
    title: 'test2'
    body: 'hello'
  }
.then ->
  item.save [
    {
      _id: 'yyy'
      title: 'test1'
      body: 'hello'
    }
  ]
.then ->
  item.all()
.then (items) ->
  console.log items
  item.remove 'xxx'
.then ->
  item.all()
.then (items) ->
  console.log items

with TypeScript

///<reference path='stone-skin.d.ts' />
var StoneSkin = require('stone-skin/with-tv4');

interface ItemSchema = {
  _id: string;
  title: string;
};

class Item extends StoneSkin<ItemSchema> {
  // ...
}

See detail stone-skin.d.ts)

Promisified Db API

StoneSkin.IndexedDb and StoneSkin.MemoryDb have same API

  • ready: Promise<any>: return resolved promise if indexedDb ready.
  • find(id: Id): Promise<T>: get first item by id
  • select(fn: (t: T) => boolean): Promise<T[]>: filtered items by function
  • first(fn: (t: T) => boolean): Promise<T>: get first item from filtered items
  • last(fn: (t: T) => boolean): Promise<T>: get last item from filtered items
  • all(): Promise<T[]>: return all items
  • clear(): Promise<void>: remove all items
  • save(t: T): Promise<T>: save item
  • save(ts: T[]): Promise<T[]>: save items
  • remove(id: Id): Promise<void>: remove item
  • remove(ids: Id[]): Promise<void>: remove items

StoneSkin.IndexedDb

  • storeName: string; You need to set this value when you extend it.
  • StoneSkin.IndexedDb<T>.prototype.toMemoryDb(): StoneSkin.MemoryDb: return memory db by its items
  • StoneSkin.IndexedDb<T>.prototype.toSyncedMemoryDb(): StoneSkin.SyncedMemoryDb: return synced memory db by its items.

StoneSkin.FileDb

  • filepath: string; You need to set this value when you extend it.

StoneSkin.LocalStorageDb

  • key: string; You need to set this value when you extend it.

StoneSkin.SyncedMemoryDb

It has almost same API without Promise wrap.

Migration helper

  • StoneSkin.utils.setupWithMigrate(currentVersion: number);
StoneSkin.utils.setupWithMigrate 3,
  initialize: ->
    console.log 'init'        # fire at only first
  '1to2': ->
    console.log 'exec 1 to 2' # fire if last setup version is 1
  '2to3': ->
    console.log 'exec 2 to 3' # fire it if last setup version is 1 or 2

Need localStorage to save last version. It only works on browser.

LICENSE

MIT