* fix #9

* fix: database env variables
This commit is contained in:
chenos 2020-11-12 11:48:27 +08:00 committed by GitHub
parent 204a516165
commit 1023e317f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 24 deletions

View File

@ -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',

View File

@ -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) {

View File

@ -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||{}),
}));

View File

@ -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;