add mongo

This commit is contained in:
Sebastian Seedorf
2022-08-17 09:09:49 +02:00
parent 1522962edd
commit fe7e6d8ae2
10 changed files with 326 additions and 2 deletions

20
src/database/start.ts Normal file
View 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)