@opengis/fastify-table 1.4.20 → 1.4.21

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.4.20",
3
+ "version": "1.4.21",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -15,6 +15,7 @@ export default async function dataInsert({
15
15
  id, table: table1, referer, data, pg: pg1, uid, tokenData = {},
16
16
  }) {
17
17
  const pg = pg1 || getPG({ name: 'client' });
18
+ if (!pg) return null;
18
19
 
19
20
  // pg client single transaction support
20
21
  if (!pg?.pk && config.pg) {
@@ -38,16 +39,24 @@ export default async function dataInsert({
38
39
  if (!insertQuery || !args.length) return null;
39
40
 
40
41
  // for transactions
41
- const client = pg?._connected ? pg : await pg.connect();
42
+ const isClient = typeof pg.query === 'function' && typeof pg.release === 'function';
43
+ const client = isClient ? pg : await pg.connect();
44
+ if (!isClient) {
45
+ client.caller = 'dataInsert';
46
+ }
42
47
 
43
- client.options = pg?.options;
44
- client.tlist = pg?.tlist;
45
- client.pgType = pg?.pgType;
46
- client.relkinds = pg?.relkinds;
47
- client.pk = pg?.pk;
48
+ if (isClient || !client.pk) {
49
+ client.options = pg.options;
50
+ client.tlist = pg.tlist;
51
+ client.pgType = pg.pgType;
52
+ client.relkinds = pg.relkinds;
53
+ client.pk = pg.pk;
54
+ }
48
55
 
49
56
  try {
50
- await client.query('begin;');
57
+ if (client.caller === 'dataInsert') {
58
+ await client.query('begin;');
59
+ }
51
60
  const res = await client.query(insertQuery, args).then(el => el || {});
52
61
 
53
62
  const id1 = res.rows?.[0]?.[pg.pk[table1]];
@@ -98,17 +107,23 @@ export default async function dataInsert({
98
107
  });
99
108
 
100
109
  if (config.redis) { rclient.incr(`pg:${table}:crud`); }
101
- await client.query('commit;');
110
+ if (client.caller === 'dataInsert') {
111
+ await client.query('commit;');
112
+ }
102
113
  return res;
103
114
  }
104
115
  catch (err) {
105
116
  logger.file('crud/insert', {
106
117
  error: err.toString(), stack: err.stack, table, id, referer, uid, form: tokenData?.form,
107
118
  });
108
- await client.query('rollback;');
119
+ if (client.caller === 'dataInsert') {
120
+ await client.query('rollback;');
121
+ }
109
122
  throw err;
110
123
  }
111
124
  finally {
112
- client.release();
125
+ if (client.caller === 'dataInsert') {
126
+ client.release();
127
+ }
113
128
  }
114
129
  }
@@ -29,6 +29,7 @@ export default async function dataUpdate({
29
29
  if (!data || !table || !id) return null;
30
30
 
31
31
  const pg = pg1 || getPG({ name: 'client' });
32
+ if (!pg) return null;
32
33
 
33
34
  // pg client single transaction support
34
35
  if (!pg?.pk && config.pg) {
@@ -75,10 +76,25 @@ export default async function dataUpdate({
75
76
  // console.log(updateQuery, filterValue);
76
77
 
77
78
  // for transactions
78
- const client = pg?._connected ? pg : await pg.connect();
79
+ const isClient = typeof pg.query === 'function' && typeof pg.release === 'function';
80
+ const client = isClient ? pg : await pg.connect();
81
+
82
+ if (!isClient) {
83
+ client.caller = 'dataUpdate';
84
+ }
85
+
86
+ if (isClient || !client.pk) {
87
+ client.options = pg.options;
88
+ client.tlist = pg.tlist;
89
+ client.pgType = pg.pgType;
90
+ client.relkinds = pg.relkinds;
91
+ client.pk = pg.pk;
92
+ }
79
93
 
80
94
  try {
81
- await client.query('begin;');
95
+ if (client.caller === 'dataUpdate') {
96
+ await client.query('begin;');
97
+ }
82
98
  const res = await client.query(updateQuery, [id, ...filterValue])
83
99
  .catch(err => {
84
100
  logger.file('crud/update', {
@@ -145,17 +161,23 @@ export default async function dataUpdate({
145
161
 
146
162
  if (config.redis) { rclient.incr(`pg:${table}:crud`); }
147
163
 
148
- await client.query('commit;');
164
+ if (client.caller === 'dataUpdate') {
165
+ await client.query('commit;');
166
+ }
149
167
  return res || {};
150
168
  }
151
169
  catch (err) {
152
170
  logger.file('crud/update', {
153
171
  error: err.toString(), stack: err.stack, table, id, referer, uid, form: tokenData?.form,
154
172
  });
155
- await client.query('rollback;');
173
+ if (client.caller === 'dataUpdate') {
174
+ await client.query('rollback;');
175
+ }
156
176
  throw err;
157
177
  }
158
178
  finally {
159
- client.release();
179
+ if (client.caller === 'dataUpdate') {
180
+ client.release();
181
+ }
160
182
  }
161
183
  }