mirror of
https://gitee.com/nocobase/nocobase.git
synced 2024-12-03 04:38:15 +08:00
fix(plugin-collection-manager): Cannot read properties of undefined (reading 'model')
This commit is contained in:
parent
c538fdf73a
commit
6fa6756356
@ -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 '..';
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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',
|
||||
|
5
packages/plugin-collection-manager/src/hooks/index.ts
Normal file
5
packages/plugin-collection-manager/src/hooks/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export * from './afterCreateForReverseField';
|
||||
export * from './beforeCreateForChildrenCollection';
|
||||
export * from './beforeCreateForReverseField';
|
||||
export * from './beforeInitOptions';
|
||||
|
@ -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'),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
3
packages/plugin-collection-manager/src/models/index.ts
Normal file
3
packages/plugin-collection-manager/src/models/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from './collection';
|
||||
export * from './field';
|
||||
|
53
packages/plugin-collection-manager/src/plugin.ts
Normal file
53
packages/plugin-collection-manager/src/plugin.ts
Normal 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;
|
@ -0,0 +1 @@
|
||||
export * from './collection-repository';
|
Loading…
Reference in New Issue
Block a user