javascript - 使用 Knex.js 和 Objection.js 连接到 Azure Postgres 数据库

标签 javascript node.js azure azure-sql-database knex.js

我一直在尝试使用 Node API 远程连接到我的 azure postgres 数据库。我正在使用最新版本的 Node、objection.js、knex.js 和 pg。

我的主机地址是(databasePath):

project-name.database.windows.net

我已经设置了一个包装器来帮助 SQL 连接。我的连接参数是在 .env 文件中定义的。每当我尝试使用配置进行连接时,都会出现以下异常:

Unhandled rejection error: Invalid Username specified. Please check the Username and retry connection. The Username should be in <username@hostname> format. at Connection.parseE (/Users/Eddie/projects/Giggle-Node/node_modules/pg/lib/connection.js:554:11) at Connection.parseMessage (/Users/Eddie/projects/Giggle-Node/node_modules/pg/lib/connection.js:379:19) at TLSSocket.<anonymous> (/Users/Eddie/projects/Giggle-Node/node_modules/pg/lib/connection.js:119:22) at emitOne (events.js:96:13) at TLSSocket.emit (events.js:188:7) at readableAddChunk (_stream_readable.js:176:18) at TLSSocket.Readable.push (_stream_readable.js:134:10) at TLSWrap.onread (net.js:547:20)

如果我尝试使用连接字符串,DNS 无法解析提供的地址。我尝试过使用以下形式的连接字符串的两种不同变体:

postgres://userName@myHost:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e2e3f2d2d09312c3a1e332716312d2a702e312d2a392c3b2d703a3f2a3f3c3f2d3b703f242b2c3b703d3133" rel="noreferrer noopener nofollow">[email protected]</a>:5432/dbName?ssl=true

postgres://userName:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="176776646440786573577a6e5f78646339677864637065726439737663767576647239766d6265723974787a" rel="noreferrer noopener nofollow">[email protected]</a>:5432/dbName?ssl=true

异常(exception)是:

Unhandled rejection Error: getaddrinfo ENOTFOUND at errnoException (dns.js:28:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)

我的包装如下

const { Model } = require( "objection" );
const Knex = require( "knex" );
const dotenv = require( "dotenv" );

dotenv.config();
class ObjectionSQLHelper
{
    constructor()
    {
        this.SQLDatabaseType = process.env.SQL_VARIANT;
        this.databasePath = process.env.DB_PATH;
        this.dataBaseName = process.env.DB_NAME;
        this.databasePort = process.env.DB_PORT;
        this.databaseUserName = process.env.DB_USER_NAME;
        this.databasePassword = process.env.DB_PASSWORD;

    }

    get connectionSchema()
    {
        const knexSchema =
        {
            client: this.SQLDatabaseType,
            connection: {
                //connectionString: this.connectionString,
                userName: this.databaseUserName,
                passWord: this.databasePassword,
                server: this.databasePath,
                host: this.databasePath,
                options:
                {
                    database: this.dataBaseName,
                    encrypt: true,
                    port: this.dataBasePort,
                    ssl: true
                }
            },
            debug: true
        }
        
        return knexSchema;
    }

    get connectionString()
    {
        return `postgres://${ this.databaseUserName }:${ this.databasePassword }@${ this.databasePath }.postgres.database.azure.com:5432/${ this.dataBaseName }?ssl=true`
    }

    connect()
    {
        this.knexConnection = Knex( this.connectionSchema );
        Model.knex( this.knexConnection );
    }
}

const objectionHelperSinglton = new ObjectionSQLHelper();
objectionHelperSinglton.connect();

我的配置或连接字符串有问题吗? 我还在我的 Azure 数据库上为当前的 IPv4 添加了防火墙异常(exception)。

最佳答案

我使用 Objection 和 Knex 已经有几年了,但我从未见过将它们作为单例的类包装器。但是,从表面上看它是有效的,我相信您的问题在于连接字符串。无法将连接字符串用于 Azure Postgres 服务,因为默认且必需的 Azure Postgres 用户名使用 @在您创建的用户名后指定主机。因此,连接字符串将不起作用,因为 @应该将用户名与主机分开,并且由于他们在用户名中使用它,因此它将破坏连接字符串,这似乎是错误消息的来源。我不相信有办法解决这个问题。我在下面举了一个例子。注意额外的@ ,这会丢失连接字符串。我建议遵循 knex 文档 here .

Azure Postgres 用户名是:user_defined_user_name@user_defined_azure_postgres_database_service_name

您的连接字符串将如下所示:postgres://user_defined_user_name@user_defined_azure_postgres_database_service_name@myHost:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3040514343675f4254705d49785f43441e405f4344574255431e54514451525143551e514a4542551e535f5d" rel="noreferrer noopener nofollow">[email protected]</a>:5432/dbName?ssl=true

虽然我认为上面列出的问题是问题所在,但还可以尝试以下其他一些方法:

  • 如果情况并非如此,您的环境变量是否可能正常工作?您是否通过控制台记录了它们?
  • 您是否直接从 Azure 门户复制了用户名和数据库 URL,并确保末尾没有任何额外的空格?
  • 您是否已设置防火墙规则以启用所有 IP(用于测试目的以验证防火墙不是问题所在)?

关于javascript - 使用 Knex.js 和 Objection.js 连接到 Azure Postgres 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53076150/

相关文章:

azure - Bot 框架 Composer 错误 : Unable to get a value for KnowledgeBaseId from state

javascript - 更改其他 Controller 中的 ng-show 值

javascript - Uncaught ReferenceError : moment is not defined while using moment in laravel with vue js

javascript - gmap 错误 d 未定义

javascript - 这是什么语法错误? (测试 Hapi.js)

javascript - node.js + socket.io : auction website development

ios - 我无法指向 Xcode 库中的 MSClient

javascript - 基于简单输入的 Onblur JS 事件

javascript - 在 Node.js 的 for in 循环中替换项目或从 Arraylist 更新它

azure - 如何从另一个azure函数触发http azure函数