Skip to content

Commit 9fed1fe

Browse files
authored
feat: support using this plugin multiple times (#5)
2 parents f5eaa7c + 5fd60e6 commit 9fed1fe

5 files changed

Lines changed: 516 additions & 313 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

README.md

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
This 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
```
1317
yarn 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
4247
const 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

5993
app.listen(process.env.PORT || 3000);
@@ -63,25 +97,26 @@ app.listen(process.env.PORT || 3000);
6397

6498
Root 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
67101
means 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
69103
non-archived child records will be available.
70104

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

75109
This 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

Comments
 (0)