* test: create view collection * feat: view collection class * feat: list view * chore: skip sync view collection * test: should create view collection in difference schema * test: create view collection in collection manager * feat: create view collection by user sql * test: view resourcer * feat: view collection * feat: view collection cannot be added, deleted, or modified * feat: view collection cannot be added, deleted, or modified * feat: view collection cannot be added, deleted, or modified * feat: view collection cannot be added, deleted, or modified * refactor: connect to database view * refactor: sync from database * chore: rename list view sql * chore: list view fields api * chore: create collection without viewName * feat: bring out fields when selecting a view * chore: bring out fields when selecting a view * feat: view field inference class * chore: bring out fields when selecting a view * chore: sync form database view * chore: sync form database view * refactor: view collection local * feat: view get api * feat: database type infer * feat: integer map * chore: remove from in view list * chore: build error * chore: uniq collection * fix: typo * chore: replace collection list source field * fix: destroy view collection * chore: timestamp field map * refactor: interface avalableTypes * refactor: interface avalableTypes * chore: list fields test * refactor: interface avalableTypes * chore: uiSchema response in field source * fix: view query * chore: collection snippet * refactor: view collection support preview * fix: handle field source * fix: typo * fix: configure fileds title * fix: configure fileds title * fix: configure fileds title * fix: sync from databse interface * fix: sync from databse interface * feat: set fields api * fix: sync from databse fix * feat: possibleTypes * chore: fields get * fix: sync from databse * fix: list view test * fix: view test in difference schema * chore: comment * feat: when there is only one source collection, the view is a subset of a Collection * feat: view collection add field * fix: inherit query with schema * fix: test * fix: ci test * fix: test with schema * chore: set pg default search path * chore: mysql test * fix: test with schema * chore: test * chore: action test * chore: view column usage return type * feat: mysql field inference * fix: tableName * chore: node sql parser * fix: sql build * fix: database build * fix: mysql test * feat: view collection uiSchema title * fix: incorrect field source display when switching views * refactor: view collection not allow modify * fix: view collection is allow add, delete, and modify * fix: mysql test * fix: sqlite test * fix: sqlite test * fix: sqlite test * fix: sqlite test * chore: add id field as default target key * style: style improve * feat: load source field options * style: style improve * chore: disable remove column in view collection * chore: support creating view collection with different schemas with the same name * chore: support creating view collection with different schemas with the same name * fix: query view in difference schema * refactor: view collection viewname * fix: query view collection in difference schema * fix: field load * chore: field options * fix: mysql test * fix: uiSchema component error when using a view field in a block * fix: sqlite test * chore: test * fix: dump user views * fix: view collection can be updated and edited in table block * chore: sync from database display last field configuration * chore: loadCollections * chore: sync from database display last field configuration * fix: field options merge issues * style: preview table * fix: view collection is allow using in kanban blocks * refactor: code improve * fix: view collection can be updated an edited in calendar block * chore: disable infer field without interface * feat: preview only shows source or interface fields * fix: test * refactor: locale * feat: sql parser * chore: remove node-sql-parser * fix: yarn.lock * test: view repository * fix: view repository test * chore: console.log * chore: console.log * fix: mysql without schema * fix: mysql without schema * chore: preview with field schema * chore: tableActionInitializers * style: preview style improve * chore: parameter is filter when there is no filterByTk * fix: preview pagination * fix: preview pagination * style: preview table style improve * fix: sync from database loading * chore: preview performance optimization * chore: preview performance optimization * feat: limit & offset * chore: preview performance optimization * test: field with dot column * fix: datetime interface display * fix: missing boolean type * fix: sync * fix: sync from database * style: style improve * style: style improve * style: style improve * chore: preview table * chore: preview table * chore: preview table * fix: styling --------- Co-authored-by: katherinehhh <katherine_15995@163.com> Co-authored-by: chenos <chenlinxh@gmail.com>
331 lines
10 KiB
TypeScript
331 lines
10 KiB
TypeScript
import path from 'path';
|
|
import { UniqueConstraintError } from 'sequelize';
|
|
|
|
import PluginErrorHandler from '@nocobase/plugin-error-handler';
|
|
import { Plugin } from '@nocobase/server';
|
|
import { Mutex } from 'async-mutex';
|
|
|
|
import { CollectionRepository } from '.';
|
|
import {
|
|
afterCreateForForeignKeyField,
|
|
afterCreateForReverseField,
|
|
beforeCreateForReverseField,
|
|
beforeDestroyForeignKey,
|
|
beforeInitOptions
|
|
} from './hooks';
|
|
|
|
import { InheritedCollection } from '@nocobase/database';
|
|
import lodash, { castArray } from 'lodash';
|
|
import * as process from 'process';
|
|
import { CollectionModel, FieldModel } from './models';
|
|
import collectionActions from './resourcers/collections';
|
|
import viewResourcer from './resourcers/views';
|
|
|
|
import { beforeCreateForViewCollection } from './hooks/beforeCreateForViewCollection';
|
|
|
|
export class CollectionManagerPlugin extends Plugin {
|
|
public schema: string;
|
|
|
|
async beforeLoad() {
|
|
if (process.env.COLLECTION_MANAGER_SCHEMA) {
|
|
this.schema = process.env.COLLECTION_MANAGER_SCHEMA;
|
|
}
|
|
|
|
this.app.db.registerModels({
|
|
CollectionModel,
|
|
FieldModel,
|
|
});
|
|
|
|
this.db.addMigrations({
|
|
namespace: 'collection-manager',
|
|
directory: path.resolve(__dirname, './migrations'),
|
|
context: {
|
|
plugin: this,
|
|
},
|
|
});
|
|
|
|
this.app.db.registerRepositories({
|
|
CollectionRepository,
|
|
});
|
|
|
|
this.app.acl.registerSnippet({
|
|
name: `pm.${this.name}.collections`,
|
|
actions: ['collections:*', 'collections.fields:*', 'dbViews:*'],
|
|
});
|
|
|
|
this.app.db.on('collections.beforeCreate', async (model) => {
|
|
if (this.app.db.inDialect('postgres') && this.schema && model.get('from') != 'db2cm' && !model.get('schema')) {
|
|
model.set('schema', this.schema);
|
|
}
|
|
});
|
|
|
|
this.app.db.on('collections.beforeCreate', beforeCreateForViewCollection(this.db));
|
|
|
|
this.app.db.on(
|
|
'collections.afterCreateWithAssociations',
|
|
async (model: CollectionModel, { context, transaction }) => {
|
|
if (context) {
|
|
await model.migrate({
|
|
transaction,
|
|
});
|
|
}
|
|
},
|
|
);
|
|
|
|
this.app.db.on('collections.beforeDestroy', async (model: CollectionModel, options) => {
|
|
await model.remove(options);
|
|
});
|
|
|
|
// 要在 beforeInitOptions 之前处理
|
|
this.app.db.on('fields.beforeCreate', beforeCreateForReverseField(this.app.db));
|
|
|
|
this.app.db.on('fields.beforeCreate', async (model, options) => {
|
|
const collectionName = model.get('collectionName');
|
|
const collection = this.app.db.getCollection(collectionName);
|
|
|
|
if (!collection) {
|
|
return;
|
|
}
|
|
|
|
if (collection.isInherited() && (<InheritedCollection>collection).parentFields().has(model.get('name'))) {
|
|
model.set('overriding', true);
|
|
}
|
|
});
|
|
|
|
this.app.db.on('fields.beforeValidate', async (model) => {
|
|
if (model.get('name') && model.get('name').includes('.')) {
|
|
model.set('field', model.get('name'));
|
|
model.set('name', model.get('name').replace(/\./g, '_'));
|
|
}
|
|
});
|
|
|
|
this.app.db.on('fields.beforeCreate', async (model, options) => {
|
|
const type = model.get('type');
|
|
const fn = beforeInitOptions[type];
|
|
if (fn) {
|
|
await fn(model, { database: this.app.db });
|
|
}
|
|
});
|
|
|
|
this.app.db.on('fields.afterCreate', afterCreateForReverseField(this.app.db));
|
|
|
|
this.app.db.on('fields.afterCreate', async (model: FieldModel, { context, transaction }) => {
|
|
if (context) {
|
|
await model.migrate({
|
|
isNew: true,
|
|
transaction,
|
|
});
|
|
}
|
|
});
|
|
|
|
// after migrate
|
|
this.app.db.on('fields.afterCreate', afterCreateForForeignKeyField(this.app.db));
|
|
|
|
this.app.db.on('fields.beforeUpdate', async (model, options) => {
|
|
const newValue = options.values;
|
|
if (
|
|
model.get('reverseKey') &&
|
|
lodash.get(newValue, 'reverseField') &&
|
|
!lodash.get(newValue, 'reverseField.key')
|
|
) {
|
|
const field = await this.app.db
|
|
.getModel('fields')
|
|
.findByPk(model.get('reverseKey'), { transaction: options.transaction });
|
|
if (field) {
|
|
throw new Error('cant update field without a reverseField key');
|
|
}
|
|
}
|
|
});
|
|
|
|
this.app.db.on('fields.afterUpdate', async (model: FieldModel, { context, transaction }) => {
|
|
const prevOptions = model.previous('options');
|
|
const currentOptions = model.get('options');
|
|
|
|
if (context) {
|
|
const prev = prevOptions['unique'];
|
|
const next = currentOptions['unique'];
|
|
|
|
if (Boolean(prev) !== Boolean(next)) {
|
|
await model.syncUniqueIndex({ transaction });
|
|
}
|
|
}
|
|
|
|
const prevDefaultValue = prevOptions['defaultValue'];
|
|
const currentDefaultValue = currentOptions['defaultValue'];
|
|
|
|
if (prevDefaultValue != currentDefaultValue) {
|
|
await model.syncDefaultValue({ transaction, defaultValue: currentDefaultValue });
|
|
}
|
|
|
|
const prevOnDelete = prevOptions['onDelete'];
|
|
const currentOnDelete = currentOptions['onDelete'];
|
|
|
|
if (prevOnDelete != currentOnDelete) {
|
|
await model.syncReferenceCheckOption({ transaction });
|
|
}
|
|
});
|
|
|
|
this.app.db.on('fields.afterSaveWithAssociations', async (model: FieldModel, { context, transaction }) => {
|
|
if (context) {
|
|
await model.load({ transaction });
|
|
}
|
|
});
|
|
|
|
// before field remove
|
|
this.app.db.on('fields.beforeDestroy', beforeDestroyForeignKey(this.app.db));
|
|
|
|
const mutex = new Mutex();
|
|
this.app.db.on('fields.beforeDestroy', async (model: FieldModel, options) => {
|
|
await mutex.runExclusive(async () => {
|
|
await model.remove(options);
|
|
});
|
|
});
|
|
|
|
this.app.db.on('fields.afterDestroy', async (model: FieldModel, options) => {
|
|
const { transaction } = options;
|
|
const collectionName = model.get('collectionName');
|
|
const childCollections = this.db.inheritanceMap.getChildren(collectionName);
|
|
|
|
const childShouldRemoveField = Array.from(childCollections).filter((item) => {
|
|
const parents = Array.from(this.db.inheritanceMap.getParents(item))
|
|
.map((parent) => {
|
|
const collection = this.db.getCollection(parent);
|
|
const field = collection.getField(model.get('name'));
|
|
return field;
|
|
})
|
|
.filter(Boolean);
|
|
|
|
return parents.length == 0;
|
|
});
|
|
|
|
await this.db.getCollection('fields').repository.destroy({
|
|
filter: {
|
|
name: model.get('name'),
|
|
collectionName: {
|
|
$in: childShouldRemoveField,
|
|
},
|
|
options: {
|
|
overriding: true,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
});
|
|
|
|
this.app.on('afterLoad', async (app, options) => {
|
|
if (options?.method === 'install') {
|
|
return;
|
|
}
|
|
const exists = await this.app.db.collectionExistsInDb('collections');
|
|
if (exists) {
|
|
try {
|
|
await this.app.db.getRepository<CollectionRepository>('collections').load();
|
|
} catch (error) {
|
|
this.app.logger.warn(error);
|
|
await this.app.db.sync();
|
|
|
|
try {
|
|
await this.app.db.getRepository<CollectionRepository>('collections').load();
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
this.app.resourcer.use(async (ctx, next) => {
|
|
const { resourceName, actionName } = ctx.action;
|
|
if (resourceName === 'collections.fields' && actionName === 'update') {
|
|
const { updateAssociationValues = [] } = ctx.action.params;
|
|
updateAssociationValues.push('uiSchema');
|
|
ctx.action.mergeParams({
|
|
updateAssociationValues,
|
|
});
|
|
}
|
|
await next();
|
|
});
|
|
|
|
this.app.acl.allow('collections', 'list', 'loggedIn');
|
|
}
|
|
|
|
async load() {
|
|
await this.importCollections(path.resolve(__dirname, './collections'));
|
|
|
|
const errorHandlerPlugin = <PluginErrorHandler>this.app.getPlugin('error-handler');
|
|
errorHandlerPlugin.errorHandler.register(
|
|
(err) => {
|
|
return err instanceof UniqueConstraintError;
|
|
},
|
|
(err, ctx) => {
|
|
return ctx.throw(400, ctx.t(`The value of ${Object.keys(err.fields)} field duplicated`));
|
|
},
|
|
);
|
|
|
|
this.app.resourcer.use(async (ctx, next) => {
|
|
if (ctx.action.resourceName === 'collections.fields' && ['create', 'update'].includes(ctx.action.actionName)) {
|
|
ctx.action.mergeParams({
|
|
updateAssociationValues: ['reverseField'],
|
|
});
|
|
}
|
|
await next();
|
|
});
|
|
|
|
this.app.resource(viewResourcer);
|
|
this.app.actions(collectionActions);
|
|
|
|
const handleFieldSource = (fields) => {
|
|
for (const field of castArray(fields)) {
|
|
if (field.get('source')) {
|
|
const [collectionSource, fieldSource] = field.get('source').split('.');
|
|
const collectionField = this.app.db.getCollection(collectionSource).getField(fieldSource);
|
|
|
|
const newOptions = {};
|
|
lodash.merge(newOptions, collectionField.options);
|
|
lodash.mergeWith(newOptions, field.get(), (objValue, srcValue) => {
|
|
if (srcValue === null) {
|
|
return objValue;
|
|
}
|
|
});
|
|
|
|
field.set('options', newOptions);
|
|
}
|
|
}
|
|
};
|
|
|
|
this.app.resourcer.use(async (ctx, next) => {
|
|
await next();
|
|
|
|
// handle collections:list
|
|
if (
|
|
ctx.action.resourceName === 'collections' &&
|
|
ctx.action.actionName == 'list' &&
|
|
ctx.action.params?.paginate == 'false'
|
|
) {
|
|
for (const collection of ctx.body) {
|
|
if (collection.get('view')) {
|
|
const fields = collection.fields;
|
|
handleFieldSource(fields);
|
|
}
|
|
}
|
|
}
|
|
|
|
//handle collections:fields:list
|
|
if (ctx.action.resourceName == 'collections.fields' && ctx.action.actionName == 'list') {
|
|
handleFieldSource(ctx.action.params?.paginate == 'false' ? ctx.body : ctx.body.rows);
|
|
}
|
|
|
|
if (ctx.action.resourceName == 'collections.fields' && ctx.action.actionName == 'get') {
|
|
handleFieldSource(ctx.body);
|
|
}
|
|
});
|
|
|
|
this.app.db.extendCollection({
|
|
name: 'collectionCategory',
|
|
namespace: 'collection-manager',
|
|
duplicator: 'required',
|
|
});
|
|
}
|
|
}
|
|
|
|
export default CollectionManagerPlugin;
|