Develop a Push API for Unbody to enable seamless record and file management operations in custom collections.
Description and Goal
Create a developer-friendly API that allows developers to easily perform CRUD operations on records and files within the Unbody ecosystem. The goal is to simplify record management and file handling in custom collection, abstract complex API details from developers, and provide an intuitive api for common operations.
Use Cases
Seamlessly Perform CRUD operations (Create, Read, Update, Delete) on records within a collection.
Create custom collection.
Upload, delete, and manage various file types (Image, Audio, Video, TextDocument)
Handle cross references in custom collections.
Implementation Steps
Create a base class to handle REST API operations with Axios interceptors, ensuring consistent communication with the Unbody backend apis.
Design the API interface and class structure, creating a PushApi instance that manages interactions with collections and records using APIKey, projectId, and sourceId.
Implement record CRUD functionality.
Develop a record save method utilizing PATCH requests.
Develop file-handling capabilities supporting various input types (fileBuffer, stream, formData) and handle file validations.
Implement updating records with file references, enabling multiple file references to be updated simultaneously.
Process and structure API responses in a developer-friendly format.
Implement robust error handling and validation.
Conduct thorough testing across various use cases.
Create comprehensive documentation and usage examples.
const profileRecords: Record[] = await profileCollectionInstance.getRecords();
// Find a specific record using JS array `find` method
const specificRecord = profileRecords.find(
(record) => record.firstName === "Jane"
);
// Or find record by recordId from collectionInstance
const specificRecord = await profileCollectionInstance.getOneRecord("recordId");
Use record instance to delete, patch, and update the record
await specificRecord.delete(); // Delete the record
await specificRecord.patch({ firstName: "Janet" }); // Partial update
await specificRecord.update({
firstName: "Jane",
lastName: "Smith",
...otherFields,
}); // Full update
4. Record Operations Without a Reference Variable or instance
// Delete a record by explicitly specifying collection name and record ID
await pushApiInstance.records.delete("ProfileCollection", "customProfileId");
// Patch (partial update) a record by collection name and record ID
await pushApiInstance.records.patch<ProfileCollection>(
"ProfileCollection", // CollectionName
"customProfileRecordId",
{
firstName: "Janet",
}
);
// Update (full object replacement) a record by collection name and record ID
await pushApiInstance.records.update<ProfileCollection>(
"ProfileCollection",
"customProfileRecordId",
{
firstName: "Jane",
lastName: "Smith",
}
);
const uploadedFile = await pushApiInstance.files.create(
"customFileRecordId",
"Buffer | Stream | FormData",
{
fileName: "example.txt",
mimeType: "text/plain",
}
);
// Delete from uploaded file instance
await uploadedFile.delete();
// Delete a file using its fileId (same as recordId)
await pushApiInstance.files.delete("exampleFileRecordId");
// Alternatively, delete a file from a record. Since files are treated as records:
await pushApiInstance.records.delete("ImageBlock", "exampleFileRecordId");
// Fetch all files
await pushApiInstance.files.getAll();
File Record Operations without Reference Variables
// Fetch all files and delete the first one
const files: File[] = await pushApiInstance.files.getAll(); // method chaining (limit, offset, sorting) can be used here also
await files[0].delete(); // Delete the first file
Proposal: Implement Unbody Push API
Brief Description
Develop a Push API for Unbody to enable seamless record and file management operations in custom collections.
Description and Goal
Create a developer-friendly API that allows developers to easily perform CRUD operations on records and files within the Unbody ecosystem. The goal is to simplify record management and file handling in custom collection, abstract complex API details from developers, and provide an intuitive api for common operations.
Use Cases
Implementation Steps
API Interface Design
1. Create a PushApi Instance
Type Definition for
PushApi
with GenericsType Definition for
CollectionInstance
2. Using Generics with Collections
To customize the type of the records in a collection, you can define a type for your collection.
Defining a Custom Type for
ProfileCollection
Retrieving Records from
collectionInstance
3. CRUD Operations on Collection Instance
Create
Update, Patch, and Delete Record
Type Definition for
RecordInstance
Use record instance to delete, patch, and update the record
Fetch Records with Method Chaining
4. Record Operations Without a Reference Variable or instance
5. File Operations
Type Definition for
FileInstance
Uploading and Deleting Files
File Record Operations without Reference Variables
6. Cross-Reference Operations
Type Definition for
CrossReferenceField
Add and Remove Cross-References from profileRecordInstance
Add cross reference using recordId and collectionName
Patch or update a record with cross-reference fields using pushApiInstance
Alternatively, directly patch the record by placing the uploaded file instance
Full update with cross-reference fields