# cloud.database

Gets reference to the database.

The method signature is shown as below:

function database(options?: object): Database

The method accepts an optional object parameter "options", whose field is defined as follows:

Field Type Required Default Description
env string No - Environment ID

Sample code

The following call can help obtain a reference to the database of the default environment:

const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()

Suppose we have an environment test used as a test environment, we can get the following test environment database:

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

We can also pass in the default environment through init to make it the default environment database when the database is obtained:

const cloud = require('wx-server-sdk')
cloud.init({
  env: 'test'
})
const testDB = cloud.database()
点击咨询小助手