@harryplusplus/knex-pglite

中文文档
0.13.13发布于 2025/10/14MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 10万

A dialect for using PGlite with Knex.js.

原始使用文档
文档目录10 节
  1. Table of Contents
  2. Language
  3. Installation
  4. Usage
  5. Easy initialization
  6. Injecting a PGlite instance
  7. API
  8. client
  9. connection
  10. License

Knex.js PGlite Dialect

test npm version npm downloads

A dialect for using PGlite with Knex.js.

Table of Contents

Language

Installation

Install with the following commands.

# using npm
npm i knex @electric-sql/pglite @harryplusplus/knex-pglite
# using pnpm
pnpm add knex @electric-sql/pglite @harryplusplus/knex-pglite

[!WARNING]
Yarn and Bun environments have not been tested yet for compatibility.

Usage

Easy initialization

Configure the client and connection properties of the Knex creation function. The value of the connection property is used as the dataDir parameter of the PGlite constructor.

[!NOTE]
For detailed specifications of dataDir in the PGlite constructor, please refer to the PGlite API dataDir documentation.

The example below shows how to do an easy initialization.

import { PGliteDialect } from "@harryplusplus/knex-pglite";
import Knex from "knex";

const knex = Knex({
  client: PGliteDialect, // Knex PGlite Dialect

  // In-memory ephemeral storage
  connection: {},
  connection: "",
  connection: "memory://",

  // IndexedDB storage
  connection: "idb://",

  // File system storage
  connection: "/my/pglite/data",
});

[!WARNING]
Without the connection property, Knex operates in unconnected mode. Since the unconnected mode works as a SQL string generator, you must define a value, even if it is an empty object, in order to use PGlite.

[!WARNING]
connection values ​​in the form of a File URI (e.g. file:///my/pglite/data) cannot be used because they are not parsed in a form suitable for initializing PGlite according to the current Knex connection string parsing rules. Please enter the file path (e.g. /my/pglite/data) rather than the file URI.

Injecting a PGlite instance

When using the Easy initialization method, the PGlite instance becomes owned by the Knex instance. In other words, a PGlite instance shares its lifecycle with a Knex instance.

For a PGlite instance with in-memory temporary storage, sharing the lifecycle with a Knex instance may not meet your needs depending on your use case. Additionally, various parameters for PGlite initialization cannot be utilized.

Configure the connection.pglite property to a synchronous or asynchronous function that returns a PGlite instance. In this case, Knex internally considers the PGlite instance to be in a borrowed state. In other words, Knex does not manage the creation and destruction of PGlite instances, since it delegates the lifecycle of PGlite instances to an external entity.

The example below shows how to inject a PGlite instance.

import { PGlite } from "@electric-sql/pglite";
import Knex from "knex";
import {
  PGliteConnectionConfig,
  PGliteDialect,
} from "@harryplusplus/knex-pglite";

const pglite = new PGlite();

const knex = Knex({
  client: PGliteDialect,
  connection: {
    pglite: () => pglite,
  } satisfies PGliteConnectionConfig as Knex.Knex.StaticConnectionConfig,
});

[!NOTE]
Here's why we use satisfies PGliteConnectionConfig as Knex.Knex.StaticConnectionConfig:

  1. Use satisfies PGliteConnectionConfig to enforce the type of the connection property required by the Knex PGlite Dialect.
  2. Assert that it conforms to the Knex type definition using as Knex.Knex.StaticConnectionConfig.

API

Configure the following properties from the parameters of the Knex creation function:

client

To use the Knex PGlite Dialect, you must specify the PGliteDialect class in the client property.

connection

The type of the connection property is:

export interface PGliteConnectionConfig {
  pglite?: PGliteProvider;
}

The following are the properties for configuring connection.

pglite

The type of the pglite attribute is:

export interface PGliteProvider {
  (): MaybePromise<PGlite>; // (): PGlite | Promise<PGlite>
}

[!NOTE]
To learn more about PGlite's various features, please read the PGlite API documentation.

License

MIT

版本与发布记录

@harryplusplus/knex-pglite 共发布 40 个版本,首次发布于 2025/10/05;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.13.13latest2025/10/141.1 年前
0.13.122025/10/141.1 年前
0.13.112025/10/141.1 年前
0.13.102025/10/141.1 年前
0.13.92025/10/141.1 年前
0.13.82025/10/141.1 年前

其他发布通道:dev → 0.1.1-dev.3。

关于 @harryplusplus/knex-pglite 的常见问题

@harryplusplus/knex-pglite 是什么?

A dialect for using PGlite with Knex.js.

如何安装 @harryplusplus/knex-pglite?

在项目目录运行 npm install @harryplusplus/knex-pglite,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@electric-sql/pglite(^0.3.10)、knex(^3.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@harryplusplus/knex-pglite 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@harryplusplus/knex-pglite 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@harryplusplus/knex-pglite 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 20.19.5。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@harryplusplus/knex-pglite 可以商用吗?

@harryplusplus/knex-pglite 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@harryplusplus/knex-pglite 的下载量有多少?

根据 npm 官方下载统计,@harryplusplus/knex-pglite 在 2026/10/18 – 2026/11/16 这 30 天内被下载 99,843 次,日均约 3,328 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@harryplusplus/knex-pglite 还在维护吗?

最新版本 0.13.13 发布于 2025/10/14(1.1 年前);至今共发布 40 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/10/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。