@opengis/fastify-table 1.3.27 → 1.3.29

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/index.js CHANGED
@@ -4,11 +4,8 @@ import { fileURLToPath } from 'node:url';
4
4
 
5
5
  import config from './config.js';
6
6
 
7
- // hb
8
- import helper from './server/helpers/index.js';
9
- import handlebarsSync from 'handlebars';
10
- import promisedHandlebars from 'promised-handlebars';
11
- const handlebars = promisedHandlebars(handlebarsSync);
7
+ // helpers
8
+ import helperPlugin from './server/helpers/index.js';
12
9
 
13
10
  // plugins
14
11
  import cronPlugin from './server/plugins/cron/index.js';
@@ -57,22 +54,18 @@ async function plugin(fastify, opt) {
57
54
  errorHandler: false,
58
55
  });
59
56
 
60
- helper(handlebars); // fastify-hb
61
-
62
57
  // core migrations (second argument for core only)
63
- execMigrations(path.join(cwd, 'server/migrations'), true).catch(err => console.log(err));
58
+ await execMigrations(path.join(cwd, 'server/migrations'), true).catch(err => console.log(err));
64
59
 
65
60
  // core templates && cls
66
61
  config.templates?.forEach(el => addTemplateDir(el));
67
62
  addTemplateDir(path.join(cwd, 'module/core'));
68
63
 
69
- // helpers
70
- fastify.register(import('./server/helpers/index.js'));
71
-
72
64
  // plugins / utils / funcs
73
65
  policyPlugin(fastify);
74
66
  metricPlugin(fastify);
75
67
  redisPlugin(fastify);
68
+ helperPlugin(fastify);
76
69
  await pgPlugin(fastify, opt);
77
70
  tablePlugin(fastify, opt);
78
71
  crudPlugin(fastify, opt);
@@ -89,9 +82,4 @@ async function plugin(fastify, opt) {
89
82
  utilRoutes(fastify, opt);
90
83
  fastify.get('/api/test-proxy', {}, (req) => req.headers);
91
84
  }
92
- export default fp(plugin);
93
-
94
- export {
95
- handlebars,
96
- handlebarsSync,
97
- }
85
+ export default fp(plugin);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "1.3.27",
3
+ "version": "1.3.29",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [
@@ -1,36 +1,41 @@
1
- import { logger, handlebars } from '../../utils.js';
1
+ import { logger } from '../../utils.js';
2
+
3
+ import handlebarsSync from 'handlebars';
4
+ import promisedHandlebars from 'promised-handlebars';
5
+
6
+ const handlebars = promisedHandlebars(handlebarsSync);
2
7
 
3
8
  // funcs
4
- import _math from './funcs/_math.js'
5
- import ifCond from './funcs/ifCond.js'
6
- import ifCondAnd from './funcs/ifCondAnd.js'
7
- import ifCondOr from './funcs/ifCondOr.js'
8
- import qrcode from './funcs/qrcode.js'
9
- import select from './funcs/select.js'
10
- import json from './funcs/json.js'
11
- import empty from './funcs/empty.js'
12
- import round from './funcs/round.js'
13
- import contentList from './funcs/contentList.js'
14
- import inc from './funcs/inc.js'
9
+ import _math from './funcs/_math.js';
10
+ import ifCond from './funcs/ifCond.js';
11
+ import ifCondAnd from './funcs/ifCondAnd.js';
12
+ import ifCondOr from './funcs/ifCondOr.js';
13
+ import qrcode from './funcs/qrcode.js';
14
+ import select from './funcs/select.js';
15
+ import json from './funcs/json.js';
16
+ import empty from './funcs/empty.js';
17
+ import round from './funcs/round.js';
18
+ import contentList from './funcs/contentList.js';
19
+ import inc from './funcs/inc.js';
15
20
 
16
21
  // format
17
- import formatAuto from './format/formatAuto.js'
18
- import formatDate from './format/formatDate.js'
19
- import formatDigit from './format/formatDigit.js'
20
- import formatNum from './format/formatNum.js'
21
- import formatNumber from './format/formatNumber.js'
22
- import formatRelative from './format/formatRelative.js'
23
- import formatUnit from './format/formatUnit.js'
24
- import num_format from './format/num_format.js'
25
- import set from './format/set.js'
22
+ import formatAuto from './format/formatAuto.js';
23
+ import formatDate from './format/formatDate.js';
24
+ import formatDigit from './format/formatDigit.js';
25
+ import formatNum from './format/formatNum.js';
26
+ import formatNumber from './format/formatNumber.js';
27
+ import formatRelative from './format/formatRelative.js';
28
+ import formatUnit from './format/formatUnit.js';
29
+ import num_format from './format/num_format.js';
30
+ import set from './format/set.js';
26
31
 
27
32
  // string
28
- import str_replace from './string/str_replace.js'
29
- import coalesce from './string/coalesce.js'
30
- import concat from './string/concat.js'
31
- import split from './string/split.js'
32
- import translit from './string/translit.js'
33
- import substr from './string/substr.js'
33
+ import str_replace from './string/str_replace.js';
34
+ import coalesce from './string/coalesce.js';
35
+ import concat from './string/concat.js';
36
+ import split from './string/split.js';
37
+ import translit from './string/translit.js';
38
+ import substr from './string/substr.js';
34
39
 
35
40
  function getKeysRecursive(obj, prefix = '') {
36
41
  if (!obj || typeof obj !== 'object' || obj?.constructor?.name !== 'Object') return [];
@@ -45,52 +50,59 @@ function getKeysRecursive(obj, prefix = '') {
45
50
  }, []);
46
51
  }
47
52
 
48
- export default async function helpers() {
49
- // avoid unhandled exception if helper not registered
50
- handlebars.registerHelper('helperMissing', function () {
51
- const options = arguments[arguments.length - 1];
52
- const args = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
53
- const keys = getKeysRecursive(options.data?.root);
54
- if (args.length || !keys.includes(options.name)) {
55
- logger.file('handlebars/error', { message: `Missing helper "${options.name}" at "${JSON.stringify(args).substring(0, 10)}"` });
56
- return args;
57
- }
53
+ // avoid unhandled exception if helper not registered
54
+ handlebars.registerHelper('helperMissing', function () {
55
+ const options = arguments[arguments.length - 1];
56
+ const args = Array.prototype.slice.call(arguments, 0, arguments.length - 1);
57
+ const keys = getKeysRecursive(options.data?.root);
58
+ if (args.length || !keys.includes(options.name)) {
59
+ logger.file('handlebars/error', { message: `Missing helper "${options.name}" at "${JSON.stringify(args).substring(0, 10)}"` });
58
60
  return args;
59
- });
61
+ }
62
+ return args;
63
+ });
60
64
 
61
- // format
62
- handlebars.registerHelper('formatAuto', formatAuto);
63
- handlebars.registerHelper('formatDate', formatDate);
64
- handlebars.registerHelper('formatDigit', formatDigit);
65
- handlebars.registerHelper('formatNum', formatNum);
66
- handlebars.registerHelper('formatNumber', formatNumber);
67
- handlebars.registerHelper('formatRelative', formatRelative);
68
- handlebars.registerHelper('formatUnit', formatUnit);
69
- handlebars.registerHelper('num_format', num_format);
70
- handlebars.registerHelper('set', set);
65
+ // format
66
+ handlebars.registerHelper('formatAuto', formatAuto);
67
+ handlebars.registerHelper('formatDate', formatDate);
68
+ handlebars.registerHelper('formatDigit', formatDigit);
69
+ handlebars.registerHelper('formatNum', formatNum);
70
+ handlebars.registerHelper('formatNumber', formatNumber);
71
+ handlebars.registerHelper('formatRelative', formatRelative);
72
+ handlebars.registerHelper('formatUnit', formatUnit);
73
+ handlebars.registerHelper('num_format', num_format);
74
+ handlebars.registerHelper('set', set);
71
75
 
72
- // string
73
- handlebars.registerHelper('str_replace', str_replace);
74
- handlebars.registerHelper('coalesce', coalesce);
75
- handlebars.registerHelper('concat', concat);
76
- handlebars.registerHelper('split', split);
77
- handlebars.registerHelper('translit', translit);
78
- handlebars.registerHelper('substr', substr);
79
- handlebars.registerHelper('mls', (value) => value);
76
+ // string
77
+ handlebars.registerHelper('str_replace', str_replace);
78
+ handlebars.registerHelper('coalesce', coalesce);
79
+ handlebars.registerHelper('concat', concat);
80
+ handlebars.registerHelper('split', split);
81
+ handlebars.registerHelper('translit', translit);
82
+ handlebars.registerHelper('substr', substr);
83
+ handlebars.registerHelper('mls', (value) => value);
80
84
 
81
- // funcs
82
- handlebars.registerHelper('json', json);
83
- handlebars.registerHelper('_math', _math);
84
- handlebars.registerHelper('ifCond', ifCond);
85
- handlebars.registerHelper('ifCondAnd', ifCondAnd);
86
- handlebars.registerHelper('ifCondOr', ifCondOr);
87
- handlebars.registerHelper('select', select);
88
- handlebars.registerHelper('empty', empty);
89
- handlebars.registerHelper('round', round);
90
- handlebars.registerHelper('contentList', contentList);
91
- handlebars.registerHelper('inc', inc);
92
- handlebars.registerHelper('qrcode', qrcode);
85
+ // funcs
86
+ handlebars.registerHelper('json', json);
87
+ handlebars.registerHelper('_math', _math);
88
+ handlebars.registerHelper('ifCond', ifCond);
89
+ handlebars.registerHelper('ifCondAnd', ifCondAnd);
90
+ handlebars.registerHelper('ifCondOr', ifCondOr);
91
+ handlebars.registerHelper('select', select);
92
+ handlebars.registerHelper('empty', empty);
93
+ handlebars.registerHelper('round', round);
94
+ handlebars.registerHelper('contentList', contentList);
95
+ handlebars.registerHelper('inc', inc);
96
+ handlebars.registerHelper('qrcode', qrcode);
97
+
98
+ // Підтримка старого коду
99
+ handlebars.registerHelper('qrcode-generator-base64', qrcode);
100
+
101
+ export default function helpers(fastify) {
93
102
 
94
- // Підтримка старого коду
95
- handlebars.registerHelper('qrcode-generator-base64', qrcode);
96
103
  }
104
+
105
+ export {
106
+ handlebars,
107
+ handlebarsSync,
108
+ }
@@ -28,6 +28,7 @@ export default async function dataInsert({
28
28
  if (!columns) return null;
29
29
 
30
30
  const names = columns.map((el) => el.name);
31
+ const types = columns.reduce((acc, { name, dataTypeID }) => ({ ...acc, [name]: pg.pgType?.[dataTypeID] }), {});
31
32
 
32
33
  Object.assign(data, {
33
34
  ...(id && pg.pk?.[table] ? { [pg.pk?.[table]]: id } : {}),
@@ -50,7 +51,7 @@ export default async function dataInsert({
50
51
 
51
52
  returning *`;
52
53
 
53
- const res = await pg.query(insertQuery, [...filterData.map((el) => (typeof el[1] === 'object' ? JSON.stringify(el[1]) : el[1]))]).catch(err => {
54
+ const res = await pg.query(insertQuery, [...filterData.map((el) => (typeof el[1] === 'object' && types[el[0]]?.includes?.('json') ? JSON.stringify(el[1]) : el[1]))]).catch(err => {
54
55
  logger.file('crud/insert', { error: err.toString(), stack: err.stack, table, id, referer, uid, data, q: insertQuery });
55
56
  throw new Error(err.toString());
56
57
  }) || {};
@@ -38,7 +38,10 @@ export default async function dataUpdate({
38
38
 
39
39
  const { columns, pk } = await getMeta({ pg, table });
40
40
 
41
- const names = columns?.map((el) => el.name);
41
+ if (!columns) return null;
42
+
43
+ const names = columns.map((el) => el.name);
44
+ const types = columns.reduce((acc, { name, dataTypeID }) => ({ ...acc, [name]: pg.pgType?.[dataTypeID] }), {});
42
45
 
43
46
  const filterData = Object.keys(data)
44
47
  .filter((el) => (/* typeof data[el] === 'boolean' ? true : data[el] && */ names?.includes(el) && !['editor_date', 'editor_id'].includes(el)));
@@ -46,12 +49,12 @@ export default async function dataUpdate({
46
49
  const systemColumns = [['editor_date', 'now()'], uid ? ['editor_id', `'${uid.replace(/'/g, "''")}'`] : []].filter((el) => names.includes(el[0])).map((el) => `${el[0]} = ${el[1]}`).join(',');
47
50
 
48
51
  const filterValue = filterData.map((el) => {
49
- const { dataTypeID = 25 } = columns?.find?.((col) => col?.name === el) || {};
52
+ const { dataTypeID = 25 } = columns.find((col) => col?.name === el) || {};
50
53
  if (pg.pgType[dataTypeID]?.endsWith('[]') && ['string', 'number'].includes(typeof data[el])) {
51
54
  Object.assign(data, { [el]: data[el].split(',') });
52
55
  }
53
56
  return [el, data[el]];
54
- }).map((el) => (typeof el[1] === 'object' && el[1] ? JSON.stringify(el[1]) : el[1]));
57
+ }).map((el) => (typeof el[1] === 'object' && types[el[0]]?.includes?.('json') && el[1] ? JSON.stringify(el[1]) : el[1]));
55
58
 
56
59
  // update geometry with srid
57
60
  if (!srids[table] && pg.tlist?.includes('public.geometry_columns')) {
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- import { handlebars } from '../../../../../index.js';
3
+ import { handlebars } from '../../../../helpers/index.js';
4
4
 
5
5
  import getTemplate from '../getTemplate.js';
6
6
  import getSelectVal from './getSelectVal.js';
package/utils.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // between our tests.
3
3
 
4
4
  // hb
5
- import { handlebars, handlebarsSync } from './index.js';
5
+ import { handlebars, handlebarsSync } from './server/helpers/index.js';
6
6
 
7
7
  // pg
8
8
  import getPG from './server/plugins/pg/funcs/getPG.js';