Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SDK database database index


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Database

Cloud development SDK database instances

Property

Command command

The database operator

Geo Geo

The geographic structure of the database

Method

Database.collection(name: string): Collection

Gets a reference to the collection. The method accepts a name parameter that specifies the name of the collection to be referenced.

Database.createCollection(collectionName: string): Promise<Object>

Creating a collection, if it already exists, fails to create

Database.serverDate(options: Object): ServerDate

Construct a reference to the service-side time. Can be used to query criteria, update field values, or add field values when records are added.

Database.runTransaction(callback: function, times: number): Promise<any>

The transaction is initiated. Can only be used in cloud functions.

Database.startTransaction(): Promise<Transaction>

To start a transaction, another API that you can also use to initiate a transaction is runTransaction. Can only be used in cloud functions.

Example of a small terminal

The following calls get a reference to the database of the default environment:

const db = wx.cloud.database()

Suppose you have an environment called test-123 that is used as a test environment, you can get the test environment database as follows:

const testDB = wx.cloud.database({
  env: 'test-123'
})

An example of the cloud function side

The following calls get a reference to the same database as the cloud function's current environment:

const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()

Assuming that there is an environment called test, which is used as a test environment, you can get the test environment database as follows:

const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const testDB = cloud.database({
  env: 'test'
})

You can also pass in the default environment through init so that the default environment database is when you get the database:

const cloud = require('wx-server-sdk')
cloud.init({
  env: 'test'
})
const testDB = cloud.database()