WIP mongodb-memory-serve

This commit is contained in:
negue
2024-10-29 22:43:03 +01:00
parent 7c9b7be012
commit cce4363319
7 changed files with 562 additions and 22 deletions

View File

@@ -0,0 +1,41 @@
/* eslint-disable-file */
import { MongoMemoryServer } from 'mongodb-memory-server';
import { dirname } from 'node:path';
import { existsSync, mkdirSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
const TEST_MODE = process.argv.includes('--test-db');
const BUILD_MODE = process.argv.includes('--build-db');
const __dirname = dirname(fileURLToPath(import.meta.url));
const pathSuffix = TEST_MODE ? '-test' : BUILD_MODE ? '-build' : '';
const mongoDbPath = `${__dirname}/../mongodb-data`+pathSuffix;
if (!existsSync(mongoDbPath)) {
mkdirSync(mongoDbPath);
}
(async () => {
// async code in where
// This will create an new instance of "MongoMemoryServer" and automatically start it
const mongod = await MongoMemoryServer.create({
instance: {
port: 27017,
dbPath: mongoDbPath,
},
dispose: {
cleanup: !BUILD_MODE && !TEST_MODE,
}
});
const uri = mongod.getUri();
console.log(`MongoDB running at ${uri}`, mongod.state);
// Keep the Node.js process running
setInterval(() => {}, 1000);
})();