feat: add provide and update template

This commit is contained in:
MiaoWoo 2019-06-11 16:11:34 +08:00
parent 829ca19b4a
commit 177c001bb8
5 changed files with 48 additions and 11 deletions

View File

@ -1,23 +1,54 @@
import { import {
controller, httpGet, httpPost controller, response, requestBody, httpGet, httpPost, queryParam
} from 'inversify-express-utils'; } from 'inversify-express-utils';
import { inject } from 'inversify'; import { inject } from 'inversify';
import { DBClient } from 'cc-server-db' import { DBClient } from 'cc-server-db'
import 'cc-server-db-mongo' 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('/') @controller('/')
export class UserController { export class Controller {
constructor( @inject(DBClient)
@inject(DBClient) private client: DBClient private client: DBClient
) { }
@httpGet('/') @httpGet('/')
public async getUsers(): Promise<any[]> { public async list(): Promise<Model[]> {
return this.client.find('users', {}); return this.client.find(TABLE, {});
}
@httpGet('/:id')
public async get(
@queryParam('id') id: string
): Promise<Model> {
return this.client.findOneById(TABLE, id);
} }
@httpPost('/') @httpPost('/')
public async newUser(): Promise<any> { public async create(
return {} @requestBody() model: Model
): Promise<Model> {
return this.client.insertOne(TABLE, model);
}
@httpPost('/:id')
public async update(
@queryParam('id') id: string,
@requestBody() model: Model
): Promise<boolean> {
return this.client.updateById(TABLE, id, model);
} }
} }

View File

@ -4,7 +4,7 @@ import { provide } from 'cc-server-ioc'
import { DBClient } from 'cc-server-db' import { DBClient } from 'cc-server-db'
@provide(DBClient) @provide(DBClient)
export class MongoDBClient<T = any> { export class MongoDBClient<T = any> implements DBClient {
public db: Db; public db: Db;
constructor() { constructor() {
@ -13,6 +13,10 @@ export class MongoDBClient<T = any> {
}); });
} }
public getProvide(): Db {
return this.db;
}
public async find(collection: string, filter: object): Promise<T[]> { public async find(collection: string, filter: object): Promise<T[]> {
return await this.db.collection(collection).find(filter).toArray(); return await this.db.collection(collection).find(filter).toArray();
} }

View File

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

View File

@ -1,5 +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(): any;
find(collection: string, filter: object): Promise<T[]>; find(collection: string, filter: object): Promise<T[]>;
findOne(collection: string, filter: Object): Promise<T>; findOne(collection: string, filter: Object): Promise<T>;
findOneById(collection: string, objectId: string): Promise<T>; findOneById(collection: string, objectId: string): Promise<T>;