@aceitadev/adatabase 0.4.0 → 0.4.5

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,9 @@
1
+ export declare class Logger {
2
+ private static readonly PREFIX;
3
+ static log(message: string, level?: 'info' | 'warn' | 'error'): void;
4
+ static schemaLog(message: string, details?: {
5
+ table?: string;
6
+ column?: string;
7
+ status?: string;
8
+ }): void;
9
+ }
package/dist/Logger.js ADDED
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Logger = void 0;
4
+ class Logger {
5
+ static log(message, level = 'info') {
6
+ const timestamp = new Date().toISOString();
7
+ let formattedMessage = `${Logger.PREFIX} ${message}`;
8
+ switch (level) {
9
+ case 'warn':
10
+ console.warn(`${formattedMessage}`); // Yellow
11
+ break;
12
+ case 'error':
13
+ console.error(`${formattedMessage}`); // Red
14
+ break;
15
+ case 'info':
16
+ default:
17
+ console.log(`${formattedMessage}`); // Cyan
18
+ break;
19
+ }
20
+ }
21
+ static schemaLog(message, details) {
22
+ if (!details) {
23
+ Logger.log(message);
24
+ return;
25
+ }
26
+ let logOutput = `${Logger.PREFIX} ${message}`;
27
+ if (details.table) {
28
+ logOutput += `
29
+ |`;
30
+ logOutput += `
31
+ |–– Table '${details.table}'`;
32
+ if (details.status) {
33
+ logOutput += ` (${details.status})`;
34
+ }
35
+ }
36
+ if (details.column) {
37
+ logOutput += `
38
+ `;
39
+ logOutput += `
40
+ \_ Column '${details.column}'`;
41
+ if (details.status) {
42
+ logOutput += ` (${details.status})`;
43
+ }
44
+ }
45
+ console.log(`${logOutput}`); // Green for schema logs
46
+ }
47
+ }
48
+ exports.Logger = Logger;
49
+ Logger.PREFIX = "[aDatabase]";
@@ -1,7 +1,6 @@
1
1
  export declare class SchemaManager {
2
2
  private models;
3
- private logger;
4
- constructor(models: Function[], logger?: Console);
3
+ constructor(models: Function[]);
5
4
  migrate(): Promise<void>;
6
5
  private createTable;
7
6
  private updateTable;
@@ -14,13 +14,13 @@ const Table_1 = require("./decorators/Table");
14
14
  const Column_1 = require("./decorators/Column");
15
15
  const Nullable_1 = require("./decorators/Nullable");
16
16
  const Database_1 = require("./Database");
17
+ const Logger_1 = require("./Logger");
17
18
  function camelToSnake(s) {
18
19
  return s.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
19
20
  }
20
21
  class SchemaManager {
21
- constructor(models, logger = console) {
22
+ constructor(models) {
22
23
  this.models = models;
23
- this.logger = logger;
24
24
  }
25
25
  migrate() {
26
26
  return __awaiter(this, void 0, void 0, function* () {
@@ -28,7 +28,7 @@ class SchemaManager {
28
28
  const table = (0, Table_1.getTableName)(model);
29
29
  if (!table)
30
30
  continue;
31
- this.logger.log(`[aMySQL] Syncing model ${model.name} to table ${table}`);
31
+ Logger_1.Logger.schemaLog("Synchronizing database schema...", { table: table, status: "started" });
32
32
  const { columns, indexes } = this.getSchemaFromModel(model);
33
33
  const existing = yield this.getExistingColumns(table);
34
34
  if (Object.keys(existing).length === 0) {
@@ -52,7 +52,7 @@ class SchemaManager {
52
52
  const conn = yield (0, Database_1.getConnection)();
53
53
  try {
54
54
  yield (0, Database_1.execute)(sql, [], conn);
55
- this.logger.log(`[aMySQL] Created table ${table}`);
55
+ Logger_1.Logger.schemaLog("Table created", { table: table, status: "created" });
56
56
  }
57
57
  finally {
58
58
  conn.release();
@@ -80,7 +80,7 @@ class SchemaManager {
80
80
  const conn = yield (0, Database_1.getConnection)();
81
81
  try {
82
82
  yield (0, Database_1.execute)(sql, [], conn);
83
- this.logger.log(`[aMySQL] Added column ${col} to ${table}`);
83
+ Logger_1.Logger.schemaLog("Column added", { table: table, column: col, status: "added" });
84
84
  }
85
85
  finally {
86
86
  conn.release();
@@ -96,7 +96,7 @@ class SchemaManager {
96
96
  const nullableMeta = (0, Nullable_1.getNullableMeta)(model);
97
97
  const adapter = (0, Database_1.getAdapter)();
98
98
  if (!colMeta) {
99
- this.logger.warn(`[aMySQL] No @Column or @Id decorators found on model ${model.name}.`);
99
+ Logger_1.Logger.log(`No @Column or @Id decorators found on model ${model.name}.`, 'warn');
100
100
  return { columns, indexes };
101
101
  }
102
102
  for (const [prop, opts] of colMeta.entries()) {
@@ -116,7 +116,7 @@ class SchemaManager {
116
116
  }
117
117
  const type = opts.type;
118
118
  if (!type) {
119
- this.logger.error(`[aMySQL] Type not provided for property '${prop}' on model '${model.name}'.`);
119
+ Logger_1.Logger.log(`Type not provided for property '${prop}' on model '${model.name}'.`, 'error');
120
120
  continue;
121
121
  }
122
122
  let sqlType = this.getSqlTypeForClass(type);
@@ -0,0 +1,4 @@
1
+ export declare function validateOperator(operator: string): string;
2
+ export declare function getAndValidateColumnName(prop: string, meta: Map<string, any>, modelName: string): string;
3
+ export declare function sanitizeIntValue(value: any): number;
4
+ export declare function validateDirectionValue(direction: string): 'ASC' | 'DESC';
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateOperator = validateOperator;
4
+ exports.getAndValidateColumnName = getAndValidateColumnName;
5
+ exports.sanitizeIntValue = sanitizeIntValue;
6
+ exports.validateDirectionValue = validateDirectionValue;
7
+ const PersistenceException_1 = require("./PersistenceException");
8
+ const util_1 = require("./util");
9
+ const ALLOWED_OPERATORS = [
10
+ '=', '!=', '<>', '>', '<', '>=', '<=', 'LIKE', 'IN', 'IS NULL', 'IS NOT NULL', 'OR'
11
+ ];
12
+ function validateOperator(operator) {
13
+ if (!ALLOWED_OPERATORS.includes(operator.toUpperCase())) {
14
+ throw new PersistenceException_1.PersistenceException(`Invalid operator used: ${operator}`, null);
15
+ }
16
+ return operator;
17
+ }
18
+ function getAndValidateColumnName(prop, meta, modelName) {
19
+ if (!meta.has(prop)) {
20
+ throw new PersistenceException_1.PersistenceException(`Property '${prop}' is not a mapped column on model '${modelName}'. It cannot be used in queries.`, null);
21
+ }
22
+ const opts = meta.get(prop);
23
+ return (opts === null || opts === void 0 ? void 0 : opts.name) ? opts.name : (0, util_1.camelToSnake)(prop);
24
+ }
25
+ function sanitizeIntValue(value) {
26
+ const intValue = parseInt(String(value), 10);
27
+ if (isNaN(intValue) || intValue < 0) {
28
+ throw new PersistenceException_1.PersistenceException(`Invalid value for LIMIT or OFFSET: ${value}`, null);
29
+ }
30
+ return intValue;
31
+ }
32
+ function validateDirectionValue(direction) {
33
+ const upperDir = direction.toUpperCase();
34
+ if (upperDir !== 'ASC' && upperDir !== 'DESC') {
35
+ throw new PersistenceException_1.PersistenceException(`Invalid ORDER BY direction: ${direction}`, null);
36
+ }
37
+ return upperDir;
38
+ }
package/dist/util.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function camelToSnake(s: string): string;
package/dist/util.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.camelToSnake = camelToSnake;
4
+ function camelToSnake(s) {
5
+ return s.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aceitadev/adatabase",
3
- "version": "0.4.0",
3
+ "version": "0.4.5",
4
4
  "description": "Uma biblioteca para facilitar a interação com bancos de dados MySQL e PostgreSQL em projetos TypeScript/Node.js.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",