feat: optimiz db client
This commit is contained in:
parent
507fa20623
commit
bae9995ba7
@ -1,7 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
controller, response, requestBody, httpGet, httpPost, queryParam, requestParam
|
controller, response, requestBody, httpGet, httpPost, queryParam, requestParam
|
||||||
} from 'inversify-express-utils';
|
} from 'inversify-express-utils';
|
||||||
import { inject } from 'inversify';
|
import { inject, postConstruct } from 'inversify';
|
||||||
import { DBClient } from 'cc-server-db'
|
import { DBClient } from 'cc-server-db'
|
||||||
import 'cc-server-db-mongo'
|
import 'cc-server-db-mongo'
|
||||||
|
|
||||||
@ -20,28 +20,33 @@ interface ExampleModel {
|
|||||||
|
|
||||||
type Model = ExampleModel
|
type Model = ExampleModel
|
||||||
|
|
||||||
@controller('/')
|
@controller('')
|
||||||
export class Controller {
|
export class Controller {
|
||||||
@inject(DBClient)
|
@inject(DBClient)
|
||||||
private client: DBClient
|
private client: DBClient
|
||||||
|
|
||||||
|
@postConstruct()
|
||||||
|
private init(): void {
|
||||||
|
this.client.setTable(TABLE);
|
||||||
|
}
|
||||||
|
|
||||||
@httpGet('/')
|
@httpGet('/')
|
||||||
public async list(): Promise<Model[]> {
|
public async list(): Promise<Model[]> {
|
||||||
return this.client.find(TABLE, {});
|
return this.client.find({});
|
||||||
}
|
}
|
||||||
|
|
||||||
@httpGet('/:id')
|
@httpGet('/:id')
|
||||||
public async get(
|
public async get(
|
||||||
@requestParam('id') id: string
|
@requestParam('id') id: string
|
||||||
): Promise<Model> {
|
): Promise<Model> {
|
||||||
return this.client.findOneById(TABLE, id);
|
return this.client.findOneById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@httpPost('/')
|
@httpPost('/')
|
||||||
public async create(
|
public async create(
|
||||||
@requestBody() model: Model
|
@requestBody() model: Model
|
||||||
): Promise<Model> {
|
): Promise<Model> {
|
||||||
return this.client.insertOne(TABLE, model);
|
return this.client.insertOne(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
@httpPost('/:id')
|
@httpPost('/:id')
|
||||||
@ -49,6 +54,6 @@ export class Controller {
|
|||||||
@requestParam('id') id: string,
|
@requestParam('id') id: string,
|
||||||
@requestBody() model: Model
|
@requestBody() model: Model
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
return this.client.updateById(TABLE, id, model);
|
return this.client.updateById(id, model);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
import { Db, ObjectID } from 'mongodb';
|
import { Db, ObjectID, Collection } from 'mongodb';
|
||||||
import { MongoDBConnection } from './connection';
|
import { MongoDBConnection } from './connection';
|
||||||
import { provide } from 'cc-server-ioc'
|
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> implements DBClient {
|
export class MongoDBClient<T = any> implements DBClient {
|
||||||
public db: Db;
|
private db: Db;
|
||||||
|
private collection: Collection;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
MongoDBConnection.getConnection((connection) => {
|
MongoDBConnection.getConnection((connection) => {
|
||||||
@ -17,39 +18,43 @@ export class MongoDBClient<T = any> implements DBClient {
|
|||||||
return this.db;
|
return this.db;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async find(collection: string, filter: object): Promise<T[]> {
|
public setTable(table: string): void {
|
||||||
return await this.db.collection(collection).find(filter).toArray();
|
this.collection = this.db.collection(table);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async findOne(collection: string, filter: Object): Promise<T> {
|
public async find(filter: object): Promise<T[]> {
|
||||||
let result = await this.db.collection(collection).find(filter).limit(1).toArray();
|
return await this.collection.find(filter).toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async findOne(filter: Object): Promise<T> {
|
||||||
|
let result = await this.collection.find(filter).limit(1).toArray();
|
||||||
return result[0];
|
return result[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public async findOneById(collection: string, objectId: string): Promise<T> {
|
public async findOneById(objectId: string): Promise<T> {
|
||||||
return await this.findOne(collection, { _id: new ObjectID(objectId) })
|
return await this.findOne({ _id: new ObjectID(objectId) })
|
||||||
}
|
}
|
||||||
|
|
||||||
public async insertOne(collection: string, model: T): Promise<T> {
|
public async insertOne(model: T): Promise<T> {
|
||||||
var insert = await this.db.collection(collection).insertOne(model);
|
var insert = await this.collection.insertOne(model);
|
||||||
return insert.ops[0];
|
return insert.ops[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateOne(collection: string, where: any, model: any): Promise<boolean> {
|
public async updateOne(where: any, model: any): Promise<boolean> {
|
||||||
let result = await this.db.collection(collection).updateOne(where, { $set: model });
|
let result = await this.collection.updateOne(where, { $set: model });
|
||||||
return result.result.ok == 1 && result.result.n > 0;
|
return result.result.ok == 1 && result.result.n > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateById(collection: string, objectId: string, model: any): Promise<boolean> {
|
public async updateById(objectId: string, model: any): Promise<boolean> {
|
||||||
return await this.updateOne(collection, { _id: new ObjectID(objectId) }, { $set: model })
|
return await this.updateOne({ _id: new ObjectID(objectId) }, { $set: model })
|
||||||
}
|
}
|
||||||
|
|
||||||
public async deleteOne(collection: string, where: any): Promise<boolean> {
|
public async deleteOne(where: any): Promise<boolean> {
|
||||||
let result = await this.db.collection(collection).deleteOne(where);
|
let result = await this.collection.deleteOne(where);
|
||||||
return result.result.ok === 1 && result.result.n > 0
|
return result.result.ok === 1 && result.result.n > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
public async deleteById(collection: string, objectId: string): Promise<boolean> {
|
public async deleteById(objectId: string): Promise<boolean> {
|
||||||
return this.deleteOne(collection, { _id: new ObjectID(objectId) });
|
return this.deleteOne({ _id: new ObjectID(objectId) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
export const DBClient = Symbol.for('DBClient')
|
export const DBClient = Symbol.for('DBClient')
|
||||||
export interface DBClient<T = any> {
|
export interface DBClient<T = any> {
|
||||||
getProvide(): any;
|
getProvide(): any;
|
||||||
find(collection: string, filter: object): Promise<T[]>;
|
setTable(table: string): void;
|
||||||
findOne(collection: string, filter: Object): Promise<T>;
|
find(filter: object): Promise<T[]>;
|
||||||
findOneById(collection: string, objectId: string): Promise<T>;
|
findOne(filter: Object): Promise<T>;
|
||||||
insertOne(collection: string, model: T): Promise<T>;
|
findOneById(objectId: string): Promise<T>;
|
||||||
updateOne(collection: string, where: any, model: any): Promise<boolean>;
|
insertOne(model: T): Promise<T>;
|
||||||
updateById(collection: string, objectId: string, model: any): Promise<boolean>;
|
updateOne(where: any, model: any): Promise<boolean>;
|
||||||
deleteOne(collection: string, where: any): Promise<boolean>;
|
updateById(objectId: string, model: any): Promise<boolean>;
|
||||||
deleteById(collection: string, objectId: string): Promise<boolean>;
|
deleteOne(where: any): Promise<boolean>;
|
||||||
|
deleteById(objectId: string): Promise<boolean>;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user