From 91174eb558b67d6a965e54da38e5524e85988724 Mon Sep 17 00:00:00 2001 From: "Xc@centOs" Date: Thu, 1 Dec 2022 18:53:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E5=8F=96=E6=95=B0=E6=8D=AE=E6=97=B6?= =?UTF-8?q?=E5=8F=91=E7=8E=B0=E5=A4=96=E9=94=AE=E7=9A=84=E7=A9=BA=E6=8C=87?= =?UTF-8?q?=E9=92=88=E8=BF=9B=E8=A1=8C=E4=BA=86=E5=A2=9E=E5=BC=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/MySQL/store.js | 49 ++++++++++++++++++++---------------------- src/MySQL/store.ts | 53 ++++++++++++++++++++++------------------------ 2 files changed, 48 insertions(+), 54 deletions(-) diff --git a/lib/MySQL/store.js b/lib/MySQL/store.js index 40759ce..2edf9f1 100644 --- a/lib/MySQL/store.js +++ b/lib/MySQL/store.js @@ -50,24 +50,16 @@ var MysqlStore = /** @class */ (function (_super) { if (i !== -1) { var attrHead = attr.slice(0, i); var attrTail = attr.slice(i + 1); + if (!r[attrHead]) { + r[attrHead] = {}; + } var rel = (0, relation_1.judgeRelation)(schema, entity2, attrHead); - (0, assert_1.default)(rel === 2 || typeof rel === 'string'); if (rel === 2) { - if (r.entity === attrHead) { - if (!r[attrHead]) { - r[attrHead] = {}; - } - resolveAttribute(attrHead, r[attrHead], attrTail, value); - } + resolveAttribute(attrHead, r[attrHead], attrTail, value); } else { (0, assert_1.default)(typeof rel === 'string'); - if (typeof r["".concat(attrHead, "Id")] === 'string') { - if (!r[attrHead]) { - r[attrHead] = {}; - } - resolveAttribute(rel, r[attrHead], attrTail, value); - } + resolveAttribute(rel, r[attrHead], attrTail, value); } } else if (attributes[attr]) { @@ -140,23 +132,28 @@ var MysqlStore = /** @class */ (function (_super) { _a)); } } - function formalizeNullObject(r, e) { - var a2 = schema[e].attributes; - var allowFormalize = true; + function removeNullObjects(r, e) { + (0, assert_1.default)(r.id && typeof r.id === 'string', "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\u53D1\u73B0id\u4E3A\u975E\u6CD5\u503C").concat(r.id, ",rowId\u662F").concat(r.id)); for (var attr in r) { - if (typeof r[attr] === 'object' && a2[attr] && a2[attr].type === 'ref') { - if (formalizeNullObject(r[attr], a2[attr].ref)) { - r[attr] = null; - } - else { - allowFormalize = false; + var rel = (0, relation_1.judgeRelation)(schema, e, attr); + if (rel === 2) { + (0, assert_1.default)(r.entityId === r[attr].id, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0entityId\u4E0E\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176entityId\u503C\u4E3A").concat(r.entityId, "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E3A").concat(r[attr].id)); + if (r.entityId === null) { + delete r[attr]; + continue; } + (0, assert_1.default)(r.entity === attr, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0entity\u503C\u4E0E\u8FDE\u63A5\u7684\u5916\u952E\u5BF9\u8C61\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176entity\u503C\u4E3A").concat(r.entity, "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u4E3A").concat(attr)); + removeNullObjects(r[attr], attr); } - else if (r[attr] !== null) { - allowFormalize = false; + else if (typeof rel === 'string') { + (0, assert_1.default)(r["".concat(attr, "Id")] === r[attr].id, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0\u5176\u5916\u952E\u4E0E\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176").concat(attr, "Id\u503C\u4E3A").concat(r["".concat(attr, "Id")], "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E3A").concat(r[attr].id)); + if (r["".concat(attr, "Id")] === null) { + delete r[attr]; + continue; + } + removeNullObjects(r[attr], rel); } } - return allowFormalize; } function formSingleRow(r) { var result2 = {}; @@ -164,7 +161,7 @@ var MysqlStore = /** @class */ (function (_super) { var value = r[attr]; resolveAttribute(entity, result2, attr, value); } - formalizeNullObject(result2, entity); + removeNullObjects(result2, entity); return result2; } if (result instanceof Array) { diff --git a/src/MySQL/store.ts b/src/MySQL/store.ts index 09634b3..dbf4c66 100644 --- a/src/MySQL/store.ts +++ b/src/MySQL/store.ts @@ -57,24 +57,16 @@ export class MysqlStore(r: Record, e: E) { - const { attributes: a2 } = schema[e]; - let allowFormalize = true; + function removeNullObjects(r: Record, e: E) { + assert(r.id && typeof r.id === 'string', `对象${e}取数据时发现id为非法值${r.id},rowId是${r.id}`) + for (let attr in r) { - if (typeof r[attr] === 'object' && a2[attr] && a2[attr].type === 'ref') { - if (formalizeNullObject(r[attr], a2[attr].ref!)) { - r[attr] = null; - } - else { - allowFormalize = false; + const rel = judgeRelation(schema, e, attr); + if (rel === 2) { + assert (r.entityId === r[attr].id, `对象${e}取数据时,发现entityId与连接的对象的主键不一致,rowId是${r.id},其entityId值为${r.entityId},连接的对象的主键为${r[attr].id}`); + if (r.entityId === null) { + delete r[attr]; + continue; } + assert (r.entity === attr, `对象${e}取数据时,发现entity值与连接的外键对象不一致,rowId是${r.id},其entity值为${r.entity},连接的对象为${attr}`); + removeNullObjects(r[attr], attr); } - else if (r[attr] !== null) { - allowFormalize = false; + else if (typeof rel === 'string') { + assert (r[`${attr}Id`] === r[attr].id, `对象${e}取数据时,发现其外键与连接的对象的主键不一致,rowId是${r.id},其${attr}Id值为${r[`${attr}Id`]},连接的对象的主键为${r[attr].id}`); + if (r[`${attr}Id`] === null) { + delete r[attr]; + continue; + } + removeNullObjects(r[attr], rel); } } - - return allowFormalize; } function formSingleRow(r: any): any { @@ -176,7 +173,7 @@ export class MysqlStore