@pgpm/stamps 0.4.0 → 0.5.0
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/Makefile
CHANGED
package/README.md
CHANGED
|
@@ -2,4 +2,273 @@
|
|
|
2
2
|
|
|
3
3
|
Timestamp utilities and audit trail functions for PostgreSQL.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
`@pgpm/stamps` provides PostgreSQL trigger functions for automatically managing timestamp and user tracking columns in your tables. This package simplifies audit trail implementation by automatically setting `created_at`, `updated_at`, `created_by`, and `updated_by` fields.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **timestamps()**: Trigger function that automatically manages `created_at` and `updated_at` columns
|
|
12
|
+
- **peoplestamps()**: Trigger function that automatically manages `created_by` and `updated_by` columns using JWT claims
|
|
13
|
+
- Automatic preservation of creation timestamps and users on updates
|
|
14
|
+
- Integration with `@pgpm/jwt-claims` for user context
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
If you have `pgpm` installed:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pgpm install @pgpm/stamps
|
|
22
|
+
pgpm deploy
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
This is a quick way to get started. The sections below provide more detailed installation options.
|
|
26
|
+
|
|
27
|
+
### Prerequisites
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
# Install pgpm globally
|
|
31
|
+
npm install -g pgpm
|
|
32
|
+
|
|
33
|
+
# Start PostgreSQL
|
|
34
|
+
pgpm docker start
|
|
35
|
+
|
|
36
|
+
# Set environment variables
|
|
37
|
+
eval "$(pgpm env)"
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Deploy
|
|
41
|
+
|
|
42
|
+
#### Option 1: Deploy by installing with pgpm
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pgpm install @pgpm/stamps
|
|
46
|
+
pgpm deploy
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
#### Option 2: Deploy from Package Directory
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
cd packages/data-types/stamps
|
|
53
|
+
pgpm deploy --createdb
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
#### Option 3: Deploy from Workspace Root
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
# Install workspace dependencies
|
|
60
|
+
pgpm install
|
|
61
|
+
|
|
62
|
+
# Deploy with dependencies
|
|
63
|
+
pgpm deploy mydb1 --yes --createdb
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Usage
|
|
67
|
+
|
|
68
|
+
### Setting Up Timestamp Tracking
|
|
69
|
+
|
|
70
|
+
```sql
|
|
71
|
+
-- Create a table with timestamp columns
|
|
72
|
+
CREATE TABLE public.posts (
|
|
73
|
+
id serial PRIMARY KEY,
|
|
74
|
+
title text,
|
|
75
|
+
content text,
|
|
76
|
+
created_at timestamptz,
|
|
77
|
+
updated_at timestamptz
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
-- Add trigger to automatically manage timestamps
|
|
81
|
+
CREATE TRIGGER set_timestamps
|
|
82
|
+
BEFORE INSERT OR UPDATE ON public.posts
|
|
83
|
+
FOR EACH ROW
|
|
84
|
+
EXECUTE FUNCTION stamps.timestamps();
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Setting Up User Tracking
|
|
88
|
+
|
|
89
|
+
```sql
|
|
90
|
+
-- Create a table with user tracking columns
|
|
91
|
+
CREATE TABLE public.posts (
|
|
92
|
+
id serial PRIMARY KEY,
|
|
93
|
+
title text,
|
|
94
|
+
content text,
|
|
95
|
+
created_at timestamptz,
|
|
96
|
+
updated_at timestamptz,
|
|
97
|
+
created_by uuid,
|
|
98
|
+
updated_by uuid
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
-- Add triggers for automatic timestamp and user tracking
|
|
102
|
+
CREATE TRIGGER set_timestamps
|
|
103
|
+
BEFORE INSERT OR UPDATE ON public.posts
|
|
104
|
+
FOR EACH ROW
|
|
105
|
+
EXECUTE FUNCTION stamps.timestamps();
|
|
106
|
+
|
|
107
|
+
CREATE TRIGGER set_peoplestamps
|
|
108
|
+
BEFORE INSERT OR UPDATE ON public.posts
|
|
109
|
+
FOR EACH ROW
|
|
110
|
+
EXECUTE FUNCTION stamps.peoplestamps();
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### How It Works
|
|
114
|
+
|
|
115
|
+
When you insert a new row:
|
|
116
|
+
- `created_at` and `updated_at` are set to the current timestamp
|
|
117
|
+
- `created_by` and `updated_by` are set to the current user ID from JWT claims
|
|
118
|
+
|
|
119
|
+
When you update an existing row:
|
|
120
|
+
- `created_at` and `created_by` are preserved (set to their original values)
|
|
121
|
+
- `updated_at` is set to the current timestamp
|
|
122
|
+
- `updated_by` is set to the current user ID from JWT claims
|
|
123
|
+
|
|
124
|
+
### Example Usage
|
|
125
|
+
|
|
126
|
+
```sql
|
|
127
|
+
-- Set the user context (typically done by your application)
|
|
128
|
+
SET jwt.claims.user_id = '00000000-0000-0000-0000-000000000001';
|
|
129
|
+
|
|
130
|
+
-- Insert a new post
|
|
131
|
+
INSERT INTO public.posts (title, content)
|
|
132
|
+
VALUES ('My First Post', 'Hello World!');
|
|
133
|
+
-- created_at, updated_at, created_by, and updated_by are automatically set
|
|
134
|
+
|
|
135
|
+
-- Update the post
|
|
136
|
+
UPDATE public.posts
|
|
137
|
+
SET content = 'Updated content'
|
|
138
|
+
WHERE id = 1;
|
|
139
|
+
-- updated_at and updated_by are automatically updated
|
|
140
|
+
-- created_at and created_by remain unchanged
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Trigger Functions
|
|
144
|
+
|
|
145
|
+
### stamps.timestamps()
|
|
146
|
+
|
|
147
|
+
Automatically manages timestamp columns on INSERT and UPDATE operations.
|
|
148
|
+
|
|
149
|
+
**Behavior:**
|
|
150
|
+
- **INSERT**: Sets both `created_at` and `updated_at` to `NOW()`
|
|
151
|
+
- **UPDATE**: Preserves `created_at`, updates `updated_at` to `NOW()`
|
|
152
|
+
|
|
153
|
+
**Required Columns:**
|
|
154
|
+
- `created_at timestamptz`
|
|
155
|
+
- `updated_at timestamptz`
|
|
156
|
+
|
|
157
|
+
### stamps.peoplestamps()
|
|
158
|
+
|
|
159
|
+
Automatically manages user tracking columns on INSERT and UPDATE operations using JWT claims.
|
|
160
|
+
|
|
161
|
+
**Behavior:**
|
|
162
|
+
- **INSERT**: Sets both `created_by` and `updated_by` to `jwt_public.current_user_id()`
|
|
163
|
+
- **UPDATE**: Preserves `created_by`, updates `updated_by` to `jwt_public.current_user_id()`
|
|
164
|
+
|
|
165
|
+
**Required Columns:**
|
|
166
|
+
- `created_by uuid`
|
|
167
|
+
- `updated_by uuid`
|
|
168
|
+
|
|
169
|
+
**Dependencies:**
|
|
170
|
+
- Requires `@pgpm/jwt-claims` for `jwt_public.current_user_id()` function
|
|
171
|
+
- User context must be set via `jwt.claims.user_id` session variable
|
|
172
|
+
|
|
173
|
+
## Dependencies
|
|
174
|
+
|
|
175
|
+
- `@pgpm/jwt-claims`: JWT claim handling for user context
|
|
176
|
+
- `@pgpm/verify`: Verification utilities for database objects
|
|
177
|
+
|
|
178
|
+
## Testing
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
pnpm test
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
The test suite validates:
|
|
185
|
+
- Automatic timestamp setting on insert and update
|
|
186
|
+
- Automatic user tracking on insert and update
|
|
187
|
+
- Preservation of creation timestamps and users on updates
|
|
188
|
+
- Integration with JWT claims for user context
|
|
189
|
+
|
|
190
|
+
## Development
|
|
191
|
+
|
|
192
|
+
See the [Development](#development) section below for information on working with this package.
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
## Development
|
|
197
|
+
|
|
198
|
+
### **Before You Begin**
|
|
199
|
+
|
|
200
|
+
```bash
|
|
201
|
+
# 1. Install pgpm
|
|
202
|
+
npm install -g pgpm
|
|
203
|
+
|
|
204
|
+
# 2. Start Postgres (Docker or local)
|
|
205
|
+
pgpm docker start
|
|
206
|
+
|
|
207
|
+
# 3. Load PG* environment variables (PGHOST, PGUSER, ...)
|
|
208
|
+
eval "$(pgpm env)"
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
### **Starting a New Project**
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
# 1. Create a workspace
|
|
217
|
+
pgpm init --workspace
|
|
218
|
+
cd my-app
|
|
219
|
+
|
|
220
|
+
# 2. Create your first module
|
|
221
|
+
pgpm init
|
|
222
|
+
|
|
223
|
+
# 3. Add a migration
|
|
224
|
+
pgpm add some_change
|
|
225
|
+
|
|
226
|
+
# 4. Deploy (auto-creates database)
|
|
227
|
+
pgpm deploy --createdb
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
### **Working With an Existing Project**
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
# 1. Clone and enter the project
|
|
236
|
+
git clone <repo> && cd <project>
|
|
237
|
+
|
|
238
|
+
# 2. Install dependencies
|
|
239
|
+
pnpm install
|
|
240
|
+
|
|
241
|
+
# 3. Deploy locally
|
|
242
|
+
pgpm deploy --createdb
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
### **Testing a Module Inside a Workspace**
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
# 1. Install workspace deps
|
|
251
|
+
pnpm install
|
|
252
|
+
|
|
253
|
+
# 2. Enter the module directory
|
|
254
|
+
cd packages/<some-module>
|
|
255
|
+
|
|
256
|
+
# 3. Run tests in watch mode
|
|
257
|
+
pnpm test:watch
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## Related Tooling
|
|
261
|
+
|
|
262
|
+
* [pgpm](https://github.com/launchql/launchql/tree/main/packages/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
|
|
263
|
+
* [pgsql-test](https://github.com/launchql/launchql/tree/main/packages/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
|
|
264
|
+
* [supabase-test](https://github.com/launchql/launchql/tree/main/packages/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
|
|
265
|
+
* [graphile-test](https://github.com/launchql/launchql/tree/main/packages/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
|
|
266
|
+
* [pgsql-parser](https://github.com/launchql/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
267
|
+
* [libpg-query-node](https://github.com/launchql/libpg-query-node): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
268
|
+
* [pg-proto-parser](https://github.com/launchql/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
269
|
+
|
|
270
|
+
## Disclaimer
|
|
271
|
+
|
|
272
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
273
|
+
|
|
274
|
+
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/launchql-stamps.control
CHANGED
package/package.json
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgpm/stamps",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Timestamp utilities and audit trail functions for PostgreSQL",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
8
|
"scripts": {
|
|
9
|
-
"bundle": "
|
|
9
|
+
"bundle": "pgpm package",
|
|
10
10
|
"test": "jest",
|
|
11
11
|
"test:watch": "jest --watch"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@pgpm/jwt-claims": "0.
|
|
15
|
-
"@pgpm/verify": "0.
|
|
14
|
+
"@pgpm/jwt-claims": "0.5.0",
|
|
15
|
+
"@pgpm/verify": "0.5.0"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"
|
|
18
|
+
"pgpm": "^0.2.0"
|
|
19
19
|
},
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|
|
@@ -25,5 +25,5 @@
|
|
|
25
25
|
"bugs": {
|
|
26
26
|
"url": "https://github.com/launchql/extensions/issues"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "d8eedbb24ad22a106634bc3b919bfb8d41976c16"
|
|
29
29
|
}
|
|
File without changes
|
|
File without changes
|