Firebase Auto CRUD API
firecrudnewapi is an NPM package that automatically generates a fully functional CRUD API for Firebase Firestore collections, making backend development fast and efficient. The package provides RESTful endpoints for creating, reading, updating, and deleting data, with support for custom routes.
Features
- Automatically generate CRUD API for Firebase Firestore collections.
- Custom routes to extend beyond basic CRUD.
- Bulk operations for batch updates and deletions.
- Schema enforcement for structuring data.
- Easy-to-use and flexible for various Firebase applications.
Installation
npm install firecrudnewapi
Usage
Here’s how you can quickly set up your Firebase-based CRUD API.
1. Initialize Firebase and Create API
const { createFirebaseCrudAPI } = require('firecrudnewapi');
const serviceAccount = require('./serviceAccountKey.json');
const databaseURL = 'https://your-firebase-project.firebaseio.com';
const productSchema = {
name: '',
price: 0,
category: '',
};
const app = createFirebaseCrudAPI(serviceAccount, databaseURL, 'products', productSchema);
app.listen(3000, () => {
console.log('Firebase CRUD API is running on port 3000');
});
2. Schema Definition
Define a schema for structuring your collection's documents:
const productSchema = {
name: '',
price: 0,
category: '',
};
The schema helps in structuring the documents with default values when creating or updating data.
3. Custom Routes
You can add custom routes to extend the functionality of the basic CRUD API. For example, to get expensive products:
const customRoutes = [
{
method: 'get',
path: '/api/products/expensive',
handler: async (req, res) => {
try {
const expensiveProducts = await firestore.collection('products').where('price', '>', 1000).get();
const results = expensiveProducts.docs.map(doc => ({ id: doc.id, ...doc.data() }));
res.status(200).send(results);
} catch (error) {
res.status(500).send(error);
}
},
},
];
Pass customRoutes
when initializing the CRUD API:
const app = createFirebaseCrudAPI(serviceAccount, databaseURL, 'products', productSchema, customRoutes);
API Endpoints
The package auto-generates the following endpoints for each collection:
1. Create: POST /api
2. Read All: GET /api
- Retrieves all documents from the Firestore collection.
- Supports filtering, sorting, and pagination.
3. Read One: GET /api/:id
- Retrieves a specific document by its ID.
4. Update: PUT /api/:id
- Updates a specific document by its ID.
5. Delete: DELETE /api/:id
- Deletes a specific document by its ID.
6. Bulk Update: POST /api/bulk-update
- Updates multiple documents at once.
- Example request:
{
"updates": [
{ "id": "docId1", "data": { "name": "Updated Product A" } },
{ "id": "docId2", "data": { "price": 200 } }
]
}
7. Bulk Delete: POST /api/bulk-delete
How to Use Custom Libraries
You can use any additional libraries in your custom routes, such as moment for date handling:
const moment = require('moment');
const customRoutes = [
{
method: 'get',
path: '/api/products/recent',
handler: async (req, res) => {
const now = moment().subtract(30, 'days').toDate();
const recentProducts = await firestore.collection('products').where('createdAt', '>', now).get();
const results = recentProducts.docs.map(doc => ({ id: doc.id, ...doc.data() }));
res.status(200).send(results);
},
},
];
Dependencies
express
: Used to create the server and handle routes.
body-parser
: Middleware to parse incoming request bodies.
firebase-admin
: The official Firebase Admin SDK to interact with Firestore.
License
This project is licensed under the ISC license.