Package Exports
- evelodb
- evelodb/evelodb.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (evelodb) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
EveloDB
An awesome local database management system with nodejs. Made by Evelocore. With B-tree Operations & AES Encryption.
Requirements
- Node.js
Table of Contents
๐ฅ Installation
Via npm
npm i evelodbManual Installation
- Download
evelodb.js - Place it in your project directory
- First run creates
./evelodatabase/automatically
Import
const eveloDB = require('evelodb')
const db = new eveloDB();Optional Configuration
let db
try {
db = new eveloDB({
directory: './evelodatabase', // ./evelodatabase/users.db
extension: 'db', // users.db
encryption: '<encryption_method>',
encryptionKey: '<encryption_key>',
noRepeat: false,
auroPrimaryKey: true
})
} catch (err) {
console.error('Init Error:', err.message);
process.exit(1);
}Configuration Parameters
| Parameter | Type | Description | Example | Default |
|---|---|---|---|---|
directory |
string | Where database files are stored | './database' |
'./evelodatabase' |
extension |
string | File extension for DB files | 'db', 'edb' |
'json' |
encryption |
string | Encryption algorithm | 'aes-256-cbc' |
null |
encryptionKey |
string | Key (length varies by algorithm) | 64-char hex for AES-256 | null |
noRepeat |
boolean | Reject duplicate data | true/false |
false |
autoPrimaryKey |
boolean | Auto-create unique IDs (__id) | true/false |
true |
โ๏ธ Operations
Create
// Structure
db.create('collection', {
key: 'value'
});
// Example
db.create('collection', {
username: 'evelocore',
name: {
firstname: 'Kumuthu',
lastname: 'Prabhasha'
},
email: 'example@gmail.com'
});Output
{ success: true }if autoPrimaryKey: true
{ success: true, __id: 'mcbdb90d-ajl393' }if noRepeat: true and repeating data is detected
{ err: 'Duplicate data - record already exists (noRepeat enabled)', code: 'DUPLICATE_DATA' }Find
// Structure
const result = db.find('collection', {
key: 'value'
});
// Example
const user = db.find('collection', {
username: 'evelocore'
});
console.log(user)Output
[
{
username: 'evelocore',
name: 'Evelocore',
developer: 'K.Prabhasha',
email: 'example@gmail.com'
}
]No result found
[]Find One
// Structure
const result = db.findOne('collection', {
key: 'value'
});
// Example
const user = db.findOne('collection', {
username: 'evelocore'
});
console.log(user)Output
{
username: 'evelocore',
name: 'Evelocore',
developer: 'K.Prabhasha',
email: 'example@gmail.com'
}No result found
nullSearch
// Structure
const result = db.search('collection', {
key: 'partial_value'
});
// Example
const user = db.search('collection', {
username: 'evelo'
});
console.log(user)Output
[
{
username: 'evelocore',
name: 'Evelocore',
developer: 'K.Prabhasha',
email: 'example@gmail.com'
}
]Check Existence
// Structure
const exists = db.check('collection', {
key: 'value'
});
// Example
const exists = db.check('accounts', {
username: 'evelocore'
});
console.log(exists)Output
trueUpdate
// Structure
db.edit('collection',
{ key: 'value' }, // find condition
{ key: 'new_value' } // new data
);
// Example
db.edit('accounts',
{ username: 'evelocore' },
{
name: 'EveloCore Official',
email: 'updated@gmail.com'
}
);Output
{ success: true, modifiedCount: 1 }if find condition not matched
{ err: 'No matching records found', code: 'NO_MATCH' }if noRepeat: true and repeating data is detected
{ err: 'Edit would create duplicate data (noRepeat enabled)', code: 'DUPLICATE_DATA' }Delete
// Structure
db.delete('collection', {
key: 'value'
});
// Example
db.delete('accounts', {
username: 'evelocore'
});Get full collection
// Structure
const result = db.get('collection');
// Example
const users = db.get('accounts');
console.log(users);Inject full collection
// Structure
const result = db.inject('collection', data);
// Example
const users = db.inject('accounts', [
{ id: 1, name: 'Evelocore' },
{ id: 2, name: 'K.Prabhasha' },
]);
const users = db.inject('appdata', {
name: 'Evelodb',
description: 'An awesome local DBMS with nodejs',
author: 'Evelocore'
})
// Also can maintain object using inject() and get()Reset Collection
// Structure
db.reset('collection');
// Example
db.reset('accounts');๐ Encryptions
Configuration
const eveloDB = require('evelodb')
let db
try {
db = new eveloDB({
directory: './evelodatabase',
extension: 'db',
encryption: '<encryption_method>',
encryptionKey: '<encryption_key>',
})
} catch (err) {
console.error('Init Error:', err.message);
process.exit(1);
}Encryption Methods
aes-128-cbc(16 bytes) - 32 hex charactersaes-192-cbc(24 bytes) - 48 hex charactersaes-256-cbc(32 bytes) - 64 hex charactersaes-128-gcm(16 bytes) - 32 hex charactersaes-256-gcm(32 bytes) - 64 hex characters
Example Configuration
const eveloDB = require('evelodb')
let db
try {
db = new eveloDB({
extension: 'db',
encryption: 'aes-256-cbc',
encryptionKey: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' // 64 hex characters
})
} catch (err) {
console.error('Init Error:', err.message);
process.exit(1);
}๐ Change Configuration
- Note: If you are using the config, you can use the same instance of the database.
- If you change the encryption / encryptionKey / extension or directory, your current db files and data was initialized with the old config will be corrupted and cannot be read.
- Solution for change configuration, use
changeConfig()method. It can change your current db config to new config and continue normally after initialize again with new config . - Eg: Converting my current
aes-256-cbcencrypted .json database from './evelodatabase' toaes-128-cbcand .db with new key and './database' directory.
const res = db.changeConfig({
from: {
directory: './evelodatabase',
extension: 'json',
encryption: 'aes-256-cbc',
encryptionKey: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
},
to: {
directory: './database',
extension: 'db',
encryption: 'aes-128-cbc',
encryptionKey: '0123456789abcdef0123456789abcdef' // 32 hex characters
},
collections: ['users', 'accounts'] // if not set collections, convert all collections
})
console.log(res)
// { success: true, converted: 1, failed: 0 }
// Initialize again
try {
db = new eveloDB({
directory: './database',
extension: 'db',
encryption: 'aes-128-cbc',
encryptionKey: '0123456789abcdef0123456789abcdef'
});
} catch (err) {
console.error('Re-init Error:', err.message);
}If you remove encryption and encryptionKey parameters in to object, it will remove the encryptions in your database and continue with json string.
const res = db.changeConfig({
from: {
encryption: 'aes-256-cbc',
encryptionKey: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
},
to: {}
})โ Testing with Examples
- Copy
test.jsfile to your project directory and run:
Show Script
// This is a test file for the EveloDB module.
const eveloDB = require('evelodb');
let db
// Initialize DB
try {
db = new eveloDB({
directory: './evelodatabase',
extension: 'db',
noRepeat: true,
// Start unencrypted to test conversion
});
} catch (err) {
console.error('Init Error:', err.message);
process.exit(1);
}
// ===== TEST FLOW =====
const testUser = { name: 'John Doe', age: 30 };
const query = { age: 30 };
const new_query = { age: 40 };
const search_query = { name: 'Joh' };
let createdId;
// Create Data
console.log('\nโ
Create Data');
try {
const res = db.create('users', testUser);
createdId = create.__id;
console.log(`Create Result:`, res);
} catch (err) {
console.error('Create Error:', err.message);
}
// Find Data
console.log('\n๐ Find Before Conversion');
try {
const res = db.find('users', { __id: createdId });
console.log(`Find Result:`, res);
} catch (err) {
console.error('Find Error:', err.message);
}
// Search Data
console.log('\n๐ Search by a piece of value');
try {
const res = db.search('users', search_query);
console.log(`Find Result:`, res);
} catch (err) {
console.error('Find Error:', err.message);
}
// Convert to encrypted format
console.log('\n๐ Convert to Encrypted Format');
try {
const res = db.changeConfig({
from: {
directory: './evelodatabase',
extension: 'db',
noRepeat: true
},
to: {
directory: './evelodatabase',
extension: 'db',
noRepeat: true,
encryption: 'aes-256-cbc',
encryptionKey: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
}
});
console.log('Conversion Result:', res);
} catch (err) {
console.error('Conversion Error:', err.message);
}
// Initialize with new config
try {
db = new eveloDB({
directory: './evelodatabase',
extension: 'db',
noRepeat: true,
encryption: 'aes-256-cbc',
encryptionKey: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
});
} catch (err) {
console.error('Re-init Error:', err.message);
}
// Find Data Again
console.log('\n๐ Try Reading Encrypted With New DB Config');
try {
const res = db.find('users', query);
console.log(`Find Result:`, res);
} catch (err) {
console.error('Find Error:', err.message);
}
// Edit Data
console.log('\n๐ Editing data');
try {
const res = db.edit('users', query, new_query);
console.log(`Editing Result:`, res);
} catch (err) {
console.error('Edit Error:', err.message);
}
// Find Data Again
console.log('\n๐ Find old object again after edit');
try {
const res = db.find('users', query);
console.log(`Find Result:`, res);
} catch (err) {
console.error('Find Error:', err.message);
}
// Find Data Again
console.log('\n๐ Find new object again after edit');
try {
const res = db.find('users', new_query);
console.log(`Find Result:`, res);
} catch (err) {
console.error('Find Error:', err.message);
}
// Convert config back to plain JSON
console.log('\n๐ Convert Back to Plain JSON');
try {
const res = db.changeConfig({
from: {
directory: './evelodatabase',
extension: 'db',
noRepeat: true,
encryption: 'aes-256-cbc',
encryptionKey: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
},
to: {
directory: './evelodatabase',
extension: 'db',
noRepeat: true
}
});
console.log('Conversion Result:', res);
} catch (err) {
console.error('Conversion Error:', err.message);
}
// Initialize with new config
try {
db = new eveloDB({
directory: './evelodatabase',
extension: 'db',
noRepeat: true
});
} catch (err) {
console.error('Re-init Error:', err.message);
}
// Delete Data
console.log('\n๐งน Clean Up');
try {
const res = db.delete('users', new_query);
console.log(`Delete Result:`, res);
} catch (err) {
console.error('Delete Error:', err.message);
}
// Reset collection
console.log('\n๐งน Reset Collection')
try {
const res = db.reset('users');
console.log(`Reset Result:`, res);
} catch (err) {
console.error('Reset Error:', err.message);
}๐ก Features
- โ JSON-based storage
- โ AES Encryption
- โ Custom path and extension
- โ B-Tree indexing
- โ Fast retrieval
- โ No Repeat option
- โ Auto Primary Key option
Copyright 2024 ยฉ Evelocore - All rights reserved
Developed by K.Prabhasha