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

localstorm

v1.0.0

Published

ORM for LocalStorage

Downloads

3

Readme

localstorm

Object/Relation Mapper for LocalStorage.

npm npm downloads Node.js CI codecov

Installation

npm install localstorm

Model

Model is an ORM (Object-Relation Mapper) for localStorage, providing simple interfaces like ActiveRecord.

NOTE: Model is NOT the best efficient accessor for localStorage, BUT provides the best small and easy way to manage localStorage and automatically map the object to your Model class.

How to use

class Player extends Model {}
let player = new Player({name: 'otiai10', age: 31});
player.name // 'otiai10'
player.age // 31
player._id // undefined, because not saved yet

player.save();
player._id // 1, because it's saved to localStorage

More complicated models with relations? See schema!

Methods

new

  • static
  • an alias for constructor
let player = Player.new({name: 'otiai20', age: 43});
player.name // 'otiai10'
player.age // 31
player._id // undefined, because not saved yet

save

let player = new Player({name: 'otiai20'});
player.save();
player._id // 2

find

  • static
let player = Player.find(2);
player.name // 'otiai10'

update

player.update({name: 'otiai22'});
Player.find(player._id).name // 'otiai22'

delete

player.delete();
Player.find(player._id) // undefined

create

  • static
  • an alias for new and save
let player = Player.create({name: 'otiai99', age: 99});
player._id // 3

// Is equivalent to
Player.new({name: 'otiai99'}).save();

all

  • static
  • returns everything as a dictionary
const dict = Player.all(); // Object
dict[1].name // 'otiai10'
dict[1] instanceof Player // true

list

  • static
  • returns everything as an array
const players = Player.list(); // [Player]
players.length // 2

// Is equivalent to
Player.filter(() => true);

filter

  • static
  • returns filtered array by filterFunc
const players = Player.filter(p => p.age < 40);
players.length // 1

useStorage

  • static
  • replace storage with anything which satisfies Storage interface
Model.useStorage(window.sessionStorage);

// For example, you can embed any extra operation for getItem/setItem/removeItem
const storage = new MyStorageWithEffortAsyncPushing();
Model.useStorage(storage);

Properties

schema

  • static
  • optional, default undefined
  • can define validations for each props of this model
  • no validations, if schema is not set
class Player extends Model {
  static schema = {
    name: Model.Types.string.isRequired,
    age:  Model.Types.number, // optional
    location: Model.Types.shape({
      address: Model.Types.string,
      visible: Model.Types.bool.isRequired,
    }),
  }
}

with relations

class Team extends Model {
  static schema = {
    name: Model.Types.string.isRequired,
    leader: Model.Types.reference(Player),
    members: Model.Types.arrayOf(Model.Types.reference(Player)),
  }
}

nextID

  • static
  • optional, default timestampID
  • replace it if you want to change algorythm of generating next id
Player.nextID = () => Date.now();
Player.create({name: 'otiai2017'})._id // 1488061388247
Player.create({name: 'otiai1986'})._id // 1488061388928

Types

Types API provides followings:

  1. Validation data type of Model when it's saved.
  2. Resolving relationship of Models.

Examples

import {Model, Types} from "localstorm";

class User extends Model {
  protected static schema = {
    name: Types.string.isRequired,
    age: Types.number,
    langs: Types.arrayOf(Types.string),
  }
}

class Team extends Model {
  protected static schema = {
    name: Types.string.isRequired,
    active: Types.bool.isRequired,
    address: Types.shape({
      country: Types.string,
      street: Types.string,
      postcode: Types.number,
    }),
    leader: Types.reference(User, {eager: true}),
    members: Types.arrayOf(Types.reference(User)),
    roles: Types.dictOf(Types.reference(User)),
  }
}