这里重点介绍三个重要的原子操作:read
, select
, count
read
- 前端
- 1this.$api.post('/a/base/atom/read', {
- 2 key: { atomId, itemId },
- 3}).then(item => {
- 4 console.log(item);
- 5});
- 后端
- 1const user = this.ctx.state.user.op;
- 2const item = await this.ctx.bean.atom.read({
- 3 key: { atomId, itemId },
- 4 user,
- 5});
select
- 前端
- 1this.$api.post('/a/base/atom/select', {
- 2 atomClass,
- 3 options,
- 4}).then(data => {
- 5 console.log(data.list, data.index, data.finished);
- 6});
- 后端
- 1const user = this.ctx.state.user.op;
- 2const items = await this.ctx.bean.atom.select({
- 3 atomClass: {
- 4 id,
- 5 module,
- 6 atomClassName,
- 7 },
- 8 options: {
- 9 where: {
- 10 ['a.atomName']: { op: 'like', val: 'atomName' }
- 11 },
- 12 orders: [
- 13 [ 'a.updatedAt', 'desc' ],
- 14 ],
- 15 page: { index: 0, size: 10 },
- 16 mode: null,
- 17 stage = 'formal',
- 18 star = 0,
- 19 label = 0,
- 20 comment = 0,
- 21 file = 0,
- 22 language,
- 23 category = 0,
- 24 tag = 0,
- 25 mine = 0,
- 26 },
- 27 user,
- 28});
- 返回值
名称 | 说明 |
---|---|
data.list | 条目列表 |
data.index | 下一页的分页索引 |
data.finished | 数据是否已经获取完毕。如果获取完毕,就不会再获取下一页 |
- 参数
名称 | 允许为空 | 说明 |
---|---|---|
atomClass | 是 | 原子类型 |
options | 否 | 查询参数 |
- 查询参数:options
原子的select
操作实现原理是:根据用户提交的参数组合sql语句,其中会联合一些系统表,从而取得用户所希望的信息
名称 | 允许为空 | 缺省值 | 说明 |
---|---|---|---|
where | 是 | 查询条件 | |
orders | 是 | 排序 | |
page.index | 否 | 分页索引 | |
page.size | 是 | 20 | 分页大小 |
mode | 是 | 联合业务表的模式,可选参数:default/full/search | |
stage | 是 | formal | 查询哪个原子阶段:draft/formal/history |
star | 是 | 0 | 联合查询用户星标 |
label | 是 | 0 | 联合查询用户标签 |
comment | 是 | 0 | 联合查询原子评论列表 |
file | 是 | 0 | 联合查询原子附件列表 |
language | 是 | 语言 | |
category | 是 | 0 | 目录 |
tag | 是 | 0 | 标签 |
mine | 是 | 0 | 我的原子 |
mode
: 可通过模块meta
文件中的tableNameModes
添加更多模式的配置,一般是配置mysql的数据视图
stage
: 原子生命周期的三个阶段,分开查询
- 联合表清单:
表名称 | 表别名 | 说明 |
---|---|---|
aAtom | a | 原子基础表 |
aAtomClass | b | 原子类型表 |
aAtomStar | d | 用户星标 |
aAtomLabelRef | e | 用户标签 |
如:testPartyView | f | 原子业务表 |
aUser | g | userIdCreated 对应的用户信息 |
aUser | g2 | userIdUpdated 对应的用户信息 |
aComment | h | 评论表 |
aFile | i | 附件表 |
aCategory | j | 目录表 |
aTagRef | k | 标签表 |
- where条件
EggBornJS中的where
条件类似于EggJS中的where
条件,并进行了扩展,支持更多条件设置,参见:Model方法
- orders排序
EggBornJS中的orders
排序用法与EggJS中的orders
排序一致,如
- 1orders: [
- 2 [ 'a.updatedAt', 'desc' ],
- 3],
- page分页
EggBornJS新增了page
分页特性,通过index
和size
的组合生成sql语句中的limit
和offset
一般而言,前端不需传递page.size
,而是由后端统一填充此值,可以在项目的config
文件中覆盖page.size
的默认值,如下:
src/backend/config/config.default.js
- 1// modules
- 2config.modules = {
- 3 'a-base': {
- 4 pageSize: 20,
- 5 },
- 6};
count
- 前端
- 1this.$api.post('/a/base/atom/count', {
- 2 atomClass,
- 3 options,
- 4}).then(count => {
- 5 console.log(count);
- 6});
- 后端
- 1const user = this.ctx.state.user.op;
- 2const count = await this.ctx.bean.atom.count({
- 3 atomClass,
- 4 options,
- 5 user,
- 6});
-
返回值: count
-
参数: 同
select
评论: