graphile-plugin-utils 1.14.0 → 1.14.2
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/README.md +4 -3
- package/esm/index.js +1 -1
- package/esm/request-lane.js +37 -0
- package/index.d.ts +2 -2
- package/index.js +3 -4
- package/package.json +10 -12
- package/request-lane.d.ts +30 -0
- package/request-lane.js +40 -0
- package/esm/system-lane.js +0 -40
- package/system-lane.d.ts +0 -46
- package/system-lane.js +0 -44
package/README.md
CHANGED
|
@@ -120,9 +120,10 @@ Install skills for AI coding agents:
|
|
|
120
120
|
# All platform skills (security, blueprints, codegen, billing, etc.)
|
|
121
121
|
npx skills add constructive-io/constructive-skills
|
|
122
122
|
|
|
123
|
-
# Individual repo skills (pgpm, testing, CLI, search, etc.)
|
|
124
|
-
npx skills add
|
|
125
|
-
npx skills add
|
|
123
|
+
# Individual repo skills (pgpm, safegres, testing, CLI, search, etc.)
|
|
124
|
+
npx skills add constructive-io/constructive --skill pgpm
|
|
125
|
+
npx skills add constructive-io/constructive --skill safegres
|
|
126
|
+
npx skills add constructive-io/constructive --skill constructive-testing
|
|
126
127
|
```
|
|
127
128
|
|
|
128
129
|
## Credits
|
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 {
|
|
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 {
|
|
4
|
-
export {
|
|
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.
|
|
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
|
|
12
|
-
Object.defineProperty(exports, "
|
|
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.
|
|
3
|
+
"version": "1.14.2",
|
|
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",
|
|
@@ -8,15 +8,6 @@
|
|
|
8
8
|
"main": "index.js",
|
|
9
9
|
"module": "esm/index.js",
|
|
10
10
|
"types": "index.d.ts",
|
|
11
|
-
"scripts": {
|
|
12
|
-
"clean": "makage clean",
|
|
13
|
-
"prepack": "npm run build",
|
|
14
|
-
"build": "makage build",
|
|
15
|
-
"build:dev": "makage build --dev",
|
|
16
|
-
"lint": "eslint . --fix",
|
|
17
|
-
"test": "jest --passWithNoTests",
|
|
18
|
-
"test:watch": "jest --watch"
|
|
19
|
-
},
|
|
20
11
|
"publishConfig": {
|
|
21
12
|
"access": "public",
|
|
22
13
|
"directory": "dist"
|
|
@@ -51,5 +42,12 @@
|
|
|
51
42
|
"pg-sql2": "^5.0.1",
|
|
52
43
|
"postgraphile": "^5.1.3"
|
|
53
44
|
},
|
|
54
|
-
"
|
|
55
|
-
|
|
45
|
+
"scripts": {
|
|
46
|
+
"clean": "makage clean",
|
|
47
|
+
"build": "makage build",
|
|
48
|
+
"build:dev": "makage build --dev",
|
|
49
|
+
"lint": "eslint . --fix",
|
|
50
|
+
"test": "jest --passWithNoTests",
|
|
51
|
+
"test:watch": "jest --watch"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -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>;
|
package/request-lane.js
ADDED
|
@@ -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
|
+
}
|
package/esm/system-lane.js
DELETED
|
@@ -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
|
-
}
|