add mongo
This commit is contained in:
20
src/database/start.ts
Normal file
20
src/database/start.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import {Db, MongoClient} from 'mongodb'
|
||||
import getConfig from 'next/config'
|
||||
import {Resolvable} from "../utils/Resolvable";
|
||||
|
||||
const { serverRuntimeConfig: {
|
||||
MONGO_URL,
|
||||
MONGO_USER,
|
||||
MONGO_PASS,
|
||||
MONGO_DB
|
||||
} } = getConfig()
|
||||
|
||||
async function getDatabase(): Promise<Db | undefined> {
|
||||
const url = `mongodb://${MONGO_USER ? `${MONGO_USER}:${MONGO_PASS ?? ''}@` : ''}${MONGO_URL}`;
|
||||
const client = new MongoClient(url);
|
||||
await client.connect();
|
||||
console.log('Connected successfully to server')
|
||||
return client.db(MONGO_DB)
|
||||
}
|
||||
|
||||
export const database = new Resolvable(getDatabase)
|
||||
Reference in New Issue
Block a user