Building Simple Data Schema: A Push API and Schema Definition Example
This guide demonstrates how to create a simple product catalog using Unbody. We’ll create a project with a custom schema, set up a Push API source, and show how to add and retrieve data.
Creating Project with Custom Schema
First, let’s create a project with necessary settings and define a simple product schema with basic fields:
// Creating Projects with custom schema
const projectSettings = new ProjectSettings();
projectSettings
.set(new TextVectorizer(TextVectorizer.OpenAI.TextEmbedding3Small))
.set(new ImageVectorizer(ImageVectorizer.Img2VecNeural.Default))
.set(new Generative(Generative.OpenAI.GPT4o))
.set(new AutoSummary(AutoSummary.OpenAI.GPT4o))
.set(new AutoVision(AutoVision.OpenAI.GPT4o));
const project = admin.projects.ref({
name: "Products Projects",
settings: projectSettings,
});
const customSchema = new CustomSchema();
const productCollection = new CustomSchema.Collection("ProductCollection");
// Add fields to Product collection
productCollection.add(new CustomSchema.Field.Text("name", "Product Name"));
productCollection.add(
new CustomSchema.Field.Number("price", "Product Price")
);
productCollection.add(
new CustomSchema.Field.Text("description", "Product Description")
);
productCollection.add(
new CustomSchema.Field.Boolean("inStock", "Stock Status")
);
// Add the collection to schema
customSchema.add(productCollection);
project.settings.set(customSchema);
// Saving the project will create the project
await project.save();
Setting Up Push API Source
After creating the project, we need to set up a Push API source to enable data ingestion:
// Create Push Api Source
const project = await admin.projects.get({
id: "de272ab3-a4e3-445a-8b0e-6e73d44857d3",
});
// Create source reference
const source = project.sources.ref({
name: "Push API Source",
type: SourceTypes.PushApi,
});
await project.sources.save(source);
Adding Product Data
Now we can use the Push API to add product data to our collection:
// Upload custom Data using Push Api
// Product Data
const productData = {
name: "Wireless Mouse",
price: 29.99,
description: "Ergonomic wireless mouse with long battery life",
inStock: true,
};
const { data } = await pushApi.records.create({
id: `product-${Date.now()}`,
collection: "ProductCollection",
payload: productData,
});
Initializing the Source
For the changes to take effect, we need to initialize the source:
const project = await admin.projects.get({
id: "de272ab3-a4e3-445a-8b0e-6e73d44857d3",
});
// Using "Push API Source" as source id
const source = await project.sources.get({
id: "c47a5279-cf03-4d4c-89b4-96a27d7ae796",
});
await source.initialize();
Fetching Product Data
Finally, let’s retrieve the product data using TypeScript interfaces for type safety:
import {
BooleanField,
NumberField,
StringField,
} from "unbody"
// Define Typescript interface and use imported unbody field types
interface IProduct {
name: StringField;
price: NumberField;
description: StringField;
inStock: BooleanField;
}
// Use unbody.get to fetch `ProductCollection` Data
const {
data: { payload },
} = await unbody.get
.collection<IProduct>("ProductCollection")
.select("name", "description", "price", "inStock")
.exec();
Learn More
For more detailed information, check out these guides: