From b9e545a445dfef8cf42d837df8e653cba7c1a736 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 14 Aug 2021 23:15:19 +0200 Subject: :art: clean up server bundle --- bundle/src/Database.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 bundle/src/Database.ts (limited to 'bundle/src/Database.ts') diff --git a/bundle/src/Database.ts b/bundle/src/Database.ts new file mode 100644 index 00000000..0efd2471 --- /dev/null +++ b/bundle/src/Database.ts @@ -0,0 +1,43 @@ +import fs from "fs"; +import { MongoMemoryServer } from "mongodb-memory-server"; +import path from "path"; +import exitHook from "async-exit-hook"; +console.log(process.arch, process.platform); +if (process.arch == "ia32") { + Object.defineProperty(process, "arch", { + value: "x64", + }); +} + +export async function setupDatabase() { + const dbPath = path.join(__dirname, "..", "..", "db"); + const dbName = "fosscord"; + const storageEngine = "wiredTiger"; + const port = 27020; + const ip = "127.0.0.1"; + var mongod: MongoMemoryServer; + fs.mkdirSync(dbPath, { recursive: true }); + + exitHook((callback: any) => { + (async () => { + console.log(`Stopping MongoDB ...`); + await mongod.stop(); + console.log(`Stopped MongoDB`); + callback(); + })(); + }); + + console.log(`[Database] starting ...`); + mongod = new MongoMemoryServer({ + instance: { + port, + ip, + dbName, + dbPath, + storageEngine, + auth: false, // by default `mongod` is started with '--noauth', start `mongod` with '--auth' + }, + }); + await mongod.start(); + process.env.MONGO_URL = mongod.getUri(dbName); +} -- cgit 1.5.1