lsflexdb
lsflexdb is a lightweight library for managing data in localStorage, allowing you to create tables and perform basic CRUD operations with ease.
Features
- Create tables in
localStorage
- Automatic or manual data saving
- Support for basic CRUD operations (Create, Read, Update, Delete)
- Data filtering and ordering capabilities
- Designed for use in both browsers and Angular projects
Installation
Install the package using the following command:
npm install lsflexdb
Usage
First, import the library.
Angular
import lsflexDB from 'lsflexdb';
Browser
Include the script in your HTML file:
<script src="path/to/lsflexdb.js"></script>
Create Database
Create an instance of the database:
const lsDB_blog = new lsflexDB("blog", { auto_save: true });
Methods
1. create(tableName, structure)
Creates a new table with the specified structure.
tableName: The name of the table
structure: An object defining the table's columns
lsDB_blog.create("users", {
username: "",
password: "",
is_active: 0
});
2. select(where)
Retrieves data from the table with optional filtering.
where (optional): An object specifying filter conditions
let allUsers = lsDB_blog.users.select();
let activeUsers = lsDB_blog.users.select({ is_active: 1 });
3. insert(values, options)
Adds new data to the table.
values: An object containing the new data
options (optional): Settings for saving, { save: true | false }
lsDB_blog.users.insert({
username: "admin",
password: "12345",
is_active: 1
}, { save: true });
4. update(data, where, options)
Updates existing data in the table.
data: An object containing the new values
where: Filter conditions for selecting rows to update
options (optional): Settings for saving, { save: true | false }
lsDB_blog.users.update(
{ is_active: 0 },
{ username: "admin" },
{ save: true }
);
5. delete(where, options)
Removes data from the table.
where: Filter conditions for selecting rows to delete
options (optional): Settings for saving, { save: true | false }
lsDB_blog.users.delete({ username: "admin" }, { save: false });
6. save()
Manually saves the data to localStorage.
lsDB_blog.users.save();
Using Different where Conditions
The where parameter supports various conditions for filtering data:
-
Basic Equality
let userByEmail = lsDB_blog.users.select({ email: "foo@bar.com" });
-
Comparison Operators
> (greater than), >= (greater than or equal), < (less than), <= (less than or equal)
let usersAbove200 = lsDB_blog.users.select({ "user_id[>]": 200 });
let usersBelow300 = lsDB_blog.users.select({ "user_id[<=]": 300 });
-
Not Equal
let inactiveUsers = lsDB_blog.users.select({ "is_active[!]": 1 });
-
Pattern Matching
let usersInLondon = lsDB_blog.users.select({ "city[~]": "lon" });
-
Between
let usersIn20s = lsDB_blog.users.select({ "age[<>]": [20, 30] });
let usersOutside40s = lsDB_blog.users.select({ "age[><]": [40, 50] });
-
In Array
let specificUsers = lsDB_blog.users.select({ user_id: [2, 123, 234, 54] });
let specificEmails = lsDB_blog.users.select({ email: ["foo@bar.com", "cat@dog.com", "admin@medoo.in"] });
-
Custom Filtering Function
let customFilteredUsers = lsDB_blog.users.select({
age: (value) => value > 20 && value < 50
});
Complete Example
import { lsflexDB } from 'lsflexdb';
const lsDB_blog = new lsflexDB("blog", { auto_save: true });
lsDB_blog.create("users", {
username: "",
password: "",
is_active: 0
});
lsDB_blog.users.insert({
username: "admin",
password: "12345",
is_active: 1
});
lsDB_blog.users.update(
{ is_active: 0 },
{ username: "admin" }
);
let activeUsers = lsDB_blog.users.select({ is_active: 1 });
lsDB_blog.users.delete({ username: "admin" });