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('collections.afterCreateWithAssociations', async (model, options) => {
if (options.context) {
process.nextTick(async () => {
await model.migrate();
});
this.app.db.on('collections.afterCreateWithAssociations', async (model, { context, transaction }) => {
if (context) {
await model.migrate({ transaction });
}
});
this.app.db.on('fields.afterCreate', async (model, options) => {
if (options.context) {
process.nextTick(async () => {
await model.migrate();
});
this.app.db.on('fields.afterCreate', async (model, { context, transaction }) => {
if (context) {
await model.migrate({ transaction });
}
});
}

View File

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

View File

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