schema的配置从字段上说分为表级配置和字段级配置
- properties内的配置属于字段级配置,描述单个字段
- 其他的都是表级配置,描述表或者字段和字段之间
- bsonType描述数据的类型
- 描述必填字段required
"required": ["username", "mobile"],
properties字段
- 用于描述存储什么样子的数据
"username": {
// 描述字段的类型
"bsonType": "string",
// title 用于schemaToCode生成的代码的标题
"title": "姓名1",
// 描述,备注字段意思是什么,再生成用户名的时候会把description当作placeholder提示
"description": "姓名2",
// 和order一个意思,表示字段生成的排序
"order": 1,
// 修建输入的空格内容
"trim": "both"
},
"gender": {
"bsonType": "int",
"title": "性别",
"description": "用户性别:0 未知 1 男性 2 女性",
"order": 2,
// 默认值
"defaultValue": 0,
// 枚举,性别的三种选项,支持带描述的数组和简单数组
"enum": [{
"text": "未知",
"value": 0
}, {
"text": "男",
"value": 1
}, {
"text": "女",
"value": 2
}]
},
- 如果做多选,定义一下渲染的组件
- 当bsonType是数组,用了枚举,默认生成控件就是多选框
"gender": {
// 如果是多选,数 据类型就是数组
"bsonType": "array",
"title": "性别",
"description": "用户性别:0 未知 1 男性 2 女性",
"order": 2,
// 默认值
"defaultValue": 0,
// 枚举,性别的三种选项,支持带描述的数组和简单数组
"enum": [{
"text": "未知",
"value": 0
}, {
"text": "男",
"value": 1
}, {
"text": "女",
"value": 2
}],
// 指定渲染的组件,并且传入参数
"componentForEdit": {
"name": "uni-data-checkbox",
"props": {
"multiple": true
}
}
},
把选项放到一张数据表,制作选择民族
- 关联数据表
"nation_china": {
"bsonType": "string",
"title": "民族",
"enum": {
"collection": "opendb-nation-china",
// 指定value和展示text
"field": "_id as value, name as text"
},
// 指定外键
"foreignKey": "opendb-nation-china._id"
},
- 这样就能渲染出选择民族
渲染树形结构
- 新建中国城市表
- 这样配置之后就能生成三级选择城市
"address": {
"bsonType": "string",
"title": "地址",
"enum": {
"collection": "opendb-city-china",
"field": "code as value, name as text"
},
"enumType": "tree"
},
电话
"mobile": {
"bsonType": "string",
"title": "电话",
"order": 3,
"description": "电话",
// 设置了正则表达式
"pattern": "^\\+?[0-9-]{3,20}$",
"trim": "both"
},
邮箱
"email": {
"bsonType": "string",
// 用了内置的验证器
"format": "email",
"title": "邮箱",
"order": 4,
"description": "邮箱地址",
"trim": "both"
},
给民族字段增加关联字段
"nation_china": {
"bsonType": "string",
"title": "民族",
"enum": {
"collection": "opendb-nation-china",
"field": "_id as value, name as text"
},
"foreignKey": "opendb-nation-china._id"
},
通过关联字段联查到数据
<unicloud-db ref="udb" v-slot:default="{data, pagination, loading, hasMore, error}" collection="opendb-contacts, opendb-nation-china" field="nation_china{name}">
<view v-if="error">{{error.message}}</view>
<view v-else-if="data">
<uni-list>
<uni-list-item v-for="(item, index) in data" :key="index" showArrow :clickable="true" @click="handleItemClick(item._id)">
<template v-slot:body>
<text>
{{item.nation_china[0].name}}
</text>
</template>
</uni-list-item>
</uni-list>
</view>
<uni-load-more :status="loading?'loading':(hasMore ? 'more' : 'noMore')"></uni-load-more>
</unicloud-db>