npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@graphile-contrib/pg-relation-not-null

v0.1.0

Published

Publish PostGraphile forward relations non-null where the PostgreSQL catalog proves the target row exists.

Readme

@graphile-contrib/pg-relation-not-null

A PostGraphile v5 plugin that marks a forward relation (from a row to the row its key references) as non-nullable where the PostgreSQL catalog proves the target row is always there for the role the schema is built under.

PostGraphile publishes every forward relation nullable, because row-level security may hide the target row from the reading role. Often the catalog shows it cannot, and assuming things never change, the client is left handling a null that never comes.

[!WARNING] Benjie's note: beware that changing a relation from non-nullable to nullable is a breaking change (existing clients won't have needed null handling code, but nulls are now possible), so be sure that these rules aren't just true for your rows now, but will remain true for the lifetime of all of your existing clients. In particular note that adding or changing your RLS policies may change the nullability assertions you can make about a row as seen by a consumer.

Install

npm install @graphile-contrib/pg-relation-not-null

Use

import { PgRelationNotNullPlugin } from '@graphile-contrib/pg-relation-not-null'

export default {
  plugins: [PgRelationNotNullPlugin()]
}

The plugin takes no options and knows no other plugin. It reads the catalog at introspection, as the role the schema is built under (the service's pgSettingsForIntrospection included), and decides each relation in PgRelationsPlugin's pgRelations_relation hook, which runs after every introspection hook — so after every smart tag, and every tag another plugin declares in the introspection, is in place. It needs no ordering.

The answer is written where a |@notNull tail of a @foreignKey smart tag lands, relation.extensions.tags.notNull: a relation the plugin proves publishes exactly as one carrying the tail, and the tail is then redundant.

The rule

A forward relation is published non-null only when all of these hold; where any one is not proved, the relation is left exactly as it was. The plugin only ever adds non-nullness, never removes it.

  1. Every key column is non-null in the introspection: NOT NULL in the catalog, or declared non-null by a tag (@notNull, a @primaryKey column).
  2. The target's existence is guaranteed by real foreign keys. For a real constraint, itself; for a declared key, every key its @backedBy names, and at least one (below). Each is a pg_constraint row with contype = 'f' that references this relation's target table by the same columns and is validated (convalidated), enforced (conenforced, PostgreSQL 18; every key of an earlier major is enforced) and not deferrable (condeferrable), so no transaction sees a referencing row without its target. The referencing relation is not a materialized view, whose stored row outlives the row its key referenced.
  3. The target hides no row from the role. The target is a table (relkind r or p) and row_security_active(target) is false for the role: row security is off on it, or the role bypasses it (BYPASSRLS, or it owns the table and row security is not forced).
  4. The role may read the target. has_column_privilege(target, column, 'SELECT') for every referenced column.

@backedBy

A key a smart tag declares — @foreignKey, which PgFakeConstraintsPlugin turns into a constraint with no pg_constraint row of its own — carries no guarantee that its target exists. Whoever declares it states which real foreign keys back it, with a tag on the key after |, where a |@notNull tail goes:

@foreignKey (currency_id) references app.currency (id)|@backedBy app.tx.tx_currency_id_fkey

The value is one or more <schema>.<table>.<constraint>, comma-separated — one per row source where the declaring relation has several (a UNION's branches). It is a pointer the plugin verifies, not a claim it believes: each named constraint is read in pg_constraint, and a name that does not exist, is not a foreign key, references another target, or does not hold by the rule proves nothing. What the tag cannot be checked for is the declaration itself — that the declaring relation's column carries the base column's value — which is the declarer's statement, as the @foreignKey is. PostGraphile ignores the tag; a schema built with and without it is the same.

@graphile-contrib/view-constraints writes @backedBy on every relation of a view it leads from real foreign keys; a human writing @foreignKey by hand may write it too.

Known limits

  • A relation of a polymorphic codec (@interface) is not proved. Such a relation is published on the interface, and its implementations reach it through copies that PgPolymorphismPlugin publishes nullable; an interface field cannot be non-null while an implementation's field is nullable, so the schema would not build. Proving these relations needs the copies to carry the answer too.
  • A target that is a view, a materialized view or anything else whose rows a predicate may filter is not proved, even where it filters nothing.
  • The answer is for the role the schema is built under. A schema served under another role is answered for the build role only; a role that may not read the target gets a permission error from the database, not a null.
  • The rule reads no plugin that adds conditions to a forward relation's own query; a plugin that filters a to-one relation's target would make a proved relation wrong (@graphile-contrib/pg-omit-archived filters only connections and lists).
  • A backward relation is never touched.

Tests

yarn test runs the rule over hand-built catalog facts, with no database: each condition fails a relation on its own. yarn test:lab builds a schema on a database of its own, as a role that does not own it, without the plugin and with it, and compares each relation's published type: non-null in every proved case, unchanged in each unproved one. It needs a superuser connection (PGHOST, PGPORT, PGUSER), as it creates that role and the database; CI runs it against PostgreSQL 15 to 18.