fix(plugin-collection-manager): Cannot read properties of undefined (reading 'model')

This commit is contained in:
chenos 2022-02-14 21:57:13 +08:00
parent c538fdf73a
commit 6fa6756356
10 changed files with 71 additions and 60 deletions

View File

@ -1,4 +1,4 @@
import Database, { Collection as DBCollection, StringFieldOptions } from '@nocobase/database';
import Database, { Collection as DBCollection } from '@nocobase/database';
import Application from '@nocobase/server';
import { createApp } from '..';

View File

@ -1,9 +1,8 @@
import Database from '@nocobase/database';
export function afterCreateForReverseField(db: Database) {
const Field = db.getCollection('fields');
return async (model, { transaction }) => {
const Field = db.getCollection('fields');
const reverseKey = model.get('reverseKey');
if (!reverseKey) {
return;

View File

@ -1,10 +1,10 @@
import Database from '@nocobase/database';
export function beforeCreateForChildrenCollection(db: Database) {
const Collection = db.getCollection('collections');
const Field = db.getCollection('fields');
return async (model, { transaction, context }) => {
const Collection = db.getCollection('collections');
const Field = db.getCollection('fields');
const parentKey = model.get('parentKey');
if (!parentKey) {
return;

View File

@ -1,9 +1,8 @@
import Database from '@nocobase/database';
export function beforeCreateForReverseField(db: Database) {
const Field = db.getCollection('fields');
return async (model, { transaction }) => {
const Field = db.getCollection('fields');
const reverseKey = model.get('reverseKey');
if (!reverseKey) {
return;

View File

@ -1,7 +1,7 @@
import { uid } from '@nocobase/utils';
import { Model } from 'sequelize';
export default {
export const beforeInitOptions = {
belongsTo(model: Model) {
const defaults = {
targetKey: 'id',

View File

@ -0,0 +1,5 @@
export * from './afterCreateForReverseField';
export * from './beforeCreateForChildrenCollection';
export * from './beforeCreateForReverseField';
export * from './beforeInitOptions';

View File

@ -1,52 +1,3 @@
import path from 'path';
import { Plugin } from '@nocobase/server';
import { CollectionModel } from './models/collection';
import { FieldModel } from './models/field';
import beforeInitOptions from './hooks/beforeInitOptions';
import { beforeCreateForChildrenCollection } from './hooks/beforeCreateForChildrenCollection';
import { beforeCreateForReverseField } from './hooks/beforeCreateForReverseField';
import { afterCreateForReverseField } from './hooks/afterCreateForReverseField';
export { default } from './plugin';
export * from './repositories';
export * from './repositories/collection-repository';
export default class CollectionManagerPlugin extends Plugin {
async beforeLoad() {
this.app.db.registerModels({
CollectionModel,
FieldModel,
});
// 要在 beforeInitOptions 之前处理
this.app.db.on('fields.beforeCreate', beforeCreateForReverseField(this.app.db));
this.app.db.on('fields.beforeCreate', beforeCreateForChildrenCollection(this.app.db));
this.app.db.on('fields.beforeCreate', async (model, options) => {
const type = model.get('type');
await this.app.db.emitAsync(`fields.${type}.beforeInitOptions`, model, options);
});
for (const key in beforeInitOptions) {
if (Object.prototype.hasOwnProperty.call(beforeInitOptions, key)) {
const fn = beforeInitOptions[key];
this.app.db.on(`fields.${key}.beforeInitOptions`, fn);
}
}
this.app.db.on('fields.afterCreate', afterCreateForReverseField(this.app.db));
this.app.db.on('collections.afterCreateWithAssociations', async (model, { context, transaction }) => {
if (context) {
await model.migrate({ transaction });
}
});
this.app.db.on('fields.afterCreate', async (model, { context, transaction }) => {
if (context) {
await model.migrate({ transaction });
}
});
}
async load() {
await this.app.db.import({
directory: path.resolve(__dirname, './collections'),
});
}
}

View File

@ -0,0 +1,3 @@
export * from './collection';
export * from './field';

View File

@ -0,0 +1,53 @@
import { Plugin } from '@nocobase/server';
import path from 'path';
import {
afterCreateForReverseField,
beforeCreateForChildrenCollection,
beforeCreateForReverseField,
beforeInitOptions
} from './hooks';
import { CollectionModel, FieldModel } from './models';
export class CollectionManagerPlugin extends Plugin {
async beforeLoad() {
this.app.db.registerModels({
CollectionModel,
FieldModel,
});
// 要在 beforeInitOptions 之前处理
this.app.db.on('fields.beforeCreate', beforeCreateForReverseField(this.app.db));
this.app.db.on('fields.beforeCreate', beforeCreateForChildrenCollection(this.app.db));
this.app.db.on('fields.beforeCreate', async (model, options) => {
const type = model.get('type');
await this.app.db.emitAsync(`fields.${type}.beforeInitOptions`, model, options);
});
for (const key in beforeInitOptions) {
if (Object.prototype.hasOwnProperty.call(beforeInitOptions, key)) {
const fn = beforeInitOptions[key];
this.app.db.on(`fields.${key}.beforeInitOptions`, fn);
}
}
this.app.db.on('fields.afterCreate', afterCreateForReverseField(this.app.db));
this.app.db.on('collections.afterCreateWithAssociations', async (model, { context, transaction }) => {
if (context) {
await model.migrate({ transaction });
}
});
this.app.db.on('fields.afterCreate', async (model, { context, transaction }) => {
if (context) {
await model.migrate({ transaction });
}
});
}
async load() {
await this.app.db.import({
directory: path.resolve(__dirname, './collections'),
});
}
}
export default CollectionManagerPlugin;

View File

@ -0,0 +1 @@
export * from './collection-repository';