@ejfdelgado/ejflab-back 1.1.2 → 1.3.0

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 (2) hide show
  1. package/package.json +5 -3
  2. package/srv/PostgresSrv.mjs +64 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ejfdelgado/ejflab-back",
3
- "version": "1.1.2",
3
+ "version": "1.3.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/ejfdelgado/ejflab-back.git"
@@ -8,7 +8,9 @@
8
8
  "description": "",
9
9
  "scripts": {
10
10
  "mylogin": "npm login",
11
- "mypublish": "npm publish --access=public"
11
+ "mypublish": "npm publish --access=public",
12
+ "diff": "unset GTK_PATH && git difftool",
13
+ "clean": "rm -rf node_modules && npm cache clean --force && rm package-lock.json && npm install"
12
14
  },
13
15
  "engines": {
14
16
  "node": ">=14.0.0"
@@ -17,7 +19,7 @@
17
19
  "license": "ISC",
18
20
  "private": false,
19
21
  "dependencies": {
20
- "@ejfdelgado/ejflab-common": "1.2.2",
22
+ "@ejfdelgado/ejflab-common": "1.3.0",
21
23
  "@google-cloud/compute": "^4.7.0",
22
24
  "@google-cloud/firestore": "^7.9.0",
23
25
  "@google-cloud/storage": "^7.11.3",
@@ -1,15 +1,21 @@
1
- import pg from 'pg'
1
+ import pg from "pg";
2
2
  import fs from "fs";
3
- import { MyTemplate } from '@ejfdelgado/ejflab-common/src/MyTemplate.js';
4
- const { Pool } = pg
5
-
3
+ import { MyTemplate } from "@ejfdelgado/ejflab-common/src/MyTemplate.js";
4
+ const { Pool } = pg;
6
5
 
7
6
  export class PostgresSrv {
8
7
  static renderer = new MyTemplate();
9
8
  static pool = null;
10
9
  static {
11
10
  PostgresSrv.renderer.registerFunction("noQuotes", PostgresSrv.noQuotes);
12
- PostgresSrv.renderer.registerFunction("sanitizeText", PostgresSrv.sanitizeText);
11
+ PostgresSrv.renderer.registerFunction(
12
+ "sanitizeNumber",
13
+ PostgresSrv.sanitizeNumber
14
+ );
15
+ PostgresSrv.renderer.registerFunction(
16
+ "sanitizeText",
17
+ PostgresSrv.sanitizeText
18
+ );
13
19
  const types = pg.types;
14
20
  types.setTypeParser(types.builtins.INT8, function (val) {
15
21
  const bigNumber = BigInt(val);
@@ -25,9 +31,26 @@ export class PostgresSrv {
25
31
  }
26
32
  static sanitizeText(val, ...args) {
27
33
  let text = val;
34
+ if ([null, undefined].indexOf(text) >= 0) {
35
+ if (typeof args[0] == "string") {
36
+ return args[0];
37
+ }
38
+ return "";
39
+ }
40
+ text = `${text}`;
28
41
  text = PostgresSrv.noQuotes(text);
29
42
  return text;
30
43
  }
44
+ static sanitizeNumber(val, ...args) {
45
+ let myNumber = parseFloat(val);
46
+ if (isNaN(myNumber)) {
47
+ if (typeof args[0] == "number") {
48
+ return args[0];
49
+ }
50
+ return 0;
51
+ }
52
+ return myNumber;
53
+ }
31
54
  static getConnectionParams() {
32
55
  const host = process.env.POSTGRES_HOST || "postgres";
33
56
  const port = parseInt(process.env.POSTGRES_PORT || "5432");
@@ -39,22 +62,22 @@ export class PostgresSrv {
39
62
  port,
40
63
  database,
41
64
  user,
42
- password
65
+ password,
43
66
  };
44
67
  }
45
68
 
46
69
  static async executeTextInTransaction(sql, model = {}) {
47
70
  const pool = PostgresSrv.getPool();
48
71
  const client = await pool.connect();
49
- await client.query('BEGIN')
72
+ await client.query("BEGIN");
50
73
  try {
51
74
  // Insert into media
52
75
  const result = await PostgresSrv.executeText(sql, model, client);
53
- await client.query('COMMIT');
76
+ await client.query("COMMIT");
54
77
  return result;
55
78
  } catch (e) {
56
- await client.query('ROLLBACK');
57
- throw e
79
+ await client.query("ROLLBACK");
80
+ throw e;
58
81
  } finally {
59
82
  client.release();
60
83
  }
@@ -67,7 +90,7 @@ export class PostgresSrv {
67
90
  if (!client) {
68
91
  const pool = PostgresSrv.getPool();
69
92
  localClient = true;
70
- client = await pool.connect()
93
+ client = await pool.connect();
71
94
  }
72
95
  const result = await client.query(sqlRendered);
73
96
  if (localClient) {
@@ -106,4 +129,34 @@ export class PostgresSrv {
106
129
  const row = result.rows[0];
107
130
  res.status(200).send({ row });
108
131
  }
132
+
133
+ static async simpleTest() {
134
+ const testsNumber = [
135
+ { type: 'sanitizeNumber', in: undefined, expected: 0, def: undefined },
136
+ { type: 'sanitizeNumber', in: null, expected: 0, def: undefined },
137
+ { type: 'sanitizeNumber', in: "", expected: 0, def: undefined },
138
+ { type: 'sanitizeNumber', in: undefined, expected: 1, def: 1 },
139
+ { type: 'sanitizeNumber', in: null, expected: 1, def: 1 },
140
+ { type: 'sanitizeNumber', in: "", expected: 1, def: 1 },
141
+ { type: 'sanitizeNumber', in: "3", expected: 3, def: 1 },
142
+ { type: 'sanitizeNumber', in: "5.26", expected: 5.26, def: 1 },
143
+ //
144
+ { type: 'sanitizeText', in: undefined, expected: "" },
145
+ { type: 'sanitizeText', in: null, expected: "" },
146
+ { type: 'sanitizeText', in: 5, expected: "5" },
147
+ { type: 'sanitizeText', in: undefined, expected: "nothing", def: "nothing" },
148
+ { type: 'sanitizeText', in: null, expected: "nothing", def: "nothing" },
149
+ { type: 'sanitizeText', in: "", expected: "", def: "nothing" },
150
+ ];
151
+ for (let i = 0; i < testsNumber.length; i++) {
152
+ const actual = testsNumber[i];
153
+ const returned = this[actual.type](actual.in, actual.def);
154
+ if (JSON.stringify(returned) != JSON.stringify(actual.expected)) {
155
+ throw new Error(`Test ${i + 1} In: ${actual.in} Actual: ${returned} but expected ${actual.expected}`);
156
+ }
157
+ }
158
+ console.log(`Test passed ${testsNumber.length}!`);
159
+ }
109
160
  }
161
+
162
+ //PostgresSrv.simpleTest();