Skip to content

Instantly share code, notes, and snippets.

@tim-rohrer
Last active July 26, 2022 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tim-rohrer/fb6042b708712d53f5b8e528582a8d25 to your computer and use it in GitHub Desktop.
Save tim-rohrer/fb6042b708712d53f5b8e528582a8d25 to your computer and use it in GitHub Desktop.
Draft MongDB setup
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