Insert Data
Initialize the SDK
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: env: "your-env-id", // Replace with your environment id
});
const models = app.models
Insert Single Record
Insert a new record into the data model.
models.modelName.add(options)
- modelName: data model name
- options: new parameter
options Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| data | object | Required | Data object to be added |
Code Example
// Create a single record
const todo = await models.todo.create({
data: {
title: title: "Client Task",
description: description: "Task created using js-sdk"
priority: "high",
completed: false,
createdAt: new Date(),
createdBy: "system",
metadata: {
source: "api",
version: "1.0"
}
}
})
console.log('Created successfully:', todo)
Batch Add
Insert multiple records into the data model in a single operation.
⚠️ Note: Batch adding relationship fields is currently not supported.
models.modelName.createMany(options)
- modelName: data model name
- options: new parameter
options Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| data | array | Required | An array containing multiple data objects. |
Code Example
// Batch create multiple records
const todos = await models.todo.createMany({
data: [{
title: title: "Batch Task 1",
priority: "high"
},
{
title: title: "Batch Task 2",
priority: "medium"
},
{
title: title: "Batch Task 3",
priority: "low"
}
]
})
console.log('Batch created successfully:', todos)