Skip to main content

Add data

Initialize SDK

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

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

const db = app.database();
const _ = db.command; // Get query command

Add new

Add a new record to the collection.

db.collection(collectionName).add(data);
  • collectionName: Collection Name
  • data: Data object to add

Parameter Description

ParameterTypeRequiredDescription
dataobjectYesData object to add

Sample Code

// Add a single record
const result = await db.collection("todos").add({
title: "Learn CloudBase"
content: "Complete the database operation tutorial"
completed: false,
priority: "high",
createdAt: new Date(),
tags: ["study", "technology"]
});

console.log("successful addition, document ID:", result._id);

Add location

// Create a GeoPoint
const point = new db.Geo.Point(longitude, latitude);

// Create a geographic path
const line = new db.Geo.LineString([
new db.Geo.Point(lngA, latA),
new db.Geo.Point(lngB, latB),
]);

Creating a region
const polygon = new db.Geo.Polygon([
new db.Geo.LineString([
new db.Geo.Point(lngA, latA),
new db.Geo.Point(lngB, latB),
new db.Geo.Point(lngC, latC),
new db.Geo.Point(lngA, latA), // close
]),
]);

const result = await db.collection("todos").add({
location: point,
path: line,
area: polygon,
});