4.0.0-beta.2 ā€¢ Published 1 month ago

@graphile-contrib/pg-omit-archived v4.0.0-beta.2

Weekly downloads
1,383
License
MIT
Repository
-
Last release
1 month ago

@graphile-contrib/pg-omit-archived

This Graphile Engine plugin can be used to give your schema support for "soft-deletes" - where you set an is_archived or is_deleted column to true and expect the record to be omitted by default (but it's still available to be recovered should you need to). It's also useful for hiding certain other classes of records by default, but allowing them to be shown by passing a parameter.

It's possible to use this plugin multiple times (for different column names/meanings).

Installing

This requires postgraphile@^4.5.5.

yarn add postgraphile @graphile-contrib/pg-omit-archived

(Or replace yarn add with npm install --save if you use npm.)

Usage

Add a boolean column is_archived to your table to indicate whether the record should be skipped over by default or not:

alter table my_table add column is_archived boolean not null default false;

Then append this plugin to your PostGraphile options.

Usage - CLI

When using this via the CLI, the database column must be named is_archived.

postgraphile --append-plugins @graphile-contrib/pg-omit-archived -c postgres:///my_db

Usage - Library

You can modify the archived column name when using PostGraphile as a library, e.g.:

const express = require("express");
const { postgraphile } = require("postgraphile");
const {
  default: PgOmitArchived,
} = require("@graphile-contrib/pg-omit-archived");

const app = express();

app.use(
  postgraphile(process.env.DATABASE_URL, "app_public", {
    /* šŸ‘‡šŸ‘‡šŸ‘‡ */
    appendPlugins: [PgOmitArchived],
    graphileBuildOptions: {
      pgArchivedColumnName: "is_archived",
    },
    /* ā˜ļøā˜ļøā˜ļø */
  }),
);

app.listen(process.env.PORT || 3000);

You can also use the plugin multiple times for different columns, for example:

const express = require("express");
const { postgraphile } = require("postgraphile");
const {
  custom: customPgOmitArchived,
} = require("@graphile-contrib/pg-omit-archived");

const app = express();

app.use(
  postgraphile(process.env.DATABASE_URL, "app_public", {
    /* šŸ‘‡šŸ‘‡šŸ‘‡ */
    appendPlugins: [
      customPgOmitArchived("archived"),
      customPgOmitArchived("deleted"),
      customPgOmitArchived("template"),
    ],
    graphileBuildOptions: {
      pgArchivedColumnName: "is_archived", // boolean column -> checked as "IS NOT TRUE"
      pgDeletedColumnName: "deleted_at", // non-boolean column -> checked as "IS NULL"
      pgTemplateColumnName: "is_template",
    },
    /* ā˜ļøā˜ļøā˜ļø */
  }),
);

app.listen(process.env.PORT || 3000);

Behaviour

Root level query fields will omit archived records by default.

Plural relation fields on an object will by default be set to INHERIT, which means that if the parent record is archived then all child records will be included; otherwise (if the parent record is NOT archived) only the non-archived child records will be available.

Singular relations and lookups ignore the is_archived column - it's assumed that if you know the exact ID then you're deliberately opting to view the archived record.

This plugin does not prevent people from seeing archived records, it merely prevents them being included by default by various collections so you must opt to see the excluded content.

Assumptions

It's assumed that if a record is archived then all of its children will also be archived. We don't actually care if this is the case or not, and will work regardless, but it's an assumption that we have. It's up to you to enforce this if it makes sense to do so ā”€ database triggers are a good solution to this.

Thanks

šŸ™ This plugin was sponsored by https://sprout.io and is used in production.

4.0.0-beta.3

1 month ago

4.0.0-alpha.1

9 months ago

4.0.0-alpha.2

9 months ago

4.0.0-beta.2

7 months ago

4.0.0-beta.1

8 months ago

4.0.0-alpha.0

9 months ago

3.2.1

2 years ago

3.2.0

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

3.0.0-rc.8

3 years ago

3.0.0-rc.7

3 years ago

3.0.0-rc.6

3 years ago

3.0.0-rc.5

3 years ago

3.0.0-rc.4

3 years ago

3.0.0-rc.2

3 years ago

3.0.0-rc.3

3 years ago

3.0.0-rc.1

3 years ago

3.0.0-rc.0

3 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

0.1.0

5 years ago