云函数
可以看做java或者php,作为后端服务
cloudfunctions/myCloud/index.js
exports.main = async (event, context) => {
const { name, age } = event
return `我是${name},今年${age}`
};
pages/index/index.vue
//callFunction方法 在前端和云端都可以调用另一个云函数
uniCloud.callFunction({
name: "myCloud",
data:{
name:"小白",
age:22
}
}).then(res => {
console.log(res)
})
浏览器
![](https://img-blog.csdnimg.cn/img_convert/64e0c0000f85cc1084d8bbebcdd78ced.png)
db.command
查询及更新指令用于在 where 中指定字段需满足的条件,指令可通过 db.command 对象取得
云函数通过传统方式操作数据库
查-cloudfunctions/cloudDome/index.js
const db = uniCloud.database();
exports.main = async (event, context) => {
// 获取 `users` 集合的引用
const collection = await db.collection('users').get()
return collection
};
查-pages/index/index.vue
<template>
<view class="content" v-for="item in user" :key="item._id">
{{item.name}} -- {{item.gender}}
</view>
</template>
<script>
import { reactive, toRefs } from "vue"
export default {
setup() {
const data = reactive({
user:{}
})
uniCloud.callFunction({
name: "cloudDome",
data:{}
}).then(res => {
data.user = res.result.data
})
return {
...toRefs(data)
}
}
}
</script>
查-浏览器
![](https://img-blog.csdnimg.cn/img_convert/68a85316db7be31ffeaeb565a2d87776.png)
doc,指定 id 的记录的引用
const db = uniCloud.database();
exports.main = async (event, context) => {
const collection = await db.collection('users').doc("640aa8330c801c2ac9373731").get()
return collection
};
limit
const db = uniCloud.database();
exports.main = async (event, context) => {
const collection = await db.collection('users').limit(3).get()
return collection
};
skip,与limit实现翻页效果,skip指跳过的数据
const db = uniCloud.database();
exports.main = async (event, context) => {
const collection = await db.collection('users').limit(3).skip(3).get()
return collection
};
orderBy
const db = uniCloud.database();
exports.main = async (event, context) => {
const collection = await db.collection('users').orderBy("_id","desc").get()
return collection
};
field,指定返回字段
const db = uniCloud.database();
exports.main = async (event, context) => {
const collection = await db.collection('users').field({ 'name': true,'_id':false }).get()
return collection
};
where与查询筛选指令 Query Command
const db = uniCloud.database();
const dbCmd = db.command
exports.main = async (event, context) => {
const collection = await db.collection('users').where({
//age:dbCmd.gt(20).and(dbCmd.lt(48))
age:dbCmd.and(dbCmd.gt(8),dbCmd.lt(48))
//age:dbCmd.or(dbCmd.and(dbCmd.gt(8),dbCmd.lt(48)),dbCmd.eq(50))
}).get()
return collection
};
正则表达式查询
const db = uniCloud.database();
const dbCmd = db.command
exports.main = async (event, context) => {
const { keyWord } = event
const collection = await db.collection('users').where({
name: new RegExp(keyWord,'ig') //i表示忽略大小写 g表示全局搜索
}).get()
return collection
};
增-cloudfunctions/cloudDome/index.js
const db = uniCloud.database();
exports.main = async (event, context) => {
const obj = {
"name": "小红",
"gender": "未知",
"tel": 1333333333,
"mail": "564654654@qq.com"
}
const collection = await db.collection('users').add(obj)
return collection
};
增-pages/index/index.vue
uniCloud.callFunction({
name: "cloudDome",
data:{}
}).then(res => {
console.log(res)
})
增-数据库
![](https://img-blog.csdnimg.cn/img_convert/2c4c47254285570b47c2e7fb6f6fb5cc.png)
改-cloudfunctions/cloudDome/index.js
const db = uniCloud.database()
exports.main = async (event, context) => {
const { keyWord } = event
const collection = await db.collection('users').doc("640aa8330c801c2ac9373731").update({
name:'张三被改名了',
age:0
})
return {
message:'修改成功',
collection
}
};
改-数据库
![](https://img-blog.csdnimg.cn/img_convert/501673be6f3f92c7d818c8e0402744af.png)
批量改-cloudfunctions/cloudDome/index.js
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
const { keyWord } = event
const collection = await db.collection('users').where({
age:dbCmd.lt(20)
}).update({
text:'小于二十岁的小年轻'
})
return {
message:'修改成功',
collection
}
};
批量改-数据库
![](https://img-blog.csdnimg.cn/img_convert/070659b816b4a77d07d7626f1191d9a1.png)
push,向数组尾部追加元素
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
const { keyWord } = event
const collection = await db.collection('users').doc("640aa867f43e603fbeb7771b").update({
like:dbCmd.push(["烟花"])
})
return {
message:'修改成功',
collection
}
};
//中间添加
const collection = await db.collection('users').doc("640aa867f43e603fbeb7771b").update({
like: dbCmd.push({
each: ["大","白"],
position: 1
})
})
inc,多用户的点赞
const collection = await db.collection('users').doc("640aa867f43e603fbeb7771b").update({
love:dbCmd.inc(1)
})
删-cloudfunctions/cloudDome/index.js
const db = uniCloud.database()
exports.main = async (event, context) => {
const collection = await db.collection('users').doc("640aae9ff43e603fbeb96150").remove()
return {
message: '修改成功',
collection
}
};
批量删-cloudfunctions/cloudDome/index.js
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
const collection = await db.collection('users').where({
name:/小/ig
}).remove()
return {
message: '修改成功',
collection
}
};
云对象
展示-cloudfunctions/demoObj/index.obj.js
const db = uniCloud.database()
module.exports = {
_before: function() { // 通用预处理器 这个不用管,留着就好
},
async get(num) {
return await db.collection("user").limit(num).get()
},
add: async () => {
await db.collection('user').add({
name:'张麻子',
content:'我张麻子没有麻子'
})
}
}
展示-pages/index/index.vue
const cloudObj = uniCloud.importObject("demoObj") //导入云对象
export default {
setup() {
cloudObj.get(3).then(res=>{
console.log(res)
})
cloudObj.add().then(res =>{
console.log(res)
})
return {}
}
}
DB Schema属性节点
foreignKey外键
文章表通过外键的形式绑定user表_id,这样文章就能绑定单个用户,形成关联
//user表
{
"_id": "606e721280b8450001e773c6",
"category_id": "606e6feb653b8400017214a3",
"title": "这里是标题",
"content": "这里是正文",
"user_id": "60b9315801033700011ba9ed",
"publish_date": 1617850851000,
"publish_ip": "119.131.174.251"
}
//文章表
"properties": {
"_id": {
"description": "存储文档 ID(用户 ID),系统自动生成"
},
"user_id": {
"bsonType": "string",
"description": "文章作者ID, 参考`uni-id-users` 表",
"foreignKey": "uni-id-users._id",
"defaultValue": {
"$env": "uid"
}
},
"title":{},
"content":{}
}
permission数据权限系统
变量名 | 说明 |
auth.uid | 用户id |
doc | 数据库中的目标数据记录,用于匹配记录内容/查询条件 |
{
"permission": {
"read": "doc.status==true", // 任何用户都可以读status字段的值为true的记录,其他记录不可读
"create": "doc.userid == auth.uid", // 文章中携带的userid 登录 用户id,才能进行发布文章
"update": false, // 禁止更新数据(admin权限用户不受限)
"delete": false // 禁止删除数据(admin权限用户不受限)
}
}
JQL语法
art文章表关联了 user标的_id,并命名为userid
const db = uniCloud.database()
//结尾的方法是getTemp,对usersTemp与artList表过滤 得到临时表
let userTemp = db.collection("usersTemp").field("_id,username,nickname").getTemp();
let artTemp = db.collection("artList").field("userid,title,content,").getTemp();
db.collection(artTemp,userTemp).orderBy("_id desc").get().then(res=>{
console.log(res)
})
}