jsondb-apicli
v1.0.2
Published
A lightweight, file-based JSON database with both CLI interface and REST API capabilities. Built with Node.js, this project provides a simple yet powerful way to manage data using JSON files.
Downloads
15
Maintainers
Readme
JSON Database CLI & API
A lightweight, file-based JSON database with both CLI interface and REST API capabilities. Built with Node.js, this project provides a simple yet powerful way to manage data using JSON files.
🚀 Features
Core Database Features
- Table Management: Create and delete tables with custom column definitions
- Data Types Support: string, number, boolean, array, date, null
- CRUD Operations: Create, Read, Update, Delete records
- Automatic ID Generation: Each record gets a unique timestamp-based ID
- Data Validation: Type conversion and validation based on column definitions
CLI Interface
- Interactive Menu: User-friendly command-line interface
- Smart Prompts: Dynamic prompts based on table structure
- Server Management: Start/stop server with status monitoring
REST API
- Full CRUD API: Complete REST endpoints for all operations
- CORS Support: Cross-origin requests enabled
- JSON Responses: Consistent JSON API responses
- Error Handling: Comprehensive error handling and status codes
📦 Installation
Global Installation (Recommended)
npm install -g jsondb-apicliLocal Installation
- Clone the repository:
git clone https://github.com/dpoetika/jsondb-apicli
cd jsondb-apicli- Install dependencies:
npm install🛠️ Usage
Global Installation Usage
# Start REST API server
jsondb-apicli start
# Run interactive CLI
jsondb-apicli cli
# Show help
jsondb-apicli helpLocal Installation Usage
# Start REST API server
npm start
# Run interactive CLI
npm run cli
# Start server directly
node server.js
# Run CLI directly
node index.js📋 Usage
CLI Interface
Run the application:
node index.jsAvailable Operations:
Create Table
- Enter table name
- Define columns with data types (e.g.,
name:string,age:number,active:boolean)
Delete Table
- Remove entire table and all its data
Insert Record
- Select table
- Enter values for each column (type conversion handled automatically)
Delete Record
- Specify table and record ID
Update Record
- Modify existing records with new values
- Leave fields empty to skip updates
List Records
- View table structure and all records
- Apply filters to search records (e.g.,
name==yunus,age>25)
Start Server
- Launch REST API server
- Monitor server status
- Stop server when needed
Data Types
| Type | Description | Example Input | Stored As |
|------|-------------|---------------|-----------|
| string | Text data | "John Doe" | "John Doe" |
| number | Numeric data | 25 | 25 |
| boolean | True/false | true or false | true |
| array | List of values | "apple,banana,orange" | ["apple", "banana", "orange"] |
| date | Date/time | "2024-01-15" | "2024-01-15T00:00:00.000Z" |
| null | Null value | null | null |
Filtering Operators
When listing records, you can apply filters using the following operators:
| Operator | Description | Example |
|----------|-------------|---------|
| == | Equal to (matches the full value) | name==yunus emre |
| != | Not equal to | age!=25 |
| > | Greater than | age>25 |
| < | Less than | age<30 |
| >= | Greater than or equal | age>=25 |
| <= | Less than or equal | age<=30 |
| :contains | Contains text (case-insensitive, partial match) | email:contains:gmail |
| :startsWith | Starts with text (case-insensitive) | name:startsWith:yu |
| :endsWith | Ends with text (case-insensitive) | email:endsWith:.com |
Multiple filters can be combined with commas:
- CLI:
name==yunus emre,age>25,email:contains:gmail - API:
?filter=name==yunus emre,age>25,email:contains:gmail
REST API
Base URL
http://localhost:3000Endpoints
Table Management
GET /tables- List all tablesPOST /tables- Create new tableDELETE /tables/:tableName- Delete tableGET /tables/:tableName- Get table structure and data
Record Operations
GET /tables/:tableName/records- Get all records from tableGET /tables/:tableName/records?filter=name==yunus emre,age>25- Get filtered recordsPOST /tables/:tableName/records- Add new recordGET /tables/:tableName/records/:recordId- Get specific recordPUT /tables/:tableName/records/:recordId- Update recordDELETE /tables/:tableName/records/:recordId- Delete record
API Examples
Create Table:
curl -X POST http://localhost:3000/tables \
-H "Content-Type: application/json" \
-d '{
"tableName": "users",
"columns": [
{"name": "name", "type": "string"},
{"name": "age", "type": "number"},
{"name": "email", "type": "string"},
{"name": "active", "type": "boolean"}
]
}'Add Record:
curl -X POST http://localhost:3000/tables/users/records \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"age": 30,
"email": "[email protected]",
"active": true
}'Get All Records:
curl http://localhost:3000/tables/users/recordsGet Filtered Records:
# Full value match
curl "http://localhost:3000/tables/users/records?filter=name==yunus%20emre"
# Partial match
curl "http://localhost:3000/tables/users/records?filter=name:contains:yunus"
# Multiple filters
curl "http://localhost:3000/tables/users/records?filter=name:contains:yunus,age>10"Update Record:
curl -X PUT http://localhost:3000/tables/users/records/{id} \
-H "Content-Type: application/json" \
-d '{
"age": 31,
"active": false
}'Delete Record:
curl -X DELETE http://localhost:3000/tables/users/records/{id}📁 File Structure
jsondb/
├── index.js # Main CLI application
├── db.js # Database operations
├── server.js # REST API server
├── package.json # Dependencies and scripts
├── README.md # This file
└── data/ # Database files (auto-created)
├── users.json
├── products.json
└── ...🔧 Database File Format
Each table is stored as a JSON file with the following structure:
{
"columns": [
{"name": "name", "type": "string"},
{"name": "age", "type": "number"},
{"name": "active", "type": "boolean"}
],
"data": [
{
"id": "1703123456789",
"name": "John Doe",
"age": 30,
"active": true
}
]
}🚨 Error Handling
The application includes comprehensive error handling:
- Table not found - When accessing non-existent tables
- Record not found - When accessing non-existent records
- Invalid data types - Automatic type conversion with fallbacks
- API errors - Proper HTTP status codes and error messages
- File system errors - Graceful handling of file operations
🔒 Security Considerations
- File-based storage - Data is stored in local JSON files
- No authentication - API is open by default (add authentication for production)
- CORS enabled - Cross-origin requests allowed
- Input validation - Basic validation on data types and formats
🚀 Production Deployment
For production use, consider:
- Add authentication to the REST API
- Use HTTPS for secure communication
- Implement rate limiting to prevent abuse
- Add logging for monitoring and debugging
- Use environment variables for configuration
- Implement backup strategies for data files
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
🆘 Support
For issues, questions, or contributions:
- Create an issue on GitHub
- Check the documentation at
http://localhost:3000/when server is running - Review the API endpoints and examples above
Built with ❤️ using Node.js, Express, and Inquirer
