feat: format and rebuild

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2019-06-29 17:02:30 +08:00
parent aeedbc4222
commit f541290f47
12 changed files with 33 additions and 57 deletions

View File

@@ -4,24 +4,15 @@ 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 isConnected: boolean = false;
private static db: Db;
public static getConnection(result: (connection: Db) => void) {
if (this.isConnected) {
return result(this.db);
} else {
this.connect((error: Error, db: Db) => {
return result(this.db);
});
}
public static async getConnection(): Promise<Db> {
if (!this.db) { this.db = await this.connect() }
return this.db;
}
private static connect(result: (error: Error, db: Db) => void) {
MongoClient.connect(connStr, { useNewUrlParser: true }, (err, client) => {
this.db = client.db(dbName);
this.isConnected = true;
return result(err, this.db);
});
private static async connect(): Promise<Db> {
let client = await MongoClient.connect(connStr, { useNewUrlParser: true });
return client.db(dbName);
}
}

View File

@@ -1,7 +1,7 @@
import { Db, ObjectID, Collection } from 'mongodb';
import { MongoDBConnection } from './connection';
import { provide } from '@cc-server/ioc'
import { DBClient } from '@cc-server/db'
import { MongoDBConnection } from './connection'
import { Db, ObjectID, Collection } from 'mongodb'
import { provide, postConstruct } from '@cc-server/ioc'
@provide(DBClient)
export class MongoDBClient<T = any> implements DBClient {
@@ -9,13 +9,12 @@ export class MongoDBClient<T = any> implements DBClient {
private db: Db;
private collection: Collection<T>;
constructor() {
MongoDBConnection.getConnection((connection) => {
this.db = connection;
if (this.table) {
this.collection = this.db.collection(this.table);
}
});
@postConstruct()
private async init() {
this.db = await MongoDBConnection.getConnection();
if (this.table) {
this.collection = this.db.collection(this.table);
}
}
public getProvide<P>(): P {

View File

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