Package Upgrade
Currently, node-sdk and tcb-admin-node are consistent in cloud functions, file storage, and database functionality, but node-sdk provides a more user-friendly development experience. In the future, node-sdk will continue to iterate to support new features, while tcb-admin-node will be in a maintenance phase and will not add new features.
- Supports batch insertion, update/delete single document (during batch queries)
- Business error codes are thrown with error stack information to facilitate problem localization.
- Some interfaces support timeout setting
- Using the current environment in Cloud Functions
- Better TypeScript support
How to Migrate from tcb-admin-node to @cloudbase/node-sdk?
sdk Initialization Method Change
Example:
- tcb-admin-node supports two initialization methods
// Method 1: Call the api using the tcb object
const tcb = require("tcb-admin-node");
tcb.init({ env: "xxx" });
const db = tcb.database();
const result = await db.collection("coll").where({}).get();
// Method 2: Call the api using the object obtained from tcb.init()
const tcb = require("tcb-admin-node");
const app = tcb.init({ env: "xxx" });
const db = app.database();
const result = await db.collection("coll").where({}).get();
- Using node-sdk only supports initializing instances via init
// Only supports calling the api using the object obtained from tcb.init(), Method 1 (calling the api directly using the object from require) is deprecated
// database
const tcb = require("@cloudbase/node-sdk");
const app = tcb.init({ env: "xxx" });
const db = app.database();
const result = await db.collection("coll").where({}).get();
// function
const result = await app.callFunction({
name: "test",
data: { a: 1 },
});
// storage
const result = await app.uploadFile({
cloudPath: "a|b test.jpeg",
fileContent,
});
Instances must be created via the init method, and then used to call api methods. Multiple init calls will create multiple distinct instances.
⚠️ When using the node-sdk library, be sure to call APIs using instances initialized via the init method, otherwise errors will occur.
Error Handling Changes
Example:
- Error Handling When Using tcb-admin-node sdk
const tcb = require("tcb-admin-node");
tcb.init({ env: "xxx" });
const result = await app.callFunction({
name: "test",
data: { a: 1 },
});
if (result.code) {
// Function execution error: write the logic for handling sdk errors here
}
- Error Handling When Using node-sdk
const tcb = require("tcb-admin-node");
const app = tcb.init({ env: "xxx" });
let result;
try {
result = await app.callFunction({
name: "test",
data: { a: 1 },
});
} catch (e) {
console.log(e.code, e.message);
// Function execution error: write the logic for handling sdk errors here
}
Custom Timeout
Database-Related
Example:
// database
const tcb = require("@cloudbase/node-sdk");
const app = tcb.init({ env: "xxx" });
const db = app.database();
const queryDocumentOpts = {
timeout: 5000,
};
// For example: Query documents
const result = await db
.collection("coll")
.where({})
.options({ timeout: 10000 })
.get();
Function-Related
// function
const tcb = require("@cloudbase/node-sdk");
const app = tcb.init({ env: "xxx" });
const functionOpts = {
timeout: 5000,
};
const result = await app.callFunction(
{
name: "test",
data: { a: 1 },
},
functionOpts
);
Storage-Related
// storage Uploading files
const tcb = require("@cloudbase/node-sdk");
const app = tcb.init({ env: "xxx" });
const uploadFileOpts = {
timeout: 5000,
};
const result = await app.uploadFile(
{
cloudPath: "a|b test.jpeg",
fileContent,
},
uploadFileOpts
);
For custom timeout settings of other file storage interfaces, refer to the File Storage Interface Documentation.