对取数据时发现外键的空指针进行了增强

This commit is contained in:
Xu Chang 2022-12-01 18:53:57 +08:00
parent 0dc7ec0b98
commit 91174eb558
2 changed files with 48 additions and 54 deletions

View File

@ -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) {

View File

@ -57,24 +57,16 @@ export class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Asyn
if (i !== -1) {
const attrHead = attr.slice(0, i);
const attrTail = attr.slice(i + 1);
if (!r[attrHead]) {
r[attrHead] = {};
}
const rel = judgeRelation(schema, entity2, attrHead);
assert(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 {
assert(typeof rel === 'string');
if (typeof r[`${attrHead}Id`] === 'string') {
if (!r[attrHead]) {
r[attrHead] = {};
}
resolveAttribute(rel, r[attrHead], attrTail, value);
}
assert (typeof rel === 'string');
resolveAttribute(rel, r[attrHead], attrTail, value);
}
}
else if (attributes[attr]) {
@ -149,24 +141,29 @@ export class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Asyn
}
function formalizeNullObject<E extends keyof ED>(r: Record<string, any>, e: E) {
const { attributes: a2 } = schema[e];
let allowFormalize = true;
function removeNullObjects<E extends keyof ED>(r: Record<string, any>, e: E) {
assert(r.id && typeof r.id === 'string', `对象${<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, `对象${<string>e}取数据时发现entityId与连接的对象的主键不一致rowId是${r.id}其entityId值为${r.entityId},连接的对象的主键为${r[attr].id}`);
if (r.entityId === null) {
delete r[attr];
continue;
}
assert (r.entity === attr, `对象${<string>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, `对象${<string>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<ED extends EntityDict & BaseEntityDict, Cxt extends Asyn
resolveAttribute(entity, result2, attr, value);
}
formalizeNullObject(result2, entity);
removeNullObjects(result2, entity);
return result2 as any;
}