Skip to main content

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 db = app.rdb();

// or specify the instance and database
// const db = app.rdb({
// instance: "<instance>",
// database: "<database>"
// });

Adding Data

Insert data into the table via the insert() method, supporting both single-row and batch insertion.

db.from(tableName).insert(values, options);
  • tableName: table name
  • values: The data to be inserted
  • options: insertion options configuration

Parameter Description

ParameterTypeRequiredDescription
valuesobject | ArrayRequiredThe values to be inserted. Pass an object to insert a single row, or pass an array to insert multiple rows.
optionsobjectNoInsertion options configuration

options Parameter Description

ParameterTypeRequiredDescription
countstringNoCounting algorithm, optional values: "exact" - underlying execution of COUNT(*)

Code Example

Creating Records

// Insert a record into the articles table
const { error } = await db
.from("articles")
.insert({ title: "New Article Title", content: "Article Content" });

console.log("Insertion result:", error ? "failed" : "succeeded");

Creating Records and Returning Data

// Insert records and return the inserted data
const { data, error } = await db
.from("articles")
.insert({ title: "New Article Title", content: "Article Content" })
.select();

console.log("Insertion result:", data);

💡 Note: The .select() method returns the inserted rows only when the table has a single primary key and that primary key is of auto-increment type.

Batch Creating Records

// Batch insert multiple records
const { error } = await db.from("articles").insert([
{ title: "First Article", content: "First Article Content" },
{ title: "Second Article", content: "Second Article Content" },
]);

console.log("Batch insertion result:", error ? "failed" : "succeeded");