From 140f8810624db9a9338a91d7668fa4d8a1bcac2f Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Fri, 18 Oct 2019 14:26:26 +0800 Subject: [PATCH] feat: remove auto connect use manual inject Signed-off-by: MiaoWoo --- packages/core/src/function/http.ts | 10 ++----- packages/core/src/index.ts | 11 +++++++- packages/core/src/server.ts | 13 +++++++++- .../src/{client/index.ts => client.ts} | 26 ++++--------------- packages/db-mongo/src/client/connection.ts | 18 ------------- packages/db-mongo/src/constants.ts | 7 +++++ packages/db-mongo/src/index.ts | 1 + packages/db/src/index.ts | 1 - 8 files changed, 37 insertions(+), 50 deletions(-) rename packages/db-mongo/src/{client/index.ts => client.ts} (67%) delete mode 100644 packages/db-mongo/src/client/connection.ts create mode 100644 packages/db-mongo/src/constants.ts diff --git a/packages/core/src/function/http.ts b/packages/core/src/function/http.ts index 5433eff..7ab44c0 100644 --- a/packages/core/src/function/http.ts +++ b/packages/core/src/function/http.ts @@ -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 { return this.client.find({}); diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index c481c89..abd308a 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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); } } diff --git a/packages/core/src/server.ts b/packages/core/src/server.ts index 13caeee..886f02e 100644 --- a/packages/core/src/server.ts +++ b/packages/core/src/server.ts @@ -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(); + } +}) diff --git a/packages/db-mongo/src/client/index.ts b/packages/db-mongo/src/client.ts similarity index 67% rename from packages/db-mongo/src/client/index.ts rename to packages/db-mongo/src/client.ts index 47089c2..f665f31 100644 --- a/packages/db-mongo/src/client/index.ts +++ b/packages/db-mongo/src/client.ts @@ -1,31 +1,15 @@ import { DBClient } from '@cc-server/db' -import { MongoDBConnection } from './connection' -import { Db, ObjectID, Collection } from 'mongodb' -import { provide, postConstruct } from '@cc-server/ioc' +import { ObjectID, Collection } from 'mongodb' -@provide(DBClient) -export class MongoDBClient implements DBClient { - private table: string; - private db: Db; +export class MongoCollection implements DBClient { private collection: Collection; - @postConstruct() - private async init() { - this.db = await MongoDBConnection.getConnection(); - if (this.table) { - this.collection = this.db.collection(this.table); - } + constructor(collection: Collection) { + this.collection = collection; } public getProvide

(): P { - return this.db as {} as P; - } - - public setTable(table: string): void { - this.table = table; - if (this.db) { - this.collection = this.db.collection(table); - } + return this.collection as {} as P; } public async find(filter: object): Promise { diff --git a/packages/db-mongo/src/client/connection.ts b/packages/db-mongo/src/client/connection.ts deleted file mode 100644 index 74ae7f9..0000000 --- a/packages/db-mongo/src/client/connection.ts +++ /dev/null @@ -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 { - if (!this.db) { this.db = await this.connect() } - return this.db; - } - - private static async connect(): Promise { - let client = await MongoClient.connect(connStr, { useNewUrlParser: true }); - return client.db(dbName); - } -} diff --git a/packages/db-mongo/src/constants.ts b/packages/db-mongo/src/constants.ts new file mode 100644 index 0000000..91615e6 --- /dev/null +++ b/packages/db-mongo/src/constants.ts @@ -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') +} diff --git a/packages/db-mongo/src/index.ts b/packages/db-mongo/src/index.ts index 7c5048b..4a043d1 100644 --- a/packages/db-mongo/src/index.ts +++ b/packages/db-mongo/src/index.ts @@ -1,2 +1,3 @@ export * from 'mongodb' export * from './client' +export * from './constants' diff --git a/packages/db/src/index.ts b/packages/db/src/index.ts index 5eca6ec..596722a 100644 --- a/packages/db/src/index.ts +++ b/packages/db/src/index.ts @@ -1,7 +1,6 @@ export const DBClient = Symbol.for('DBClient') export interface DBClient { getProvide

(): P; - setTable(table: string): void; find(filter: object): Promise; findOne(filter: Object): Promise; findOneById(objectId: string): Promise;