Hi Pavel,
Yes, global variables are supported in GC Functions and I’ve already used them. Here is my implementation:
var client;
const getClient = async () => {
if (client && client.isConnected()) {
console.log("MONGODB CLIENT ALREADY CONNECTED!");
} else
try {
client = await MongoClient.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("MONGODB CLIENT RECONNECTED!");
} catch (e) {
throw e;
}
return client;
};
exports.getPlacesFromDB = functions.https.onCall((data, context) => {
var limitOption = 10;
var query = {};
var sortOption = {};
return getClient().then((client) => {
const db = client.db("database");
const collection = db.collection("collection");
var _cursor = collection.find(query).skip(skips).limit(limitOption).sort(sortOption);
return _cursor.map(({ _id, ...d }) => ({ _id: _id.toString(), ...d })).toArray().then((result) => {
// Returning the message to the client.
return { text: 'Retrieved data!', data: result };
})
.catch((error) => {
functions.logger.log("error:", error.message);
throw new functions.https.HttpsError('cannot get data!', error.message, error);
});
}).catch((error) => {
functions.logger.log("error:", error.message);
throw new functions.https.HttpsError('no connection', error.message, error);
});
});
In the AWS article I shared, they are suggesting to disable callbackWaitsForEmptyEventLoop in the Context object of the Lambda function. I am trying to see how can this be done in GCP?
Thanks!