feat: remove auto connect use manual inject

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2019-10-18 14:26:26 +08:00
parent 5fa5603735
commit 140f881062
8 changed files with 37 additions and 50 deletions

View File

@@ -1,10 +1,8 @@
import { DBClient } from '@cc-server/db'
import { inject, postConstruct } from '@cc-server/ioc';
import { inject, named } from '@cc-server/ioc';
import { Vaild, NotBlank, NotNull, controller, requestBody, get, post, requestParam } from '@cc-server/binding'
import '@cc-server/db-mongo'
const TABLE = 'users'
class ExampleModel {
_id: string;
@NotBlank("username must not be blank!")
@@ -18,13 +16,9 @@ class ExampleModel {
@controller('/example')
export class Controller {
@inject(DBClient)
@named("users")
private client: DBClient
@postConstruct()
private init(): void {
this.client.setTable(TABLE);
}
@get('/')
public async list(): Promise<ExampleModel[]> {
return this.client.find({});

View File

@@ -79,6 +79,10 @@ export class CcServerBoot {
.use(bodyParser.raw());
}
get container() {
return this._container;
}
get server() {
return this._server;
}
@@ -122,7 +126,7 @@ export class CcServerBoot {
return this;
}
public start() {
public print() {
console.log(prettyjson.render({ routes: { http: getRouteInfo(this._container), websocket: getNamespaceInfo() } }));
return this;
}
@@ -130,5 +134,10 @@ export class CcServerBoot {
public listen(port: number = 80) {
this._server.listen(port);
console.log(`Server listen on port ${port} :)`);
return this;
}
public start(port: number = 80) {
return this.static('public').build().print().listen(port);
}
}

View File

@@ -2,5 +2,16 @@ import { CcServerBoot, express } from './index'
import './function/http';
import './function/websocket';
import { DBClient } from '@cc-server/db';
import { MongoClient } from 'mongodb';
import { MongoCollection } from '@cc-server/db-mongo';
new CcServerBoot().static('public').build().start();
let boot = new CcServerBoot().static('public');
MongoClient.connect("mongodb://192.168.2.5:27017", { useNewUrlParser: true }, (error, client) => {
if (error) {
console.log(error)
} else {
boot.container.bind(DBClient).toConstantValue(new MongoCollection(client.db("faas").collection("users"))).whenTargetNamed("users")
boot.build().listen();
}
})