node-flat-file-db
v3.0.0
Published
A simple flat-file database module for Node.js
Downloads
20
Maintainers
Readme
Node FlatFileDB
Node FlatFileDB is a simple Node.js module for a flat-file database. It uses JSON files to store data and provides basic CRUD (Create, Read, Update, Delete) operations.
Installation
To install the package, use the following command:
npm install node-flat-file-db
Usage
Importing and Initializing
First, require the module and initialize it with the path to the database file.
const FlatFileDB = require('node-flat-file-db');
const path = require('path');
const db = new FlatFileDB(path.join(__dirname, 'database.json'));
Creating a Record
To create a new record, use the create
method with an ID and the record data.
db.create('1', { name: 'Alice', age: 30 });
console.log('Record created:', db.read('1'));
Reading a Record
To read a record, use the read
method with the record ID.
const record = db.read('1');
console.log('Record read:', record);
Updating a Record
To update an existing record, use the update
method with the record ID and the new data.
db.update('1', { name: 'Alice', age: 31 });
console.log('Record updated:', db.read('1'));
Deleting a Record
To delete a record, use the delete
method with the record ID.
db.delete('1');
console.log('Record deleted:', db.read('1'));
Getting All Records
To get all records, use the getAll
method.
const allRecords = db.getAll();
console.log('All records:', allRecords);
Example
Here's a full example of how to use Node FlatFileDB:
const FlatFileDB = require('node-flat-file-db');
const path = require('path');
const db = new FlatFileDB(path.join(__dirname, 'database.json'));
try {
// Create a new record
db.create('1', { name: 'Alice', age: 30 });
console.log('Record created:', db.read('1'));
// Update the record
db.update('1', { name: 'Alice', age: 31 });
console.log('Record updated:', db.read('1'));
// Read all records
console.log('All records:', db.getAll());
// Delete the record
db.delete('1');
console.log('Record deleted:', db.read('1'));
} catch (error) {
console.error(error.message);
}
License
This project is licensed under the MIT License.