微信小程序云開發(fā)最重要的有兩點:
1、云數(shù)據(jù)庫;
2、云函數(shù);
學會這兩點基本就能夠進行微信小程序的云開發(fā);
首先,我們先看微信小程序云數(shù)據(jù)庫的基本操作:
1)打開微 信開發(fā)者工具,創(chuàng)建一個云開發(fā)微信小程序,在創(chuàng)建項目時勾選使用云開發(fā)即可;(注意:使用云開發(fā)需要使用appId,不能使用測試號,第一次使用云開發(fā)的用戶需要去開通云開發(fā)功能)
2)點擊菜單欄的云開發(fā)選項,進入云開發(fā)控制面板,創(chuàng)建一個新的開發(fā)環(huán)境,然后點擊數(shù)據(jù)庫,在當前環(huán)境的數(shù)據(jù)庫中創(chuàng)建一個新的集合lists:
?然后點擊數(shù)據(jù)庫中的數(shù)據(jù)權限根配置自己所需的數(shù)據(jù)庫權限;
詳細權限可見:權限控制 | 微信開放文檔
3)在項目app.js中做如下配置:
// app.js
App({
onLaunch: function () {
if (!wx.cloud) {
console.error('請使用 2.2.3 或以上的基礎庫以使用云能力');
} else {
wx.cloud.init({
// env 參數(shù)說明:
// env 參數(shù)決定接下來小程序發(fā)起的云開發(fā)調用(wx.cloud.xxx)會默認請求到哪個云環(huán)境的資源
// 此處請?zhí)钊氕h(huán)境 ID, 環(huán)境 ID 可打開云控制臺查看
// 如不填則使用默認環(huán)境(第一個創(chuàng)建的環(huán)境)
env: '所需開發(fā)環(huán)境的環(huán)境ID',
traceUser: true,
});
}
this.globalData = {};
}
});
環(huán)境ID在這個地方:
?4)云數(shù)據(jù)庫的初始化及增刪改查:
以如上數(shù)據(jù)庫lists集合為例:
// 初始化數(shù)據(jù)庫
const?db?=?wx.cloud.database();
// 獲取 數(shù)據(jù)庫中l(wèi)ists集合
const?lists=db.collection('lists');
插入:
db.collection('集合名稱').add({
data:{
// data 字段表示需新增的 JSON 數(shù)據(jù)
}
}).then(res=>{console.log(res))
拋出如下信息即為添加成功:
?刪除:
db.collection('集合名稱').doc('要刪除字段的_id').remove().then(res=>{})
拋出如下信息即為刪除成功:
更新:
db.collection('集合名稱').doc('需要更改的那條數(shù)據(jù)的_id').update({
// data 傳入需要局部更新的數(shù)據(jù)
data: {
}
}).then(res=>{})
?eg:
const _ = db.command
db.collection('lists').doc('0ab5303b6286010904a878280d163117').update({
data: {
// 表示指示數(shù)據(jù)庫將age字段自增 1
age: _.inc(1)
},
success: function(res) {
console.log(res.data)
}
})
拋出如下信息即為更新成功:
查詢:
查詢一條記錄的數(shù)據(jù):
db.collection('集合名稱').doc('該條數(shù)據(jù)的_id').get().then(res => {
// res.data 包含該記錄的數(shù)據(jù)
console.log(res.data)
})
根據(jù)條件查詢:
db.collection('集合名稱').where({
// 查詢條件 如:
age:18
}).get().then(res => {
// res.data 包含age為18的所有數(shù)據(jù)
console.log(res.data)
})
查詢集合所有數(shù)據(jù):
如果要獲取一個集合的數(shù)據(jù),比如獲取 todos 集合上的所有記錄,可以在集合上調用?get
?方法獲取,但通常不建議這么使用,在小程序中我們需要盡量避免一次性獲取過量的數(shù)據(jù),只應獲取必要的數(shù)據(jù)。為了防止誤操作以及保護小程序體驗,小程序端在獲取集合數(shù)據(jù)時服務器一次默認并且最多返回 20 條記錄,云函數(shù)端這個數(shù)字則是 100。開發(fā)者可以通過?limit
?方法指定需要獲取的記錄數(shù)量,但小程序端不能超過 20 條,云函數(shù)端不能超過 100 條。
db.collection('集合名稱').get().then(res => {
// res.data 是一個包含集合中有權限訪問的所有記錄的數(shù)據(jù),不超過 20 條
console.log(res.data)
})
下面是在云函數(shù)端獲取一個集合所有記錄的例子,因為有最多一次取 100 條的限制,因此很可能一個請求無法取出所有數(shù)據(jù),需要分批次?。?/p>
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const MAX_LIMIT = 100
exports.main = async (event, context) => {
// 先取出集合記錄總數(shù)
const countResult = await db.collection('集合名稱').count()
const total = countResult.total
// 計算需分幾次取
const batchTimes = Math.ceil(total / 100)
// 承載所有讀操作的 promise 的數(shù)組
const tasks = []
for (let i = 0; i < batchTimes; i++) {
const promise = db.collection('集合名稱').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
tasks.push(promise)
}
// 等待所有
return (await Promise.all(tasks)).reduce((acc, cur) => {
return {
data: acc.data.concat(cur.data),
errMsg: acc.errMsg,
}
})
}
更多數(shù)據(jù)庫操縱語句查看:初始化 | 微信開放文檔文章來源:http://www.zghlxwxcb.cn/news/detail-482494.html
更多API查看:微信開放文檔文章來源地址http://www.zghlxwxcb.cn/news/detail-482494.html
到了這里,關于微信小程序云開發(fā) 1 - 數(shù)據(jù)庫的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!