emilsoftware-utilities 1.3.7 → 1.3.9

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,31 @@
1
+ import { Logger } from "./logger";
2
+ import { Options } from "es-node-firebird";
3
+ export declare abstract class DatabaseUpdater {
4
+ protected static options: Options;
5
+ protected static logger: Logger;
6
+ /**
7
+ * Checks if a column exists in a specific table.
8
+ * @param options Database connection options.
9
+ * @param table Table name.
10
+ * @param column Column name.
11
+ * @returns True if the column exists, false otherwise.
12
+ */
13
+ protected static columnExists(options: Options, table: string, column: string): Promise<boolean>;
14
+ /**
15
+ * Retrieves the current database version from the PARAMETRI table.
16
+ * @param options Database connection options.
17
+ * @returns The current database version or null if not found.
18
+ */
19
+ protected static getDatabaseVersion(options: Options): Promise<string | null>;
20
+ /**
21
+ * Updates the database version in the PARAMETRI table.
22
+ * @param options Database connection options.
23
+ * @param version The new database version.
24
+ */
25
+ protected static setDatabaseVersion(options: Options, version: string): Promise<void>;
26
+ /**
27
+ * Ensures the PARAMETRI table exists and initializes it if necessary.
28
+ * @param options Database connection options.
29
+ */
30
+ protected static createParametersTable(options: Options): Promise<void>;
31
+ }
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.DatabaseUpdater = void 0;
40
+ var orm_1 = require("./orm");
41
+ var logger_1 = require("./logger");
42
+ var DatabaseUpdater = /** @class */ (function () {
43
+ function DatabaseUpdater() {
44
+ }
45
+ //#endregion
46
+ //#region Utility Methods
47
+ /**
48
+ * Checks if a column exists in a specific table.
49
+ * @param options Database connection options.
50
+ * @param table Table name.
51
+ * @param column Column name.
52
+ * @returns True if the column exists, false otherwise.
53
+ */
54
+ DatabaseUpdater.columnExists = function (options, table, column) {
55
+ return __awaiter(this, void 0, void 0, function () {
56
+ var query, result;
57
+ return __generator(this, function (_a) {
58
+ switch (_a.label) {
59
+ case 0:
60
+ query = "\n SELECT 1 \n FROM RDB$RELATION_FIELDS \n WHERE RDB$RELATION_NAME = ? \n AND RDB$FIELD_NAME = ?";
61
+ return [4 /*yield*/, orm_1.Orm.query(options, query, [
62
+ table.toUpperCase(),
63
+ column.toUpperCase(),
64
+ ])];
65
+ case 1:
66
+ result = _a.sent();
67
+ return [2 /*return*/, result.length > 0];
68
+ }
69
+ });
70
+ });
71
+ };
72
+ /**
73
+ * Retrieves the current database version from the PARAMETRI table.
74
+ * @param options Database connection options.
75
+ * @returns The current database version or null if not found.
76
+ */
77
+ DatabaseUpdater.getDatabaseVersion = function (options) {
78
+ return __awaiter(this, void 0, void 0, function () {
79
+ var parameters;
80
+ return __generator(this, function (_a) {
81
+ switch (_a.label) {
82
+ case 0: return [4 /*yield*/, orm_1.Orm.query(options, "SELECT CODPAR, DESPAR FROM PARAMETRI WHERE CODPAR = ?", ["VersioneDB"])];
83
+ case 1:
84
+ parameters = (_a.sent());
85
+ return [2 /*return*/, parameters.length > 0 ? parameters[0].DESPAR : null];
86
+ }
87
+ });
88
+ });
89
+ };
90
+ /**
91
+ * Updates the database version in the PARAMETRI table.
92
+ * @param options Database connection options.
93
+ * @param version The new database version.
94
+ */
95
+ DatabaseUpdater.setDatabaseVersion = function (options, version) {
96
+ return __awaiter(this, void 0, void 0, function () {
97
+ return __generator(this, function (_a) {
98
+ switch (_a.label) {
99
+ case 0: return [4 /*yield*/, orm_1.Orm.query(options, "UPDATE PARAMETRI SET DESPAR = ? WHERE CODPAR = ?", [
100
+ version,
101
+ "VersioneDB",
102
+ ])];
103
+ case 1:
104
+ _a.sent();
105
+ return [2 /*return*/];
106
+ }
107
+ });
108
+ });
109
+ };
110
+ //#endregion
111
+ //#region Initialization Methods
112
+ /**
113
+ * Ensures the PARAMETRI table exists and initializes it if necessary.
114
+ * @param options Database connection options.
115
+ */
116
+ DatabaseUpdater.createParametersTable = function (options) {
117
+ return __awaiter(this, void 0, void 0, function () {
118
+ var columnAlreadyExists, createTableQuery, error_1, versioneDb;
119
+ return __generator(this, function (_a) {
120
+ switch (_a.label) {
121
+ case 0:
122
+ _a.trys.push([0, 6, , 7]);
123
+ return [4 /*yield*/, this.columnExists(options, "PARAMETRI", "CODPAR")];
124
+ case 1:
125
+ columnAlreadyExists = _a.sent();
126
+ if (columnAlreadyExists)
127
+ return [2 /*return*/];
128
+ createTableQuery = "\n CREATE TABLE PARAMETRI (\n CODPAR VARCHAR(15) NOT NULL,\n DESPAR VARCHAR(255),\n NOTE BLOB SUB_TYPE 1 SEGMENT SIZE 80,\n GRUPPO VARCHAR(20)\n );";
129
+ return [4 /*yield*/, orm_1.Orm.query(options, createTableQuery)];
130
+ case 2:
131
+ _a.sent();
132
+ return [4 /*yield*/, orm_1.Orm.query(options, "ALTER TABLE PARAMETRI ADD CONSTRAINT PK_PARAMETRI PRIMARY KEY (CODPAR);")];
133
+ case 3:
134
+ _a.sent();
135
+ return [4 /*yield*/, orm_1.Orm.query(options, "GRANT ALL ON PARAMETRI TO PUBLIC;")];
136
+ case 4:
137
+ _a.sent();
138
+ return [4 /*yield*/, orm_1.Orm.query(options, "GRANT SELECT ON PARAMETRI TO TABX;")];
139
+ case 5:
140
+ _a.sent();
141
+ return [3 /*break*/, 7];
142
+ case 6:
143
+ error_1 = _a.sent();
144
+ this.logger.error("Error creating table PARAMETRI:", error_1);
145
+ return [3 /*break*/, 7];
146
+ case 7: return [4 /*yield*/, this.getDatabaseVersion(options)];
147
+ case 8:
148
+ versioneDb = _a.sent();
149
+ if (versioneDb !== null && versioneDb !== undefined)
150
+ return [2 /*return*/];
151
+ return [4 /*yield*/, orm_1.Orm.query(options, "INSERT INTO PARAMETRI (CODPAR, DESPAR, NOTE, GRUPPO) VALUES (?,?,?,?)", ["VersioneDB", "0.0a", "versione", null])];
152
+ case 9:
153
+ _a.sent();
154
+ return [2 /*return*/];
155
+ }
156
+ });
157
+ });
158
+ };
159
+ DatabaseUpdater.logger = new logger_1.Logger(DatabaseUpdater.name);
160
+ return DatabaseUpdater;
161
+ }());
162
+ exports.DatabaseUpdater = DatabaseUpdater;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import autobind from "./autobind";
2
+ import { DatabaseUpdater } from "./DatabaseUpdater";
2
3
  import logExecutionTime from "./log-execution-time";
3
4
  import { Logger, LogLevels } from "./logger";
4
5
  import { Orm } from "./orm";
5
6
  import { Utilities } from "./utilities";
6
- export { autobind, logExecutionTime, Logger, LogLevels, Orm, Utilities };
7
+ export { autobind, logExecutionTime, Logger, LogLevels, Orm, Utilities, DatabaseUpdater };
package/dist/index.js CHANGED
@@ -3,9 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Utilities = exports.Orm = exports.LogLevels = exports.Logger = exports.logExecutionTime = exports.autobind = void 0;
6
+ exports.DatabaseUpdater = exports.Utilities = exports.Orm = exports.LogLevels = exports.Logger = exports.logExecutionTime = exports.autobind = void 0;
7
7
  var autobind_1 = __importDefault(require("./autobind"));
8
8
  exports.autobind = autobind_1.default;
9
+ var DatabaseUpdater_1 = require("./DatabaseUpdater");
10
+ Object.defineProperty(exports, "DatabaseUpdater", { enumerable: true, get: function () { return DatabaseUpdater_1.DatabaseUpdater; } });
9
11
  var log_execution_time_1 = __importDefault(require("./log-execution-time"));
10
12
  exports.logExecutionTime = log_execution_time_1.default;
11
13
  var logger_1 = require("./logger");
package/dist/logger.d.ts CHANGED
@@ -2,6 +2,7 @@ import winston from "winston";
2
2
  export declare enum LogLevels {
3
3
  INFO = "INFO",
4
4
  ERROR = "ERROR",
5
+ WARNING = "WARNING",
5
6
  DEBUG = "DEBUG",
6
7
  LOG = "LOG",
7
8
  DATABASE = "DATABASE"
@@ -24,6 +25,7 @@ export declare class Logger {
24
25
  info(...data: Object[]): void;
25
26
  dbLog(...data: Object[]): void;
26
27
  debug(...data: Object[]): void;
28
+ warning(...data: Object[]): void;
27
29
  log(...data: Object[]): void;
28
30
  error(...data: Object[]): void;
29
31
  private print;
package/dist/logger.js CHANGED
@@ -97,11 +97,12 @@ exports.Logger = exports.LogLevels = void 0;
97
97
  var winston_1 = __importDefault(require("winston"));
98
98
  var path = __importStar(require("path"));
99
99
  var fs_1 = require("fs");
100
- var chalk_1 = __importDefault(require("chalk"));
100
+ var colorette_1 = require("colorette");
101
101
  var LogLevels;
102
102
  (function (LogLevels) {
103
103
  LogLevels["INFO"] = "INFO";
104
104
  LogLevels["ERROR"] = "ERROR";
105
+ LogLevels["WARNING"] = "WARNING";
105
106
  LogLevels["DEBUG"] = "DEBUG";
106
107
  LogLevels["LOG"] = "LOG";
107
108
  LogLevels["DATABASE"] = "DATABASE";
@@ -130,6 +131,7 @@ var Logger = /** @class */ (function () {
130
131
  levels: {
131
132
  error: 1,
132
133
  warn: 2,
134
+ warning: 2,
133
135
  info: 3,
134
136
  http: 4,
135
137
  verbose: 5,
@@ -142,6 +144,7 @@ var Logger = /** @class */ (function () {
142
144
  winston_1.default.addColors({
143
145
  database: "green",
144
146
  error: "red",
147
+ warning: "yellow",
145
148
  info: "blue",
146
149
  debug: "magenta",
147
150
  log: "cyan",
@@ -213,6 +216,13 @@ var Logger = /** @class */ (function () {
213
216
  }
214
217
  this.print.apply(this, __spreadArray([LogLevels.DEBUG], data, false));
215
218
  };
219
+ Logger.prototype.warning = function () {
220
+ var data = [];
221
+ for (var _i = 0; _i < arguments.length; _i++) {
222
+ data[_i] = arguments[_i];
223
+ }
224
+ this.print.apply(this, __spreadArray([LogLevels.WARNING], data, false));
225
+ };
216
226
  Logger.prototype.log = function () {
217
227
  var data = [];
218
228
  for (var _i = 0; _i < arguments.length; _i++) {
@@ -247,19 +257,19 @@ var Logger = /** @class */ (function () {
247
257
  // Log to console with colors
248
258
  switch (level) {
249
259
  case LogLevels.INFO:
250
- console.info(chalk_1.default.blue("[INFO][".concat(now, "][").concat(fileName, "]")), logEntry.message);
260
+ console.info((0, colorette_1.blue)("[INFO][".concat(now, "][").concat(fileName, "]")), logEntry.message);
251
261
  break;
252
262
  case LogLevels.ERROR:
253
- console.error(chalk_1.default.red("[ERROR][".concat(now, "][").concat(fileName, "]")), logEntry.message);
263
+ console.error((0, colorette_1.red)("[ERROR][".concat(now, "][").concat(fileName, "]")), logEntry.message);
254
264
  break;
255
265
  case LogLevels.DEBUG:
256
- console.debug(chalk_1.default.magenta("[DEBUG][".concat(now, "][").concat(fileName, "]")), logEntry.message);
266
+ console.debug((0, colorette_1.magenta)("[DEBUG][".concat(now, "][").concat(fileName, "]")), logEntry.message);
257
267
  break;
258
268
  case LogLevels.LOG:
259
- console.log(chalk_1.default.cyan("[LOG][".concat(now, "][").concat(fileName, "]")), logEntry.message);
269
+ console.log((0, colorette_1.cyan)("[LOG][".concat(now, "][").concat(fileName, "]")), logEntry.message);
260
270
  break;
261
271
  case LogLevels.DATABASE:
262
- console.log(chalk_1.default.green("[DATABASE][".concat(now, "][").concat(fileName, "]")), logEntry.message);
272
+ console.log((0, colorette_1.green)("[DATABASE][".concat(now, "][").concat(fileName, "]")), logEntry.message);
263
273
  break;
264
274
  }
265
275
  // Log to file
package/package.json CHANGED
@@ -1,36 +1,38 @@
1
- {
2
- "name": "emilsoftware-utilities",
3
- "version": "1.3.7",
4
- "description": "Utilities for EmilSoftware",
5
- "main": "dist/index.js",
6
- "module": "dist/index.js",
7
- "types": "dist/index.d.ts",
8
- "scripts": {
9
- "test": "echo \"Error: no test specified\" && exit 1",
10
- "build": "tsc",
11
- "prepublishOnly": "npm run build"
12
- },
13
- "repository": {
14
- "type": "git",
15
- "url": "git+https://github.com/mttdev382/emilsoftware-utilities.git"
16
- },
17
- "author": "",
18
- "license": "ISC",
19
- "bugs": {
20
- "url": "https://github.com/mttdev382/emilsoftware-utilities/issues"
21
- },
22
- "homepage": "https://github.com/mttdev382/emilsoftware-utilities#readme",
23
- "dependencies": {
24
- "chalk": "^5.3.0",
25
- "es-node-firebird": "^1.2.6",
26
- "winston": "^3.11.0"
27
- },
28
- "devDependencies": {
29
- "@types/express": "^4.17.21",
30
- "@types/node": "^20.10.5",
31
- "typescript": "^5.3.3"
32
- },
33
- "files": [
34
- "dist"
35
- ]
36
- }
1
+ {
2
+ "name": "emilsoftware-utilities",
3
+ "version": "1.3.9",
4
+ "description": "Utilities for EmilSoftware",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "scripts": {
9
+ "release": "standard-version",
10
+ "test": "echo \"Error: no test specified\" && exit 1",
11
+ "build": "tsc",
12
+ "prepublishOnly": "npm run build"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/mttdev382/emilsoftware-utilities.git"
17
+ },
18
+ "author": "",
19
+ "license": "ISC",
20
+ "bugs": {
21
+ "url": "https://github.com/mttdev382/emilsoftware-utilities/issues"
22
+ },
23
+ "homepage": "https://github.com/mttdev382/emilsoftware-utilities#readme",
24
+ "dependencies": {
25
+ "colorette": "^2.0.20",
26
+ "es-node-firebird": "^1.2.6",
27
+ "winston": "^3.11.0"
28
+ },
29
+ "devDependencies": {
30
+ "@types/express": "^4.17.21",
31
+ "@types/node": "^20.10.5",
32
+ "standard-version": "^9.5.0",
33
+ "typescript": "^5.3.3"
34
+ },
35
+ "files": [
36
+ "dist"
37
+ ]
38
+ }