@opengis/fastify-table 1.1.78 → 1.1.79

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.
Files changed (77) hide show
  1. package/index.js +76 -76
  2. package/package.json +45 -45
  3. package/server/migrations/0.sql +80 -80
  4. package/server/migrations/cls.sql +39 -39
  5. package/server/migrations/context.sql +95 -0
  6. package/server/migrations/properties.sql +144 -144
  7. package/server/migrations/roles.sql +179 -175
  8. package/server/migrations/users.sql +170 -170
  9. package/server/plugins/cron/funcs/addCron.js +130 -130
  10. package/server/plugins/cron/index.js +6 -6
  11. package/server/plugins/crud/funcs/dataDelete.js +21 -21
  12. package/server/plugins/crud/funcs/dataInsert.js +38 -38
  13. package/server/plugins/crud/funcs/dataUpdate.js +50 -50
  14. package/server/plugins/crud/funcs/getAccess.js +48 -48
  15. package/server/plugins/crud/funcs/getOpt.js +13 -13
  16. package/server/plugins/crud/funcs/setOpt.js +21 -21
  17. package/server/plugins/crud/funcs/setToken.js +44 -44
  18. package/server/plugins/crud/funcs/utils/getFolder.js +10 -10
  19. package/server/plugins/crud/funcs/utils/logChanges.js +62 -62
  20. package/server/plugins/crud/index.js +23 -23
  21. package/server/plugins/hook/index.js +8 -8
  22. package/server/plugins/logger/errorStatus.js +19 -19
  23. package/server/plugins/logger/index.js +21 -21
  24. package/server/plugins/migration/funcs/exec.migrations.js +79 -79
  25. package/server/plugins/migration/index.js +7 -7
  26. package/server/plugins/pg/funcs/getMeta.js +28 -28
  27. package/server/plugins/pg/funcs/getPG.js +33 -33
  28. package/server/plugins/pg/pgClients.js +21 -21
  29. package/server/plugins/policy/funcs/checkPolicy.js +92 -92
  30. package/server/plugins/policy/funcs/checkXSS.js +1 -1
  31. package/server/plugins/policy/index.js +12 -12
  32. package/server/plugins/policy/sqlInjection.js +33 -33
  33. package/server/plugins/redis/client.js +8 -8
  34. package/server/plugins/redis/funcs/redisClients.js +3 -3
  35. package/server/plugins/redis/index.js +17 -17
  36. package/server/plugins/table/funcs/addTemplateDir.js +8 -8
  37. package/server/plugins/table/funcs/getFilterSQL/index.js +96 -96
  38. package/server/plugins/table/funcs/getFilterSQL/util/formatValue.js +170 -170
  39. package/server/plugins/table/funcs/getFilterSQL/util/getCustomQuery.js +13 -13
  40. package/server/plugins/table/funcs/getFilterSQL/util/getFilterQuery.js +66 -66
  41. package/server/plugins/table/funcs/getFilterSQL/util/getOptimizedQuery.js +12 -12
  42. package/server/plugins/table/funcs/getFilterSQL/util/getTableSql.js +34 -34
  43. package/server/plugins/table/funcs/getSelect.js +19 -19
  44. package/server/plugins/table/funcs/getSelectMeta.js +60 -60
  45. package/server/plugins/table/funcs/getTemplates.js +19 -19
  46. package/server/plugins/table/funcs/gisIRColumn.js +72 -72
  47. package/server/plugins/table/funcs/loadTemplate.js +1 -1
  48. package/server/plugins/table/funcs/loadTemplatePath.js +1 -1
  49. package/server/plugins/table/funcs/metaFormat/getSelectVal.js +38 -22
  50. package/server/plugins/table/funcs/metaFormat/index.js +31 -30
  51. package/server/plugins/table/funcs/userTemplateDir.js +1 -1
  52. package/server/plugins/table/index.js +13 -13
  53. package/server/plugins/util/funcs/eventStream.js +28 -28
  54. package/server/plugins/util/index.js +7 -7
  55. package/server/routes/cron/index.js +14 -14
  56. package/server/routes/crud/controllers/deleteCrud.js +36 -36
  57. package/server/routes/crud/controllers/insert.js +80 -80
  58. package/server/routes/crud/controllers/table.js +91 -91
  59. package/server/routes/crud/controllers/update.js +82 -82
  60. package/server/routes/crud/index.js +21 -21
  61. package/server/routes/logger/controllers/logger.file.js +92 -92
  62. package/server/routes/logger/controllers/utils/checkUserAccess.js +19 -19
  63. package/server/routes/logger/controllers/utils/getRootDir.js +26 -26
  64. package/server/routes/logger/index.js +17 -17
  65. package/server/routes/properties/controllers/properties.add.js +55 -55
  66. package/server/routes/properties/controllers/properties.get.js +17 -17
  67. package/server/routes/properties/index.js +16 -16
  68. package/server/routes/table/controllers/data.js +144 -144
  69. package/server/routes/table/controllers/filter.js +60 -60
  70. package/server/routes/table/controllers/form.js +42 -42
  71. package/server/routes/table/controllers/search.js +74 -74
  72. package/server/routes/table/controllers/suggest.js +84 -84
  73. package/server/routes/table/index.js +28 -28
  74. package/server/routes/table/schema.js +64 -64
  75. package/server/routes/util/controllers/status.monitor.js +8 -8
  76. package/server/routes/util/index.js +11 -11
  77. package/utils.js +122 -122
@@ -1,23 +1,23 @@
1
- // import getOpt from './funcs/getOpt.js';
2
- // import setOpt from './funcs/setOpt.js';
3
-
4
- // import isFileExists from './funcs/isFileExists.js';
5
-
6
- // import dataUpdate from './funcs/dataUpdate.js';
7
- // import dataInsert from './funcs/dataInsert.js';
8
-
9
- // import getAccessFunc from './funcs/getAccess.js';
10
-
11
- async function plugin(fastify) {
12
- // fastify.decorate('setOpt', setOpt);
13
- // fastify.decorate('getOpt', getOpt);
14
-
15
- // fastify.decorate('dataUpdate', dataUpdate);
16
- // fastify.decorate('dataInsert', dataInsert);
17
-
18
- // fastify.decorate('getAccess', getAccessFunc);
19
-
20
- // fastify.decorate('isFileExists', isFileExists);
21
- }
22
-
23
- export default plugin;
1
+ // import getOpt from './funcs/getOpt.js';
2
+ // import setOpt from './funcs/setOpt.js';
3
+
4
+ // import isFileExists from './funcs/isFileExists.js';
5
+
6
+ // import dataUpdate from './funcs/dataUpdate.js';
7
+ // import dataInsert from './funcs/dataInsert.js';
8
+
9
+ // import getAccessFunc from './funcs/getAccess.js';
10
+
11
+ async function plugin(fastify) {
12
+ // fastify.decorate('setOpt', setOpt);
13
+ // fastify.decorate('getOpt', getOpt);
14
+
15
+ // fastify.decorate('dataUpdate', dataUpdate);
16
+ // fastify.decorate('dataInsert', dataInsert);
17
+
18
+ // fastify.decorate('getAccess', getAccessFunc);
19
+
20
+ // fastify.decorate('isFileExists', isFileExists);
21
+ }
22
+
23
+ export default plugin;
@@ -1,8 +1,8 @@
1
- import addHook from './funcs/addHook.js';
2
- import applyHook from './funcs/applyHook.js';
3
-
4
- async function plugin(fastify) {
5
- // fastify.decorate('addHook', addHook);
6
- // fastify.decorate('applyHook', applyHook);
7
- }
8
- export default plugin;
1
+ import addHook from './funcs/addHook.js';
2
+ import applyHook from './funcs/applyHook.js';
3
+
4
+ async function plugin(fastify) {
5
+ // fastify.decorate('addHook', addHook);
6
+ // fastify.decorate('applyHook', applyHook);
7
+ }
8
+ export default plugin;
@@ -1,19 +1,19 @@
1
- import applyHookSync from '../hook/funcs/applyHookSync.js';
2
-
3
- function errorStatus(error) {
4
- const hook = applyHookSync('errorStatus', error);
5
- if (hook) return hook;
6
-
7
- if (error.routine === 'exec_stmt_raise' && error.file === 'pl_exec.c') {
8
- return 601;
9
- }
10
- if (error.routine === 'ExecConstraints') {
11
- return 602;
12
- }
13
- if (error.type === 'DatabaseError') {
14
- return 600;
15
- }
16
-
17
- return 500;
18
- }
19
- export default errorStatus;
1
+ import applyHookSync from '../hook/funcs/applyHookSync.js';
2
+
3
+ function errorStatus(error) {
4
+ const hook = applyHookSync('errorStatus', error);
5
+ if (hook) return hook;
6
+
7
+ if (error.routine === 'exec_stmt_raise' && error.file === 'pl_exec.c') {
8
+ return 601;
9
+ }
10
+ if (error.routine === 'ExecConstraints') {
11
+ return 602;
12
+ }
13
+ if (error.type === 'DatabaseError') {
14
+ return 600;
15
+ }
16
+
17
+ return 500;
18
+ }
19
+ export default errorStatus;
@@ -1,21 +1,21 @@
1
- import errorMessage from './errorMessage.js';
2
-
3
- async function plugin(fastify) {
4
- fastify.setErrorHandler(async (error, request, reply) => {
5
- // validation not error
6
- if (error.validation) {
7
- request.log.warn(request, { code: error?.code, status: 422, error: error.toString() });
8
- return reply.status(422).send(error.toString());
9
- }
10
-
11
- // logger
12
- request.log.error(error, request);
13
- console.error({ msg: error.message, where: error.where, stack: error.stack });
14
-
15
- // errorMessage
16
- const msg = errorMessage(error);
17
-
18
- return reply.status(error.statusCode || 500).send(msg);
19
- });
20
- }
21
- export default plugin;
1
+ import errorMessage from './errorMessage.js';
2
+
3
+ async function plugin(fastify) {
4
+ fastify.setErrorHandler(async (error, request, reply) => {
5
+ // validation not error
6
+ if (error.validation) {
7
+ request.log.warn(request, { code: error?.code, status: 422, error: error.toString() });
8
+ return reply.status(422).send(error.toString());
9
+ }
10
+
11
+ // logger
12
+ request.log.error(error, request);
13
+ console.error({ msg: error.message, where: error.where, stack: error.stack });
14
+
15
+ // errorMessage
16
+ const msg = errorMessage(error);
17
+
18
+ return reply.status(error.statusCode || 500).send(msg);
19
+ });
20
+ }
21
+ export default plugin;
@@ -1,79 +1,79 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
-
4
- const time = Date.now();
5
-
6
- import getPG from '../../pg/funcs/getPG.js';
7
-
8
- function getCallerDir() {
9
- const originalFunc = Error.prepareStackTrace;
10
-
11
- let callerfile;
12
- try {
13
- const err = new Error();
14
- // let currentfile;
15
-
16
- Error.prepareStackTrace = function (err, stack) { return stack; };
17
-
18
- const currentfile = err.stack.shift().getFileName();
19
-
20
- while (err.stack.length) {
21
- callerfile = err.stack.shift().getFileName();
22
-
23
- if (currentfile !== callerfile) break;
24
- }
25
- }
26
- catch (err) { }
27
-
28
- Error.prepareStackTrace = originalFunc;
29
-
30
- return path.dirname(callerfile);
31
- }
32
-
33
- function sequence(files, data, fn) {
34
- return files.reduce((promise, filename) => promise.then(() => fn({
35
- ...data, filename,
36
- })), Promise.resolve());
37
- }
38
-
39
- async function execSql({
40
- pg, dir, filename,
41
- }) {
42
- const start = Date.now();
43
- const filepath = path.join(dir, filename);
44
- const sql = fs.readFileSync(filepath, 'utf-8');
45
- try {
46
- console.log(filename, 'start', Date.now() - start);
47
- await pg.query(sql);
48
- console.log(filename, 'finish', Date.now() - start);
49
- }
50
- catch (err) {
51
- console.log(filepath, 'error', err.toString(), Date.now() - start);
52
- }
53
- }
54
-
55
- export default async function execMigrations(opt) {
56
- try {
57
- const pg = opt?.pg || getPG({ name: 'client' });
58
- const rootDir = getCallerDir();
59
- const dir = path.join(rootDir.replace(/\\/g, '/').replace(/^file:\/\/\//, ''), rootDir.endsWith('plugins') ? '../..' : '', 'server/migrations');
60
-
61
- console.log('migrations start', dir, Date.now() - time);
62
- const exists = fs.existsSync(dir);
63
- if (exists) {
64
- // get directory sql file list
65
- const content = fs.readdirSync(dir, { withFileTypes: true })
66
- ?.filter((el) => el.isFile() && path.extname(el.name) === '.sql')
67
- ?.map((el) => el.name) || [];
68
-
69
- // execute sql files
70
- if (content?.length) {
71
- await sequence(content, { pg, dir }, execSql);
72
- }
73
- }
74
- console.log('migrations finish', dir, exists, Date.now() - time);
75
- }
76
- catch (err) {
77
- console.error('migrations error', err.toString(), Date.now() - time);
78
- }
79
- }
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+
4
+ const time = Date.now();
5
+
6
+ import getPG from '../../pg/funcs/getPG.js';
7
+
8
+ function getCallerDir() {
9
+ const originalFunc = Error.prepareStackTrace;
10
+
11
+ let callerfile;
12
+ try {
13
+ const err = new Error();
14
+ // let currentfile;
15
+
16
+ Error.prepareStackTrace = function (err, stack) { return stack; };
17
+
18
+ const currentfile = err.stack.shift().getFileName();
19
+
20
+ while (err.stack.length) {
21
+ callerfile = err.stack.shift().getFileName();
22
+
23
+ if (currentfile !== callerfile) break;
24
+ }
25
+ }
26
+ catch (err) { }
27
+
28
+ Error.prepareStackTrace = originalFunc;
29
+
30
+ return path.dirname(callerfile);
31
+ }
32
+
33
+ function sequence(files, data, fn) {
34
+ return files.reduce((promise, filename) => promise.then(() => fn({
35
+ ...data, filename,
36
+ })), Promise.resolve());
37
+ }
38
+
39
+ async function execSql({
40
+ pg, dir, filename,
41
+ }) {
42
+ const start = Date.now();
43
+ const filepath = path.join(dir, filename);
44
+ const sql = fs.readFileSync(filepath, 'utf-8');
45
+ try {
46
+ console.log(filename, 'start', Date.now() - start);
47
+ await pg.query(sql);
48
+ console.log(filename, 'finish', Date.now() - start);
49
+ }
50
+ catch (err) {
51
+ console.log(filepath, 'error', err.toString(), Date.now() - start);
52
+ }
53
+ }
54
+
55
+ export default async function execMigrations(opt) {
56
+ try {
57
+ const pg = opt?.pg || getPG({ name: 'client' });
58
+ const rootDir = getCallerDir();
59
+ const dir = path.join(rootDir.replace(/\\/g, '/').replace(/^file:\/\/\//, ''), rootDir.endsWith('plugins') ? '../..' : '', 'server/migrations');
60
+
61
+ console.log('migrations start', dir, Date.now() - time);
62
+ const exists = fs.existsSync(dir);
63
+ if (exists) {
64
+ // get directory sql file list
65
+ const content = fs.readdirSync(dir, { withFileTypes: true })
66
+ ?.filter((el) => el.isFile() && path.extname(el.name) === '.sql')
67
+ ?.map((el) => el.name) || [];
68
+
69
+ // execute sql files
70
+ if (content?.length) {
71
+ await sequence(content, { pg, dir }, execSql);
72
+ }
73
+ }
74
+ console.log('migrations finish', dir, exists, Date.now() - time);
75
+ }
76
+ catch (err) {
77
+ console.error('migrations error', err.toString(), Date.now() - time);
78
+ }
79
+ }
@@ -1,7 +1,7 @@
1
- import execMigrations from './funcs/exec.migrations.js';
2
-
3
- async function plugin(fastify) {
4
- // fastify.decorate('execMigrations', execMigrations);
5
- }
6
-
7
- export default plugin;
1
+ import execMigrations from './funcs/exec.migrations.js';
2
+
3
+ async function plugin(fastify) {
4
+ // fastify.decorate('execMigrations', execMigrations);
5
+ }
6
+
7
+ export default plugin;
@@ -1,28 +1,28 @@
1
- import getPG from './getPG.js';
2
-
3
- const data = {};
4
-
5
- // decorator
6
- export default async function getMeta(opt) {
7
- const pg = opt?.pg || getPG({ name: 'client' });
8
- const table = opt?.table || opt;
9
-
10
- if (data[table]) return data[table];
11
-
12
- if (!pg.tlist?.includes(table)) {
13
- return { error: `${table} - not found`, status: 400 };
14
- }
15
-
16
- const { fields } = await pg.query(`select * from ${table} where $1=$1 limit 0`, [1]);
17
- const { pks1 } = await pg.one(`SELECT json_object_agg(c.conrelid::regclass, a.attname) as pks1 FROM pg_constraint c
18
- left join pg_attribute a on c.conrelid=a.attrelid and a.attnum = c.conkey[1] WHERE c.contype='p'::"char"`, { cache: 0 });
19
- const pk = table.startsWith('public.') ? pks1[table.replace('public.', '')] : pks1[table];
20
-
21
- const geomAttr = fields.find((el) => pg.pgType[el.dataTypeID] === 'geometry')?.name; // change geometry text to geometry code
22
-
23
- const res = {
24
- pk, columns: fields, geom: geomAttr, view: pg.relkinds?.[table] === 'v',
25
- };
26
- data[table] = res;
27
- return res;
28
- }
1
+ import getPG from './getPG.js';
2
+
3
+ const data = {};
4
+
5
+ // decorator
6
+ export default async function getMeta(opt) {
7
+ const pg = opt?.pg || getPG({ name: 'client' });
8
+ const table = opt?.table || opt;
9
+
10
+ if (data[table]) return data[table];
11
+
12
+ if (!pg.tlist?.includes(table)) {
13
+ return { error: `${table} - not found`, status: 400 };
14
+ }
15
+
16
+ const { fields } = await pg.query(`select * from ${table} where $1=$1 limit 0`, [1]);
17
+ const { pks1 } = await pg.one(`SELECT json_object_agg(c.conrelid::regclass, a.attname) as pks1 FROM pg_constraint c
18
+ left join pg_attribute a on c.conrelid=a.attrelid and a.attnum = c.conkey[1] WHERE c.contype='p'::"char"`, { cache: 0 });
19
+ const pk = table.startsWith('public.') ? pks1[table.replace('public.', '')] : pks1[table];
20
+
21
+ const geomAttr = fields.find((el) => pg.pgType[el.dataTypeID] === 'geometry')?.name; // change geometry text to geometry code
22
+
23
+ const res = {
24
+ pk, columns: fields, geom: geomAttr, view: pg.relkinds?.[table] === 'v',
25
+ };
26
+ data[table] = res;
27
+ return res;
28
+ }
@@ -1,33 +1,33 @@
1
- import pg from 'pg';
2
-
3
- const { types } = pg;
4
- types.setTypeParser(1082, (stringValue) => stringValue);
5
-
6
- import config from '../../../../config.js';
7
- import pgClients from '../pgClients.js';
8
- import init from './init.js';
9
-
10
- function getPG(param) {
11
- const {
12
- user, password, host, port, db, database, name: origin,
13
- } = param || {};
14
- const name = origin || db || database || param || 'client';
15
- if (pgClients[name]) return pgClients[name];
16
-
17
- const dbConfig = {
18
- user: user || config.pg?.user,
19
- password: password || config.pg?.password,
20
- host: host || config.pg?.host,
21
- port: port || config.pg?.port,
22
- database: db || database || config.pg?.db || config.pg?.database,
23
- };
24
-
25
- pgClients[name] = new pg.Pool(dbConfig);
26
- pgClients[name].init = async () => {
27
- await init(pgClients[name]);
28
- };
29
- init(pgClients[name]);
30
- return pgClients[name];
31
- }
32
-
33
- export default getPG;
1
+ import pg from 'pg';
2
+
3
+ const { types } = pg;
4
+ types.setTypeParser(1082, (stringValue) => stringValue);
5
+
6
+ import config from '../../../../config.js';
7
+ import pgClients from '../pgClients.js';
8
+ import init from './init.js';
9
+
10
+ function getPG(param) {
11
+ const {
12
+ user, password, host, port, db, database, name: origin,
13
+ } = param || {};
14
+ const name = origin || db || database || param || 'client';
15
+ if (pgClients[name]) return pgClients[name];
16
+
17
+ const dbConfig = {
18
+ user: user || config.pg?.user,
19
+ password: password || config.pg?.password,
20
+ host: host || config.pg?.host,
21
+ port: port || config.pg?.port,
22
+ database: db || database || config.pg?.db || config.pg?.database,
23
+ };
24
+
25
+ pgClients[name] = new pg.Pool(dbConfig);
26
+ pgClients[name].init = async () => {
27
+ await init(pgClients[name]);
28
+ };
29
+ init(pgClients[name]);
30
+ return pgClients[name];
31
+ }
32
+
33
+ export default getPG;
@@ -1,21 +1,21 @@
1
- import pg from 'pg';
2
-
3
- import config from '../../../config.js';
4
- import init from './funcs/init.js';
5
-
6
- const pgClients = {};
7
- if (config.pg) {
8
- const client = new pg.Pool({
9
- host: config.pg?.host || '127.0.0.1',
10
- port: config.pg?.port || 5432,
11
- database: config.pg?.database || 'postgres',
12
- user: config.pg?.user || 'postgres',
13
- password: config.pg?.password || 'postgres',
14
- });
15
- client.init = async () => {
16
- await init(client);
17
- };
18
- client.init();
19
- pgClients.client = client;
20
- }
21
- export default pgClients;
1
+ import pg from 'pg';
2
+
3
+ import config from '../../../config.js';
4
+ import init from './funcs/init.js';
5
+
6
+ const pgClients = {};
7
+ if (config.pg) {
8
+ const client = new pg.Pool({
9
+ host: config.pg?.host || '127.0.0.1',
10
+ port: config.pg?.port || 5432,
11
+ database: config.pg?.database || 'postgres',
12
+ user: config.pg?.user || 'postgres',
13
+ password: config.pg?.password || 'postgres',
14
+ });
15
+ client.init = async () => {
16
+ await init(client);
17
+ };
18
+ client.init();
19
+ pgClients.client = client;
20
+ }
21
+ export default pgClients;