Product Overview
Features and Strengths
Use Cases
System Limits
@cloudbase/node-sdk 3.2.0 or later versions in the corresponding cloud function directory.package.json is generated by default in the cloud function directory, and the user will be prompted whether to install dependencies locally immediately. Note that the runtime environment of the cloud function is Node.js, so ensure Node.js is installed before dependencies are installed locally, with both node and npm in the environmental variables. If dependencies are not installed locally, run the following command in the directory:npm install --save @cloudbase/node-sdk@3.2
const cloudbase = require("@cloudbase/node-sdk");// Specify the CloudBase environment ID.const app = cloudbase.init({env: "some-env-id",});exports.main = async (event, context) => {const models = app.models;// Now you can call the CRUD methods on the models.// models.post.create({// data: {// body: "Hello, World\\n\\nfrom china",// title: "Hello, World",// slug: "hello-world-cn",// },// }).then(({ data } => { console.log(data)}))};
npm install @cloudbase/js-sdk --save
import cloudbase from "@cloudbase/js-sdk";// Import the SDK.const app = cloudbase.init({env: "your-cloud-env-id", // Replace this value with your CloudBase environment ID.clientId: "your-cloud-env-id", // Replace this value with your CloudBase environment ID.});const auth = app.auth({persistence: "local",});await auth.signInAnonymously(); // Or use other login methods.const models = app.models;// Now you can call the CRUD methods on the models.// Example: Create a post data record.// models.post.create({// data: {// body: "Hello, World\\n\\nfrom china",// title: "Hello, World",// slug: "hello-world-cn",// },// }).then(({ data } => { console.log(data)}))
@cloudbase/js-sdk supports multi-platform calls. If you have web and Mini Program development requirements, use @cloudbase/js-sdk via API call.@cloudbase/js-sdk has built-in adapters for web clients and Mini Programs, no configuration is needed on these two platforms.@cloudbase/js-sdk can incorporate multiple adapters. At runtime, the platform type is determined by the isMatch function of each adapter, and then the corresponding compatibility logic is introduced. For example, the following code is compatible with QQ Mini Games, Cocos native, and Baidu Mini Games:import cloudbase from '@cloudbase/js-sdk';import adapter as adapterOfQQGame from 'cloudbase-adapter-qq_game';import adapter as adapterOfCocosNative from 'cloudbase-adapter-cocos_native';import adapter as adapterOfBDGame from 'cloudbase-adapter-bd_game';cloudbase.useAdapters([adapterOfQQGame,adapterOfCocosNative,adapterOfBDGame]);
@cloudbase/js-sdk in a Mini Program, if the Mini Program package size needs to be reduced, you can import functional modules as needed.// Kernel.import cloudbase from "@cloudbase/js-sdk/app";// Login module.import "@cloudbase/js-sdk/auth";// Cloud function module.import "@cloudbase/js-sdk/functions";// Cloud storage module.import "@cloudbase/js-sdk/storage";// Database module.import "@cloudbase/js-sdk/database";// Real-time push module, which should be imported after the database module is imported.import "@cloudbase/js-sdk/realtime";// Ad reporting module.import "@cloudbase/js-sdk/analytics";const app = cloudbase.init({env: "your-env-id",});
피드백