Mini Program Cloud Base provides multiple basic capabilities. Its primary capabilities are introduced below.

# Database

Cloud Base provides a JSON database in which each record is an object in JSON format. A single database can have multiple collections (equivalent to tables in a relational database). A collection can be regarded as a JSON array, and each object in the array is recorded as a JSON object.

The comparison between a relational database and a JSON database is as follows:

Relational File-based
Database Database
Table Collection
Row Record/Doc
Column Field

The following is an example of a data collection. Assume that we have a books collection that stores book records. There are two books in the collection:

[
  {
    "_id": "Wzh76lk5_O_dt0vO",
    "title": "The Catcher in the Rye",
    "author": "J. D. Salinger",
    "characters": [
      "Holden Caulfield",
      "Stradlater",
      "Mr. Antolini"
    ],
    "publishInfo": {
      "year": 1951,
      "country": "United States"
    }
  },
  {
    "_id": "Wzia0lk5_O_dt0vR",
    "_openid": "ohl4L0Rnhq7vmmbT_DaNQa4ePaz0",
    "title": "The Lady of the Camellias",
    "author": "Alexandre Dumas fils",
    "characters": [
      "Marguerite Gautier",
      "Armand Duval",
      "Prudence",
      "Count de Varville"
    ],
    "publishInfo": {
      "year": 1848,
      "country": "France"
    }
  }
]

In the book information, we use title and author to record the book's title and author, characters to record main characters in the book, and publishInfo to record publication information of the book. We can see that a field can be a string or digit, or an object or array, that is, a JSON object.

Each record has an _id field that serves as its unique identifier and an _openid field that identifies the creator of the record, i.e. the Mini Program user. Note that records created on the management end (console or cloud function) do not have an _openid field because this is for records created by an admin. Developers can customize the _id field, but cannot customize or modify the _openid. The _openid field takes a default value created by the system based on the Mini Program user at the time of file creation. This value can be used by developers to identify and locate a specific file.

Database APIs can be categorized as Mini Program APIs and server APIs. Mini Program APIs implement strict access controls on calling. Developers can directly call APIs in Mini Program for non-sensitive data operations. For data with higher security requirements, operations can be performed by calling server APIs via cloud functions. The cloud function environment is completely isolated from the client, so you can work with database privately and securely via cloud functions.

Database APIs can be called to add, delete, modify, and query database. You can use APIs to perform database operations in three steps: obtain the database reference, construct the query/update conditions, and make a request. The following is an example of a database query in a Mini Program for a record of a book published in the United States.

// 1. Obtain the database reference
const db = wx.cloud.database()
// 2. Construct a query statement
// Use the collection method to obtain the reference of a collection
// Use the where method to import an object. The database returns the JSON file whose field in the collection is equal to the specified value. APIs also support advanced query statements (e.g. greater than, less than, in, etc.). For details, refer to the document for a list of supported statements.
// The get method triggers a network request to retrieve data from the database
db.collection('books').where({
  publishInfo: {
    country: 'United States'
  }
}).get({
  success: function(res) {
  // Output [{ "title": "The Catcher in the Rye", ... }]
  console.log(res)
 }
})

For more information about the usage of database APIs and database management, refer to the Database Guide chapter.

# Storage

Cloud Base provides storage space along with the ability to upload files to the cloud and download files from the cloud subject to permissions management. Developers can manipulate cloud storage by calling APIs via the Mini Program or via cloud functions.

In the Mini Program, you can call wx.cloud.uploadFile and wx.cloud.downloadFile to upload and download files to and from the cloud. The simple code below allows users to select a photo via the Mini Program and upload it to the cloud for further management:

// Allow users to choose a photo
wx.chooseImage({
  success: chooseResult => {
    // Upload the photo to the cloud storage space
    wx.cloud.uploadFile({
      // Specify the cloud directory for upload
      cloudPath: 'my-photo.png',
      // Specify the temporary file directory of the Mini Program to upload
      filePath: chooseResult.tempFilePaths[0],
      // Success callback
      success: res => {
        console.log('Uploaded successfully', res)
      },
    })
  },
})

After a successful upload, the uploaded photo can be viewed via the console.

For more information about storage APIs and management, refer to the Storage Guide chapter.

# Cloud Function

A cloud function is a string of code that is running on the cloud without the need for a management server. You can write a cloud function via the developer toolkit and then upload and deploy it with one-click to run the backend code.

Mini Programs come with APIs that are designed specifically for cloud functions. In a cloud function, developers can use the getWXContext method provided by wx-server-sdk to obtain the context (appid, openid, etc.) of each call. This allows developers to easily and reliably obtain login state info (openid) without having to maintain a complex authentication system.

For example, we define a cloud function named "add" as follows, which adds the uploaded parameters a and b:

// index.js is an entry file. When a cloud function is called, the main method exported from the file is executed.
// event includes parameters transmitted when the caller (Mini Program) calls this function. It also contains the user login status `openId` and Mini Program `appId` that can be obtained by the getWXContext method.
const cloud = require('wx-server-sdk')
exports.main = (event, context) => {
  let { userInfo, a, b} = event
  let { OPENID, APPID } = cloud.getWXContext() // The openId and appId obtained here are reliable
  let sum = a + b

  return {
    OPENID,
    APPID,
    sum
  }
}

After a cloud function is uploaded and deployed via the developer toolkit, we can call it within the Mini Program as follows:

wx.cloud.callFunction({
  // The name of the cloud function to be called
  name: 'add',
  // The parameters to be transmitted to the cloud function
  data:{
    a: 12,
    b: 19,
  },
  // Success callback
  complete: console.log
})
// The promise method is also supported
wx.cloud.callFunction({
  name: 'add',
  data:{
    a: 12,
    b: 19
  }
}).then(console.log)

In a cloud function, if you need to work with databases, manage cloud files, or call other cloud functions, you can use wx-server-sdk in the official npm package.

For more information on cloud function management and APIs, refer to the Storage Guide chapter.

# Cloud Call

Cloud Call is a capability provided by Cloud Base that supports calling Mini Program APIs via cloud functions. It allows server APIs to be called via cloud functions so that operations such as sending template messages and obtaining Mini Program codes can be performed. For more information, refer to the Development Guide chapter.

# HTTP API

Cloud Base resources can also be accessed through HTTP APIs, which allows access from outside the Mini Program. For more information, refer to the HTTP API documentation.

After reading this chapter, we now understand what Cloud Base is, what capabilities it provides, and what it does. Let’s move on to the Development Guide chapter to learn how to get started with Cloud Base.