From 177c001bb83b791557847a44ac4d7989b8768639 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Tue, 11 Jun 2019 16:11:34 +0800 Subject: [PATCH] feat: add provide and update template --- .../cc-server-core/src/function/handle.ts | 49 +++++++++++++++---- .../src/{ => client}/connection.ts | 0 .../src/{client.ts => client/index.ts} | 6 ++- packages/cc-server-db-mongo/src/index.ts | 3 +- packages/cc-server-db/src/index.ts | 1 + 5 files changed, 48 insertions(+), 11 deletions(-) rename packages/cc-server-db-mongo/src/{ => client}/connection.ts (100%) rename packages/cc-server-db-mongo/src/{client.ts => client/index.ts} (94%) diff --git a/packages/cc-server-core/src/function/handle.ts b/packages/cc-server-core/src/function/handle.ts index a430e46..710b08f 100644 --- a/packages/cc-server-core/src/function/handle.ts +++ b/packages/cc-server-core/src/function/handle.ts @@ -1,23 +1,54 @@ import { - controller, httpGet, httpPost + controller, response, requestBody, httpGet, httpPost, queryParam } from 'inversify-express-utils'; import { inject } from 'inversify'; import { DBClient } from 'cc-server-db' import 'cc-server-db-mongo' +//process.env.FAAS_MONGO_URL = 'mongodb://192.168.0.2:27017'; +process.env.FAAS_MONGO_DB = "faas"; + +const TABLE = 'users' + +interface ExampleModel { + _id: string; + username: string; + password: string; + age: number; + email: string; +} + +type Model = ExampleModel + @controller('/') -export class UserController { - constructor( - @inject(DBClient) private client: DBClient - ) { } +export class Controller { + @inject(DBClient) + private client: DBClient @httpGet('/') - public async getUsers(): Promise { - return this.client.find('users', {}); + public async list(): Promise { + return this.client.find(TABLE, {}); + } + + @httpGet('/:id') + public async get( + @queryParam('id') id: string + ): Promise { + return this.client.findOneById(TABLE, id); } @httpPost('/') - public async newUser(): Promise { - return {} + public async create( + @requestBody() model: Model + ): Promise { + return this.client.insertOne(TABLE, model); + } + + @httpPost('/:id') + public async update( + @queryParam('id') id: string, + @requestBody() model: Model + ): Promise { + return this.client.updateById(TABLE, id, model); } } diff --git a/packages/cc-server-db-mongo/src/connection.ts b/packages/cc-server-db-mongo/src/client/connection.ts similarity index 100% rename from packages/cc-server-db-mongo/src/connection.ts rename to packages/cc-server-db-mongo/src/client/connection.ts diff --git a/packages/cc-server-db-mongo/src/client.ts b/packages/cc-server-db-mongo/src/client/index.ts similarity index 94% rename from packages/cc-server-db-mongo/src/client.ts rename to packages/cc-server-db-mongo/src/client/index.ts index 71337ad..dea68e4 100644 --- a/packages/cc-server-db-mongo/src/client.ts +++ b/packages/cc-server-db-mongo/src/client/index.ts @@ -4,7 +4,7 @@ import { provide } from 'cc-server-ioc' import { DBClient } from 'cc-server-db' @provide(DBClient) -export class MongoDBClient { +export class MongoDBClient implements DBClient { public db: Db; constructor() { @@ -13,6 +13,10 @@ export class MongoDBClient { }); } + public getProvide(): Db { + return this.db; + } + public async find(collection: string, filter: object): Promise { return await this.db.collection(collection).find(filter).toArray(); } diff --git a/packages/cc-server-db-mongo/src/index.ts b/packages/cc-server-db-mongo/src/index.ts index 7eec814..de72b13 100644 --- a/packages/cc-server-db-mongo/src/index.ts +++ b/packages/cc-server-db-mongo/src/index.ts @@ -1 +1,2 @@ -export * from './client' \ No newline at end of file +export * from './client' +export * from 'mongodb'; \ No newline at end of file diff --git a/packages/cc-server-db/src/index.ts b/packages/cc-server-db/src/index.ts index 2c1e10a..db6652a 100644 --- a/packages/cc-server-db/src/index.ts +++ b/packages/cc-server-db/src/index.ts @@ -1,5 +1,6 @@ export const DBClient = Symbol.for('DBClient') export interface DBClient { + getProvide(): any; find(collection: string, filter: object): Promise; findOne(collection: string, filter: Object): Promise; findOneById(collection: string, objectId: string): Promise;