JavaScript/thinkjs/model

来自康健生活
Kangkk​(留言 | 贡献)2026年11月26日 (四) 08:37的版本 (文字替换 -“{{Leftrightflex|”替换为“{{Double-columns|”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

model

复合查询

1. 复合查询,获取画板数组,关联表为画板表和画板历史表。sql 语句为。

let result = await designArtboardModel.join({
    table: 'design_artboard_history',
    as: 'dah',
    join: 'left',
    on: ['history_id', 'id']
  }).field(`${prefix}design_artboard.id,
    ${prefix}design_artboard.name,
    ${prefix}design_artboard.team_id,
    ${prefix}design_artboard.work_id,
    ${prefix}design_artboard.group_id,
    ${prefix}design_artboard.history_id,
    ${prefix}design_artboard.resource,
    ${prefix}design_artboard.format,
    ${prefix}design_artboard.x,
    ${prefix}design_artboard.y,
    ${prefix}design_artboard.width,
    ${prefix}design_artboard.height,
    dah.create_time,
    dah.message`)
  .where({[`${prefix}design_artboard.team_id`]: team_id, [`${prefix}design_artboard.work_id`]: work_id, ...where})
  .order(`${prefix}design_artboard.name`)
  .select();
SELECT
	md_design_artboard.id,
	md_design_artboard.name,
	md_design_artboard.team_id,
	md_design_artboard.work_id,
	md_design_artboard.group_id,
	md_design_artboard.history_id,
	md_design_artboard.resource,
	md_design_artboard.format,
	md_design_artboard.x,
	md_design_artboard.y,
	md_design_artboard.width,
	md_design_artboard.height,
	dah.create_time,
	dah.message
FROM
	`md_design_artboard`
LEFT JOIN
	`md_design_artboard_history`
	AS
		`dah`
	ON
		`md_design_artboard`.`history_id` = `dah`.`id`
WHERE
		( md_design_artboard.team_id = '54' )
	AND
		( md_design_artboard.work_id = '82' )
ORDER BY
	md_design_artboard.name
;

或者

let result = await designArtboardModel.alias('adh').join({
    table: 'design_artboard_history',
    as: 'dah',
    join: 'left',
    on: ['history_id', 'id']
  }).field(
    `adh.id,
    adh.name,
    adh.team_id,
    adh.work_id,
    adh.group_id,
    adh.history_id,
    adh.resource,
    adh.format,
    adh.x,
    adh.y,
    adh.width,
    adh.height,
    dah.create_time,
    dah.message`)
  .where({[`adh.team_id`]: team_id, [`adh.work_id`]: work_id, ...where})
  .order(`adh.name`)
  .select();
SELECT
	adh.id,adh.name,
	adh.team_id,
	adh.work_id,
	adh.group_id,
	adh.history_id,
	adh.resource,
	adh.format,
	adh.x,
	adh.y,
	adh.width,
	adh.height,
	dah.create_time,
	dah.message
FROM
		`md_design_artboard`
	AS
		adh
LEFT JOIN
		`md_design_artboard_history`
	AS
		`dah`
ON
	`adh`.`history_id` = `dah`.`id`
WHERE
		( adh.team_id = '54' )
	AND
	( adh.work_id = '82' )
ORDER BY
	adh.name
;

输出结果:

[
  RowDataPacket {
    id: 604,
    name: '2-图层和颜色-19',
    team_id: 54,
    work_id: 82,
    group_id: 50,
    history_id: 165,
    resource: 'E3DCE35C-F4AF-4B1E-9014-A6F3A899B42D',
    format: 'sketch',
    x: 510,
    y: -227,
    width: '721',
    height: '1536',
    create_time: '2021-07-14 01:21:17',
    message: ''
  },
  RowDataPacket {
    id: 608,
    name: 'Screenshot from 2021-06-25 16-55-22',
    team_id: 54,
    work_id: 82,
    group_id: 0,
    history_id: 169,
    resource: 'C6425DC1-B6A0-5037-1657-A710639CE686',
    format: 'png',
    x: 0,
    y: 1936,
    width: '1920',
    height: '1080',
    create_time: '2021-07-14 19:23:20',
    message: ''
  },
  RowDataPacket {
    id: 606,
    name: '无封面',
    team_id: 54,
    work_id: 82,
    group_id: 0,
    history_id: 167,
    resource: 'C93CF7B7-AACE-D36C-3BD9-8581594DDBCA',
    format: 'png',
    x: 2020,
    y: 1936,
    width: '240',
    height: '225',
    create_time: '2021-07-14 14:34:41',
    message: ''
  }
]

多个 join。

await designArtboardModel.alias('adh').join({
  table: 'design_artboard_history',
  as: 'dah',
  join: 'left',
  on: ['history_id', 'id']
}).join({
  table: 'team',
  as: 't',
  join: 'left',
  on: ['team_id', 'id']
}).join({
  table: 'work',
  as: 'w',
  join: 'left',
  on: ['work_id', 'id']
}).field(`adh.id, adh.name, adh.team_id, adh.work_id, adh.group_id, adh.history_id, adh.resource, adh.format, adh.x, adh.y, adh.width, adh.height, t.name as t_name, w.name as w_name, dah.create_time, dah.version, dah.message`).where({ [`adh.team_id`] : team_id,
  [`adh.work_id`] : work_id,
  ...where
}).order(`adh.name`).select();
SELECT adh.id, adh.name, adh.team_id, adh.work_id, adh.group_id
  , adh.history_id, adh.resource, adh.format, adh.x, adh.y
  , adh.width, adh.height, t.name AS t_name, w.name AS w_name, dah.create_time
  , dah.version, dah.message
FROM `md_design_artboard` adh
  LEFT JOIN `md_design_artboard_history` `dah` ON `adh`.`history_id` = `dah`.`id`
  LEFT JOIN `md_team` `t` ON `adh`.`team_id` = `t`.`id`
  LEFT JOIN `md_work` `w` ON `adh`.`work_id` = `w`.`id`
WHERE adh.team_id = '10'
  AND adh.work_id = '192'
ORDER BY adh.name