fix: db init error
This commit is contained in:
parent
86cf26355e
commit
4be889ea63
@ -5,12 +5,16 @@ import { DBClient } from 'cc-server-db'
|
|||||||
|
|
||||||
@provide(DBClient)
|
@provide(DBClient)
|
||||||
export class MongoDBClient<T = any> implements DBClient {
|
export class MongoDBClient<T = any> implements DBClient {
|
||||||
|
private table: string;
|
||||||
private db: Db;
|
private db: Db;
|
||||||
private collection: Collection;
|
private collection: Collection;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
MongoDBConnection.getConnection((connection) => {
|
MongoDBConnection.getConnection((connection) => {
|
||||||
this.db = connection;
|
this.db = connection;
|
||||||
|
if (this.table) {
|
||||||
|
this.collection = this.db.collection(this.table);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,8 +23,11 @@ export class MongoDBClient<T = any> implements DBClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public setTable(table: string): void {
|
public setTable(table: string): void {
|
||||||
|
this.table = table;
|
||||||
|
if (this.db) {
|
||||||
this.collection = this.db.collection(table);
|
this.collection = this.db.collection(table);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async find(filter: object): Promise<T[]> {
|
public async find(filter: object): Promise<T[]> {
|
||||||
return await this.collection.find(filter).toArray();
|
return await this.collection.find(filter).toArray();
|
||||||
|
Loading…
Reference in New Issue
Block a user