# Cloud Development

# Prerequisites

Sample Code

// Add the following configuration in app.json
"plugins": {
  "cloud": {
    "version": "0.0.1",
    "provider": "641a62448a00310001441def"
  }
}
// Add the following content in app.js
ft.loadExtApi({
  name: 'cloud',
  fun: requirePlugin('cloud')
})

# init

Supported since Base Library 3.0.35

init(Object object)

Initialization.

Parameters

Object object

Property Type Default Value Required Description
env string object Yes

Sample Code

ft.cloud.init({
  env: 'test-111',
  appid: 'xxx', // WeChat Mini-Program ID
  secret: 'xxx', // WeChat Mini-Program AppSecret
})

# database

Supported since Base Library 3.0.35

database(Object object)

Database initialization.

Parameters

Object object

Property Type Default Value Required Description
env string object Yes

Sample Code

const testDB = ft.cloud.database({
  env: 'test'
})

# Cloud Function

Supported since Base Library 3.0.35

callFunction(Object object)

Cloud Function.

Parameters

Object object

Property Type Default Value Required Description
name string Yes Function name
data object
config object

object.success Callback Function

Parameters

Object res

Property Type Description
errMsg string
result any Result returned by the cloud function
requestID string Cloud function execution ID, can be used for log query

Sample Code

ft.cloud.callFunction({
  // Cloud function name
  name: 'add',
  // Parameters passed to the cloud function
  data: {
    a: 1,
    b: 2,
  },
  success: function(res) {
  }
})

# add

Supported since Base Library 3.0.35

add(Object object)

Insert data (Database).

Parameters

Object object

Property Type Default Value Required Description
data object Yes Data to insert

object.success Callback Function

Parameters

Object res

Property Type Description
errMsg string
_id string

Sample Code

db.collection('xxx').add({
  data: {
    description: "learn cloud database",
  },
  success: function(res) {
    console.log(res)
  }
})

# get

Supported since Base Library 3.0.35

get(Object object)

Query data (Database).

Parameters

Object object

Property Type Default Value Required Description
data object Yes Data to insert

object.success Callback Function

Parameters

Object res

Property Type Description
errMsg string
data object Data

Sample Code

db.collection('xxx').doc('xxx').get({
  success: function(res) {
    console.log(res.data)
  }
})

# update

Supported since Base Library 3.0.35

update(Object object)

Update data (Database).

Parameters

Object object

Property Type Default Value Required Description
data object Yes Data to update

object.success Callback Function

Parameters

Object res

Property Type Description
errMsg string
data object Data

Sample Code

db.collection('xxx').doc('xxx').update({
  data: {
    done: true
  },
  success: function(res) {
    console.log(res.data)
  }
})

# remove

Supported since Base Library 3.0.35

remove(Object object)

Delete data (Database).

Parameters

Object object

Property Type Default Value Required Description
data object Yes

object.success Callback Function

Parameters

Object res

Property Type Description
errMsg string
data object Data

Sample Code

db.collection('xxx').doc('xxx').remove({
  success: function(res) {
    console.log(res.data)
  }
})

# Querying and Updating Arrays/Nested Objects

Supported since Base Library 3.0.35

Sample Code

// Simple matching
db.collection('xxx').where({
  progress: 50
}).get()

// Matching nested fields in records

db.collection('xxx').where({
  style: {
    color: 'red'
  }
}).get()

// Method 2
db.collection('xxx').where({
  'style.color': 'red'
}).get()

// Matching arrays
db.collection('xxx').where({
  numbers: [10, 20, 30]
}).get()

// Matching elements in arrays
db.collection('xxx').where({
  numbers: 20
}).get()

# Building Query Conditions

Supported since Base Library 3.0.35

Sample Code

// Specify the upper limit of the query result set
db.collection('todos').limit(10)
  .get()
  .then(console.log)
  .catch(console.error)

// Specify to start returning results from after a specified sequence when returning query results, commonly used for pagination
db.collection('todos').skip(10)
  .get()
  .then(console.log)
  .catch(console.error)

// Specify the fields that records need to return in the result set
db.collection('todos').field({
  description: true,
  done: true,
  progress: true,
}).get()
  .then(console.log)
  .catch(console.error)

# uploadFile

Supported since Base Library 3.0.35

uploadFile(Object object)

Upload file (Storage).

Parameters

Object object

Property Type Default Value Required Description
cloudPath string Yes Cloud storage path
filePath string Yes Path of the file resource to upload
config Object Configuration
success string Success callback
fail string Failure callback
complete string Completion callback

object.success Callback Function

Parameters

Object res

Property Type Description
errMsg string File ID
fileID object Data
statusCode number HTTP status code returned by the server

Sample Code

ft.cloud.uploadFile({
  cloudPath: 'xxx.png',
  filePath: '',
  success: res => {
    console.log(res.fileID)
  },
  fail: err => {
  }
})

# downloadFile

Supported since Base Library 3.0.35

downloadFile(Object object)

Download file from cloud storage space (Storage).

Parameters

Object object

Property Type Default Value Required Description
fileID string Yes
config Object Configuration
success string Success callback
fail string Failure callback
complete string Completion callback

object.success Callback Function

Parameters

Object res

Property Type Description
errMsg string File ID
tempFilePath object Temporary file path
statusCode number HTTP status code returned by the server

Sample Code

ft.cloud.downloadFile({
  fileID: 'xxx',
  success: res => {
    console.log(res.tempFilePath)
  },
  fail: err => {
  }
})

# deleteFile

Supported since Base Library 3.0.35

deleteFile(Object object)

Delete files from cloud storage space, up to 50 files at a time (Storage).

Parameters

Object object

Property Type Default Value Required Description
fileList Object Yes File list
success string Success callback
fail string Failure callback
complete string Completion callback

object.success Callback Function

Parameters

Object res

Property Type Description
errMsg string File ID
fileList object File list

Sample Code

ft.cloud.deleteFile({
  fileList: ['xxx'],
  success: res => {
    console.log(res.fileList)
  },
  fail: err => {
  },
  complete: res => {
  }
})

# getTempFileURL

Supported since Base Library 3.0.35

getTempFileURL(Object object)

Exchange cloud file ID for a real URL. Links obtained for publicly readable files do not expire, while links for private files are valid for ten minutes. Up to 50 files can be processed at a time (Storage).

Parameters

Object object

Property Type Default Value Required Description
fileList Object Yes File list
success string Success callback
fail string Failure callback
complete string Completion callback

object.success Callback Function

Parameters

Object res

Property Type Description
errMsg string File ID
fileList object File list

Sample Code

ft.cloud.getTempFileURL({
  fileList: ['cloud://xxx', 'cloud://yyy'],
  success: res => {
    console.log(res.fileList)
  },
  fail: err => {
  }
})

# Unimplemented Features

  1. Query Commands
  2. Aggregate Queries
  3. Real-time Data Push
  4. Geolocation
  5. traceUser
  6. serverDate
  7. Constructing Regular Expressions
  8. Creating Collections
  9. Support for passing cloud file IDs in components like image, audio, etc.

::: tips Note

Alternative solutions for unimplemented features:

Can be implemented through cloud function calls, as follows:

Import the wx-server-sdk library in the cloud function, for example:

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

exports.main = async (event) => {
  const { ENV, OPENID, APPID } = cloud.getWXContext()
  const db = cloud.database()
  cosnt _ = db.command
  const dbResult = await db.collection('test').where({ progress: _gt(10) }).get()
  return {
    dbResult,
    ENV,
    OPENID,
    APPID,
  }
}

:::

© FinClip with ❤ , Since 2017