• features
  • 原生

node.js 和 libpq 之间的原生绑定由 node-pg-native 包提供。node-postgres 可以使用此包并使用原生绑定访问 PostgreSQL 服务器,同时为你提供与 JavaScript 版本库相同的接口。

¥Native bindings between node.js & libpq are provided by the node-pg-native package. node-postgres can consume this package & use the native bindings to access the PostgreSQL server while giving you the same interface that is used with the JavaScript version of the library.

要首先使用原生绑定,你需要安装它们:

¥To use the native bindings first you'll need to install them:

$ npm install pg pg-native

一旦安装了 pg-native,你就不需要从 pg 中获取 ClientPool 构造函数,而是执行以下操作:

¥Once pg-native is installed instead of requiring a Client or Pool constructor from pg you do the following:

import pg from 'pg'
const { native } = pg
const { Client, Pool } = native

当你访问 'pg' 上的 .native 属性时,它将自动需要 pg-native 包并将其封装在相同的 API 中。

¥When you access the .native property on 'pg' it will automatically require the pg-native package and wrap it in the same API.

已注意在两者之间进行规范化,但由于使用 libpq 而不是直接在 JavaScript 中处理二进制协议的性质,仍可能存在一些极端情况,在这些情况下,事情的行为会略有不同,因此建议你选择在开发和生产中使用 JavaScript 驱动程序或原生绑定。就其价值而言:我使用纯 JavaScript 驱动程序,因为 JavaScript 驱动程序更易于移植(不需要编译器),并且纯 JavaScript 驱动程序是 plenty 快。

¥Care has been taken to normalize between the two, but there might still be edge cases where things behave subtly differently due to the nature of using libpq over handling the binary protocol directly in JavaScript, so it's recommended you chose to either use the JavaScript driver or the native bindings both in development and production. For what its worth: I use the pure JavaScript driver because the JavaScript driver is more portable (doesn't need a compiler), and the pure JavaScript driver is plenty fast.

一些使用 PostgreSQL 高级功能的模块(例如 pg-query-streampg-cursorpg-copy-streams)需要直接对二进制流进行操作,因此与原生绑定不兼容。

¥Some of the modules using advanced features of PostgreSQL such as pg-query-stream, pg-cursor,and pg-copy-streams need to operate directly on the binary stream and therefore are incompatible with the native bindings.

Last updated on August 24, 2024