如何在 Node JS 中更改 GCP 数据存储区数据库连接?

问题描述 投票:0回答:1

我正在尝试更改我在节点应用程序中连接到的 GCP 数据存储区中的数据库。

我已建立连接并进行身份验证并运行,我的应用程序是使用 Cloud Run 部署的。我可以使用

const datastore = new Datastore();
连接到默认数据库。

但是,我无法更改 GCP 项目中的命名数据库。我尝试了许多不同的选项,但缺乏文档。我在网上能找到的关于访问命名数据库的唯一内容是 Google 文档中的这个小段落。

下面是我的整个测试文件:

const { Datastore } = require("@google-cloud/datastore");

const datastore = new Datastore();

export const testFunction = async (
    postBody: any,
) => {
  try {
      quickstart(postBody);
  } catch (e) {
    console.error(`Error executing workflow: ${e}`);
  }
}

async function quickstart(postBody: any) {

    const { test, name } = postBody;
    
    // The kind for the new entity
    const kind = 'hello';

    // The Cloud Datastore key for the new entity
    const taskKey = datastore.key([kind, test]);
  
    // Prepares the new entity
    const task = {
      key: taskKey,
      data: {
          name : name 
      },
    };
  
    // Saves the entity
    await datastore.save(task);
    console.log(`Saved ${task.key.test}: ${task.data.name }`);
  }
export default { testFunction };

我尝试了以下一些方法,但没有成功:

  • const datastore = new Datastore("test-database");

  • const datastore = new Datastore({ databaseId: "test-database", });

  • const datastore = new Datastore({ namespace: "test-database", });

node.js google-cloud-platform google-cloud-datastore
1个回答
0
投票

今天发布了 Node.js 客户端库中对命名数据库的支持 (https://github.com/googleapis/nodejs-datastore/releases/tag/v8.2.0)。

const datastore = new Datastore({ databaseId: "test-database", });
应该适用于 v8.2.0 .

© www.soinside.com 2019 - 2024. All rights reserved.