lsflexdb
v1.0.4
Published
A lightweight local storage database library for browser and Angular.
Downloads
280
Maintainers
Readme
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:
// Create a new database instance
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 tablestructure
: 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
// Get all users
let allUsers = lsDB_blog.users.select();
// Filter users based on conditions
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 dataoptions
(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 valueswhere
: Filter conditions for selecting rows to updateoptions
(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 deleteoptions
(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
// Select users with a specific email let userByEmail = lsDB_blog.users.select({ email: "[email protected]" });
Comparison Operators
>
(greater than),>=
(greater than or equal),<
(less than),<=
(less than or equal)
// Select users with user_id greater than 200 let usersAbove200 = lsDB_blog.users.select({ "user_id[>]": 200 }); // Select users with user_id less than or equal to 300 let usersBelow300 = lsDB_blog.users.select({ "user_id[<=]": 300 });
Not Equal
// Select users who are not active let inactiveUsers = lsDB_blog.users.select({ "is_active[!]": 1 });
Pattern Matching
LIKE
operator using[~]
// Select users where city contains 'lon' let usersInLondon = lsDB_blog.users.select({ "city[~]": "lon" });
Between
// Select users with age between 20 and 30 let usersIn20s = lsDB_blog.users.select({ "age[<>]": [20, 30] }); // Select users with age not between 40 and 50 let usersOutside40s = lsDB_blog.users.select({ "age[><]": [40, 50] });
In Array
// Select users with specific user_ids let specificUsers = lsDB_blog.users.select({ user_id: [2, 123, 234, 54] }); // Select users with specific emails let specificEmails = lsDB_blog.users.select({ email: ["[email protected]", "[email protected]", "[email protected]"] });
Custom Filtering Function
// Use a custom function to filter users let customFilteredUsers = lsDB_blog.users.select({ age: (value) => value > 20 && value < 50 });
Complete Example
import { lsflexDB } from 'lsflexdb';
// Create the database
const lsDB_blog = new lsflexDB("blog", { auto_save: true });
// Create the 'users' table
lsDB_blog.create("users", {
username: "",
password: "",
is_active: 0
});
// Insert a new user
lsDB_blog.users.insert({
username: "admin",
password: "12345",
is_active: 1
});
// Update the user's status
lsDB_blog.users.update(
{ is_active: 0 },
{ username: "admin" }
);
// Get active users
let activeUsers = lsDB_blog.users.select({ is_active: 1 });
// Delete the user
lsDB_blog.users.delete({ username: "admin" });