jeytabase v1.0.2
Jeytabase
Jeytabase is a lightweight JSON-based database management library for Node.js. It provides a simple way to create, read, update, and delete data in JSON files, with support for data encryption, automatic backups, and advanced filtering and sorting capabilities.
Features
- Create, read, update, and delete (CRUD) operations
- Advanced filtering with operators like
[>]
,[<]
,[!]
,[~]
, and more - Customizable sorting with multiple fields and sorting directions
- Data encryption support with AES-256-CBC encryption
- Automatic backups with configurable intervals
- Automatic data saving after changes
- Easy-to-use interface with modular design
Installation
Install Jeytabase using npm:
npm install jeytabase
Usage
Here's a basic example of how to use Jeytabase:
const Jeytabase = require('jeytabase');
// Initialize the database
const db = new Jeytabase('myDatabase', {
path: 'myDatabasePath',
auto_save: true,
auto_backup: 3600,
encryption: true,
encrypt_key: 'mySecretKey'
});
// Create a table
db.create('users', {
name: '',
age: 0,
email: ''
});
// Insert data into the table
db.insert('users', {
name: 'Alice',
age: 30,
email: 'alice@example.com'
});
// Select data with filtering
const results = db.select('users', {
"age[>=]": '25'
});
// Update data
db.update('users', { age: 31 }, { name: 'Alice' });
// Delete data
db.delete('users', { age: '[<]18' });
console.log(results);
Configuration Options
When initializing Jeytabase, you can configure several options:
path: The directory where the database files are stored. Default is 'database'.
start_backup: Whether to create a backup when the database starts. Default is true.
auto_backup: Interval for automatic backups in seconds. Default is 86400 (24 hours).
auto_save: Automatically save changes to the database file after each operation. Default is true.
encryption: Enable data encryption using AES-256-CBC. Default is false.
encrypt_key: The encryption key for securing data.
Model
Using models with Jeytabase:
const Jeytabase = require('jeytabase');
// Initialize the database
const jeydb = new Jeytabase('blog', {
path: 'database',
start_backup: false,
auto_backup: 86400,
auto_save: true,
encryption: true,
encrypt_key: 'mySecretKey'
});
// Create a users table
jeydb.create('users', {
username: '',
password: '',
is_active: 0
}, {
update_timestamp: true,
insert_timestamp: true
});
// Create a posts table
jeydb.create('posts', {
uid: '',
title: '',
text: ''
}, {
update_timestamp: true,
insert_timestamp: true
});
// Define model variables ( export from module )
const user_model = jeydb.blog.models.users,
const post_model = jeydb.blog.models.posts,
// Delete data
user_model.delete();
post_model.delete();
// Insert new values
user_model.insert({
username: 'zeydan',
password: '12345',
is_active: 1
}, { save: false });
user_model.insert({
username: 'admin',
password: '12345',
is_active: 1
}, { save: false });
user_model.insert({
username: 'user1',
password: '12345',
is_active: 1
}, { save: false });
user_model.insert({
username: 'user2',
password: '12345',
is_active: 1
}, { save: false });
// Manually save data
user_model.save();
API Methods
List of available methods:
create(tableName, structure, options)
Creates a new table with the specified structure.
tableName: Name of the table.
structure: An object representing the table's structure (field names and default values).
options: Additional options for the table, such as insert_timestamp and update_timestamp.
select(tableName, where, order)
Selects data from a table with optional filtering and sorting.
tableName: Name of the table.
where: An object specifying conditions for filtering. Supports operators like [>], [<], [!], [~], etc.
order: Sorting options, can be a function or an array specifying columns and sort directions.
insert(tableName, values, options)
Inserts a new row into the specified table.
tableName: Name of the table.
values: An object with field values to insert.
options: Optional settings for the operation.
update(tableName, data, where, options)
Updates rows in the table based on the specified conditions.
tableName: Name of the table.
data: An object containing the new field values.
where: Conditions for filtering rows to update.
options: Optional settings for the operation.
delete(tableName, where, options)
Deletes rows from the table that match the specified conditions.
tableName: Name of the table.
where: Conditions for filtering rows to delete.
options: Optional settings for the operation.
save(tableName)
Saves the current state of a table to the JSON file.
Methods Variables
List of variables:
Options
Available option for methods
save: true // Prevents auto save new changes.
Where
Possible "where" operations
"email": "foo@bar.com", // email = 'foo@bar.com'
"user_id[>]": 200, // user_id > 200
"user_id[>=]": 200, // user_id >= 200
"user_id[<]": 200, // user_id < 200
"user_id[<=]": 200, // user_id <= 200
"user_id[!]": 200, // user_id != 200
"city[~]": "lon" // "city" LIKE '%lon%'
["age[<>]": [200, 500]] // age BETWEEN 200 AND 500
["age[><]": [200, 500]] // age NOT BETWEEN 200 AND 500
Complex Where Operations
Complex "where" operations
// Pass a function to filter rows
[age: (r) => {
return r > 5
}]
// user_id IN (2,123,234,54) OR email IN ('foo@bar.com','cat@dog.com','admin@medoo.in')
"OR": {
"user_id": [2, 123, 234, 54],
"email": ["foo@bar.com", "cat@dog.com", "admin@medoo.in"]
}
// "user_name" != 'foo' AND "user_id" != 1024
"AND": {
"user_name[!]": "foo",
"user_id[!]": 1024
}
(user_name = 'foo' OR email = 'foo@bar.com') AND password = '12345'
"AND": {
"OR": {
"user_name" => "foo",
"email" => "foo@bar.com"
},
"password" => "12345"
}
ORDER
Possible "ORDER" operations. Pass "ORDER" inside "where".
"ORDER": "user_id",
"ORDER": {"user_id": [43, 12, 57, 98, 144, 1]}
"ORDER": {"profile_id": "DESC"}
"ORDER": {"date": "ASC"}
LIMIT
Possible "LIMIT" operations. Pass "LIMIT" inside "where".
// Get the first 100 rows.
'LIMIT': 100
// Start from the top 20 rows and get the next 100.
'LIMIT': [20, 100],
Encryption
If encryption is enabled, all data is encrypted using AES-256-CBC before saving to disk. To enable encryption, provide an encrypt_key when initializing the database.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Feel free to submit a pull request or open an issue to discuss changes or improvements.