diff --git a/lib/store/actionDef.js b/lib/store/actionDef.js index 4994337..0b928dc 100644 --- a/lib/store/actionDef.js +++ b/lib/store/actionDef.js @@ -50,34 +50,48 @@ function makeIntrinsicWatchers(schema) { return watchers; } function checkUniqueBetweenRows(rows, uniqAttrs) { + var e_1, _a, e_2, _b; // 先检查这些行本身之间有无unique冲突 - var uniqRows = (0, lodash_1.uniqBy)(rows, function (d) { - var e_1, _a; - var s = ''; - try { - for (var uniqAttrs_1 = tslib_1.__values(uniqAttrs), uniqAttrs_1_1 = uniqAttrs_1.next(); !uniqAttrs_1_1.done; uniqAttrs_1_1 = uniqAttrs_1.next()) { - var a = uniqAttrs_1_1.value; - if (d[a] === null || d[a] === undefined) { - s + d.id; - } - else { - s + "-".concat(d[a]); - } - } - } - catch (e_1_1) { e_1 = { error: e_1_1 }; } - finally { + var dict = {}; + try { + for (var rows_1 = tslib_1.__values(rows), rows_1_1 = rows_1.next(); !rows_1_1.done; rows_1_1 = rows_1.next()) { + var row = rows_1_1.value; + var s = ''; try { - if (uniqAttrs_1_1 && !uniqAttrs_1_1.done && (_a = uniqAttrs_1.return)) _a.call(uniqAttrs_1); + for (var uniqAttrs_1 = (e_2 = void 0, tslib_1.__values(uniqAttrs)), uniqAttrs_1_1 = uniqAttrs_1.next(); !uniqAttrs_1_1.done; uniqAttrs_1_1 = uniqAttrs_1.next()) { + var a = uniqAttrs_1_1.value; + if (row[a] === null || row[a] === undefined) { + s + row.id; + } + else { + s + "-".concat(row[a]); + } + } + } + catch (e_2_1) { e_2 = { error: e_2_1 }; } + finally { + try { + if (uniqAttrs_1_1 && !uniqAttrs_1_1.done && (_b = uniqAttrs_1.return)) _b.call(uniqAttrs_1); + } + finally { if (e_2) throw e_2.error; } + } + if (dict[s]) { + throw new types_1.OakUniqueViolationException([{ + id: row.id, + attrs: uniqAttrs, + }]); + } + else { + dict[s] = 1; } - finally { if (e_1) throw e_1.error; } } - return s; - }); - if (uniqRows.length < rows.length) { - throw new types_1.OakUniqueViolationException([{ - attrs: uniqAttrs, - }]); + } + catch (e_1_1) { e_1 = { error: e_1_1 }; } + finally { + try { + if (rows_1_1 && !rows_1_1.done && (_a = rows_1.return)) _a.call(rows_1); + } + finally { if (e_1) throw e_1.error; } } } function checkCountLessThan(count, uniqAttrs, than, id) { @@ -110,7 +124,7 @@ function checkUnique(entity, row, context, uniqAttrs, extraFilter) { // 说明有null值,不需要检查约束 return; } - var filter2 = extraFilter ? (0, filter_1.addFilterSegment)([filter, extraFilter]) : filter; + var filter2 = extraFilter ? (0, filter_1.addFilterSegment)(filter, extraFilter) : filter; var count = context.count(entity, { filter: filter2 }, { dontCollect: true }); return checkCountLessThan(count, uniqAttrs, 0, row.id); } @@ -190,7 +204,7 @@ function analyzeActionDefDict(schema, actionDefDict) { } } var _loop_2 = function (entity) { - var e_2, _e; + var e_3, _e; var indexes = schema[entity].indexes; if (indexes) { var _loop_4 = function (index) { @@ -221,7 +235,7 @@ function analyzeActionDefDict(schema, actionDefDict) { type: 'logical', priority: 20, checker: function (operation, context) { - var e_3, _a, e_4, _b, _c; + var e_4, _a, e_5, _b, _c; var _d = operation, data = _d.data, operationFilter = _d.filter; var attrs = Object.keys(data); var refAttrs = (0, lodash_1.intersection)(attrs, uniqAttrs_2); @@ -230,7 +244,7 @@ function analyzeActionDefDict(schema, actionDefDict) { return; } try { - for (var refAttrs_1 = (e_3 = void 0, tslib_1.__values(refAttrs)), refAttrs_1_1 = refAttrs_1.next(); !refAttrs_1_1.done; refAttrs_1_1 = refAttrs_1.next()) { + for (var refAttrs_1 = (e_4 = void 0, tslib_1.__values(refAttrs)), refAttrs_1_1 = refAttrs_1.next(); !refAttrs_1_1.done; refAttrs_1_1 = refAttrs_1.next()) { var attr = refAttrs_1_1.value; // 如果有更新为null值,不用再检查约束 if (data[attr] === null || data[attr] === undefined) { @@ -238,12 +252,12 @@ function analyzeActionDefDict(schema, actionDefDict) { } } } - catch (e_3_1) { e_3 = { error: e_3_1 }; } + catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (refAttrs_1_1 && !refAttrs_1_1.done && (_a = refAttrs_1.return)) _a.call(refAttrs_1); } - finally { if (e_3) throw e_3.error; } + finally { if (e_4) throw e_4.error; } } if (refAttrs.length === uniqAttrs_2.length) { // 如果更新了全部属性,直接检查 @@ -254,12 +268,12 @@ function analyzeActionDefDict(schema, actionDefDict) { $not: operationFilter, }]), }, { dontCollect: true }); - var checkCount = checkCountLessThan(count, uniqAttrs_2); + var checkCount = checkCountLessThan(count, uniqAttrs_2, 0, operationFilter === null || operationFilter === void 0 ? void 0 : operationFilter.id); // 更新的行只能有一行 var rowCount = context.count(entity, { filter: operationFilter, }, { dontCollect: true }); - var checkRowCount = checkCountLessThan(rowCount, uniqAttrs_2, 1); + var checkRowCount = checkCountLessThan(rowCount, uniqAttrs_2, 1, operationFilter === null || operationFilter === void 0 ? void 0 : operationFilter.id); // 如果更新的行数为零似乎也可以,但这应该不可能出现吧,by Xc 20230131 if (checkRowCount instanceof Promise) { return Promise.all([checkCount, checkRowCount]).then(function () { return undefined; }); @@ -268,19 +282,19 @@ function analyzeActionDefDict(schema, actionDefDict) { // 否则需要结合本行现有的属性来进行检查 var projection = { id: 1 }; try { - for (var uniqAttrs_3 = (e_4 = void 0, tslib_1.__values(uniqAttrs_2)), uniqAttrs_3_1 = uniqAttrs_3.next(); !uniqAttrs_3_1.done; uniqAttrs_3_1 = uniqAttrs_3.next()) { + for (var uniqAttrs_3 = (e_5 = void 0, tslib_1.__values(uniqAttrs_2)), uniqAttrs_3_1 = uniqAttrs_3.next(); !uniqAttrs_3_1.done; uniqAttrs_3_1 = uniqAttrs_3.next()) { var attr = uniqAttrs_3_1.value; Object.assign(projection, (_c = {}, _c[attr] = 1, _c)); } } - catch (e_4_1) { e_4 = { error: e_4_1 }; } + catch (e_5_1) { e_5 = { error: e_5_1 }; } finally { try { if (uniqAttrs_3_1 && !uniqAttrs_3_1.done && (_b = uniqAttrs_3.return)) _b.call(uniqAttrs_3); } - finally { if (e_4) throw e_4.error; } + finally { if (e_5) throw e_5.error; } } var checkWithRows = function (rows2) { var rows22 = rows2.map(function (ele) { return Object.assign(ele, data); }); @@ -306,17 +320,17 @@ function analyzeActionDefDict(schema, actionDefDict) { } }; try { - for (var indexes_1 = (e_2 = void 0, tslib_1.__values(indexes)), indexes_1_1 = indexes_1.next(); !indexes_1_1.done; indexes_1_1 = indexes_1.next()) { + for (var indexes_1 = (e_3 = void 0, tslib_1.__values(indexes)), indexes_1_1 = indexes_1.next(); !indexes_1_1.done; indexes_1_1 = indexes_1.next()) { var index = indexes_1_1.value; _loop_4(index); } } - catch (e_2_1) { e_2 = { error: e_2_1 }; } + catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (indexes_1_1 && !indexes_1_1.done && (_e = indexes_1.return)) _e.call(indexes_1); } - finally { if (e_2) throw e_2.error; } + finally { if (e_3) throw e_3.error; } } } }; diff --git a/lib/types/Port.d.ts b/lib/types/Port.d.ts index 8e0519a..0c65b45 100644 --- a/lib/types/Port.d.ts +++ b/lib/types/Port.d.ts @@ -5,8 +5,9 @@ export declare type Exportation Partial>; + headers?: K[]; + makeHeaders?: (dataList: Partial[]) => string[]; + fn: (data: ED[T]['Schema'], context?: AsyncContext, properties?: Record) => Promise>> | Partial>; }; export declare type Importation = { name: string; diff --git a/lib/utils/money.js b/lib/utils/money.js index def3365..2305a30 100644 --- a/lib/utils/money.js +++ b/lib/utils/money.js @@ -34,7 +34,7 @@ var ThousandCont = function (value) { if (value1) { result = value1 + result; } - result = result + '.' + numArr[1]; + result = numArr[1] ? result + '.' + numArr[1] : result; return result; }; exports.ThousandCont = ThousandCont; diff --git a/package.json b/package.json index 8d12c3c..59e93dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "oak-domain", - "version": "2.6.9", + "version": "2.6.10", "author": { "name": "XuChang" }, diff --git a/src/store/actionDef.ts b/src/store/actionDef.ts index 2605131..f6264f8 100644 --- a/src/store/actionDef.ts +++ b/src/store/actionDef.ts @@ -53,22 +53,26 @@ function makeIntrinsicWatchers(schema: StorageSchema) function checkUniqueBetweenRows(rows: Record[], uniqAttrs: string[]) { // 先检查这些行本身之间有无unique冲突 - const uniqRows = uniqBy(rows, (d) => { + const dict: Record = {}; + for (const row of rows) { let s = ''; for (const a of uniqAttrs) { - if (d[a as string] === null || d[a as string] === undefined) { - s + d.id; + if (row[a] === null || row[a] === undefined) { + s + row.id; } else { - s + `-${d[a as string]}`; + s + `-${row[a]}`; } } - return s; - }); - if (uniqRows.length < rows.length) { - throw new OakUniqueViolationException([{ - attrs: uniqAttrs, - }]); + if (dict[s]) { + throw new OakUniqueViolationException([{ + id: row.id, + attrs: uniqAttrs, + }]); + } + else { + dict[s] = 1; + } } } @@ -110,7 +114,7 @@ function checkUnique | AsyncC // 说明有null值,不需要检查约束 return; } - const filter2 = extraFilter ? addFilterSegment([filter, extraFilter]) : filter; + const filter2 = extraFilter ? addFilterSegment(filter, extraFilter) : filter; const count = context.count(entity, { filter: filter2 }, { dontCollect: true }); return checkCountLessThan(count, uniqAttrs, 0, row.id) } @@ -199,7 +203,7 @@ export function analyzeActionDefDict { const { data } = operation; - + if (data instanceof Array) { checkUniqueBetweenRows(data, uniqAttrs); const checkResult = data.map( @@ -245,20 +249,20 @@ export function analyzeActionDefDict undefined ); - } + } } // 否则需要结合本行现有的属性来进行检查 const projection = { id: 1 }; @@ -283,7 +287,7 @@ export function analyzeActionDefDict undefined ); - } + } }; const currentRows = context.select(entity, { diff --git a/src/types/Port.ts b/src/types/Port.ts index bb35b31..d4e2f84 100644 --- a/src/types/Port.ts +++ b/src/types/Port.ts @@ -7,8 +7,9 @@ export type Exportation Partial>; + headers?: K[]; + makeHeaders?: (dataList: Partial[]) => string[]; + fn: (data: ED[T]['Schema'], context?: AsyncContext, properties?: Record) => Promise>> | Partial>; }; export type Importation = { @@ -17,5 +18,5 @@ export type Importation>[], context: AsyncContext, option?: Record ) => Promise; + fn: (data: Partial>[], context: AsyncContext, option?: Record) => Promise; }; \ No newline at end of file diff --git a/src/utils/money.ts b/src/utils/money.ts index ba80952..9d77fdc 100644 --- a/src/utils/money.ts +++ b/src/utils/money.ts @@ -34,7 +34,7 @@ const ThousandCont: (value: number) => string | undefined = (value) => { if (value1) { result = value1 + result; } - result = result + '.' + numArr[1]; + result = numArr[1] ? result + '.' + numArr[1] : result; return result; };