json-filedb-manager
v1.1.1
Published
This project provides a basic implementation of a file-based database system using Node.js. It includes functionality for creating and managing databases and tables, as well as performing CRUD (Create, Read, Update, Delete) operations on table data.
Downloads
9
Readme
Simple Database System
This project provides a basic implementation of a file-based database system using Node.js. It includes functionality for creating and managing databases and tables, as well as performing CRUD (Create, Read, Update, Delete) operations on table data.
Features
- Database Management:
- Create and delete databases.
- Table Management:
- Create tables.
- Insert, retrieve, update, and delete records.
- Drop tables.
Installation
npm install json-filedb-manager
Usage
Creating a Database
To create a new database, use the CreateDB
class:
const { CreateDB } = require('./path/to/your/file');
const db = new CreateDB('myDatabase');
This will create a directory named data/myDatabase if it does not already exist.
Creating a Table
To create a new table within a database, use the Table class:
const { Table } = require('./path/to/your/file');
const table = new Table(db, 'myTable');
This will create a file named data/myDatabase/myTable.json with an empty array as its initial content.
Alter Table Name
To alter the table name, use the alterTableName method:
table.alterTableName('alterMyTable')
This will alter table name from myTable to alterMyTable
Insert Data
To insert data into a table, use the insert method:
const data = { id: 1, name: 'Sample Data' };
table.insert(data);
Retrieve Data
To get all records from a table, use the getAll method:
const allData = table.getAll();
console.log(allData);
To get a record by its ID, use the getById method:
const record = table.getById(1);
console.log(record);
Update Data
To update a record by its ID, use the updateById method:
const updatedData = { id: 1, name: 'Updated Data' };
table.updateById(1, updatedData);
Delete Data
To delete a record by its ID, use the deleteById method:
table.deleteById(1);
To delete all records in a table, use the deleteAll method:
table.deleteAll();
Drop Table
To delete the table file, use the dropTable method:
table.dropTable();
Drop Database
To delete the entire database (including all tables), use the drop method from the CreateDB class:
db.drop();
Error Handling
Ensure that the id field is present in the data when inserting or updating records.
The methods handle file operations and will log errors to the console if issues arise. Specific errors include file system errors during deletion and JSON parsing errors.