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

@vendia/serverless-express compatability #124

Open
ckahn1 opened this issue Dec 17, 2021 · 1 comment
Open

@vendia/serverless-express compatability #124

ckahn1 opened this issue Dec 17, 2021 · 1 comment

Comments

@ckahn1
Copy link

ckahn1 commented Dec 17, 2021

I am trying to implement serverless-mysql with my serverless-express app. I'm having issues initializing serverless-mysql and then passing that connection into the main handler so I can run queries in functions that are called by the router. I found this example online that runs a setup task for connecting to a db, but I'm concerned if I implement this with serverless-mysql it won't manage the connections properly.


// lambda.js
require('source-map-support/register')
const serverlessExpress = require('@vendia/serverless-express')
const app = require('./app')

let serverlessExpressInstance

function asyncTask () {
  return new Promise((resolve) => {
    setTimeout(() => resolve('connected to database'), 1000)
  })
}

async function setup (event, context) {
  const asyncValue = await asyncTask()
  console.log(asyncValue)
  serverlessExpressInstance = serverlessExpress({ app })
  return serverlessExpressInstance(event, context)
}

function handler (event, context) {
  if (serverlessExpressInstance) return serverlessExpressInstance(event, context)

  return setup(event, context)
}

exports.handler = handler 
@ckahn1
Copy link
Author

ckahn1 commented Dec 17, 2021

In addition to this, I would need to import the serverless mysql in these other functions, which I think would try and reinitialize the mysql connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant