Skip to main content

Performing CRUD Operations

Firebase Express SDK provides a powerful toolset to perform CRUD (Create, Read, Update, Delete) operations on your Firestore database using HTTP requests. In this guide, we will walk you through how to use Firebase Express SDK to handle each of these operations.

HTTP GET - Reading Data

You can use the HTTP GET request to retrieve data from your Firestore collections. Here's how to define an API endpoint for reading data:

firebaseStarter.addActions([
{
collection: "users",
endpoint: "/api/getusers",
request: {
type: "GET",
},
},
// Other GET endpoints
]);

HTTP POST - Creating Data

To create new data entries, you can use the HTTP POST request. Here's how to define an API endpoint for creating data:

firebaseStarter.addActions([
{
collection: "users",
endpoint: "/api/adduser",
request: {
type: "POST",
},
},
// Other POST endpoints
]);

HTTP PUT - Updating Data

The HTTP PUT request is used to update existing data entries. You can define an API endpoint for updating data like this:

firebaseStarter.addActions([
{
collection: "users",
endpoint: "/api/changeuserbyid/:id",
request: {
type: "PUT",
paramKey: "id",
},
},
// Other PUT endpoints
]);

HTTP PATCH - Partially Updating Data

To perform partial updates on existing data, you can use the HTTP PATCH request. Define an API endpoint for partial updates:

firebaseStarter.addActions([
{
collection: "users",
endpoint: "/api/changeuseragebyid/:id",
request: {
type: "PATCH",
paramKey: "id",
},
},
// Other PATCH endpoints
]);
Note

Request body must be provided for POST, PATCH, PUT requests.

HTTP DELETE - Deleting Data

The HTTP DELETE request is used to delete data entries. Here's how to define an API endpoint for deleting data:

firebaseStarter.addActions([
{
collection: "users",
endpoint: "/api/deleteuserbyid/:id",
request: {
type: "DELETE",
paramKey: "id",
},
},
// Other DELETE endpoints
]);

Make a DELETE request to the endpoint to delete the document with the specified ID.

http://localhost:3000/api/deleteuserbyid/49DGoDgK1alNx8QWCv7n

Handling Document Attributes in Request Body

When using Firebase Express SDK, you can utilize the documentAttributes configuration to define the expected properties for creating or updating documents. The request body should provide the necessary attributes to ensure accurate data handling. If any required attributes are missing, a warning message will be returned in the response.

Request Body Structure

Here's how you can structure the request body to match the documentAttributes configuration:

{
"attribute1": "value1",
"attribute2": "value2",
// ... Other attributes
}

For instance, if your documentAttributes for the "users" collection include "name", "surname", and "age", the request body should provide values for these attributes:

{
"name": "John",
"surname": "Doe",
"age": 25
}

Handling Missing Attributes

In cases where the request body is missing certain attributes defined in documentAttributes, Firebase Express SDK is configured to return a response with a warning. This helps ensure that the necessary data is provided for successful data manipulation.

For example, if the request body is missing the "surname" attribute, the response could include a warning message like this:

{
"status": "ERROR",
"message": "Missed keys: surname"
}

Firebase Express SDK simplifies this process by enabling you to define and enforce document attribute requirements, allowing for consistent and reliable data management.

Conclusion

By defining API endpoints for each CRUD operation, you can easily manage your Firestore data using Firebase Express SDK. These examples demonstrate how to structure your API endpoints for different operations.

Firebase Express SDK simplifies the process of handling CRUD operations, allowing you to focus on building powerful applications without dealing with the complexities of database interactions.