Last active
July 26, 2022 01:21
-
-
Save tim-rohrer/fb6042b708712d53f5b8e528582a8d25 to your computer and use it in GitHub Desktop.
Draft MongDB setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as MongoDB from "mongodb" | |
import { QuickenImportModel } from "./storage.types.js" | |
let db: MongoDB.Db | |
let quickenCollection: MongoDB.Collection<QuickenImportModel> | |
export const setupDatabaseServices = async () => { | |
const env = process.env.NODE_ENV || "development" | |
const isDevelopment = env === "development" || "test" | |
const host = process.env.MONGODB_HOST_PORT || "localhost:27017" | |
let uri: string | undefined | |
let mongoClientOptions: MongoDB.MongoClientOptions | |
if (!isDevelopment) { | |
mongoClientOptions = { | |
maxPoolSize: 50, | |
w: "majority", | |
wtimeoutMS: 2500, | |
} | |
uri = process.env.MONGO_URI | |
if (uri === undefined) throw new Error("Undefined URI") | |
} else { | |
const userName = process.env.MONGODB_USERNAME || "not" | |
const password = process.env.MONGODB_PASSWORD || "configured" | |
uri = `mongodb://${userName}:${password}@${host}` | |
mongoClientOptions = { | |
authMechanism: "SCRAM-SHA-256", | |
ssl: false, | |
authSource: "admin", | |
} | |
} | |
const clientConnected = await mongoConnection(uri, mongoClientOptions) | |
db = clientConnected.db("test") | |
quickenCollection = db.collection<QuickenImportModel>("quicken") | |
} | |
const mongoConnection = async ( | |
uri: string, | |
options: MongoDB.MongoClientOptions, | |
) => await MongoDB.MongoClient.connect(uri, options) | |
export { quickenCollection } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment