Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update with-mongodb to be TypeScript-friendly #19383

Merged
merged 6 commits into from
Dec 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions examples/with-mongodb/util/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if (!MONGODB_DB) {
let cached = global.mongo

if (!cached) {
cached = global.mongo = {}
cached = global.mongo = { conn: null, promise: null }
}

export async function connectToDatabase() {
Expand All @@ -31,27 +31,18 @@ export async function connectToDatabase() {
}

if (!cached.promise) {
const conn = {}

const opts = {
useNewUrlParser: true,
useUnifiedTopology: true,
}

cached.promise = MongoClient.connect(MONGODB_URI, opts)
.then((client) => {
conn.client = client

return client.db(MONGODB_DB)
})
.then((db) => {
conn.db = db

cached.conn = conn
})
cached.promise = MongoClient.connect(MONGODB_URI, opts).then((client) => {
return {
client,
db: client.db(MONGODB_DB),
}
})
}

await cached.promise

cached.conn = await cached.promise
return cached.conn
}