Skip to content

Instantly share code, notes, and snippets.

@Lightnet
Created November 12, 2021 02:33
Show Gist options
  • Save Lightnet/69f137287c918b2d46c8a25aee7a09dc to your computer and use it in GitHub Desktop.
Save Lightnet/69f137287c918b2d46c8a25aee7a09dc to your computer and use it in GitHub Desktop.
reused instance for JavaScript in nodejs
// https://www.tutorialguruji.com/javascript/reusing-database-connections-with-azure-functions-using-javascript/amp/
// https://pretagteam.com/question/reusing-database-connections-with-azure-functions-using-javascript
class DBTest{
constructor(){
this.data="test";
}
setText(params) {
this.data=params;
}
getText() {
return this.data;
}
}
let dbInstance;
module.exports = async function() {
console.log("CHECKING DATA...");
if (!dbInstance) {
console.log("INIT DATA");
//dbInstance = "test";
dbInstance = new DBTest();
}else{
console.log("reused!")
}
return dbInstance;
};
// function.js
/*
const getDb = require('./getDb.js');
module.exports = async function(context, trigger) {
let db = await getDb();
// ... do stuff with db ..
};
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment