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

19
src/database/groups.ts Normal file
View File

@@ -0,0 +1,19 @@
import {database} from "./start";
import {Dict, Group} from "../types";
import crypto from "crypto"
export async function setGroups(groups: Dict<Group>, ignored: string[], base: string[]) {
const collection = (await database.resolve())?.collection('setups')
if (!collection) return
await collection.deleteMany({}).catch(e => {
if (e.message !== 'ns not found') throw e
})
const result = await collection.insertOne({
groups,
ignored,
base,
createdOn: new Date(),
modifiedOn: new Date()
})
return result.insertedId.toString()
}

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)