2024-04-30 07:51:31 +00:00
|
|
|
/**
|
|
|
|
* This file is part of the NocoBase (R) project.
|
|
|
|
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
|
|
* Authors: NocoBase Team.
|
|
|
|
*
|
|
|
|
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
|
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
|
|
*/
|
|
|
|
|
2024-03-03 15:06:24 +00:00
|
|
|
import { ACL } from '@nocobase/acl';
|
2024-04-26 09:44:59 +00:00
|
|
|
import { getNameByParams, parseRequest, ResourceManager } from '@nocobase/resourcer';
|
2024-03-18 10:28:35 +00:00
|
|
|
import EventEmitter from 'events';
|
2024-03-03 15:06:24 +00:00
|
|
|
import compose from 'koa-compose';
|
|
|
|
import { loadDefaultActions } from './load-default-actions';
|
|
|
|
import { ICollectionManager } from './types';
|
2024-07-18 06:44:53 +00:00
|
|
|
import { Logger } from '@nocobase/logger';
|
2024-11-05 16:29:06 +00:00
|
|
|
import { wrapMiddlewareWithLogging } from '@nocobase/utils';
|
2024-03-03 15:06:24 +00:00
|
|
|
|
|
|
|
export type DataSourceOptions = any;
|
|
|
|
|
2024-10-27 10:16:23 +00:00
|
|
|
export type LoadingProgress = {
|
|
|
|
total: number;
|
|
|
|
loaded: number;
|
|
|
|
};
|
|
|
|
|
2024-03-03 15:06:24 +00:00
|
|
|
export abstract class DataSource extends EventEmitter {
|
|
|
|
public collectionManager: ICollectionManager;
|
|
|
|
public resourceManager: ResourceManager;
|
|
|
|
public acl: ACL;
|
2024-10-27 10:16:23 +00:00
|
|
|
|
2024-07-18 06:44:53 +00:00
|
|
|
logger: Logger;
|
2024-03-03 15:06:24 +00:00
|
|
|
|
|
|
|
constructor(protected options: DataSourceOptions) {
|
|
|
|
super();
|
|
|
|
this.init(options);
|
|
|
|
}
|
|
|
|
|
2024-10-27 10:16:23 +00:00
|
|
|
_sqlLogger: Logger;
|
2024-10-22 09:07:57 +00:00
|
|
|
|
|
|
|
get sqlLogger() {
|
|
|
|
return this._sqlLogger || this.logger;
|
|
|
|
}
|
|
|
|
|
2024-03-03 15:06:24 +00:00
|
|
|
get name() {
|
|
|
|
return this.options.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static testConnection(options?: any): Promise<boolean> {
|
|
|
|
return Promise.resolve(true);
|
|
|
|
}
|
|
|
|
|
2024-10-27 10:16:23 +00:00
|
|
|
setLogger(logger: Logger) {
|
|
|
|
this.logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
setSqlLogger(logger: Logger) {
|
|
|
|
this._sqlLogger = logger;
|
|
|
|
}
|
|
|
|
|
2024-03-03 15:06:24 +00:00
|
|
|
init(options: DataSourceOptions = {}) {
|
|
|
|
this.acl = this.createACL();
|
|
|
|
|
|
|
|
this.resourceManager = this.createResourceManager({
|
2024-03-18 10:28:35 +00:00
|
|
|
prefix: process.env.API_BASE_PATH,
|
2024-03-03 15:06:24 +00:00
|
|
|
...options.resourceManager,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.collectionManager = this.createCollectionManager(options);
|
2024-04-26 09:44:59 +00:00
|
|
|
this.resourceManager.registerActionHandlers(loadDefaultActions());
|
2024-03-03 15:06:24 +00:00
|
|
|
|
|
|
|
if (options.acl !== false) {
|
|
|
|
this.resourceManager.use(this.acl.middleware(), { tag: 'acl', after: ['auth'] });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-26 09:44:59 +00:00
|
|
|
middleware(middlewares: any = []) {
|
|
|
|
const dataSource = this;
|
|
|
|
|
|
|
|
if (!this['_used']) {
|
|
|
|
for (const [fn, options] of middlewares) {
|
|
|
|
this.resourceManager.use(fn, options);
|
|
|
|
}
|
2024-11-05 16:29:06 +00:00
|
|
|
|
2024-04-26 09:44:59 +00:00
|
|
|
this['_used'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return async (ctx, next) => {
|
|
|
|
ctx.dataSource = dataSource;
|
|
|
|
|
|
|
|
ctx.getCurrentRepository = () => {
|
|
|
|
const { resourceName, resourceOf } = ctx.action;
|
|
|
|
|
|
|
|
return this.collectionManager.getRepository(resourceName, resourceOf);
|
|
|
|
};
|
|
|
|
|
2024-11-05 16:29:06 +00:00
|
|
|
const middlewares = [this.collectionToResourceMiddleware(), this.resourceManager.middleware()];
|
|
|
|
|
|
|
|
return compose(middlewares.map((fn) => wrapMiddlewareWithLogging(fn)))(ctx, next);
|
2024-04-26 09:44:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
createACL() {
|
|
|
|
return new ACL();
|
|
|
|
}
|
|
|
|
|
|
|
|
createResourceManager(options) {
|
|
|
|
return new ResourceManager(options);
|
|
|
|
}
|
2024-07-19 14:26:27 +00:00
|
|
|
|
|
|
|
publicOptions() {
|
|
|
|
return null;
|
|
|
|
}
|
2024-10-27 10:16:23 +00:00
|
|
|
|
|
|
|
emitLoadingProgress(progress: LoadingProgress) {
|
|
|
|
this.emit('loadingProgress', progress);
|
|
|
|
}
|
2024-04-26 09:44:59 +00:00
|
|
|
|
|
|
|
async load(options: any = {}) {}
|
2024-07-18 06:44:53 +00:00
|
|
|
async close() {}
|
2024-04-26 09:44:59 +00:00
|
|
|
|
|
|
|
abstract createCollectionManager(options?: any): ICollectionManager;
|
|
|
|
|
|
|
|
protected collectionToResourceMiddleware() {
|
2024-11-05 16:29:06 +00:00
|
|
|
const self = this;
|
|
|
|
return async function collectionToResource(ctx, next) {
|
2024-03-03 15:06:24 +00:00
|
|
|
const params = parseRequest(
|
|
|
|
{
|
|
|
|
path: ctx.request.path,
|
|
|
|
method: ctx.request.method,
|
|
|
|
},
|
|
|
|
{
|
2024-11-05 16:29:06 +00:00
|
|
|
prefix: self.resourceManager.options.prefix,
|
|
|
|
accessors: self.resourceManager.options.accessors,
|
2024-03-03 15:06:24 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
if (!params) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
const resourceName = getNameByParams(params);
|
|
|
|
// 如果资源名称未被定义
|
2024-11-05 16:29:06 +00:00
|
|
|
if (self.resourceManager.isDefined(resourceName)) {
|
2024-03-03 15:06:24 +00:00
|
|
|
return next();
|
|
|
|
}
|
2024-03-31 08:22:45 +00:00
|
|
|
|
|
|
|
const splitResult = resourceName.split('.');
|
|
|
|
|
|
|
|
const collectionName = splitResult[0];
|
|
|
|
|
2024-11-05 16:29:06 +00:00
|
|
|
if (!self.collectionManager.hasCollection(collectionName)) {
|
2024-03-03 15:06:24 +00:00
|
|
|
return next();
|
|
|
|
}
|
2024-03-31 08:22:45 +00:00
|
|
|
|
2024-11-05 16:29:06 +00:00
|
|
|
self.resourceManager.define({
|
2024-03-03 15:06:24 +00:00
|
|
|
name: resourceName,
|
|
|
|
});
|
2024-03-31 08:22:45 +00:00
|
|
|
|
2024-03-03 15:06:24 +00:00
|
|
|
return next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|