• Stars
    star
    247
  • Rank 164,117 (Top 4 %)
  • Language
    JavaScript
  • Created almost 12 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ“¦ depot.js is a storage library with a simple API

πŸ“¦ depot.js

build status

Description

depot.js is a namespaced localStorage wrapper with a simple API. There are other tools out there but none of them had what I was looking for.

Setup

You can install depot.js via npm:

  npm install depotjs --save

or load it directly via <script src="depot.browser.js"></script>. The dist folder contains the most recent minified browser version depot.browser.js.

Dependencies

depot.js does not depend on any other libraries however if you plan to support older browsers you will need to include ES5-shim.

If you plan to run it on browsers that don't support localStorage you may try to include storage polyfill.

API

  • save(record)

  • saveAll(array)

  • updateAll(hash)

  • update(hash)

  • find(hash | function)

  • all()

  • destroy(id | record)

  • destroyAll(none | hash | function)

  • get(id)

  • size()

Usage

Import depot

import depot from 'depotjs';

Define new store

const todos = depot('todos');

Add new records

_id property will be generated as GUID and attached to each new record:

todos.save({ title: "todo1" });
todos.save({ title: "todo2", completed: true });
todos.save({ title: "todo3", completed: true });

Add multiple records at once

todos.saveAll([ { title: "todo1" }, { title: "todo2" }, { title: "todo3" } ]);

Update all records

todos.updateAll({ completed: false });

Return all records

todos.all(); // [{ _id: 1, title "todo1" }, { _id: 2, title: todo2 }]

Find records

  • find based on given criteria
todos.find({ completed: true }); // [{ _id: 2, title: "todo2" }, { _id: 3, title: "todo3" }]
  • find based on given function
todos.find(record => record.completed && record.title == "todo3"); // [{ _id: 3, title: "todo3" }]

Return single record by id

todos.get(1); // { _id: 1, title: "todo1" }

Destroy single record

  • by record id
todos.destroy(1);
  • by record object
todos.destroy(todo);

Destroy all records

  • destroy all
todos.destroyAll();
  • destroy by given criteria
todos.destroyAll({ completed: true });
  • destroy by given function
todos.destroyAll(record => record.completed && record.title === "todo3");

Options

You can pass a second parameter to depot with additional options.

const todos = depot("todos", options);

Available options:

  • idAttribute - used to override record id property (default: _id)
const todos = depot("todos", { idAttribute: 'id' });
  • storageAdaptor - used to override storage type (default: localStorage)
const todos = depot('todos', { storageAdaptor: sessionStorage });

License:

The MIT License

More Repositories

1

phonegap-websocket

Websocket PhoneGap plugin for Android
Java
203
star
2

asEvented

Micro event emitter which provides the observer pattern to JavaScript object.
JavaScript
97
star
3

backbone.service

Backbone service for non restful or semi restful apis
JavaScript
44
star
4

tweetstream

Twitter Stream API + Google Map API + Backbone.js + Node.js = tweetstream
JavaScript
15
star
5

flight-todo

Todo application implemented in Twitter Flight.
JavaScript
11
star
6

backbone-nowjs

A nowjs connector for backbone
JavaScript
10
star
7

checkers

checkers with node.js
JavaScript
6
star
8

tictactoe

tic tac toe in JavaScript backed by Backbone
JavaScript
5
star
9

slidebox

Zepto plugin which turns checkboxes into iPhone like slideboxes.
JavaScript
3
star
10

extend

JavaScript inheritance
JavaScript
3
star
11

flight-tiny-demo

tiny demo built in flight
JavaScript
2
star
12

panel

panel component
JavaScript
2
star
13

gofjs

Game of life
JavaScript
2
star
14

twitwid

twitwid
JavaScript
2
star
15

packit

JavaScript template compressor based on Node.js
JavaScript
2
star
16

functional_patterns

Collection of higher order functions in JavaScript.
JavaScript
1
star
17

jquery.tmplloader

remote template loader
JavaScript
1
star
18

leonardo

javascript canvas library
JavaScript
1
star
19

sierpinski-triangle

experimenting with processing.js
JavaScript
1
star
20

typemachine

Simple jQuery plugin which simulates live typing.
JavaScript
1
star
21

sinatra-geapp

sinatra on google engine deployment from windows
1
star
22

fun-with-lisp

learning common lisp
Common Lisp
1
star
23

DirectedGraph

Directed Graph in JavaScript
JavaScript
1
star
24

databind.jquery

jQuery plugin for binding elements to other elements via html
JavaScript
1
star
25

ngPersistence

JavaScript
1
star
26

asPromise

Micro promise implementation which behaves well with any JavaScript object.
JavaScript
1
star
27

fcmanager

FCManager - jQuery plugin which manages FullCalendar
JavaScript
1
star