22
33This Graphile Engine plugin can be used to give your schema support for
44"soft-deletes" - where you set an ` is_archived ` or ` is_deleted ` column to true
5- and expect the record to be omitted by default (but it's still available to
6- be recovered should you need to).
5+ and expect the record to be omitted by default (but it's still available to be
6+ recovered should you need to). It's also useful for hiding certain other classes
7+ of records by default, but allowing them to be shown by passing a parameter.
8+
9+ It's possible to use this plugin multiple times (for different column
10+ names/meanings).
711
812## Installing
913
10- This requires ` postgraphile@^4.0.1 `
14+ This requires ` postgraphile@^4.5.5 ` .
1115
1216```
1317yarn add postgraphile @graphile-contrib/pg-omit-archived
@@ -36,7 +40,8 @@ postgraphile --append-plugins @graphile-contrib/pg-omit-archived -c postgres:///
3640
3741### Usage - Library
3842
39- You can modify the archived column name when using PostGraphile as a library, e.g.:
43+ You can modify the archived column name when using PostGraphile as a library,
44+ e.g.:
4045
4146``` js
4247const express = require (" express" );
@@ -53,7 +58,36 @@ app.use(
5358 pgArchivedColumnName: " is_archived" ,
5459 },
5560 /* ☝️☝️☝️ */
56- })
61+ }),
62+ );
63+
64+ app .listen (process .env .PORT || 3000 );
65+ ```
66+
67+ You can also use the plugin multiple times for different columns, for example:
68+
69+ ``` js
70+ const express = require (" express" );
71+ const { postgraphile } = require (" postgraphile" );
72+ const PgOmitArchived = require (" @graphile-contrib/pg-omit-archived" );
73+
74+ const app = express ();
75+
76+ app .use (
77+ postgraphile (process .env .DATABASE_URL , " app_public" , {
78+ /* 👇👇👇 */
79+ appendPlugins: [
80+ PgOmitArchived .custom (" archived" ),
81+ PgOmitArchived .custom (" deleted" ),
82+ PgOmitArchived .custom (" template" ),
83+ ],
84+ graphileBuildOptions: {
85+ pgArchivedColumnName: " is_archived" , // boolean column -> checked as "IS NOT TRUE"
86+ pgDeletedColumnName: " deleted_at" , // non-boolean column -> checked as "IS NULL"
87+ pgTemplateColumnName: " is_template" ,
88+ },
89+ /* ☝️☝️☝️ */
90+ }),
5791);
5892
5993app .listen (process .env .PORT || 3000 );
@@ -63,25 +97,26 @@ app.listen(process.env.PORT || 3000);
6397
6498Root level query fields will omit archived records by default.
6599
66- Plural relation fields on an object will by default be set to INHERIT, which
100+ Plural relation fields on an object will by default be set to ` INHERIT ` , which
67101means that if the parent record is archived then all child records will be
68- included; otherwise (if the parent record is NOT archived) only the
102+ included; otherwise (if the parent record is _ NOT _ archived) only the
69103non-archived child records will be available.
70104
71105Singular relations and lookups ignore the ` is_archived ` column - it's assumed
72106that if you know the exact ID then you're deliberately opting to view the
73107archived record.
74108
75109This plugin ** does not** prevent people from seeing archived records, it merely
76- prevents them being included _ by default_ by various collections so you must
77- opt to see archived content.
110+ prevents them being included _ by default_ by various collections so you must opt
111+ to see the excluded content.
78112
79113## Assumptions
80114
81- It's assumed that if a record is archived then all of its children should also
82- be archived. We don't actually care if this is the case or not, and will work
83- regardless, but it's an assumption that we have.
115+ It's assumed that if a record is archived then all of its children will also be
116+ archived. We don't actually care if this is the case or not, and will work
117+ regardless, but it's an assumption that we have. It's up to you to enforce this
118+ if it makes sense to do so ─ database triggers are a good solution to this.
84119
85120## Thanks
86121
87- 🙏 This plugin was sponsored by https://sprout.io
122+ 🙏 This plugin was sponsored by https://sprout.io and is used in production.
0 commit comments