0.1.0-alpha.0 • Published 12 months ago

@andideve/storejs v0.1.0-alpha.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

Store.js

Store.js is a simple JavaScript library that can be used to create, modify, and delete objects in localStorage. This library is an alternative to indexed-db that can store objects and upgrade versions.

Installation

Store.js can be installed using npm:

$ npm i @andideve/storejs

After installation, you can import this library into your project:

import { Store } from '@andideve/storejs';

Usage

Creating an Instance

const users = new Store({
  name: 'users',
  version: 1,
});

Creating an Object

To create a new object, use the users.createOne() method:

const id = users.createOne({
  name: 'John Doe',
  age: 30,
  email: 'johndoe@example.com',
});

Getting Object

To get an object from localStorage, use the users.getOne() method:

const user = users.getOne(id);
console.log(user.name); // John Doe

Getting All Objects

To get all objects from localStorage, use the users.getAll() method:

const allUsers = users.getAll();

console.log(allUsers);
/*
[
  { id: '1', name: 'John Doe', created_at: 123, updated_at: 123, _version: 1 },
]
*/

Getting All Objects with Conditions

To get all objects with conditions from localStorage, use the users.getWhere() method:

users.getWhere({ age: 30 });

Modifying Objects

To modify an object that already exists in localStorage, use the users.updateOne() method:

users.updateOne(id, { age: 35 });

Deleting Object

To delete an object from localStorage, use the users.deleteOne() method:

users.deleteOne(id);

Deleting All Objects with Condition

To delete all objects with conditions from localStorage, use the users.deleteWhere() method:

users.deleteWhere({ age: 30 });

Upgrading version

You can upgrade your store version by specifying a new version number when creating a new instance, like this:

const users = new Store({
  name: 'users',
  version: 2,
});

In this example, the store will be upgraded from version 1 to version 2. If the store was previously created with a lower version, the upgrade function will be called to update the existing data.

Upgrade is an optional function that you can define in the store configuration. This function takes one parameter old, which contains the old data stored in the store. You can use this data to update the existing data structure or create new data.

Here's an example code that shows how to upgrade the store version and change the data structure stored in the store:

const users = new Store({
  name: 'users',
  version: 2,
  upgrade(old) {
    return {
      ...old,
      verified: false,
      _version: 2,
    };
  },
});

MIT License

0.1.0-alpha.0

12 months ago