Skip to content

Commit

Permalink
ROCKS-13 Allow mongo uri or user,pass,host,port,db
Browse files Browse the repository at this point in the history
  • Loading branch information
patschilf committed Jul 6, 2023
1 parent 3f36a89 commit 0dd2029
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
13 changes: 9 additions & 4 deletions app/layers/mongo/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
export default defineNuxtConfig({
buildDir: `./.build/.nuxt`,
nitro: {
runtimeConfig: {
mongodb: {
uri: ``,
authDb: `admin`,
runtimeConfig: {
mongodb: {
uri: ``,
host: `localhost`,
port: 27017,
db: `db`,
user: `user`,
pass: `pass`,
authSource: ``,
},
},
},
Expand Down
13 changes: 6 additions & 7 deletions app/layers/mongo/server/plugins/mongoose.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import Specimen from "entity-types/Specimen"
import mongoose from "mongoose"
import mongoose, { type ConnectOptions } from "mongoose"
import { type NitroApp } from "nitropack"

export default defineNitroPlugin(async (nitroApp: NitroApp) => {
const { uri, authDb } = useRuntimeConfig().mongodb
const { uri, user, pass, host, port, db, authSource } = useRuntimeConfig().mongodb

if (!uri) {
throw new Error(`No MongoDB connection string provided.`)
const connectOptions: ConnectOptions = {}
if (authSource) {
connectOptions.authSource = authSource
}

await mongoose.connect(uri, {
authSource: authDb,
})
await mongoose.connect(uri || `mongodb://${user}:${pass}@${host}:${port}/${db}`, connectOptions)
console.info(`Connected to MongoDB.`)

await Specimen.init()
Expand Down

0 comments on commit 0dd2029

Please sign in to comment.