Created
December 31, 2019 14:36
-
-
Save manico/d20d952e714be1cda5590944d262969c to your computer and use it in GitHub Desktop.
Lambda Optimise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MongoClient } from 'mongodb'; | |
const dbUri = `mongodb+srv://${process.env.MONGO_DB_USERNAME}:${process.env.MONGO_DB_PASSWORD}@${process.env.MONGO_DB_CLUSTER}?retryWrites=true&w=majority`; | |
let dbCachedConnection = null; | |
const connectToDb = async () => { | |
if (dbCachedConnection) { | |
return dbCachedConnection; | |
} | |
const dbClient = new MongoClient(dbUri, { | |
useNewUrlParser: true, | |
useUnifiedTopology: true, | |
}); | |
await dbClient.connect(); | |
dbCachedConnection = dbClient.db('workboard'); | |
return dbCachedConnection; | |
}; | |
export async function handler(event, context) { | |
const dbConnection = await connectToDb(); | |
const dbBoardCollection = dbConnection.collection('board'); | |
const dbBoardDocs = await dbBoardCollection.find({}).toArray(); | |
return { | |
statusCode: 200, | |
body: JSON.stringify(dbBoardDocs), | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment