mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 09:38:19 +08:00
add db mock
This commit is contained in:
1 parent
d01034bca8
commit
09ced5cdff
7 files changed
+109
-9
No files matched your search
@@ -0,0 +1,31 @@
|
||||
/* eslint-disable import/no-mutable-exports */
|
||||
import { EventEmitter } from 'events';
|
||||
import mongodb, { Db, MongoClient } from 'mongodb';
|
||||
|
||||
export let db: Db = null;
|
||||
export let client: MongoClient = null;
|
||||
|
||||
export const bus = new EventEmitter();
|
||||
|
||||
mongodb.MongoClient.connect(
|
||||
// @ts-ignore
|
||||
global.__MONGO_URI__,
|
||||
{ useNewUrlParser: true, useUnifiedTopology: true },
|
||||
).then((c) => {
|
||||
// @ts-ignore
|
||||
db = c.db(global.__MONGO_DB_NAME__);
|
||||
client = c;
|
||||
bus.emit('connect');
|
||||
});
|
||||
|
||||
export function collection(c: string) {
|
||||
return db.collection(c);
|
||||
}
|
||||
|
||||
export function getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
export function getDb() {
|
||||
return db;
|
||||
}
|
||||
Reference in new issue
Block a user