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

local_orm

v0.6.1

Published

A simple ORM-like layer on top of localStorage

Downloads

7

Readme

local_orm

CodeShip Code Climate

A simple ORM-like wrapper around localStorage with functional interface, types and validations.

Installation

npm install local_orm --save

Usage

Define a schema (yes, we call it a schema):

const { define: define, types: t, validations: v } = require("local_orm");

const Store = define({
  name: "books_schema",
  schema: {
    books: {
      title: {
        type: t.string,
        validations: [v.present, v.maxLength(32)]
      },
      year: {
        type: t.integer,
        validations: [v.min(1900), v.max(2999)]
      },
      genre: {
        type: t.string,
        validations: [v.present, v.oneOf('fiction', 'non-fiction')],
        defaultVal: 'fiction'
      }
    }
  }
});

Let's save some books.


// Create a book
let [err, book] = Store.books.save({ title: "War And Peace" });
console.log(book);
// => { id: "0326d5ce-d3db-4bf7-853f-37d4d5adf6a8", title: "War And Peace", genre: 'fiction' }
// ( Note that we have an id now, and that genre was populated with a default value )

// Let's try another one
let [err, book] = Store.books.save({ year: "1984" });
console.log(book); // => null

// Was there some errors?
console.log(err); // => { 'year': ['should be an integer'], 'title': ['should be present'] }

// Oh, I see now...
let [err, book] = Store.books.save({ title: "So Long, and Thanks for all the Fish", year: 1984 });

We can load books from localStorage now.

// Find a book by id
let book = Store.books.find(id);

// Load all books
let books = Store.books.all();

// Filter by title
let books = Store.books.where({title: 'War And Peace'});

// Any function is also accepted
let books = Store.books.where((b) => b.year > 1980);

Store API

  • build builds a new entity with set default values. Returns a new entity

  • validate validates an entity. Returns an array [errors, isValid], where errors is an object like this one:

{
  "title": [ "should be present", "should have more than 5 characters" ],
  "year": [ "should be less of equal to 2999" ]
}
  • save creates a new entity or updates the existing one (if it has an id). Returns an array [errors, entity]

  • find finds an entity by id. Throws an error if it does not exist. Returns an entity

  • destroy destroys an entity by id. Throws an error if it does not exist. Returns true

  • all loads all entities. Returns an array of entities

  • where filters out entities. Accepts object or function. Returns an array of entities

Validations

Validation is a simple plain JavaScript function that takes a value and returns an array like this one [error, valid]. Local_orm comes with several predefined validations.

  • present requires a value to be present (not undefined and not null)
validations: [v.present]
  • min, max define a range for integer types (inclusive)
validations: [v.min(0), v.max(127)]
  • minLength, maxLength define a length range for strings or arrays (really, anything that has length)
validations: [v.maxLength(32)]
  • oneOf require a value to be in a particular set of values (like enum)
validations: [v.oneOf("sun", "moon")]

Also, note that when you define a type, under the curtains the corresponding validation is added to the list.

As noted earlier, validations are just functions, so it is easy to define your own:

const positive = (val) => {
  if (val > 0) {
    return [null, true];
  } else {
    return ["should be positive", false];
  }
};

validations: [ positive ]

Contributing

Feel free to fork, add features and send pull requests. Please make sure to add corresponding tests.