@graffy/pg

中文文档
0.19.7发布于 2026/09/16Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 6898

The standard Postgres module for Graffy. Each instance this module mounts a Postgres table as a Graffy subtree.

原始使用文档
文档目录8 节
  1. Usage
  2. Options
  3. Database connection
  4. Data model
  5. Filters
  6. Order by
  7. Full Text Search
  8. Aggregations

Postgres

The standard Postgres module for Graffy. Each instance this module mounts a Postgres table as a Graffy subtree.

Requires the pg library to be installed as a peer dependency.

Usage

import pg from '@graffy/pg';
import Graffy from '@graffy/core';
import link from '@graffy/link';

const store = new Graffy();
store.use(path, pg(options));

Options

  • table, the name of the table. If not provided, the last segment of the path is used. This table must exist.
  • idCol: the name of the column to use as ID. Defaults to id. This column must exist and be the primary key or have a unique constraint.
  • verCol: the name of the column to store the Graffy version number. This column must exist, and must have a DEFAULT SQL expression defined - this expression is evaluated to calculate the version number. Graffy versions must monotonically increase, so this expression is typically based on CURRENT_TIMESTAMP.
  • joins: other tables that have foreign keys referencing the ID column in this table, where we want to filter this table using columns of that other table via a "join". The value is a map of join names to join options.
    • join names are the names used to refer to joined tables in filter expressions. Typically, these are the names of those tables.
    • join options are objects with optional properties table, idCol, refCol and verCol. refCol is the name of the column in the join table that references this one.
  • connection: a pg Client or Pool object (recommended), or the arguments for constructing a new Pool object. Optional.

Database connection

Graffy Postgres can be configured to use a specific pg.Client object on a per-request basis, by including a pgClient property on the read or write options. This is useful for implementing transactions, partitioning, row-level security, etc.

If no pgClient is provided for a particular operation, Graffy Postgres falls back to the "global" pg.Client or pg.Pool object defined in the connection parameter in the initialization options. If no connection parameter was passed, a new pg.Pool will be created using PSQL environment variables.

Data model

Graffy Postgres interprets each property as the name of a column, except for $count, $sum etc. as described in the aggregation section below.

It also interprets the $key as specifying filtering, sorting, pagination and aggregation parameters.

Filters

Query filters are JSON-based, somewhat like MongoDB.

  1. Filters expressions follow a property, operator, value order. Values are scalar values (strings or numbers).
  2. Property names are always object keys. They may be strings with dots ..
  3. Operators are placed in objects as well and have a leading $.
  4. Values are JSON values.
  5. Multiple properties in an object are combined with AND. Items in an arrays are combined with OR.
  6. The supported operators are:
    • $eq: optional in most cases.
    • $lt, $lte, $gt, $gte: Ranges
    • $re, $ire: Regex match (case sensitive and insensitive versions)
    • $text: Full text search, always case insensitive
    • $not: Modifies other filters or inverts a condition
    • $and, $or: Combines conditions; optional in most cases
    • $all, $has, $any: Apply conditions to the elements of a collection (list or map)

Basic

  1. { foo: 5 } and { foo: { $eq: 5 } } compile to SQL foo = 5.
  2. { foo: { $gt: 5, $lt: 6 } } becomes foo > 5 AND foo < 6.
  3. { foo: { $ire: '^wor.*' } } becomes foo ~* "^wor.*".
  4. { foo: { $text: 'potatoes' } } becomes foo @@ websearch_to_tsquery('potatoes'). For this to work, foo must be a TSVector column.

Or

  1. { foo: [5, 6] } means foo equals 5 or foo equals 6, and the compiler is smart enough to simplify this to foo IN (5, 6). There is no separate $in.
  2. { foo: [ 5, { $gt: 6 } ] } becomes foo = 5 OR foo > 6
  3. [ { foo: 6 }, { bar: 7 } ] becomes foo = 6 OR bar = 7

Not

  1. { foo: { $not: 6 } } becomes foo <> 6 (the SQL not equals operator)
  2. { foo: { $not: [5, 6] } becomes foo NOT IN (5, 6)
  3. { foo: { $not: [ 5, { $gt: 6 } ] } } becomes NOT (foo = 5 OR foo > 6)

Logic

  1. By default, objects mean AND and arrays mean OR: [ { foo: 5, bar: 6 }, { baz: 7, qux: 4 } ] becomes (foo = 5 AND bar = 6) OR (baz = 7 AND qux = 4)

  2. Use $and and $or operators explicitly to use any structure in either context:

    { $and: [ { $or: { foo: 5, bar: 6 } }, { $or: { baz: 7, qux: 4 } } ] } becomes (foo = 5 OR bar = 6) AND (baz = 7 OR qux = 4).

Contains and Contained by

  1. { tags: { $cts: ['foo', 'bar'] } } becomes tags @> '{"foo","bar"}'. Tags must contain both foo and bar. Note that the array of conditions here does not have OR semantics.
  2. { tags: { $ctd: ['foo', 'bar', 'baz'] } } becomes tags <@ '{"foo","bar","baz"}'. Every tag must be one of foo, bar or baz.

Joins

  1. joinName: { ...expression },

Notes

  1. We drop several MongoDB operators while retaining the capability:
    • $ne: Use $not instead.
    • $in: Use an array of values; it is a combination of implicit $or and $eq.
    • $nin: Use $not and an array of values.
    • $exists: Use null or { $not: null }. Postgres does not distinguish between undefined and null, and neither does Graffy (in this context at least; it's complicated.)
  2. Graffy $has is equivalent to MongoDB $all (each of the provided conditions is met by at least one element in the array). Graffy $all (every element of the array meets a condition) has no direct MongoDB equivalent, but can be expressed as { $not: { $elemMatch: { $not: (cond) } } }
  3. Graffy has a separate operator for case-insensitive regex, and configures the text search locale in the database object rather than the query. This makes MongoDB's regex $options, full text $language etc. unnecessary.
  4. Graffy does not have equivalents for MongoDB operators $type, $expr, $jsonSchema, $where, $mod, $size and the geospatial operators.

Order by

The root of the Graffy filter object must be an object. (Use $or if required.) The property $order specifies the order. Its value must be an array of order specifiers, each of which may be a string property name. The order is ascending by default; property names may be prefixed with ! to switch to descending.

A full-text search query typically has three requirements:

  • Filter: { tsv: { $text: 'query' } }. Return only results that match.
  • Order: { $order: [{ $text: ['tsv', 'query'] }], ... }. Sort results by relevance.
  • Projection: { tsv: { query: true } }. Return snippets of the document surrounding matches.

In all three, tsv is a computed column of type TSVector.

Aggregations

In Graffy PG, aggregations are specified using the $group argument in $key, and special properties like $count, $sum etc. in the projection. $group may be true or an array.

Consider a table of books with columns authorId and copiesSold. We want the to compute aggregates on the copiesSold column.

Without Group By

Let's say we want the total copies sold of all the books in our database. We use $group: true, like:

{
  books: {
  $key: { $group: true },
  $sum: { copiesSold: true }
}

Note how the field to sum is specified; this way, multiple fields may be specified.

As always, the result will mirror the query:

books: [{
  $key: { $group: true },
  $sum: { copiesSold: 12345 }
}]

With Group By

Now let's say we want the separate totals for each author. As we might have a very large number of authors, we might need to paginate over the results.

The grouping properties (e.g. authorId) may also be included in the projection, and these values may even be used to construct links.

{
  books: {
  $key: { $group: ['authorId'], $first: 30 },
  authorId: true,
  author: { namme: true }, // Link to ['users', authorId]
  $sum: { copiesSold: true }
}

Aggregate functions

Graffy supports the following aggregate functions.

  • $count of rows; this is just specified as $count: true, without any fields under it. (All other aggregate functions require fields to be specified.)
  • $sum, $avg, $max, $min
  • $card (cardinality), or the number of unique values in a column

版本与发布记录

@graffy/pg 共发布 170 个版本,首次发布于 2020/12/22;过去 12 个月发布了 21 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.19.7latest2026/09/162 个月前
0.19.62026/08/173 个月前
0.19.52026/08/033 个月前
0.19.42026/06/304 个月前
0.19.32026/05/215 个月前
0.19.22026/05/176 个月前

其他发布通道:pre → 0.19.1-alpha.3。

关于 @graffy/pg 的常见问题

@graffy/pg 是什么?

The standard Postgres module for Graffy. Each instance this module mounts a Postgres table as a Graffy subtree.

如何安装 @graffy/pg?

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

@graffy/pg 支持 TypeScript 吗?

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

@graffy/pg 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@graffy/pg 可以商用吗?

@graffy/pg 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@graffy/pg 的下载量有多少?

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

@graffy/pg 还在维护吗?

最新版本 0.19.7 发布于 2026/09/16(2 个月前);至今共发布 170 个版本,其中过去 12 个月发布了 21 个;首次发布于 2020/12/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。