Skip to main content

Insert Data

Initialize the SDK

import cloudbase from "@cloudbase/js-sdk";

const app = cloudbase.init({
env: "your-env-id", // Replace with your environment id
});

const db = app.rdb();

Insert Data

Insert data into a table through the insert() method, supporting single row and bulk inserts.

db.from(tableName).insert(values, options)
  • tableName: table name
  • values: data to insert
  • options: insert options configuration

Parameter Description

ParameterTypeRequiredDescription
valuesobject | ArrayYesValues to insert. Pass an object to insert a single row, or pass an array to insert multiple rows.
optionsobjectNoInsert options configuration

options Parameter Description

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

Code Examples

Create a Record

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

console.log('Insert result:', error ? 'Failed' : 'Success');

Create a Record and Return Data

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

console.log('Insert 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.

Bulk Create Records

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

console.log('Bulk insert result:', error ? 'Failed' : 'Success');