ORM (Object-Relation Mapper) for LocalStorage of your browsers.
MIT License
localstorm has been officially transferred to the jstorm. This transfer aims to ensure the continuous development and improvement of the project to support more usecases.
To use jstorm
with localStorage
, you can simply try:
import { Model } from "jstorm/browser/local";
// Then, all the same with localstorm ;)
Object/Relation Mapper for LocalStorage.
npm install localstorm
Model
is an ORM (Object-Relation Mapper) for localStorage
, providing simple interfaces like ActiveRecord
.
NOTE:
Model
is NOT the best efficient accessor forlocalStorage
, BUT provides the best small and easy way to managelocalStorage
and automatically map the object to yourModel
class.
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
!
constructor
let player = Player.new({name: 'otiai20', age: 43});
player.name // 'otiai10'
player.age // 31
player._id // undefined, because not saved yet
let player = new Player({name: 'otiai20'});
player.save();
player._id // 2
let player = Player.find(2);
player.name // 'otiai10'
player.update({name: 'otiai22'});
Player.find(player._id).name // 'otiai22'
player.delete();
Player.find(player._id) // undefined
new
and save
let player = Player.create({name: 'otiai99', age: 99});
player._id // 3
// Is equivalent to
Player.new({name: 'otiai99'}).save();
const dict = Player.all(); // Object
dict[1].name // 'otiai10'
dict[1] instanceof Player // true
const players = Player.list(); // [Player]
players.length // 2
// Is equivalent to
Player.filter(() => true);
const players = Player.filter(p => p.age < 40);
players.length // 1
Model.useStorage(window.sessionStorage);
// For example, you can embed any extra operation for getItem/setItem/removeItem
const storage = new MyStorageWithEffortAsyncPushing();
Model.useStorage(storage);
undefined
schema
is not setclass 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)),
}
}
timestampID
Player.nextID = () => Date.now();
Player.create({name: 'otiai2017'})._id // 1488061388247
Player.create({name: 'otiai1986'})._id // 1488061388928
Types
API provides followings:
Model
when it's saved.Model
s.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)),
}
}