@opengis/fastify-table 1.3.44 → 1.3.46

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.3.44",
3
+ "version": "1.3.46",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -30,7 +30,7 @@ export default async function dataDelete({
30
30
  });
31
31
  const table1 = table.replace(/"/g, '');
32
32
  if (!pg.tlist?.includes(table1)) return 'table not exist';
33
- const delQuery = `delete from ${table} WHERE ${pk} = $1 returning *`;
33
+ const delQuery = `delete from ${table} WHERE ${pk}::text = $1::text returning *`;
34
34
 
35
35
  const extraRes = await extraData({
36
36
  table, form: tokenData?.form, id, uid,
@@ -4,6 +4,7 @@ import getRedis from '../../redis/funcs/getRedis.js';
4
4
  import pgClients from '../../pg/pgClients.js';
5
5
 
6
6
  import logChanges from './utils/logChanges.js';
7
+ import logger from '../../logger/getLogger.js';
7
8
  import extraData from '../../extra/extraData.js';
8
9
 
9
10
  const rclient = getRedis();
@@ -49,7 +50,11 @@ export default async function dataInsert({
49
50
 
50
51
  returning *`;
51
52
 
52
- const res = await pg.query(insertQuery, [...filterData.map((el) => (typeof el[1] === 'object' && (!Array.isArray(el[1]) || typeof el[1]?.[0] === 'object') ? JSON.stringify(el[1]) : el[1]))]) || {};
53
+ const res = await pg.query(insertQuery, [...filterData.map((el) => (typeof el[1] === 'object' && (!Array.isArray(el[1]) || typeof el[1]?.[0] === 'object') ? JSON.stringify(el[1]) : el[1]))])
54
+ .catch(err => {
55
+ logger.file('crud/insert', { error: err.toString(), stack: err.stack, table, id, referer, uid, q: insertQuery });
56
+ throw new Error(err.toString());
57
+ }).then(el => el || {});
53
58
 
54
59
  const table1 = pg.pk[table] ? table : table.replace(/"/g, '');
55
60
 
@@ -46,7 +46,7 @@ export default async function dataUpdate({
46
46
  const filterData = Object.keys(data)
47
47
  .filter((el) => (/* typeof data[el] === 'boolean' ? true : data[el] && */ names?.includes(el) && !['editor_date', 'editor_id'].includes(el)));
48
48
 
49
- const systemColumns = [['editor_date', 'now()', 'updated_at', 'now()'], uid ? ['editor_id', `'${uid.replace(/'/g, "''")}'`, 'updated_by', `'${uid.replace(/'/g, "''")}'`] : []].filter((el) => names.includes(el[0])).map((el) => `${el[0]} = ${el[1]}`).join(',');
49
+ const systemColumns = [['editor_date', 'now()'], ['updated_at', 'now()'], uid ? ['editor_id', `'${uid.replace(/'/g, "''")}'`] : null, uid ? ['updated_by', `'${uid.replace(/'/g, "''")}'`] : null].filter((el) => el && names.includes(el[0])).map((el) => `${el[0]} = ${el[1]}`).join(',');
50
50
 
51
51
  const filterValue = filterData.map((el) => {
52
52
  const { dataTypeID = 25 } = columns.find((col) => col?.name === el) || {};
@@ -68,7 +68,7 @@ export default async function dataUpdate({
68
68
 
69
69
  const updateQuery = `UPDATE ${table} SET ${systemColumns ? `${systemColumns}${filterData?.length ? ',' : ''}` : ''}
70
70
  ${filterData?.map((key, i) => assignValue(key, i, srids[table]?.[key] || 4326, pg.pgType?.[columns.find(col => col.name === key)?.dataTypeID || '']))?.join(',')}
71
- WHERE ${pk} = $1 returning *`;
71
+ WHERE ${pk}::text = $1::text returning *`;
72
72
  // console.log(updateQuery, filterValue);
73
73
  const res = await pg.query(updateQuery, [id, ...filterValue])
74
74
  .catch(err => {
@@ -2,14 +2,14 @@ import config from '../../../../config.js';
2
2
  import hookList from '../hookList.js';
3
3
 
4
4
  export default async function applyHook(name, data) {
5
- const { debug } = config;
6
- if (debug) console.log('applyHook', name);
5
+ const { trace } = config;
6
+ if (trace) console.log('applyHook', name);
7
7
  if (!hookList[name]?.length) return null;
8
8
  const result = {};
9
9
  await Promise.all(hookList[name].map(async (hook) => {
10
10
  const hookData = await hook({ ...data, config });
11
11
  if (hookData) {
12
- if (debug) console.log('applyHook', name, hookData);
12
+ if (trace) console.log('applyHook', name, hookData);
13
13
  Object.assign(result, hookData);
14
14
  }
15
15
  })).catch((err) => {
@@ -8,7 +8,7 @@ import getSelect from './getSelect.js';
8
8
  const limit = 50;
9
9
  const selectMeta = {};
10
10
 
11
- export default async function getSelectMeta({ name, pg = pgClients.client, nocache }) {
11
+ export default async function getSelectMeta({ name, pg = pgClients.client, nocache, parent }) {
12
12
  if (selectMeta[name] && !nocache) return selectMeta[name];
13
13
 
14
14
  const cls = await getSelect(name, pg);
@@ -23,7 +23,7 @@ export default async function getSelectMeta({ name, pg = pgClients.client, nocac
23
23
  const { sql: original } = cls;
24
24
  if (!original.toLowerCase) { console.log(`sql select null: ${name}`); return null; }
25
25
 
26
- const sql = `with c(id,text) as (select * from (${original})q limit ${limit}) select * from c`;
26
+ const sql = `with c(id,text) as (select * from (${original.replace('{{parent}}', parent)})q limit ${limit}) select * from c`;
27
27
 
28
28
  /*= == meta table === */
29
29
 
@@ -32,10 +32,10 @@ export default async function getSelectMeta({ name, pg = pgClients.client, nocac
32
32
 
33
33
  const dataOrigin = await pg1.query(sql?.replace(`limit ${limit}`, 'limit 0'));
34
34
 
35
- const dataOrigin1 = await pg1.query(`${original} limit 0`);
35
+ const dataOrigin1 = await pg1.query(`${original.replace('{{parent}}', parent)} limit 0`);
36
36
 
37
37
  // const table = getTable(original);
38
- const count = cls?.count ?? await pg1.query(`select count(*) from (${original})q`).then((el) => el?.rows?.[0].count);
38
+ const count = cls?.count ?? await pg1.query(`select count(*) from (${original.replace('{{parent}}', parent)})q`).then((el) => el?.rows?.[0].count);
39
39
 
40
40
  // column name
41
41
  const cols = dataOrigin.fields.map((el) => el.name);
@@ -56,7 +56,7 @@ export default async function suggest(req) {
56
56
 
57
57
  const meta = table && column
58
58
  ? getTableColumnMeta(table, column, query?.key || query?.val)
59
- : await getSelectMeta({ pg: pg1, name: selectName, nocache: query?.nocache });
59
+ : await getSelectMeta({ pg: pg1, name: selectName, nocache: query?.nocache, parent });
60
60
 
61
61
  if (meta?.minLength && query.key && query.key.length < meta?.minLength) {
62
62
  return { message: `min length: ${meta.minLength}` };