云开发
技术产生背景
Saas: 常见软件
Pass: 钉钉,云服务器,阿里云,腾讯云。 amazon ec2, amzure
Lass: 算力,存储,网络,存储
Serverless(faas+baas): Serverless
一些概念
云数据库, 可以类比MongoDB,可以通过 http api 访问操作
官方 quickStart
1 | // 官方 quickStart 例子 |
1 | // /selectRecord/index.js |
1 | const getOpenId = require('./getOpenId/index'); |
小程序请求1
2
3
4
5
6
7
8
9
10wx.cloud.callFunction({
name: 'quickStart', // 请求的一个云函数
data:{ type: 'getItemByItemName', title} // data 对应 event, type 是具体那个接口
}).then( resp =>{
console.log('返回结果', resp)
}).catch( error =>{
console.error('异常',error )
})
云数据库
云函数本地调试
云函数本地调试, 有些Bug,遇到问题重启即可。云函数本地调试
控制台数据库高级操作
云开发控制台-> 高级操作, 控制台数据库高级操作
日期字段的处理
日期 最好是转成事件戳进行存储, new Date().getTime()
, 这样按照 orderBy(日期字段, desc | asc)
记录一下使用过的SQL语句
promise 风格
1 | const db = wx.cloud.database() |
回调 风格
1 | const db = wx.cloud.database() |
db.collection().count()
db.collection('item').count().then( e=> e.total)
db.collection().get()
db.collection('item').limit(pageSize).skip(skipIndex).get().then( e => e.data)
db.command
const _ = db.command
_.eq
, 是否相等_.push
,数组push_.pull
,数组匹配元素移除
db.collection().aggregate()
聚合 aggregate
match 根据条件过滤文档
sample 随机从文档中选取指定数量的记录db.collection('item').aggregate().match({ publish:_.eq(1) }).sample({ size:1 }).end()
在集合中发起聚合操作,选取1条 publish字段等于1的记录返回
db.collection().update()
db.collection('user').where({}).update({ data: { })}})
HTTP API 请求
https://developers.weixin.qq.com/minigame/dev/wxcloud/reference-http-api/
- 获取 accesstoken
- 触发云函数消耗资源太多,建议后台访问云数据库时换成 http api 请求
例子
以 查询api为例
- sql拼接需要注意
- 返回结果是字符串,可以按照自己需要转成 对象