Git Product home page Git Product logo

chromestorm's Introduction

chromestorm

version downloads Node.js CI Chrome E2E Test codecov Maintainability

ORM-like API provider for chrome.storage.

Note: If you want it for window.localStorage, check localstorm.

Example Usage

// In your background.{ts|js}

import { Model } from "chromestrom";

// 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()); // 2
    console.log(await Player.find(x._id)); // Player {name:"otiai10", age: 17}
})();

Basic APIs

Defining your model class

import { Model } from "chromestorm";

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 chrome.storage:

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

Now _id is generated because it's saved on chrome.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

// TODO:

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

// TODO:

Issues

chromestorm's People

Contributors

otiai10 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.