@opengis/fastify-table 2.0.17 → 2.0.19

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.
@@ -0,0 +1,22 @@
1
+ import config from "../../../config.js";
2
+ const { accessToken = "0NWcGQxKRP8AsRxD" } = config.auth || {};
3
+ /**
4
+ *
5
+ * @summary check user access to logger interface - per admin user type or user group
6
+ * @returns {Object} message, status
7
+ */
8
+ export default function checkUserAccess({ user = {}, token, }) {
9
+ if (token && token === accessToken) {
10
+ return { message: "access granted", status: 200 };
11
+ }
12
+ // console.log(user);
13
+ if (!user.user_type?.includes?.("admin") &&
14
+ !config?.local &&
15
+ !config.auth?.disable) {
16
+ return { message: "access restricted", status: 403 };
17
+ }
18
+ /* if (!['admin', 'superadmin']?.includes(user.user_type) && count === '0') {
19
+ return { message: 'access restricted', status: 403 };
20
+ } */
21
+ return { message: "access granted", status: 200 };
22
+ }
@@ -1,30 +1,14 @@
1
- /* eslint-disable no-console */
2
- const streams = {};
3
- import build from "pino-abstract-transport";
4
1
  import fs from "node:fs";
2
+ import build from "pino-abstract-transport";
5
3
  import labels from "./labels.js";
6
- import timestampWithTimeZone from "./timestampWithTimeZone.js";
7
- import config from "../../../config.js";
8
- const generator = () => () => `${(timestampWithTimeZone().split("T")[0] || "").replace(/:/g, "_")}.log`;
9
- const { dir = "log", interval = "1d", compress = "gzip", // maxFiles = 90, local: teeToStdout,
10
- } = config?.log || {};
4
+ import getRootDir from "./getRootDir.js";
5
+ const dir = getRootDir();
6
+ const streams = {};
11
7
  function createFileStream({ level, status }) {
12
- console.log(dir, level, generator()(), interval, compress);
13
- /* const params = {
14
- maxFiles, // logs to save limit
15
- history: 'history', // history file name
16
- interval, // rotate daily
17
- compress, // compress rotated files
18
- teeToStdout, // debug / logs to stdout
19
- path: `${dir}/${level}`, // absolute path (root directory)
20
- intervalBoundary: true, // true - log name with lower boundary of rotation interval
21
- initialRotation: true, // true - log rotation check on init
22
- // intervalUTC: true, // local tz -> utc
23
- };
24
- return createStream(generator({ interval }), params); */
25
8
  const dt = new Date().toISOString().split("T")[0];
26
9
  const fileName = `${dir}/${level}/${dt}${status ? `_${status}` : ""}.log`;
27
10
  fs.mkdirSync(`${dir}/${level}`, { recursive: true });
11
+ console.log("creating log stream: " + fileName);
28
12
  const stream = fs.createWriteStream(fileName, {
29
13
  encoding: "utf8",
30
14
  flags: "a+",
@@ -8,8 +8,6 @@ import serializers from "./serializers.js";
8
8
  import timestampWithTimeZone from "./timestampWithTimeZone.js";
9
9
  const isServer = process.argv[2];
10
10
  const rclient2 = getRedis({ db: 2 });
11
- if (!config.log)
12
- config.log = {};
13
11
  const level = config.log?.level || process.env.PINO_LOG_LEVEL || "info";
14
12
  console.log(`log level: ${level}`);
15
13
  const options = {
@@ -0,0 +1,22 @@
1
+ /* eslint-disable no-console */
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import config from "../../../config.js";
5
+ let logDir = null;
6
+ export default function getRootDir() {
7
+ // absolute / relative path
8
+ if (config.logDir) {
9
+ console.log(`logging to: ${config.logDir}`);
10
+ return config.logDir;
11
+ }
12
+ if (logDir) {
13
+ console.log(`logging to: ${logDir}`);
14
+ return logDir;
15
+ }
16
+ const file = ["config.json", "/data/local/config.json"].find((el) => fs.existsSync(el) ? el : null);
17
+ // .env / config.json => process.cwd()
18
+ const root = file && file.startsWith("/data/local") ? "/data/local" : process.cwd();
19
+ logDir = path.join(root, config.log?.dir || "log");
20
+ console.log(`logging to: ${logDir}`);
21
+ return logDir;
22
+ }
@@ -7,8 +7,8 @@ export default function getTemplateDir(type) {
7
7
  const arr = Array.isArray(type)
8
8
  ? type.map((el) => getTemplatePath(el)).flat()
9
9
  : getTemplatePath(type);
10
- if (!loadTemplate[type]) {
11
- loadTemplate[type] = arr.filter((el) => fs.existsSync(el[1]));
10
+ if (!loadTemplate[type.toString()]) {
11
+ loadTemplate[type.toString()] = arr.filter((el) => fs.existsSync(el[1]));
12
12
  }
13
- return loadTemplate[type];
13
+ return loadTemplate[type.toString()];
14
14
  }
@@ -1,5 +1,6 @@
1
1
  import { handlebars } from "../../../../helpers/index.js";
2
2
  import getTemplate from "../getTemplate.js";
3
+ import getTemplates from "../getTemplates.js";
3
4
  import getSelectVal from "./getSelectVal.js";
4
5
  import pgClients from "../../../pg/pgClients.js";
5
6
  export default async function metaFormat({ rows: original, table, cls = {}, htmls = {}, sufix = true, reassign = true, }, pg = pgClients.client) {
@@ -11,11 +12,21 @@ export default async function metaFormat({ rows: original, table, cls = {}, html
11
12
  name: el,
12
13
  data: loadTable?.meta?.cls[el],
13
14
  }));
15
+ const list = getTemplates(["cls", "select"]).map((el) => el[0]);
16
+ const columnNameAsCls = Object.keys(original?.[0] || {})
17
+ .filter((key) => list.includes(`${table}.${key}`) || list.includes(key))
18
+ .map((key) => ({
19
+ name: key,
20
+ data: list.find((clsName) => [key, `${table}.${key}`].includes(clsName)),
21
+ }));
14
22
  const htmlCols = Object.keys(htmls || {})
15
23
  .map((key) => ({ name: key, html: htmls[key] }))
16
24
  .concat(loadTable?.columns?.filter?.((e) => e.name && e.html && e.format && ["html", "slot"].includes(e.format)) || []);
17
25
  if (!original?.length ||
18
- (!selectCols?.length && !metaCls?.length && !htmlCols?.length))
26
+ (!selectCols?.length &&
27
+ !metaCls?.length &&
28
+ !columnNameAsCls.length &&
29
+ !htmlCols?.length))
19
30
  return original;
20
31
  const rows = reassign ? original : JSON.parse(JSON.stringify(original));
21
32
  // html && slot (vue component) format
@@ -31,7 +42,10 @@ export default async function metaFormat({ rows: original, table, cls = {}, html
31
42
  }));
32
43
  }));
33
44
  // cls & select format
34
- await Promise.all(selectCols.concat(metaCls)?.map(async (attr) => {
45
+ await Promise.all(selectCols
46
+ .concat(metaCls)
47
+ .concat(columnNameAsCls)
48
+ ?.map(async (attr) => {
35
49
  const values = [
36
50
  ...new Set(rows?.map((el) => el[attr.name]).flat()),
37
51
  ].filter((el) => (typeof el === "boolean" ? true : el));
@@ -52,9 +66,7 @@ export default async function metaFormat({ rows: original, table, cls = {}, html
52
66
  }
53
67
  else {
54
68
  Object.assign(el, {
55
- [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: val.color
56
- ? val
57
- : val.text || val,
69
+ [val?.color ? `${attr.name}_data` : `${attr.name}_text`]: val.color ? val : val.text || val,
58
70
  });
59
71
  }
60
72
  });
@@ -2,8 +2,8 @@ import path from "node:path";
2
2
  import { lstat, readdir, readFile } from "node:fs/promises";
3
3
  import { createReadStream, existsSync } from "node:fs";
4
4
  import readline from "node:readline";
5
- import checkUserAccess from "./utils/checkUserAccess.js";
6
- import getRootDir from "./utils/getRootDir.js";
5
+ import checkUserAccess from "../../../plugins/logger/checkUserAccess.js";
6
+ import getRootDir from "../../../plugins/logger/getRootDir.js";
7
7
  const limit = 200000;
8
8
  const rootDir = getRootDir();
9
9
  /**
@@ -18,6 +18,9 @@ export default async function filterAPI(req, reply, iscalled) {
18
18
  if (!table) {
19
19
  return { status: 404, message: "not found" };
20
20
  }
21
+ if (!pg.pk?.[table]) {
22
+ return { status: 404, message: "table not found" };
23
+ }
21
24
  const sqlTable = sql
22
25
  ?.filter?.((el) => !el?.disabled && el?.sql?.replace)
23
26
  ?.map?.((el, i) => ` left join lateral (${el.sql.replace("{{uid}}", user?.uid)}) ${el.name || `t${i}`} on 1=1 `)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengis/fastify-table",
3
- "version": "2.0.17",
3
+ "version": "2.0.19",
4
4
  "type": "module",
5
5
  "description": "core-plugins",
6
6
  "keywords": [