增加了一些属性上的默认索引的建立

This commit is contained in:
Xu Chang 2023-01-30 16:43:15 +08:00
parent e8367b4c54
commit eae65bed4b
2 changed files with 96 additions and 12 deletions

View File

@ -49,19 +49,25 @@ var SqlTranslator = /** @class */ (function () {
// 增加默认的索引
var intrinsticIndexes = [
{
name: "".concat(entity, "_create_at"),
name: "".concat(entity, "_create_at_auto_create"),
attributes: [{
name: '$$createAt$$',
}, {
name: '$$deleteAt$$',
}]
}, {
name: "".concat(entity, "_update_at"),
name: "".concat(entity, "_update_at_auto_create"),
attributes: [{
name: '$$updateAt$$',
}, {
name: '$$deleteAt$$',
}],
}, {
name: "".concat(entity, "_trigger_ts"),
name: "".concat(entity, "_trigger_ts_auto_create"),
attributes: [{
name: '$$triggerTimestamp$$',
}, {
name: '$$deleteAt$$',
}],
}
];
@ -69,9 +75,11 @@ var SqlTranslator = /** @class */ (function () {
if (attributes[attr].type === 'ref') {
if (!(indexes === null || indexes === void 0 ? void 0 : indexes.find(function (ele) { return ele.attributes[0].name === attr; }))) {
intrinsticIndexes.push({
name: "".concat(entity, "_fk_").concat(attr),
name: "".concat(entity, "_fk_").concat(attr, "_auto_create"),
attributes: [{
name: attr,
}, {
name: '$$deleteAt$$',
}]
});
}
@ -81,18 +89,49 @@ var SqlTranslator = /** @class */ (function () {
if ((entityIdDef === null || entityIdDef === void 0 ? void 0 : entityIdDef.type) === 'varchar') {
if (!(indexes === null || indexes === void 0 ? void 0 : indexes.find(function (ele) { var _a; return ele.attributes[0].name === 'entity' && ((_a = ele.attributes[1]) === null || _a === void 0 ? void 0 : _a.name) === 'entityId'; }))) {
intrinsticIndexes.push({
name: "".concat(entity, "_fk_entity_entityId"),
name: "".concat(entity, "_fk_entity_entityId_auto_create"),
attributes: [{
name: 'entity',
}, {
name: 'entityId',
}, {
name: '$$deleteAt$$',
}]
});
}
}
}
if (attr.endsWith('State') && attributes[attr].type === 'varchar') {
if (!(indexes === null || indexes === void 0 ? void 0 : indexes.find(function (ele) { return ele.attributes[0].name === attr; }))) {
intrinsticIndexes.push({
name: "".concat(entity, "_attr_auto_create"),
attributes: [{
name: attr,
}, {
name: '$$deleteAt$$',
}]
});
}
}
if (attr === 'expired' && attributes[attr].type === 'boolean') {
var expiresAtDef = attributes.expiresAt;
if ((expiresAtDef === null || expiresAtDef === void 0 ? void 0 : expiresAtDef.type) === 'datetime') {
if (!(indexes === null || indexes === void 0 ? void 0 : indexes.find(function (ele) { var _a; return ele.attributes[0].name === 'expired' && ((_a = ele.attributes[1]) === null || _a === void 0 ? void 0 : _a.name) === 'expiresAt'; }))) {
intrinsticIndexes.push({
name: "".concat(entity, "_expires_expiredAt_auto_create"),
attributes: [{
name: 'expired',
}, {
name: 'expiresAt',
}, {
name: '$$deleteAt$$',
}]
});
}
}
}
};
// 增加外键上的索引
// 增加外键等相关属性上的索引
for (var attr in attributes) {
_loop_1(attr);
}

View File

@ -53,33 +53,41 @@ export abstract class SqlTranslator<ED extends EntityDict> {
// 增加默认的索引
const intrinsticIndexes: Index<ED[keyof ED]['OpSchema']>[] = [
{
name: `${entity}_create_at`,
name: `${entity}_create_at_auto_create`,
attributes: [{
name: '$$createAt$$',
}, {
name: '$$deleteAt$$',
}]
}, {
name: `${entity}_update_at`,
name: `${entity}_update_at_auto_create`,
attributes: [{
name: '$$updateAt$$',
}, {
name: '$$deleteAt$$',
}],
}, {
name: `${entity}_trigger_ts`,
name: `${entity}_trigger_ts_auto_create`,
attributes: [{
name: '$$triggerTimestamp$$',
}, {
name: '$$deleteAt$$',
}],
}
];
// 增加外键上的索引
// 增加外键等相关属性上的索引
for (const attr in attributes) {
if (attributes[attr].type === 'ref') {
if (!(indexes?.find(
ele => ele.attributes[0].name === attr
))) {
intrinsticIndexes.push({
name: `${entity}_fk_${attr}`,
name: `${entity}_fk_${attr}_auto_create`,
attributes: [{
name: attr,
}, {
name: '$$deleteAt$$',
}]
});
}
@ -92,11 +100,48 @@ export abstract class SqlTranslator<ED extends EntityDict> {
ele => ele.attributes[0].name === 'entity' && ele.attributes[1]?.name === 'entityId'
))) {
intrinsticIndexes.push({
name: `${entity}_fk_entity_entityId`,
name: `${entity}_fk_entity_entityId_auto_create`,
attributes: [{
name: 'entity',
}, {
name: 'entityId',
}, {
name: '$$deleteAt$$',
}]
});
}
}
}
if (attr.endsWith('State') && attributes[attr].type === 'varchar') {
if (!(indexes?.find(
ele => ele.attributes[0].name === attr
))) {
intrinsticIndexes.push({
name: `${entity}_attr_auto_create`,
attributes: [{
name: attr,
}, {
name: '$$deleteAt$$',
}]
});
}
}
if (attr === 'expired' && attributes[attr].type === 'boolean') {
const expiresAtDef = attributes.expiresAt;
if (expiresAtDef?.type === 'datetime') {
if (!(indexes?.find(
ele => ele.attributes[0].name === 'expired' && ele.attributes[1]?.name === 'expiresAt'
))) {
intrinsticIndexes.push({
name: `${entity}_expires_expiredAt_auto_create`,
attributes: [{
name: 'expired',
}, {
name: 'expiresAt',
}, {
name: '$$deleteAt$$',
}]
});
}