这里重点介绍三个重要的原子操作:read, select, count

read

- 前端

  1. 1this.$api.post('/a/base/atom/read', {
  2. 2 key: { atomId, itemId },
  3. 3}).then(item => {
  4. 4 console.log(item);
  5. 5});

- 后端

  1. 1const user = this.ctx.state.user.op;
  2. 2const item = await this.ctx.bean.atom.read({
  3. 3 key: { atomId, itemId },
  4. 4 user,
  5. 5});

select

- 前端

  1. 1this.$api.post('/a/base/atom/select', {
  2. 2 atomClass,
  3. 3 options,
  4. 4}).then(data => {
  5. 5 console.log(data.list, data.index, data.finished);
  6. 6});

- 后端

  1. 1const user = this.ctx.state.user.op;
  2. 2const items = await this.ctx.bean.atom.select({
  3. 3 atomClass: {
  4. 4 id,
  5. 5 module,
  6. 6 atomClassName,
  7. 7 },
  8. 8 options: {
  9. 9 where: {
  10. 10 ['a.atomName']: { op: 'like', val: 'atomName' }
  11. 11 },
  12. 12 orders: [
  13. 13 [ 'a.updatedAt', 'desc' ],
  14. 14 ],
  15. 15 page: { index: 0, size: 10 },
  16. 16 mode: null,
  17. 17 stage = 'formal',
  18. 18 star = 0,
  19. 19 label = 0,
  20. 20 comment = 0,
  21. 21 file = 0,
  22. 22 language,
  23. 23 category = 0,
  24. 24 tag = 0,
  25. 25 mine = 0,
  26. 26 },
  27. 27 user,
  28. 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排序一致,如

  1. 1orders: [
  2. 2 [ 'a.updatedAt', 'desc' ],
  3. 3],
  • page分页

EggBornJS新增了page分页特性,通过indexsize的组合生成sql语句中的limitoffset

一般而言,前端不需传递page.size,而是由后端统一填充此值,可以在项目的config文件中覆盖page.size的默认值,如下:

src/backend/config/config.default.js

  1. 1// modules
  2. 2config.modules = {
  3. 3 'a-base': {
  4. 4 pageSize: 20,
  5. 5 },
  6. 6};

count

- 前端

  1. 1this.$api.post('/a/base/atom/count', {
  2. 2 atomClass,
  3. 3 options,
  4. 4}).then(count => {
  5. 5 console.log(count);
  6. 6});

- 后端

  1. 1const user = this.ctx.state.user.op;
  2. 2const count = await this.ctx.bean.atom.count({
  3. 3 atomClass,
  4. 4 options,
  5. 5 user,
  6. 6});
  • 返回值: count

  • 参数: 同select