graphile-history 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
4
+ Copyright (c) 2025 Constructive <developers@constructive.io>
5
+ Copyright (c) 2020-present, Interweb, Inc.
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # graphile-history
2
+
3
+ PostGraphile v5 plugin for per-row **version history**, **point-in-time reads**, and **restore mutations**.
4
+
5
+ It discovers tables tagged with the `@history` smart tag โ€” the companion
6
+ `<table>_history` tables produced by the constructive-db `DataHistory` module โ€”
7
+ and augments the GraphQL schema with:
8
+
9
+ - a `history` field on each source row type: the row's full version stream,
10
+ newest first (`recorded_at DESC`);
11
+ - a `versionAt(at: Datetime!)` field: the version that was current at a given
12
+ instant;
13
+ - a `versionsBetween(from: Datetime!, to: Datetime!)` field: every version
14
+ recorded within an inclusive time window, newest first;
15
+ - a `restore<Table>Version(input: { <pk>, recordedAt, reinsert })` root
16
+ mutation that rewrites the live row from a historical version (optionally
17
+ re-inserting a deleted row).
18
+
19
+ All reads and writes flow through the request's `withPgClient` + `pgSettings`,
20
+ so row-level security and mutation policies are enforced exactly as for any
21
+ other operation. Because a restore writes through the source table, the source
22
+ `DataHistory` trigger records the restore itself as a new version.
23
+
24
+ ## Usage
25
+
26
+ ```typescript
27
+ import { HistoryPreset } from 'graphile-history';
28
+
29
+ const preset = {
30
+ extends: [
31
+ HistoryPreset(),
32
+ ],
33
+ };
34
+ ```
35
+
36
+ ### Options
37
+
38
+ | Option | Default | Description |
39
+ | --- | --- | --- |
40
+ | `historySuffix` | `'_history'` | Suffix used to derive the history table name when the `@history` tag has no explicit name. |
41
+ | `recordedAtColumn` | `'recorded_at'` | Version timestamp column on the history table. |
42
+ | `operationColumn` | `'history_op'` | Operation marker column (INSERT/UPDATE/DELETE). |
43
+ | `immutableColumns` | `['created_at', 'updated_at']` | Source columns never written by a restore (primary keys are always excluded). |
44
+
45
+ ## Example
46
+
47
+ ```graphql
48
+ query {
49
+ postByRowId(rowId: 1) {
50
+ title
51
+ history { title historyOp recordedAt }
52
+ versionAt(at: "2024-02-15T00:00:00Z") { title }
53
+ versionsBetween(from: "2024-01-01T00:00:00Z", to: "2024-03-01T00:00:00Z") {
54
+ title
55
+ recordedAt
56
+ }
57
+ }
58
+ }
59
+
60
+ mutation {
61
+ restorePostVersion(input: { id: 1, recordedAt: "2024-02-15T00:00:00Z" }) {
62
+ version { title }
63
+ restored { title body }
64
+ }
65
+ }
66
+ ```
67
+
68
+ ---
69
+
70
+ ## Education and Tutorials
71
+
72
+ 1. ๐Ÿš€ [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
73
+ Get started with modular databases in minutes. Install prerequisites and deploy your first module.
74
+
75
+ 2. ๐Ÿ“ฆ [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
76
+ Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
77
+
78
+ 3. โœ๏ธ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
79
+ Master the workflow for adding, organizing, and managing database changes with pgpm.
80
+
81
+ 4. ๐Ÿงช [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
82
+ Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
83
+
84
+ 5. โšก [Supabase Testing](https://constructive.io/learn/supabase)
85
+ Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
86
+
87
+ 6. ๐Ÿ’ง [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
88
+ Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
89
+
90
+ 7. ๐Ÿ”ง [Troubleshooting](https://constructive.io/learn/troubleshooting)
91
+ Common issues and solutions for pgpm, PostgreSQL, and testing.
92
+
93
+ ## Related Constructive Tooling
94
+
95
+ ### ๐Ÿ“ฆ Package Management
96
+
97
+ * [pgpm](https://github.com/constructive-io/constructive/tree/main/pgpm/pgpm): **๐Ÿ–ฅ๏ธ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
98
+
99
+ ### ๐Ÿงช Testing
100
+
101
+ * [pgsql-test](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-test): **๐Ÿ“Š Isolated testing environments** with per-test transaction rollbacksโ€”ideal for integration tests, complex migrations, and RLS simulation.
102
+ * [pglite-test](https://github.com/constructive-io/constructive/tree/main/postgres/pglite-test): **๐Ÿชถ Drop-in pgsql-test replacement backed by PGlite** โ€” in-process Postgres, no server required, instance-per-suite isolation.
103
+ * [pgsql-seed](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-seed): **๐ŸŒฑ PostgreSQL seeding utilities** for CSV, JSON, SQL data loading, and pgpm deployment.
104
+ * [supabase-test](https://github.com/constructive-io/constructive/tree/main/postgres/supabase-test): **๐Ÿงช Supabase-native test harness** preconfigured for the local Supabase stackโ€”per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
105
+ * [graphile-test](https://github.com/constructive-io/constructive/tree/main/graphile/graphile-test): **๐Ÿ” Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
106
+ * [pg-query-context](https://github.com/constructive-io/constructive/tree/main/postgres/pg-query-context): **๐Ÿ”’ Session context injection** to add session-local context (e.g., `SET LOCAL`) into queriesโ€”ideal for setting `role`, `jwt.claims`, and other session settings.
107
+
108
+ ### ๐Ÿง  Parsing & AST
109
+
110
+ * [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **๐Ÿ”„ SQL conversion engine** that interprets and converts PostgreSQL syntax.
111
+ * [libpg-query-node](https://www.npmjs.com/package/libpg-query): **๐ŸŒ‰ Node.js bindings** for `libpg_query`, converting SQL into parse trees.
112
+ * [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **๐Ÿ“ฆ Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
113
+ * [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **๐Ÿท๏ธ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
114
+ * [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **๐Ÿ“ Type definitions** for PostgreSQL AST nodes in TypeScript.
115
+ * [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **๐Ÿ› ๏ธ AST utilities** for constructing and transforming PostgreSQL syntax trees.
116
+
117
+ ### ๐Ÿ“š Documentation & Skills
118
+
119
+ * [constructive-skills](https://github.com/constructive-io/constructive-skills): **๐Ÿ“– Platform documentation and AI agent skills** โ€” feature catalog, blueprint reference, SDK guides (i18n, billing, limits, events, uploads, security, entities, search, AI), and deployment guides.
120
+
121
+ Install skills for AI coding agents:
122
+
123
+ ```bash
124
+ # All platform skills (security, blueprints, codegen, billing, etc.)
125
+ npx skills add constructive-io/constructive-skills
126
+
127
+ # Individual repo skills (pgpm, testing, CLI, search, etc.)
128
+ npx skills add https://github.com/constructive-io/constructive --skill pgpm
129
+ npx skills add https://github.com/constructive-io/constructive --skill constructive-testing
130
+ ```
131
+
132
+ ## Credits
133
+
134
+ **๐Ÿ›  Built by the [Constructive](https://constructive.io) team โ€” creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
135
+
136
+ ## Disclaimer
137
+
138
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
139
+
140
+ No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
package/esm/index.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * graphile-history โ€” PostGraphile v5 History Plugin
3
+ *
4
+ * Per-row version history, point-in-time reads, and restore mutations for
5
+ * tables tagged with `@history` (the constructive-db `DataHistory` module).
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { HistoryPreset } from 'graphile-history';
10
+ *
11
+ * const preset = {
12
+ * extends: [
13
+ * HistoryPreset(),
14
+ * ],
15
+ * };
16
+ * ```
17
+ */
18
+ export { createHistoryPlugin, HistoryPlugin } from './plugin';
19
+ export { HistoryPreset } from './preset';
20
+ export type { HistoryColumn, HistoryPluginOptions, HistoryTableInfo } from './types';
package/esm/index.js ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * graphile-history โ€” PostGraphile v5 History Plugin
3
+ *
4
+ * Per-row version history, point-in-time reads, and restore mutations for
5
+ * tables tagged with `@history` (the constructive-db `DataHistory` module).
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { HistoryPreset } from 'graphile-history';
10
+ *
11
+ * const preset = {
12
+ * extends: [
13
+ * HistoryPreset(),
14
+ * ],
15
+ * };
16
+ * ```
17
+ */
18
+ // Plugin
19
+ export { createHistoryPlugin, HistoryPlugin } from './plugin';
20
+ // Preset
21
+ export { HistoryPreset } from './preset';
@@ -0,0 +1,36 @@
1
+ /**
2
+ * PostGraphile v5 History Plugin
3
+ *
4
+ * Discovers tables tagged with `@history` (companion `<table>_history` tables
5
+ * produced by the constructive-db `DataHistory` module) and augments the schema
6
+ * with:
7
+ * - a `history` connection field on each source row type โ€” the row's full
8
+ * version stream ordered by `recorded_at DESC`;
9
+ * - a `versionAt(at: Datetime!)` field returning the version that was current
10
+ * at a given point in time;
11
+ * - a `restore<Table>Version` root mutation that rewrites the live row from a
12
+ * historical version (optionally re-inserting a deleted row).
13
+ *
14
+ * All reads and writes go through the request's `withPgClient` + `pgSettings`,
15
+ * so row-level security and mutation policies are enforced exactly as they are
16
+ * for any other operation. Because restores write through the source table, the
17
+ * source `DataHistory` trigger records the restore itself as a new version.
18
+ *
19
+ * Smart tag format (stamped by `metaschema_generators.data_history`):
20
+ * @history { "history_table": "posts_history", ... }
21
+ * The tag value may also be a bare string naming the history table, or simply a
22
+ * truthy marker (in which case the history table is derived by suffix).
23
+ */
24
+ import 'graphile-build';
25
+ import 'graphile-build-pg';
26
+ import type { GraphileConfig } from 'graphile-config';
27
+ import type { HistoryPluginOptions } from './types';
28
+ declare global {
29
+ namespace GraphileConfig {
30
+ interface Plugins {
31
+ HistoryPlugin: true;
32
+ }
33
+ }
34
+ }
35
+ export declare function createHistoryPlugin(options?: HistoryPluginOptions): GraphileConfig.Plugin;
36
+ export declare const HistoryPlugin: GraphileConfig.Plugin;