graphile-plugin-utils 1.14.0 → 1.14.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/esm/index.js CHANGED
@@ -3,5 +3,5 @@
3
3
  export { getQueryBuilder } from './query-builder';
4
4
  // pg-resource introspection helpers (connection-filter / pg-aggregates)
5
5
  export { isComputedScalarAttributeResource } from './pg-resources';
6
- export { SYSTEM_LANE_ROLE, withSystemLaneClient } from './system-lane';
6
+ export { withRequestPgClient } from './request-lane';
7
7
  export { JSON_SCHEMA_SCALARS, PG_TYPE_SCALARS } from './scalar-maps';
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Run a callback under the request's pgSettings inside ONE explicit transaction.
3
+ *
4
+ * The grafast context's `withPgClient(pgSettings, cb)` applies pgSettings as
5
+ * transaction-LOCAL `set_config(key, value, is_local => true)`. When `cb` issues
6
+ * more than one statement WITHOUT an explicit surrounding transaction, each
7
+ * statement runs in its own implicit (autocommit) transaction, so a LOCAL
8
+ * setting applied for one statement is already gone by the next. The request
9
+ * role's jwt claims (notably `jwt.claims.database_id`) then vanish between
10
+ * statements, and `jwt_private.current_database_id()` raises
11
+ * DATABASE_CLAIM_REQUIRED even though the request carried the claim.
12
+ *
13
+ * This helper acquires the client without settings (`withPgClient(null, ...)`),
14
+ * opens a single explicit transaction, and applies the request settings inside
15
+ * it — so every statement in `cb` observes the same role and jwt claims. It
16
+ * mirrors the `pg-query-context` pattern used elsewhere in the codebase for
17
+ * manual, multi-statement RLS work.
18
+ */
19
+ async function applyRequestSettings(tx, pgSettings) {
20
+ if (!pgSettings)
21
+ return;
22
+ const entries = Object.entries(pgSettings)
23
+ .filter(([, value]) => value != null)
24
+ .map(([key, value]) => [key, String(value)]);
25
+ if (entries.length === 0)
26
+ return;
27
+ await tx.query({
28
+ text: 'SELECT set_config(el->>0, el->>1, true) FROM json_array_elements($1::json) el',
29
+ values: [JSON.stringify(entries)],
30
+ });
31
+ }
32
+ export function withRequestPgClient(withPgClient, pgSettings, cb) {
33
+ return withPgClient(null, (client) => client.withTransaction(async (tx) => {
34
+ await applyRequestSettings(tx, pgSettings);
35
+ return cb(tx);
36
+ }));
37
+ }
package/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { getQueryBuilder } from './query-builder';
2
2
  export { isComputedScalarAttributeResource } from './pg-resources';
3
- export type { SystemLaneOptions, SystemLanePgClient, SystemLaneWithPgClient } from './system-lane';
4
- export { SYSTEM_LANE_ROLE, withSystemLaneClient } from './system-lane';
3
+ export type { RequestPgClient, WithPgClient } from './request-lane';
4
+ export { withRequestPgClient } from './request-lane';
5
5
  export type { GraphQLScalarName } from './scalar-maps';
6
6
  export { JSON_SCHEMA_SCALARS, PG_TYPE_SCALARS } from './scalar-maps';
package/index.js CHANGED
@@ -1,16 +1,15 @@
1
1
  "use strict";
2
2
  // Shared internals for Constructive PostGraphile v5 plugins.
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.PG_TYPE_SCALARS = exports.JSON_SCHEMA_SCALARS = exports.withSystemLaneClient = exports.SYSTEM_LANE_ROLE = exports.isComputedScalarAttributeResource = exports.getQueryBuilder = void 0;
4
+ exports.PG_TYPE_SCALARS = exports.JSON_SCHEMA_SCALARS = exports.withRequestPgClient = exports.isComputedScalarAttributeResource = exports.getQueryBuilder = void 0;
5
5
  // PgCondition → PgSelectQueryBuilder walker (search / BM25 / pgvector)
6
6
  var query_builder_1 = require("./query-builder");
7
7
  Object.defineProperty(exports, "getQueryBuilder", { enumerable: true, get: function () { return query_builder_1.getQueryBuilder; } });
8
8
  // pg-resource introspection helpers (connection-filter / pg-aggregates)
9
9
  var pg_resources_1 = require("./pg-resources");
10
10
  Object.defineProperty(exports, "isComputedScalarAttributeResource", { enumerable: true, get: function () { return pg_resources_1.isComputedScalarAttributeResource; } });
11
- var system_lane_1 = require("./system-lane");
12
- Object.defineProperty(exports, "SYSTEM_LANE_ROLE", { enumerable: true, get: function () { return system_lane_1.SYSTEM_LANE_ROLE; } });
13
- Object.defineProperty(exports, "withSystemLaneClient", { enumerable: true, get: function () { return system_lane_1.withSystemLaneClient; } });
11
+ var request_lane_1 = require("./request-lane");
12
+ Object.defineProperty(exports, "withRequestPgClient", { enumerable: true, get: function () { return request_lane_1.withRequestPgClient; } });
14
13
  var scalar_maps_1 = require("./scalar-maps");
15
14
  Object.defineProperty(exports, "JSON_SCHEMA_SCALARS", { enumerable: true, get: function () { return scalar_maps_1.JSON_SCHEMA_SCALARS; } });
16
15
  Object.defineProperty(exports, "PG_TYPE_SCALARS", { enumerable: true, get: function () { return scalar_maps_1.PG_TYPE_SCALARS; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphile-plugin-utils",
3
- "version": "1.14.0",
3
+ "version": "1.14.1",
4
4
  "description": "Shared internals for Constructive PostGraphile v5 plugins - PgCondition query-builder walker and pg-resource introspection helpers",
5
5
  "author": "Constructive <developers@constructive.io>",
6
6
  "homepage": "https://github.com/constructive-io/constructive",
@@ -51,5 +51,5 @@
51
51
  "pg-sql2": "^5.0.1",
52
52
  "postgraphile": "^5.1.3"
53
53
  },
54
- "gitHead": "487c4ec9ee997d7da80584a3d41d7120d4354c54"
54
+ "gitHead": "0b4ffa11a6d7958ba1068ce8e19616d4b018c0d6"
55
55
  }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Run a callback under the request's pgSettings inside ONE explicit transaction.
3
+ *
4
+ * The grafast context's `withPgClient(pgSettings, cb)` applies pgSettings as
5
+ * transaction-LOCAL `set_config(key, value, is_local => true)`. When `cb` issues
6
+ * more than one statement WITHOUT an explicit surrounding transaction, each
7
+ * statement runs in its own implicit (autocommit) transaction, so a LOCAL
8
+ * setting applied for one statement is already gone by the next. The request
9
+ * role's jwt claims (notably `jwt.claims.database_id`) then vanish between
10
+ * statements, and `jwt_private.current_database_id()` raises
11
+ * DATABASE_CLAIM_REQUIRED even though the request carried the claim.
12
+ *
13
+ * This helper acquires the client without settings (`withPgClient(null, ...)`),
14
+ * opens a single explicit transaction, and applies the request settings inside
15
+ * it — so every statement in `cb` observes the same role and jwt claims. It
16
+ * mirrors the `pg-query-context` pattern used elsewhere in the codebase for
17
+ * manual, multi-statement RLS work.
18
+ */
19
+ /** The subset of grafast's PgClient the request lane needs. */
20
+ export interface RequestPgClient {
21
+ query(opts: {
22
+ text: string;
23
+ values?: unknown[];
24
+ }): Promise<{
25
+ rows: Array<Record<string, unknown>>;
26
+ }>;
27
+ withTransaction<T>(cb: (tx: RequestPgClient) => Promise<T>): Promise<T>;
28
+ }
29
+ export type WithPgClient = <T>(pgSettings: Record<string, string> | null, cb: (client: RequestPgClient) => Promise<T>) => Promise<T>;
30
+ export declare function withRequestPgClient<T>(withPgClient: WithPgClient, pgSettings: Record<string, string> | null, cb: (tx: RequestPgClient) => Promise<T>): Promise<T>;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ /**
3
+ * Run a callback under the request's pgSettings inside ONE explicit transaction.
4
+ *
5
+ * The grafast context's `withPgClient(pgSettings, cb)` applies pgSettings as
6
+ * transaction-LOCAL `set_config(key, value, is_local => true)`. When `cb` issues
7
+ * more than one statement WITHOUT an explicit surrounding transaction, each
8
+ * statement runs in its own implicit (autocommit) transaction, so a LOCAL
9
+ * setting applied for one statement is already gone by the next. The request
10
+ * role's jwt claims (notably `jwt.claims.database_id`) then vanish between
11
+ * statements, and `jwt_private.current_database_id()` raises
12
+ * DATABASE_CLAIM_REQUIRED even though the request carried the claim.
13
+ *
14
+ * This helper acquires the client without settings (`withPgClient(null, ...)`),
15
+ * opens a single explicit transaction, and applies the request settings inside
16
+ * it — so every statement in `cb` observes the same role and jwt claims. It
17
+ * mirrors the `pg-query-context` pattern used elsewhere in the codebase for
18
+ * manual, multi-statement RLS work.
19
+ */
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.withRequestPgClient = withRequestPgClient;
22
+ async function applyRequestSettings(tx, pgSettings) {
23
+ if (!pgSettings)
24
+ return;
25
+ const entries = Object.entries(pgSettings)
26
+ .filter(([, value]) => value != null)
27
+ .map(([key, value]) => [key, String(value)]);
28
+ if (entries.length === 0)
29
+ return;
30
+ await tx.query({
31
+ text: 'SELECT set_config(el->>0, el->>1, true) FROM json_array_elements($1::json) el',
32
+ values: [JSON.stringify(entries)],
33
+ });
34
+ }
35
+ function withRequestPgClient(withPgClient, pgSettings, cb) {
36
+ return withPgClient(null, (client) => client.withTransaction(async (tx) => {
37
+ await applyRequestSettings(tx, pgSettings);
38
+ return cb(tx);
39
+ }));
40
+ }
@@ -1,40 +0,0 @@
1
- /**
2
- * The system lane: server-owned reads and writes that are not a tenant
3
- * session — schema metadata, module registration, storage config.
4
- *
5
- * Grafast's `withPgClient(null, cb)` acquires a client with NO settings, so
6
- * `cb` runs with whatever role the pool connected as (in practice `postgres`,
7
- * a superuser that bypasses RLS) and each statement lands in its own implicit
8
- * transaction. This helper names the role instead: one transaction with a
9
- * transaction-local `role`, so the lane's reach is the grants of a named role
10
- * rather than the connection's. It is the server-owned counterpart to the
11
- * request lane's `withRequestPgClient`.
12
- */
13
- /**
14
- * Role the system lane runs as: a named role instead of whatever the pool
15
- * connected as.
16
- *
17
- * This is NOT an RLS-subject role. `ROLES.md` documents `administrator` as the
18
- * BYPASSRLS role, so the lane still bypasses RLS — what changes is that it no
19
- * longer inherits the connection's superuser and its work is one transaction.
20
- * Narrowing it further needs grants that do not exist yet.
21
- */
22
- export const SYSTEM_LANE_ROLE = 'administrator';
23
- /**
24
- * Run `cb` in one transaction under the system lane's role.
25
- *
26
- * The role is set with a transaction-local `set_config`, so it is discarded
27
- * with the transaction and cannot leak to the next borrower of the pooled
28
- * connection the way a session-level `SET ROLE` would. The client's own
29
- * `withTransaction` owns commit and rollback.
30
- */
31
- export function withSystemLaneClient(withPgClient, cb, opts = {}) {
32
- const role = opts.role ?? SYSTEM_LANE_ROLE;
33
- return withPgClient(null, (client) => client.withTransaction(async (tx) => {
34
- await tx.query({
35
- text: 'SELECT set_config($1, $2, true)',
36
- values: ['role', role],
37
- });
38
- return cb(tx);
39
- }));
40
- }
package/system-lane.d.ts DELETED
@@ -1,46 +0,0 @@
1
- /**
2
- * The system lane: server-owned reads and writes that are not a tenant
3
- * session — schema metadata, module registration, storage config.
4
- *
5
- * Grafast's `withPgClient(null, cb)` acquires a client with NO settings, so
6
- * `cb` runs with whatever role the pool connected as (in practice `postgres`,
7
- * a superuser that bypasses RLS) and each statement lands in its own implicit
8
- * transaction. This helper names the role instead: one transaction with a
9
- * transaction-local `role`, so the lane's reach is the grants of a named role
10
- * rather than the connection's. It is the server-owned counterpart to the
11
- * request lane's `withRequestPgClient`.
12
- */
13
- /** The subset of grafast's PgClient the system lane needs. */
14
- export interface SystemLanePgClient {
15
- query(opts: {
16
- text: string;
17
- values?: unknown[];
18
- }): Promise<{
19
- rows: Array<Record<string, unknown>>;
20
- }>;
21
- withTransaction<T>(cb: (tx: SystemLanePgClient) => Promise<T>): Promise<T>;
22
- }
23
- export type SystemLaneWithPgClient = <T>(pgSettings: Record<string, string> | null, cb: (client: SystemLanePgClient) => Promise<T>) => Promise<T>;
24
- /**
25
- * Role the system lane runs as: a named role instead of whatever the pool
26
- * connected as.
27
- *
28
- * This is NOT an RLS-subject role. `ROLES.md` documents `administrator` as the
29
- * BYPASSRLS role, so the lane still bypasses RLS — what changes is that it no
30
- * longer inherits the connection's superuser and its work is one transaction.
31
- * Narrowing it further needs grants that do not exist yet.
32
- */
33
- export declare const SYSTEM_LANE_ROLE = "administrator";
34
- export interface SystemLaneOptions {
35
- /** Role to run as. Defaults to {@link SYSTEM_LANE_ROLE}. */
36
- role?: string;
37
- }
38
- /**
39
- * Run `cb` in one transaction under the system lane's role.
40
- *
41
- * The role is set with a transaction-local `set_config`, so it is discarded
42
- * with the transaction and cannot leak to the next borrower of the pooled
43
- * connection the way a session-level `SET ROLE` would. The client's own
44
- * `withTransaction` owns commit and rollback.
45
- */
46
- export declare function withSystemLaneClient<T>(withPgClient: SystemLaneWithPgClient, cb: (client: SystemLanePgClient) => Promise<T>, opts?: SystemLaneOptions): Promise<T>;
package/system-lane.js DELETED
@@ -1,44 +0,0 @@
1
- "use strict";
2
- /**
3
- * The system lane: server-owned reads and writes that are not a tenant
4
- * session — schema metadata, module registration, storage config.
5
- *
6
- * Grafast's `withPgClient(null, cb)` acquires a client with NO settings, so
7
- * `cb` runs with whatever role the pool connected as (in practice `postgres`,
8
- * a superuser that bypasses RLS) and each statement lands in its own implicit
9
- * transaction. This helper names the role instead: one transaction with a
10
- * transaction-local `role`, so the lane's reach is the grants of a named role
11
- * rather than the connection's. It is the server-owned counterpart to the
12
- * request lane's `withRequestPgClient`.
13
- */
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.SYSTEM_LANE_ROLE = void 0;
16
- exports.withSystemLaneClient = withSystemLaneClient;
17
- /**
18
- * Role the system lane runs as: a named role instead of whatever the pool
19
- * connected as.
20
- *
21
- * This is NOT an RLS-subject role. `ROLES.md` documents `administrator` as the
22
- * BYPASSRLS role, so the lane still bypasses RLS — what changes is that it no
23
- * longer inherits the connection's superuser and its work is one transaction.
24
- * Narrowing it further needs grants that do not exist yet.
25
- */
26
- exports.SYSTEM_LANE_ROLE = 'administrator';
27
- /**
28
- * Run `cb` in one transaction under the system lane's role.
29
- *
30
- * The role is set with a transaction-local `set_config`, so it is discarded
31
- * with the transaction and cannot leak to the next borrower of the pooled
32
- * connection the way a session-level `SET ROLE` would. The client's own
33
- * `withTransaction` owns commit and rollback.
34
- */
35
- function withSystemLaneClient(withPgClient, cb, opts = {}) {
36
- const role = opts.role ?? exports.SYSTEM_LANE_ROLE;
37
- return withPgClient(null, (client) => client.withTransaction(async (tx) => {
38
- await tx.query({
39
- text: 'SELECT set_config($1, $2, true)',
40
- values: ['role', role],
41
- });
42
- return cb(tx);
43
- }));
44
- }