fix(plugin-collection-manager): missing transaction

This commit is contained in:
chenos 2022-01-24 14:57:04 +08:00
parent 003745681b
commit 1238f1ee8c
3 changed files with 23 additions and 23 deletions

View File

@ -35,19 +35,15 @@ export default class CollectionManagerPlugin extends Plugin {
} }
this.app.db.on('fields.afterCreate', afterCreateForReverseField(this.app.db)); this.app.db.on('fields.afterCreate', afterCreateForReverseField(this.app.db));
this.app.db.on('collections.afterCreateWithAssociations', async (model, options) => { this.app.db.on('collections.afterCreateWithAssociations', async (model, { context, transaction }) => {
if (options.context) { if (context) {
process.nextTick(async () => { await model.migrate({ transaction });
await model.migrate();
});
} }
}); });
this.app.db.on('fields.afterCreate', async (model, options) => { this.app.db.on('fields.afterCreate', async (model, { context, transaction }) => {
if (options.context) { if (context) {
process.nextTick(async () => { await model.migrate({ transaction });
await model.migrate();
});
} }
}); });
} }

View File

@ -1,8 +1,8 @@
import { SyncOptions } from 'sequelize'; import { SyncOptions, Transactionable } from 'sequelize';
import Database, { Collection, MagicAttributeModel } from '@nocobase/database'; import Database, { Collection, MagicAttributeModel } from '@nocobase/database';
import { FieldModel } from './field'; import { FieldModel } from './field';
interface LoadOptions { interface LoadOptions extends Transactionable {
// TODO // TODO
skipField?: boolean; skipField?: boolean;
skipExist?: boolean; skipExist?: boolean;
@ -14,7 +14,7 @@ export class CollectionModel extends MagicAttributeModel {
} }
async load(loadOptions: LoadOptions = {}) { async load(loadOptions: LoadOptions = {}) {
const { skipExist, skipField } = loadOptions; const { skipExist, skipField, transaction } = loadOptions;
const name = this.get('name'); const name = this.get('name');
let collection: Collection; let collection: Collection;
@ -36,21 +36,23 @@ export class CollectionModel extends MagicAttributeModel {
} }
if (!skipField) { if (!skipField) {
await this.loadFields(); await this.loadFields({ transaction });
} }
return collection; return collection;
} }
async loadFields() { async loadFields(options: Transactionable = {}) {
// @ts-ignore // @ts-ignore
const instances: FieldModel[] = await this.getFields(); const instances: FieldModel[] = await this.getFields(options);
for (const instance of instances) { for (const instance of instances) {
await instance.load(); await instance.load(options);
} }
} }
async migrate(options?: SyncOptions) { async migrate(options?: SyncOptions & Transactionable) {
const collection = await this.load(); const collection = await this.load({
transaction: options.transaction,
});
await collection.sync({ await collection.sync({
force: false, force: false,
alter: { alter: {

View File

@ -1,7 +1,7 @@
import { SyncOptions } from 'sequelize'; import { SyncOptions, Transactionable } from 'sequelize';
import Database, { MagicAttributeModel } from '@nocobase/database'; import Database, { MagicAttributeModel } from '@nocobase/database';
interface LoadOptions { interface LoadOptions extends Transactionable {
// TODO // TODO
skipExist?: boolean; skipExist?: boolean;
} }
@ -25,8 +25,10 @@ export class FieldModel extends MagicAttributeModel {
return collection.setField(name, this.get()); return collection.setField(name, this.get());
} }
async migrate(options?: SyncOptions) { async migrate(options?: SyncOptions & Transactionable) {
const field = await this.load(); const field = await this.load({
transaction: options.transaction,
});
await field.sync(options); await field.sync(options);
} }
} }