"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.count = exports.fetchRows = exports.aggregate = exports.select = exports.operate = void 0; const types_1 = require("oak-domain/lib/types"); const relation_1 = require("oak-domain/lib/store/relation"); const assert_1 = require("oak-domain/lib/utils/assert"); async function operate(params, context) { const { entity, operation, option } = params; /* const userId = context.getCurrentUserId(); if (!userId) { // operate默认必须用户登录 throw new OakUnloggedInException(); } */ if (!context.allowUserUpdate()) { throw new types_1.OakUserUnpermittedException('您被禁更新'); } if (operation instanceof Array) { const result = []; for (const oper of operation) { const r = await context.operate(entity, oper, option || {}); result.push(r); } return result; } return await context.operate(entity, operation, option || {}); } exports.operate = operate; /** * 因为有cascadeSelect,这里要按查询的要求build返回的结构树,告知每一层上相关的id/total/aggr信息 * @param schema * @param entity * @param result * @returns */ function buildResultTree(schema, entity, result) { const pruneInner = (e, r, tree) => { for (const attr in r) { if (attr.endsWith('$$aggr')) { tree[attr] = r[attr]; } else if (typeof r[attr] === 'object') { const rel = (0, relation_1.judgeRelation)(schema, e, attr); if (rel === 2 || typeof rel === 'string') { tree[attr] = {}; pruneInner(rel === 2 ? attr : rel, r[attr], tree[attr]); } else if (rel instanceof Array) { (0, assert_1.assert)(r[attr] instanceof Array); tree[attr] = { data: {}, }; if (r[attr].hasOwnProperty('#total')) { tree[attr].total = r[attr]['#total']; } r[attr].forEach((rr) => { tree[attr].data[rr.id] = {}; pruneInner(rel[0], rr, tree[attr].data[rr.id]); }); } } } }; const root = { data: {}, }; /** * 这个total是在cascadeStore的selectAsync处理的,有点古怪 */ if (result.hasOwnProperty('#total')) { root.total = result['#total']; } result.forEach((row) => { root.data[row.id] = {}; pruneInner(entity, row, root.data[row.id]); }); return root; } async function select(params, context) { const { entity, selection, option } = params; let selection2 = selection; const data = await context.select(entity, selection2, option || {}); return buildResultTree(context.getSchema(), entity, data); } exports.select = select; async function aggregate(params, context) { const { entity, aggregation, option } = params; return context.aggregate(entity, aggregation, option || {}); } exports.aggregate = aggregate; async function fetchRows(params, context) { await Promise.all(params.map((ele) => context.select(ele.entity, ele.selection, ele.option || {}))); } exports.fetchRows = fetchRows; async function count(params, context) { const { entity, selection, option } = params; const { filter } = selection; const count = await context.count(entity, Object.assign({}, { filter }), option || {}); return count; } exports.count = count; /* export type AspectDict = { operation: (options: { entity: T, operation: ED[T]['Operation'], params?: OperateParams }, context: Context) => Promise; select: ( options: { entity: T, selection: S, params?: object }, context: Context) => Promise>; }; */