gpt4 book ai didi

node.js - 如何使用云功能正确连接到 MongoDB?

转载 作者:行者123 更新时间:2023-12-05 02:32:05 25 4
gpt4 key购买 nike

我只想为每个运行 Cloud Functions 的实例连接一次我的 Atlas 集群。

这是我的实例代码:

const MongoClient = require("mongodb").MongoClient;

const client = new MongoClient("myUrl", {
useNewUrlParser: true,
useUnifiedTopology: true,
});

exports.myHttpMethod = functions.region("europe-west1").runWith({
memory: "128MB",
timeoutSeconds: 20,
}).https.onCall((data, context) => {
console.log("Data is: ", data);
client.connect(() => {
const testCollection = client.db("myDB").collection("test");
testCollection.insertOne(data);
});
});

而且我想避免在每个函数调用中使用 client.connect(),这似乎真的太多了。

我想做这样的事情:

const MongoClient = require("mongodb").MongoClient;

const client = await MongoClient.connect("myUrl", {
useNewUrlParser: true,
useUnifiedTopology: true,
});

const db = client.db("myDB");

exports.myHttpMethod = functions.region("europe-west1").runWith({
memory: "128MB",
timeoutSeconds: 20,
}).https.onCall((data, context) => {
console.log("Data is: ", data);
const testCollection = db.collection("test");
testCollection.insertOne(data);
});

但我不能这样等待
在我的 AWS Lambda 函数(在 python 中运行)中,我没有这个问题,每个实例我只能连接一次,所以我想有一个等价物,但我不太了解 JS/Node JS。

最佳答案

您可以将数据库客户端存储为 global variable .从文档中,

Cloud Functions often recycles the execution environment of a previous invocation. If you declare a variable in global scope, its value can be reused in subsequent invocations without having to be recomputed.

尝试重构代码,如下所示:

import * as functions from "firebase-functions";

import { MongoClient } from "mongodb";

let client: MongoClient | null;

const getClient = async () => {
if (!client) {
const mClient = new MongoClient("[MONGODB_URI]", {});
client = await mClient.connect();
functions.logger.log("Connected to MongoDB");
} else {
functions.logger.log("Using existing MongoDB connection");
}
functions.logger.log("Returning client");
return client;
};

export const helloWorld = functions.https.onRequest(
async (request, response) => {
const db = (await getClient()).db("[DATABASE]");
const result = await db.collection("[COLLECTION]").findOne({});
response.send("Hello from Firebase!");
}
);

这应该重用该实例的连接。

enter image description here

关于node.js - 如何使用云功能正确连接到 MongoDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71353766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com