graphile-plugin-utils 1.12.2 → 1.14.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/package.json +2 -2
- package/__tests__/pg-resources.test.d.ts +0 -1
- package/__tests__/pg-resources.test.js +0 -30
- package/__tests__/system-lane.test.d.ts +0 -1
- package/__tests__/system-lane.test.js +0 -70
- package/esm/__tests__/pg-resources.test.js +0 -28
- package/esm/__tests__/system-lane.test.js +0 -68
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphile-plugin-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.0",
|
|
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": "
|
|
54
|
+
"gitHead": "487c4ec9ee997d7da80584a3d41d7120d4354c54"
|
|
55
55
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const pg_resources_1 = require("../pg-resources");
|
|
4
|
-
const tableCodec = { attributes: { id: {} } };
|
|
5
|
-
const scalarCodec = {};
|
|
6
|
-
function resource(overrides) {
|
|
7
|
-
return {
|
|
8
|
-
isUnique: true,
|
|
9
|
-
codec: scalarCodec,
|
|
10
|
-
parameters: [{ codec: tableCodec }],
|
|
11
|
-
...overrides
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
describe('isComputedScalarAttributeResource', () => {
|
|
15
|
-
it('accepts a unique scalar function taking a table row', () => {
|
|
16
|
-
expect((0, pg_resources_1.isComputedScalarAttributeResource)(resource({}))).toBe(true);
|
|
17
|
-
});
|
|
18
|
-
it('rejects a resource without parameters', () => {
|
|
19
|
-
expect((0, pg_resources_1.isComputedScalarAttributeResource)(resource({ parameters: [] }))).toBe(false);
|
|
20
|
-
});
|
|
21
|
-
it('rejects a table-returning resource (codec has attributes)', () => {
|
|
22
|
-
expect((0, pg_resources_1.isComputedScalarAttributeResource)(resource({ codec: tableCodec }))).toBe(false);
|
|
23
|
-
});
|
|
24
|
-
it('rejects a non-unique resource', () => {
|
|
25
|
-
expect((0, pg_resources_1.isComputedScalarAttributeResource)(resource({ isUnique: false }))).toBe(false);
|
|
26
|
-
});
|
|
27
|
-
it('rejects when the first parameter is not a table row', () => {
|
|
28
|
-
expect((0, pg_resources_1.isComputedScalarAttributeResource)(resource({ parameters: [{ codec: scalarCodec }] }))).toBe(false);
|
|
29
|
-
});
|
|
30
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const system_lane_1 = require("../system-lane");
|
|
4
|
-
/**
|
|
5
|
-
* A client whose `withTransaction` behaves like grafast's: it brackets the
|
|
6
|
-
* callback with BEGIN and COMMIT, and rolls back when the callback throws.
|
|
7
|
-
*/
|
|
8
|
-
const makeClient = () => {
|
|
9
|
-
const calls = [];
|
|
10
|
-
const client = {
|
|
11
|
-
query: async (opts) => {
|
|
12
|
-
calls.push(opts);
|
|
13
|
-
return { rows: [] };
|
|
14
|
-
},
|
|
15
|
-
withTransaction: async (cb) => {
|
|
16
|
-
calls.push({ text: 'BEGIN' });
|
|
17
|
-
try {
|
|
18
|
-
const result = await cb(client);
|
|
19
|
-
calls.push({ text: 'COMMIT' });
|
|
20
|
-
return result;
|
|
21
|
-
}
|
|
22
|
-
catch (err) {
|
|
23
|
-
calls.push({ text: 'ROLLBACK' });
|
|
24
|
-
throw err;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
return { client, calls };
|
|
29
|
-
};
|
|
30
|
-
const makeWithPgClient = (client, settings) => ((pgSettings, cb) => {
|
|
31
|
-
settings.push(pgSettings);
|
|
32
|
-
return cb(client);
|
|
33
|
-
});
|
|
34
|
-
describe('withSystemLaneClient', () => {
|
|
35
|
-
it('runs the callback in one transaction under a transaction-local role', async () => {
|
|
36
|
-
const { client, calls } = makeClient();
|
|
37
|
-
const settings = [];
|
|
38
|
-
const result = await (0, system_lane_1.withSystemLaneClient)(makeWithPgClient(client, settings), async (tx) => {
|
|
39
|
-
await tx.query({ text: 'SELECT 1' });
|
|
40
|
-
return 'ok';
|
|
41
|
-
});
|
|
42
|
-
expect(result).toBe('ok');
|
|
43
|
-
expect(settings).toEqual([null]);
|
|
44
|
-
expect(calls.map((c) => c.text)).toEqual([
|
|
45
|
-
'BEGIN',
|
|
46
|
-
'SELECT set_config($1, $2, true)',
|
|
47
|
-
'SELECT 1',
|
|
48
|
-
'COMMIT'
|
|
49
|
-
]);
|
|
50
|
-
expect(calls[1].values).toEqual(['role', system_lane_1.SYSTEM_LANE_ROLE]);
|
|
51
|
-
});
|
|
52
|
-
it('honours an explicit role', async () => {
|
|
53
|
-
const { client, calls } = makeClient();
|
|
54
|
-
await (0, system_lane_1.withSystemLaneClient)(makeWithPgClient(client, []), async () => null, {
|
|
55
|
-
role: 'anonymous'
|
|
56
|
-
});
|
|
57
|
-
expect(calls[1].values).toEqual(['role', 'anonymous']);
|
|
58
|
-
});
|
|
59
|
-
it('leaves the rollback to the client and rethrows the callback failure', async () => {
|
|
60
|
-
const { client, calls } = makeClient();
|
|
61
|
-
await expect((0, system_lane_1.withSystemLaneClient)(makeWithPgClient(client, []), async () => {
|
|
62
|
-
throw new Error('boom');
|
|
63
|
-
})).rejects.toThrow('boom');
|
|
64
|
-
expect(calls.map((c) => c.text)).toEqual([
|
|
65
|
-
'BEGIN',
|
|
66
|
-
'SELECT set_config($1, $2, true)',
|
|
67
|
-
'ROLLBACK'
|
|
68
|
-
]);
|
|
69
|
-
});
|
|
70
|
-
});
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { isComputedScalarAttributeResource } from '../pg-resources';
|
|
2
|
-
const tableCodec = { attributes: { id: {} } };
|
|
3
|
-
const scalarCodec = {};
|
|
4
|
-
function resource(overrides) {
|
|
5
|
-
return {
|
|
6
|
-
isUnique: true,
|
|
7
|
-
codec: scalarCodec,
|
|
8
|
-
parameters: [{ codec: tableCodec }],
|
|
9
|
-
...overrides
|
|
10
|
-
};
|
|
11
|
-
}
|
|
12
|
-
describe('isComputedScalarAttributeResource', () => {
|
|
13
|
-
it('accepts a unique scalar function taking a table row', () => {
|
|
14
|
-
expect(isComputedScalarAttributeResource(resource({}))).toBe(true);
|
|
15
|
-
});
|
|
16
|
-
it('rejects a resource without parameters', () => {
|
|
17
|
-
expect(isComputedScalarAttributeResource(resource({ parameters: [] }))).toBe(false);
|
|
18
|
-
});
|
|
19
|
-
it('rejects a table-returning resource (codec has attributes)', () => {
|
|
20
|
-
expect(isComputedScalarAttributeResource(resource({ codec: tableCodec }))).toBe(false);
|
|
21
|
-
});
|
|
22
|
-
it('rejects a non-unique resource', () => {
|
|
23
|
-
expect(isComputedScalarAttributeResource(resource({ isUnique: false }))).toBe(false);
|
|
24
|
-
});
|
|
25
|
-
it('rejects when the first parameter is not a table row', () => {
|
|
26
|
-
expect(isComputedScalarAttributeResource(resource({ parameters: [{ codec: scalarCodec }] }))).toBe(false);
|
|
27
|
-
});
|
|
28
|
-
});
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { SYSTEM_LANE_ROLE, withSystemLaneClient } from '../system-lane';
|
|
2
|
-
/**
|
|
3
|
-
* A client whose `withTransaction` behaves like grafast's: it brackets the
|
|
4
|
-
* callback with BEGIN and COMMIT, and rolls back when the callback throws.
|
|
5
|
-
*/
|
|
6
|
-
const makeClient = () => {
|
|
7
|
-
const calls = [];
|
|
8
|
-
const client = {
|
|
9
|
-
query: async (opts) => {
|
|
10
|
-
calls.push(opts);
|
|
11
|
-
return { rows: [] };
|
|
12
|
-
},
|
|
13
|
-
withTransaction: async (cb) => {
|
|
14
|
-
calls.push({ text: 'BEGIN' });
|
|
15
|
-
try {
|
|
16
|
-
const result = await cb(client);
|
|
17
|
-
calls.push({ text: 'COMMIT' });
|
|
18
|
-
return result;
|
|
19
|
-
}
|
|
20
|
-
catch (err) {
|
|
21
|
-
calls.push({ text: 'ROLLBACK' });
|
|
22
|
-
throw err;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
return { client, calls };
|
|
27
|
-
};
|
|
28
|
-
const makeWithPgClient = (client, settings) => ((pgSettings, cb) => {
|
|
29
|
-
settings.push(pgSettings);
|
|
30
|
-
return cb(client);
|
|
31
|
-
});
|
|
32
|
-
describe('withSystemLaneClient', () => {
|
|
33
|
-
it('runs the callback in one transaction under a transaction-local role', async () => {
|
|
34
|
-
const { client, calls } = makeClient();
|
|
35
|
-
const settings = [];
|
|
36
|
-
const result = await withSystemLaneClient(makeWithPgClient(client, settings), async (tx) => {
|
|
37
|
-
await tx.query({ text: 'SELECT 1' });
|
|
38
|
-
return 'ok';
|
|
39
|
-
});
|
|
40
|
-
expect(result).toBe('ok');
|
|
41
|
-
expect(settings).toEqual([null]);
|
|
42
|
-
expect(calls.map((c) => c.text)).toEqual([
|
|
43
|
-
'BEGIN',
|
|
44
|
-
'SELECT set_config($1, $2, true)',
|
|
45
|
-
'SELECT 1',
|
|
46
|
-
'COMMIT'
|
|
47
|
-
]);
|
|
48
|
-
expect(calls[1].values).toEqual(['role', SYSTEM_LANE_ROLE]);
|
|
49
|
-
});
|
|
50
|
-
it('honours an explicit role', async () => {
|
|
51
|
-
const { client, calls } = makeClient();
|
|
52
|
-
await withSystemLaneClient(makeWithPgClient(client, []), async () => null, {
|
|
53
|
-
role: 'anonymous'
|
|
54
|
-
});
|
|
55
|
-
expect(calls[1].values).toEqual(['role', 'anonymous']);
|
|
56
|
-
});
|
|
57
|
-
it('leaves the rollback to the client and rethrows the callback failure', async () => {
|
|
58
|
-
const { client, calls } = makeClient();
|
|
59
|
-
await expect(withSystemLaneClient(makeWithPgClient(client, []), async () => {
|
|
60
|
-
throw new Error('boom');
|
|
61
|
-
})).rejects.toThrow('boom');
|
|
62
|
-
expect(calls.map((c) => c.text)).toEqual([
|
|
63
|
-
'BEGIN',
|
|
64
|
-
'SELECT set_config($1, $2, true)',
|
|
65
|
-
'ROLLBACK'
|
|
66
|
-
]);
|
|
67
|
-
});
|
|
68
|
-
});
|