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

jstorm

v0.12.0

Published

JavaScript Storage ORM (Object-Relational Mapper) for LocalStorage and Chrome Storage API

Downloads

4

Readme

jstorm

version Try it on RunKit downloads Node.js CI Chrome E2E Test codecov Maintainability

ORM-like API provider for window.localStorage and chrome.storage, or any other data store.

// For window.localStorage:
import { Model } from "jstorm/browser/local";

// For chrome.storage.sync:
import { Model } from "jstorm/chrome/sync";
// For your custom storage:
import { Model } from "jstorm";
Model._area_ = yourCoolStorageAccessor;
// NOTE: It should implement `chrome.storage.StorageArea` interface.

Getting started

npm install jstorm
# or yarn add, pnpm add, whatever

Example Usage

// In your JS/TS
import { Model } from "jstorm/chrome/local";

// Define your model,
class Player extends Model {
    public name: string;
    public age: number;
    greet(): string {
        return `Hello, my name is ${this.name}!`;
    }
}

// and use it.
(async () => {
    // Save records to chrome.storage.
    const x = await Player.create({ name: "otiai10", age: 17 });
    const y = await Player.create({ name: "hiromu", age: 32 });

    // Retrieve records from chrome.storage.
    console.log(await Player.list()); // [Player, Player] length 2
    console.log(await Player.find(x._id)); // Player {name:"otiai10", age: 17}
})();

Basic APIs

Defining your model class

import { Model } from "jstorm";

class Player extends Model {
    // You can define your own members of your model.
    public name: string;
    public age: number;

    // Optional: If you'd like to minify/mangle your JS,
    // you'd better set the namespace of this mode explicitly.
    static override _namespace_ = "Player";
}

That's all to get started. Let's enjoy.

new

To construct new model object:

const john = Player.new({name: "John", age: 17});
console.log(john._id); // null

NOTE: new does NOT save constructed object yet. Please use save to make it persistent.

save

To save unsaved obejct to the storage:

await john.save();
console.log(john._id); // 1672363730924

Now _id is generated because it's saved on the storage.

create

Just a short-hand of new and save:

const paul = await Player.create({name: "Paul", age: 16});
console.log(paul._id); // 1672968746499

list

To list up all entities saved on this namespace:

const all = await Player.list();
console.log(all.length);  // 2
console.log(all[0].name); // John

dict

To get all entities saved on this namespace as a dict:

const dict = await Player.dict();
console.log(Object.entries(dict));
// [[1672363730924, Player], [1672968746499, Player]]

find

To find specific object saved on this namespace:

const found = await Player.find("1672968746499");
console.log(found?.name); // Paul

filter

To find objects which should match a specific criteria:

const criteria = (p: Player): bool => (Player.age > 16);
const filtered = await Player.filter(criteria);
console.log(filtered.length); // 1

update

await john.update({ age: 21 });
const found = await Player.find(john._id);
console.log(found.age); // 21

delete

To delete a specific object:

await john.delete();
const found = await Player.find(john._id);
console.log(found); // null

drop

To delete all objects saved on this namespace:

await Player.drop();
const list = await Player.list();
console.log(list.length); // 0

Advanced properties

schema

Use static schema to define relation between your Models.

import { Model, Types } from "jstorm/chrome/local";

class Team extends Model {
    public name: string;
    public captain: Player;
    public admins: Player[];

    static override schema = {
        name: Types.string.isRequired,
        captain: Types.model(Player, { eager: true }).isRequired,
        admins: Types.arrayOf(Types.model(Player)),
    }
}

NOTE: When {eager: true} is provided in schema definition of Types.model, this model always look up your storage to populate specified fields, eagerly. Otherwise, this model just instantiate this field from what's saved under this model.

Issues

  • https://github.com/otiai10/jstorm/issues