1. 云对象操作数据库
创建云对象
![](https://img.haomeiwen.com/i9611412/a36fcab285ba8325.png)
云对象操作数据库
![](https://img.haomeiwen.com/i9611412/03f6083c76b3a94f.png)
![](https://img.haomeiwen.com/i9611412/4f51417e22a1ae6c.png)
![](https://img.haomeiwen.com/i9611412/4fd17c0a93537400.png)
2. DB Schema**
![](https://img.haomeiwen.com/i9611412/b69a300762b8621d.png)
Schema 本地和云端上传下载
![](https://img.haomeiwen.com/i9611412/6eceec5566c74ff7.png)
新建表
![](https://img.haomeiwen.com/i9611412/740e7231718aa4a2.png)
![](https://img.haomeiwen.com/i9611412/1476f08dc2694553.png)
![](https://img.haomeiwen.com/i9611412/3eb4176677b46eb8.png)
关于 Schema 的表结构
自定义一个 Schema
{
"bsonType": "object",
"required": [],
"permission": { // 这里即使开启,字段中也可以二次限制读写更新删除
"read": true,
"create": true,
"update": false,
"delete": false
},
"properties": {
"_id": {
"description": "ID,系统自动生成"
},
"title": {
"bsonType": "string",
"title": "文章标题",
"description": "文章的标题"
},
"content": {
"bsonType": "string",
"title": "内容",
"description": "文章的内容"
},
"posttime": {
"bsonType": "timestamp",
"title": "发布时间",
"description": "文章内容",
"defaultValue": {
"$env": "now"
}
},
"hits": {
"bsonType": "int",
"title": "阅读量",
"defaultValue": 30
}
}
}
新增
![](https://img.haomeiwen.com/i9611412/bb78da0cb49645de.png)
如果没有添加标题也可以新增,前端需要校验下,这里后端处理下,我们再必填选项中把title放入即可,这样如果没有添加标题就会提示
{
"bsonType": "object",
"required": ["title"], // 这里必填字段
"permission": {
"read": true,
"create": true,
"update": false,
"delete": false
},
....
}
不传数据验证
![](https://img.haomeiwen.com/i9611412/006bb045a90279a6.png)
![](https://img.haomeiwen.com/i9611412/2f3ef9e29d2ebf53.png)
![](https://img.haomeiwen.com/i9611412/55d4bd7f60003974.png)
2. JQL 操作数据库
学习 JQL 语法必须了解 DB Schema
新建表
{
"bsonType": "object",
"required": [],
"permission": {
"read": true,
"create": true,
"update": false,
"delete": true
},
"properties": {
"_id": {
"description": "ID,系统自动生成"
},
"name": {
"bsonType": "string",
"description": "姓名",
"minLength": 2,
"maxLength": 10
},
"age": {
"bsonType": "int",
"description": "年龄",
"minimum": 0,
"maximum": 120
},
"tel": {
"bsonType": "string",
"description": "电话号码"
},
"email": {
"bsonType": "string",
"description": "邮箱地址"
},
"posttime": {
"bsonType": "timestamp",
"description": "添加时间",
"defaultValue": {
"$env": "now"
}
}
}
}
![](https://img.haomeiwen.com/i9611412/565291f6998079bf.png)
3. openDB
内置的各种表,目前openDB
已经支持几十张表。点击查看
![](https://img.haomeiwen.com/i9611412/fcfe16aab9884559.png)
4. 引入 uni-id
uni-id-pages
![](https://img.haomeiwen.com/i9611412/855830e79647732a.png)
下载时需要点击注册合并,会把页面自动导入到我们项目,这时候运行会报错,需要重启下。
![](https://img.haomeiwen.com/i9611412/e35d983ef7c0a61d.png)
![](https://img.haomeiwen.com/i9611412/3b2bc25b9a05e25e.png)
![](https://img.haomeiwen.com/i9611412/71ccf182ac9eedb3.png)
![](https://img.haomeiwen.com/i9611412/e441c23bbeb6b086.png)
![](https://img.haomeiwen.com/i9611412/99671ebaa9b61a78.png)
![](https://img.haomeiwen.com/i9611412/c6d86d727b67a1d1.png)
5. DBschema 与 uni-id 配合使用
关联表
![](https://img.haomeiwen.com/i9611412/bf3727f2cee2ad3c.png)
![](https://img.haomeiwen.com/i9611412/4a8d64b0c8e90b0f.png)
![](https://img.haomeiwen.com/i9611412/a318e894b76de16f.png)
![](https://img.haomeiwen.com/i9611412/0cdd51737114ffe2.png)
![](https://img.haomeiwen.com/i9611412/6b95ff7668c6aaaa.png)
DBSchema 表验证问题
![](https://img.haomeiwen.com/i9611412/7d429bfd9996ab60.png)
![](https://img.haomeiwen.com/i9611412/859e7ebc8a20f53d.png)
用户退出登录
相对于聚合,JQL 联表查询语法更简单
![](https://img.haomeiwen.com/i9611412/4055eb638043e2ef.png)
![](https://img.haomeiwen.com/i9611412/25b97766d9494da7.png)
![](https://img.haomeiwen.com/i9611412/3045dd29bf240fae.png)
![](https://img.haomeiwen.com/i9611412/a84dd39e4e1afd45.png)
![](https://img.haomeiwen.com/i9611412/8546246e928bbf07.png)
![](https://img.haomeiwen.com/i9611412/61614c04f5ff9476.png)
![](https://img.haomeiwen.com/i9611412/c63ba3842cdf94be.png)
![](https://img.haomeiwen.com/i9611412/2d404e39d281818f.png)
![](https://img.haomeiwen.com/i9611412/1ea86abff16ae9f1.png)
查询一条数据的时候,本来就一个对象还是被数组包括,这里可以在 get 开启 getOne 来获取对象,不要数组。
![](https://img.haomeiwen.com/i9611412/47724132e7702186.png)
网友评论