menu

ZEIT

Our mission is to make cloud computing as easy and accessible as mobile computing. You can find our Next.js community here.

Channels
Team

Maintain Database Connection

October 11, 2019 at 5:06pm

Maintain Database Connection

October 11, 2019 at 5:06pm

How do I maintain the database connection throughout the functions lifetime--while the function is warmed up/is warm.


October 11, 2019 at 7:59pm

You can do something like:

let connection = null
module.exports = (req, res) => {
if (!connection) {
// create connection
}
// use connection
}
Edited
  • reply
  • like

You can do something like:

let connection = null
module.exports = (req, res) => {
if (!connection) {
// create connection
}
// use connection
}

I appreciate the answer, but it only solves the problem partially. Can you elaborate on how this works, or provide a reference to the documentation? I also store the connection in a seperate js file. How is it affected?

Edited
  • reply
  • like

Best Practices for DB connections with Now v2

thumbsup
10
message-simple
19
This answers it.

  • reply
  • like

Took awhile to find it.

Edited
  • reply
  • like

October 13, 2019 at 1:35pm

Glad you sorted it out!

  • reply
  • like
private
This channel has been archived