pgsql-test 2.0.4 → 2.0.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 +58 -0
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -79,6 +79,16 @@ afterAll(() => teardown());
79
79
 
80
80
  ## `getConnections()` Overview
81
81
 
82
+ ```ts
83
+ import { getConnections } from 'pgsql-test';
84
+
85
+ // Complete object destructuring
86
+ const { pg, db, admin, teardown, manager } = await getConnections();
87
+
88
+ // Most common pattern
89
+ const { db, teardown } = await getConnections();
90
+ ```
91
+
82
92
  The `getConnections()` helper sets up a fresh PostgreSQL test database and returns a structured object with:
83
93
 
84
94
  * `pg`: a `PgTestClient` connected as the root or superuser — useful for administrative setup or introspection
@@ -97,6 +107,19 @@ The `PgTestClient` returned by `getConnections()` is a fully-featured wrapper ar
97
107
 
98
108
  ## `PgTestClient` API Overview
99
109
 
110
+ ```ts
111
+ let pg: PgTestClient;
112
+ let teardown: () => Promise<void>;
113
+
114
+ beforeAll(async () => {
115
+ ({ pg, teardown } = await getConnections());
116
+ });
117
+
118
+ beforeEach(() => pg.beforeEach());
119
+ afterEach(() => pg.afterEach());
120
+ afterAll(() => teardown());
121
+ ```
122
+
100
123
  The `PgTestClient` returned by `getConnections()` wraps a `pg.Client` and provides convenient helpers for query execution, test isolation, and context switching.
101
124
 
102
125
  ### Common Methods
@@ -150,6 +173,25 @@ test('user count starts at 2', async () => {
150
173
 
151
174
  ### 🔐 Role-Based Context
152
175
 
176
+
177
+ The `pgsql-test` framework provides powerful tools to simulate authentication contexts during tests, which is particularly useful when testing Row-Level Security (RLS) policies.
178
+
179
+ #### Setting Test Context
180
+
181
+ Use `setContext()` to simulate different user roles and JWT claims:
182
+
183
+ ```ts
184
+ db.setContext({
185
+ role: 'authenticated',
186
+ 'jwt.claims.user_id': '123',
187
+ 'jwt.claims.org_id': 'acme'
188
+ });
189
+ ```
190
+
191
+ This applies the settings using `SET LOCAL` statements, ensuring they persist only for the current transaction and maintain proper isolation between tests.
192
+
193
+ #### Testing Role-Based Access
194
+
153
195
  ```ts
154
196
  describe('authenticated role', () => {
155
197
  beforeEach(async () => {
@@ -166,6 +208,22 @@ describe('authenticated role', () => {
166
208
  });
167
209
  ```
168
210
 
211
+ #### Database Connection Options
212
+
213
+ For non-superuser testing, use the connection options described in the [options](#getconnections-options) section. The `db.connection` property allows you to customize the non-privileged user account for your tests.
214
+
215
+ Use `setContext()` to simulate Role-Based Access Control (RBAC) during tests. This is useful when testing Row-Level Security (RLS) policies. Your actual server should manage role/user claims via secure tokens (e.g., setting `current_setting('jwt.claims.user_id')`), but this interface helps emulate those behaviors in test environments.
216
+
217
+ #### Common Testing Scenarios
218
+
219
+ This approach enables testing various access patterns:
220
+ - Authenticated vs. anonymous user access
221
+ - Per-user data filtering
222
+ - Admin privilege bypass behavior
223
+ - Custom claim-based restrictions (organization membership, admin status)
224
+
225
+ > **Note:** While this interface helps simulate RBAC for testing, your production server should manage user/role claims via secure authentication tokens, typically by setting values like `current_setting('jwt.claims.user_id')` through proper authentication middleware.
226
+
169
227
  ### 🔌 SQL File Seeding
170
228
 
171
229
  Use `.sql` files to set up your database state before tests:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pgsql-test",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "author": "Dan Lynch <pyramation@gmail.com>",
5
5
  "description": "PostgreSQL Testing in TypeScript",
6
6
  "main": "index.js",
@@ -42,5 +42,5 @@
42
42
  "pg": "^8.16.0",
43
43
  "pg-copy-streams": "^6.0.6"
44
44
  },
45
- "gitHead": "b347f01f28aa33c195f78d911eab8757777b0c88"
45
+ "gitHead": "9ca7885acaf53b419b6c589f739011e047dd6954"
46
46
  }