feat: remove auto connect use manual inject

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 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 { 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 { Vaild, NotBlank, NotNull, controller, requestBody, get, post, requestParam } from '@cc-server/binding'
import '@cc-server/db-mongo' import '@cc-server/db-mongo'
const TABLE = 'users'
class ExampleModel { class ExampleModel {
_id: string; _id: string;
@NotBlank("username must not be blank!") @NotBlank("username must not be blank!")
@ -18,13 +16,9 @@ class ExampleModel {
@controller('/example') @controller('/example')
export class Controller { export class Controller {
@inject(DBClient) @inject(DBClient)
@named("users")
private client: DBClient private client: DBClient
@postConstruct()
private init(): void {
this.client.setTable(TABLE);
}
@get('/') @get('/')
public async list(): Promise<ExampleModel[]> { public async list(): Promise<ExampleModel[]> {
return this.client.find({}); return this.client.find({});

View File

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

View File

@ -1,31 +1,15 @@
import { DBClient } from '@cc-server/db' import { DBClient } from '@cc-server/db'
import { MongoDBConnection } from './connection' import { ObjectID, Collection } from 'mongodb'
import { Db, ObjectID, Collection } from 'mongodb'
import { provide, postConstruct } from '@cc-server/ioc'
@provide(DBClient) export class MongoCollection<T = any> implements DBClient {
export class MongoDBClient<T = any> implements DBClient {
private table: string;
private db: Db;
private collection: Collection<T>; private collection: Collection<T>;
@postConstruct() constructor(collection: Collection<T>) {
private async init() { this.collection = collection;
this.db = await MongoDBConnection.getConnection();
if (this.table) {
this.collection = this.db.collection(this.table);
}
} }
public getProvide<P>(): P { public getProvide<P>(): P {
return this.db as {} as P; return this.collection as {} as P;
}
public setTable(table: string): void {
this.table = table;
if (this.db) {
this.collection = this.db.collection(table);
}
} }
public async find(filter: object): Promise<T[]> { public async find(filter: object): Promise<T[]> {

View File

@ -1,18 +0,0 @@
import { Db, MongoClient } from 'mongodb';
const connStr = process.env.FAAS_MONGO_URL || 'mongodb://192.168.0.2:27017';
const dbName = process.env.FAAS_MONGO_DB || "faas";
export class MongoDBConnection {
private static db: Db;
public static async getConnection(): Promise<Db> {
if (!this.db) { this.db = await this.connect() }
return this.db;
}
private static async connect(): Promise<Db> {
let client = await MongoClient.connect(connStr, { useNewUrlParser: true });
return client.db(dbName);
}
}

View File

@ -0,0 +1,7 @@
export const TYPE = {
URL: Symbol.for('URL'),
DB: Symbol.for('DB'),
Client: Symbol.for('Client'),
Database: Symbol.for('Database'),
Collection: Symbol.for('Collection')
}

View File

@ -1,2 +1,3 @@
export * from 'mongodb' export * from 'mongodb'
export * from './client' export * from './client'
export * from './constants'

View File

@ -1,7 +1,6 @@
export const DBClient = Symbol.for('DBClient') export const DBClient = Symbol.for('DBClient')
export interface DBClient<T = any> { export interface DBClient<T = any> {
getProvide<P>(): P; getProvide<P>(): P;
setTable(table: string): void;
find(filter: object): Promise<T[]>; find(filter: object): Promise<T[]>;
findOne(filter: Object): Promise<T>; findOne(filter: Object): Promise<T>;
findOneById(objectId: string): Promise<T>; findOneById(objectId: string): Promise<T>;