my-crud-api-fatezila
v1.0.0
Published
A simple CRUD API package that can be used as both npm module and npx command
Readme
My CRUD API Package
A simple, flexible CRUD API package that can be used both as an npm module and as an npx command-line tool.
Features
- ✅ Create, Read, Update, Delete operations
- 🔍 Search functionality with multiple criteria
- 📊 Statistics and data insights
- 🖥️ CLI Interface for command-line usage
- 🌐 HTTP Server mode for API endpoints
- 📦 NPM Module for programmatic usage
Installation
As an NPM Package (for use in other projects)
npm install my-crud-apiAs an NPX Command (global usage)
npx my-crud-api --helpUsage
1. As an NPM Module
const CrudAPI = require('my-crud-api');
// Create a new API instance
const api = new CrudAPI();
// Create a new item
const item = api.create({
name: 'John Doe',
email: '[email protected]',
age: 30
});
// Read all items
const allItems = api.read();
// Read a specific item
const specificItem = api.read(1);
// Update an item
const updatedItem = api.update(1, {
name: 'Jane Doe',
age: 31
});
// Delete an item
const result = api.delete(1);
// Search items
const searchResults = api.search({
name: 'John'
});
// Get statistics
const stats = api.getStats();2. As an NPX Command
Create an item
npx my-crud-api create "John Doe" --description "Software Developer" --type "person"Read items
# Read all items
npx my-crud-api read
# Read specific item
npx my-crud-api read 1Update an item
npx my-crud-api update 1 --name "Jane Doe" --age 31Delete an item
npx my-crud-api delete 1Search items
npx my-crud-api search --name "John" --type "person"Get statistics
npx my-crud-api statsStart HTTP server
npx my-crud-api server --port 3000Clear all data
npx my-crud-api clear3. HTTP Server Mode
When you run the server, it provides REST API endpoints:
npx my-crud-api serverAvailable endpoints:
GET /api/items- Get all itemsGET /api/items/:id- Get item by IDPOST /api/items- Create new itemPUT /api/items/:id- Update itemDELETE /api/items/:id- Delete itemGET /api/search- Search items (with query parameters)GET /api/stats- Get statistics
Example API calls:
# Create an item
curl -X POST http://localhost:3000/api/items \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "[email protected]"}'
# Get all items
curl http://localhost:3000/api/items
# Update an item
curl -X PUT http://localhost:3000/api/items/1 \
-H "Content-Type: application/json" \
-d '{"name": "Jane Doe"}'
# Search items
curl "http://localhost:3000/api/search?name=John"API Reference
CrudAPI Class
Constructor
const api = new CrudAPI();Methods
create(item)
Creates a new item.
- Parameters:
item(object) - The item to create - Returns: The created item with ID and timestamps
- Throws: Error if item is invalid
read(id?)
Reads one or all items.
- Parameters:
id(number, optional) - Item ID to read - Returns: Single item or array of all items
- Throws: Error if item not found
update(id, updates)
Updates an existing item.
- Parameters:
id(number) - Item ID to updateupdates(object) - Fields to update
- Returns: Updated item
- Throws: Error if item not found
delete(id)
Deletes an item.
- Parameters:
id(number) - Item ID to delete - Returns: Success message
- Throws: Error if item not found
search(criteria)
Searches items by criteria.
- Parameters:
criteria(object) - Search criteria - Returns: Array of matching items
getStats()
Gets API statistics.
- Returns: Statistics object with total items, next ID, etc.
clear()
Clears all data.
- Returns: Success message
Development
Local Development
- Clone the repository
- Install dependencies:
npm install - Test the CLI:
node bin/cli.js --help - Test the server:
node index.js
Publishing to NPM
Make sure you're logged in to npm:
npm loginUpdate the version in
package.json:npm version patch # or minor, majorPublish the package:
npm publishTest the published package:
npx my-crud-api --help
Examples
Example 1: Using in a Node.js Project
// app.js
const CrudAPI = require('my-crud-api');
const api = new CrudAPI();
// Add some sample data
api.create({ name: 'Alice', role: 'Developer' });
api.create({ name: 'Bob', role: 'Designer' });
api.create({ name: 'Charlie', role: 'Manager' });
// Search for developers
const developers = api.search({ role: 'Developer' });
console.log('Developers:', developers);
// Get statistics
const stats = api.getStats();
console.log('Total users:', stats.totalItems);Example 2: Building a Web Application
// server.js
const express = require('express');
const CrudAPI = require('my-crud-api');
const app = express();
const api = new CrudAPI();
app.use(express.json());
// Your custom routes using the CRUD API
app.get('/users', (req, res) => {
const users = api.read();
res.json(users);
});
app.post('/users', (req, res) => {
const user = api.create(req.body);
res.status(201).json(user);
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});License
MIT
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
Support
If you encounter any issues or have questions, please open an issue on the GitHub repository.
