修正了初始化数据过程

This commit is contained in:
Xu Chang 2023-08-06 09:07:54 +08:00
parent bd62c3ce23
commit 5e8954012e
4 changed files with 40 additions and 24 deletions

View File

@ -168,7 +168,6 @@ class AppLoader extends types_1.AppLoader {
await this.dbStore.initialize(dropIfExists);
const data = this.requireSth('lib/data/index');
const context = await this.contextBuilder()(this.dbStore);
await context.begin();
for (const entity in data) {
let rows = data[entity];
if (entity === 'area') {
@ -176,17 +175,25 @@ class AppLoader extends types_1.AppLoader {
rows = require('./data/area.json');
}
if (rows.length > 0) {
await this.dbStore.operate(entity, {
data: rows,
action: 'create',
}, context, {
dontCollect: true,
dontCreateOper: true,
});
console.log(`data in ${entity} initialized!`);
await context.begin();
try {
await this.dbStore.operate(entity, {
data: rows,
action: 'create',
}, context, {
dontCollect: true,
dontCreateOper: true,
});
await context.commit();
console.log(`data in ${entity} initialized!`);
}
catch (err) {
await context.rollback();
console.error(`data on ${entity} initilization failed!`);
throw err;
}
}
}
await context.commit();
this.dbStore.disconnect();
}
getStore() {

View File

@ -13,11 +13,12 @@ class DbStore extends oak_db_1.MysqlStore {
this.relationAuth = new RelationAuth_1.RelationAuth(storageSchema, actionCascadeGraph, relationCascadeGraph, authDeduceRelationMap, selectFreeEntities);
}
async cascadeUpdateAsync(entity, operation, context, option) {
if (!option.blockTrigger) {
// 如果是在modi处理过程中所有的trigger也可以延时到apply时再处理这时候因为modi中的数据并不实际存在处理会有问题
if (!option.blockTrigger && !option.modiParentEntity) {
await this.executor.preOperation(entity, operation, context, option);
}
const result = await super.cascadeUpdateAsync(entity, operation, context, option);
if (!option.blockTrigger) {
if (!option.blockTrigger && !option.modiParentEntity) {
await this.executor.postOperation(entity, operation, context, option);
}
return result;

View File

@ -209,7 +209,6 @@ export class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt extends Async
const data = this.requireSth('lib/data/index');
const context = await this.contextBuilder()(this.dbStore);
await context.begin();
for (const entity in data) {
let rows = data[entity];
if (entity === 'area') {
@ -217,17 +216,25 @@ export class AppLoader<ED extends EntityDict & BaseEntityDict, Cxt extends Async
rows = require('./data/area.json');
}
if (rows.length > 0) {
await this.dbStore.operate(entity as keyof ED, {
data: rows,
action: 'create',
} as any, context, {
dontCollect: true,
dontCreateOper: true,
});
console.log(`data in ${entity} initialized!`);
await context.begin();
try {
await this.dbStore.operate(entity as keyof ED, {
data: rows,
action: 'create',
} as any, context, {
dontCollect: true,
dontCreateOper: true,
});
await context.commit();
console.log(`data in ${entity} initialized!`);
}
catch (err) {
await context.rollback();
console.error(`data on ${entity} initilization failed!`);
throw err;
}
}
}
await context.commit();
this.dbStore.disconnect();
}

View File

@ -25,11 +25,12 @@ export class DbStore<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncCo
}
protected async cascadeUpdateAsync<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: AsyncContext<ED>, option: MysqlOperateOption) {
if (!option.blockTrigger) {
// 如果是在modi处理过程中所有的trigger也可以延时到apply时再处理这时候因为modi中的数据并不实际存在处理会有问题
if (!option.blockTrigger && !option.modiParentEntity) {
await this.executor.preOperation(entity, operation, context, option);
}
const result = await super.cascadeUpdateAsync(entity, operation, context, option);
if (!option.blockTrigger) {
if (!option.blockTrigger && !option.modiParentEntity) {
await this.executor.postOperation(entity, operation, context, option);
}
return result;