pg-query-context 2.1.4 → 2.1.6

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.
Files changed (2) hide show
  1. package/README.md +75 -0
  2. package/package.json +9 -2
package/README.md CHANGED
@@ -12,6 +12,81 @@
12
12
  <a href="https://www.npmjs.com/package/pg-query-context"><img height="20" src="https://img.shields.io/github/package-json/v/launchql/launchql?filename=packages%2Fpg-query-context%2Fpackage.json"/></a>
13
13
  </p>
14
14
 
15
+ # pg-query-context
16
+
17
+ A small utility for executing PostgreSQL queries within a session context using [`pg`](https://www.npmjs.com/package/pg). It allows you to temporarily set PostgreSQL session variables (`set_config`) for RLS (Row-Level Security) and other scoped operations.
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install pg-query-context
23
+ ```
24
+
25
+ ## Features
26
+
27
+ * Sets session-level context (e.g., role, user ID) using `set_config`.
28
+ * Automatically wraps execution in a transaction (`BEGIN`/`COMMIT`).
29
+ * Automatically rolls back on error.
30
+ * Supports both `Pool` and `Client` from `pg`.
31
+
32
+ ## Usage
33
+
34
+ ```ts
35
+ import pgQueryContext from 'pg-query-context';
36
+ import { Pool } from 'pg';
37
+
38
+ const pool = new Pool();
39
+
40
+ const result = await pgQueryContext({
41
+ client: pool,
42
+ context: {
43
+ 'role': 'authenticated',
44
+ 'myapp.user_id': '123e4567-e89b-12d3-a456-426614174000'
45
+ },
46
+ query: 'SELECT * FROM app_private.do_something_secure($1)',
47
+ variables: ['input-value']
48
+ });
49
+
50
+ console.log(result.rows);
51
+ ```
52
+
53
+ ## API
54
+
55
+ ### `pgQueryContext(options: ExecOptions): Promise<QueryResult>`
56
+
57
+ #### Options
58
+
59
+ | Name | Type | Required | Description |
60
+ | ----------- | ------------------------ | -------- | ------------------------------------------------------ |
61
+ | `client` | `Pool` or `ClientBase` | ✅ | The PostgreSQL client or pool to use |
62
+ | `context` | `Record<string, string>` | ❌ | Key-value session variables to be set via `set_config` |
63
+ | `query` | `string` | ✅ | SQL query to run |
64
+ | `variables` | `any[]` | ❌ | Parameterized query variables |
65
+
66
+ ## Example with `express`
67
+
68
+ ```ts
69
+ app.post('/secure-endpoint', async (req, res) => {
70
+ const authToken = req.headers.authorization;
71
+
72
+ const result = await pgQueryContext({
73
+ client: pool,
74
+ context: {
75
+ 'role': 'authenticated',
76
+ 'myapp.token': authToken,
77
+ },
78
+ query: 'SELECT * FROM app_private.verify_token($1)',
79
+ variables: [authToken],
80
+ });
81
+
82
+ if (!result.rows.length) {
83
+ return res.status(401).json({ error: 'Unauthorized' });
84
+ }
85
+
86
+ res.json({ user: result.rows[0] });
87
+ });
88
+ ```
89
+
15
90
  ## Related LaunchQL Tooling
16
91
 
17
92
  ### 🧪 Testing
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pg-query-context",
3
- "version": "2.1.4",
3
+ "version": "2.1.6",
4
4
  "author": "Dan Lynch <pyramation@gmail.com>",
5
5
  "description": "pg query context",
6
6
  "main": "index.js",
@@ -35,5 +35,12 @@
35
35
  "devDependencies": {
36
36
  "@types/pg": "^8.15.2"
37
37
  },
38
- "gitHead": "36e9eb71ef72f72d21e9557fb190da1900f31b87"
38
+ "keywords": [
39
+ "postgresql",
40
+ "query",
41
+ "context",
42
+ "pg",
43
+ "graphile"
44
+ ],
45
+ "gitHead": "cc4675c134273a66b11a881d084d04362f6837af"
39
46
  }