summary refs log tree commit diff
path: root/bundle/src/Database.ts
blob: 0efd24719050143b2a35e4a2870d7baa86d7399a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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);
}