parent
204a516165
commit
1023e317f3
@ -28,12 +28,12 @@ describe('middleware', () => {
|
||||
},
|
||||
});
|
||||
database = new Database({
|
||||
username: process.env.TEST_DB_USER,
|
||||
password: process.env.TEST_DB_PASSWORD,
|
||||
database: process.env.TEST_DB_DATABASE,
|
||||
host: process.env.TEST_DB_HOST,
|
||||
port: process.env.TEST_DB_PORT as any,
|
||||
dialect: process.env.TEST_DB_DIALECT as any,
|
||||
username: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_DATABASE,
|
||||
host: process.env.DB_HOST,
|
||||
port: process.env.DB_PORT as any,
|
||||
dialect: process.env.DB_DIALECT as any,
|
||||
dialectOptions: {
|
||||
charset: 'utf8mb4',
|
||||
collate: 'utf8mb4_unicode_ci',
|
||||
|
@ -1,12 +1,23 @@
|
||||
import Koa from 'koa';
|
||||
import Database from '@nocobase/database';
|
||||
import Resourcer, { Action, ParsedParams } from '@nocobase/resourcer';
|
||||
import Resourcer from '@nocobase/resourcer';
|
||||
|
||||
export interface ApplicationOptions {
|
||||
database: any;
|
||||
resourcer: any;
|
||||
}
|
||||
|
||||
export class Application extends Koa {
|
||||
|
||||
database: Database;
|
||||
public readonly database: Database;
|
||||
|
||||
resourcer: Resourcer;
|
||||
public readonly resourcer: Resourcer;
|
||||
|
||||
constructor(options: ApplicationOptions) {
|
||||
super();
|
||||
this.database = new Database(options.database);
|
||||
this.resourcer = new Resourcer();
|
||||
}
|
||||
|
||||
async plugins(plugins: any[]) {
|
||||
for (const pluginOption of plugins) {
|
@ -1,30 +1,34 @@
|
||||
import Database from '@nocobase/database';
|
||||
import Resourcer, { Action } from '@nocobase/resourcer';
|
||||
import actions from '@nocobase/actions';
|
||||
import Application from './applicatiion';
|
||||
import Application from './application';
|
||||
import bodyParser from 'koa-bodyparser';
|
||||
import cors from '@koa/cors';
|
||||
import middleware from './middleware';
|
||||
|
||||
export * from './application';
|
||||
export * from './middleware';
|
||||
|
||||
export default {
|
||||
/**
|
||||
* 这部分还比较杂,细节待改进
|
||||
*
|
||||
* @param options
|
||||
*/
|
||||
create(options: any): Application {
|
||||
console.log(options);
|
||||
|
||||
const app = new Application();
|
||||
const database = new Database(options.database);
|
||||
const resourcer = new Resourcer();
|
||||
|
||||
app.database = database;
|
||||
app.resourcer = resourcer;
|
||||
const app = new Application(options);
|
||||
|
||||
app.use(bodyParser());
|
||||
app.use(cors());
|
||||
|
||||
resourcer.registerHandlers(actions.common);
|
||||
// 这段代码处理的不完整
|
||||
app.resourcer.registerHandlers(actions.common);
|
||||
|
||||
app.use(async (ctx, next) => {
|
||||
ctx.db = database;
|
||||
ctx.database = database;
|
||||
ctx.db = app.database;
|
||||
ctx.database = app.database;
|
||||
await next();
|
||||
});
|
||||
|
||||
@ -50,8 +54,8 @@ export default {
|
||||
|
||||
app.use(middleware({
|
||||
prefix: '/api',
|
||||
database,
|
||||
resourcer,
|
||||
database: app.database,
|
||||
resourcer: app.resourcer,
|
||||
...(options.resourcer||{}),
|
||||
}));
|
||||
|
||||
|
@ -8,8 +8,12 @@ interface MiddlewareOptions extends KoaMiddlewareOptions {
|
||||
resourcer?: Resourcer;
|
||||
database?: Database;
|
||||
}
|
||||
|
||||
export default function middleware(options: MiddlewareOptions = {}) {
|
||||
/**
|
||||
* database + resourcer 结合的中间件(暂时不知道起什么名好)
|
||||
*
|
||||
* @param options
|
||||
*/
|
||||
export function middleware(options: MiddlewareOptions = {}) {
|
||||
const {
|
||||
prefix,
|
||||
database,
|
||||
@ -110,4 +114,6 @@ export default function middleware(options: MiddlewareOptions = {}) {
|
||||
return next();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default middleware;
|
||||
|
Loading…
Reference in New Issue
Block a user