pg-query-context 2.4.1 → 2.6.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/esm/index.js CHANGED
@@ -1,29 +1,33 @@
1
1
  function setContext(ctx) {
2
2
  return Object.keys(ctx || {}).reduce((m, el) => {
3
- m.push(`SELECT set_config('${el}', '${ctx[el]}', true);`);
3
+ m.push({ query: 'SELECT set_config($1, $2, true)', values: [el, ctx[el]] });
4
4
  return m;
5
5
  }, []);
6
6
  }
7
7
  async function execContext(client, ctx) {
8
8
  const local = setContext(ctx);
9
- for (const query of local) {
10
- await client.query(query);
9
+ for (const { query, values } of local) {
10
+ await client.query(query, values);
11
11
  }
12
12
  }
13
- export default async ({ client, context = {}, query = '', variables = [] }) => {
13
+ export default async ({ client, context = {}, query = '', variables = [], skipTransaction = false }) => {
14
14
  const isPool = 'connect' in client;
15
15
  const shouldRelease = isPool;
16
16
  let pgClient = null;
17
17
  try {
18
18
  pgClient = isPool ? await client.connect() : client;
19
- await pgClient.query('BEGIN');
19
+ if (!skipTransaction) {
20
+ await pgClient.query('BEGIN');
21
+ }
20
22
  await execContext(pgClient, context);
21
23
  const result = await pgClient.query(query, variables);
22
- await pgClient.query('COMMIT');
24
+ if (!skipTransaction) {
25
+ await pgClient.query('COMMIT');
26
+ }
23
27
  return result;
24
28
  }
25
29
  catch (error) {
26
- if (pgClient) {
30
+ if (pgClient && !skipTransaction) {
27
31
  await pgClient.query('ROLLBACK').catch(() => { });
28
32
  }
29
33
  throw error;
package/index.d.ts CHANGED
@@ -4,6 +4,7 @@ interface ExecOptions {
4
4
  context?: Record<string, string>;
5
5
  query: string;
6
6
  variables?: any[];
7
+ skipTransaction?: boolean;
7
8
  }
8
- declare const _default: ({ client, context, query, variables }: ExecOptions) => Promise<QueryResult>;
9
+ declare const _default: ({ client, context, query, variables, skipTransaction }: ExecOptions) => Promise<QueryResult>;
9
10
  export default _default;
package/index.js CHANGED
@@ -2,30 +2,34 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function setContext(ctx) {
4
4
  return Object.keys(ctx || {}).reduce((m, el) => {
5
- m.push(`SELECT set_config('${el}', '${ctx[el]}', true);`);
5
+ m.push({ query: 'SELECT set_config($1, $2, true)', values: [el, ctx[el]] });
6
6
  return m;
7
7
  }, []);
8
8
  }
9
9
  async function execContext(client, ctx) {
10
10
  const local = setContext(ctx);
11
- for (const query of local) {
12
- await client.query(query);
11
+ for (const { query, values } of local) {
12
+ await client.query(query, values);
13
13
  }
14
14
  }
15
- exports.default = async ({ client, context = {}, query = '', variables = [] }) => {
15
+ exports.default = async ({ client, context = {}, query = '', variables = [], skipTransaction = false }) => {
16
16
  const isPool = 'connect' in client;
17
17
  const shouldRelease = isPool;
18
18
  let pgClient = null;
19
19
  try {
20
20
  pgClient = isPool ? await client.connect() : client;
21
- await pgClient.query('BEGIN');
21
+ if (!skipTransaction) {
22
+ await pgClient.query('BEGIN');
23
+ }
22
24
  await execContext(pgClient, context);
23
25
  const result = await pgClient.query(query, variables);
24
- await pgClient.query('COMMIT');
26
+ if (!skipTransaction) {
27
+ await pgClient.query('COMMIT');
28
+ }
25
29
  return result;
26
30
  }
27
31
  catch (error) {
28
- if (pgClient) {
32
+ if (pgClient && !skipTransaction) {
29
33
  await pgClient.query('ROLLBACK').catch(() => { });
30
34
  }
31
35
  throw error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pg-query-context",
3
- "version": "2.4.1",
3
+ "version": "2.6.0",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "pg query context",
6
6
  "main": "index.js",
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/pg": "^8.16.0",
36
- "makage": "^0.1.10"
36
+ "makage": "^0.1.12"
37
37
  },
38
38
  "keywords": [
39
39
  "postgresql",
@@ -42,5 +42,5 @@
42
42
  "pg",
43
43
  "graphile"
44
44
  ],
45
- "gitHead": "3ffd5718e86ea5fa9ca6e0930aeb510cf392f343"
45
+ "gitHead": "b758178b808ce0bf451e86c0bd7e92079155db7c"
46
46
  }