Created
November 12, 2021 02:33
-
-
Save Lightnet/69f137287c918b2d46c8a25aee7a09dc to your computer and use it in GitHub Desktop.
reused instance for JavaScript in nodejs
This file contains hidden or 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
// 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