folderbase
v1.5.0
Published
Folderbase is a folder based database for NodeJS!
Downloads
2
Maintainers
Readme
Folderbase
Folderbase is a folder based database for NodeJS!
This is a nosql, easy to setup database in which you just need to install folderbase and start using it!
Installation
To install folderbase, just run the following command:-
Latest version
npm i folderbase@latest
OR
yarn add folderbase@latest
Specific version
npm i folderbase@<version>
OR
yarn add folderbase@<version>
Note: Remember to replace <version> with your version, for example 1.0.0!
Docs
In folderbase, database are called folders, tables are called files, rows are called objects and columns are called fields!
Importing the module
To import and use this module, you need to define your nodejs project as a module, you can simply do this by adding a field in your package.json
file:-
{
"type": "module"
}
This package only exports a class named as Folder
which helps in all of the database and table related operations!
import Folder from '@alphasoftx/folderbase';
Creating a new folder (database)
To create a new folder in folderbase, you can use Folder
class by the following method:-
// Creating a database with name 'todo-app'
let todoApp = new Folder('todo-app');
This will create a new folder in your project with name todo-app
where the data of this database will be saved!
If the folder already exists, and it contains files, the files will be added to todoApp.files
!
If tou have multiple databases and you want to create your all databases in a particular folder, you can use the following method:-
// Creating 'todo-app' in a folder named 'databases'
let todoApp = new Folder('todo-app','databases');
Removing a folder:-
To remove a folder, you can easily use the following code:-
// Removing a folder named 'todo-app'
todoApp.remove();
This will remove the folder todo-app
!
Note: The variable todoApp should not be used after removal of the folder 'todo-app'!
Adding a new file (table)
To add a new file in folderbase, run the following code:-
// Adding a file 'users' in 'todo-app' folder
todoApp.addFile('users');
// File will be saved in todoApp.files.users
This will create a users.json
in todo-app
folder where the data of users file will be saved!
Removing a file
To remove a file, use following code:-
// Removing a file 'users' from 'todo-app' folder
todoApp.removeFile('users');
This will remove the users.json
file!
Working with files
Adding data
To add data in you table, you can use add
query:-
todoApp.files.users.add({
name: 'Raju',
username: 'RajeshSharma',
password: 'Rajesh31012013'
});
// This will add this user to 'users' file,
// with an extra field '_id' which contains a unique id of that field!
Note: Don't add '_id' field in your object while adding, because it will be overwritten to a unique id!
Retrieving data
To get data from the file, you can use find
query:-
// To get all the entries
todoApp.files.users.find({});
// To get filtered data
// Returns all entries with username 'RajeshSharma'
todoApp.files.users.find({ username: 'RajeshSharma' });
// Filters strings by Regular expressions
// Returns all entries which contains 'Rajesh' in username
todoApp.files.users.find({ username: /Rajesh/ });
// Filter entries case insensitively
// Return all entries with username 'RajeshSharma' case insensitively
todoApp.files.users.find({ username: /^RajeshSharma$/i });
Updating data
To update data, you can use update
query:-
// First parameter: find query obj,
// Second parameter: obj with fields to update
// Third parameter: replace (boolean), default value is false
// Updates the password field of object
todoApp.files.users.update({
username: 'Aditya'
},{
password: 'Adi18012008'
});
// Replace the whole user except _id field
todoApp.files.users.update({
username: 'Aditya'
},{
name: 'Aditya Verma',
username: 'AdityaVerma',
password: 'Adi18012008'
}, /* replace */ true);
Removing data
To remove data from files, you can use remove
query:-
// Removes all fields with name 'Abhishek'
todoApp.files.users.remove({
name: 'Abhishek'
});
Hope this was helpful for you!