@aceitadev/adatabase 0.4.0 → 0.5.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.
- package/dist/Logger.d.ts +9 -0
- package/dist/Logger.js +44 -0
- package/dist/SchemaManager.d.ts +1 -2
- package/dist/SchemaManager.js +8 -7
- package/dist/Security.d.ts +4 -0
- package/dist/Security.js +38 -0
- package/dist/util.d.ts +1 -0
- package/dist/util.js +6 -0
- package/package.json +1 -1
package/dist/Logger.d.ts
ADDED
package/dist/Logger.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
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(`[33m${formattedMessage}[0m`); // Yellow
|
|
11
|
+
break;
|
|
12
|
+
case 'error':
|
|
13
|
+
console.error(`[31m${formattedMessage}[0m`); // Red
|
|
14
|
+
break;
|
|
15
|
+
case 'info':
|
|
16
|
+
default:
|
|
17
|
+
console.log(`[36m${formattedMessage}[0m`); // Cyan
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
static schemaLog(message, details) {
|
|
22
|
+
const PREFIX = `[32m[aDatabase][0m`;
|
|
23
|
+
if (details && details.table && !details.column) {
|
|
24
|
+
if (details.status === 'started') {
|
|
25
|
+
process.stdout.write(`${PREFIX} Synchronizing table '${details.table}'...`);
|
|
26
|
+
}
|
|
27
|
+
else if (details.status === 'created') {
|
|
28
|
+
process.stdout.write(` Done.
|
|
29
|
+
`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else if (details && details.table && details.column) {
|
|
33
|
+
if (details.status === 'added') {
|
|
34
|
+
console.log(`
|
|
35
|
+
${PREFIX} Column '${details.column}' added to table '${details.table}'.`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
console.log(`${PREFIX} ${message}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.Logger = Logger;
|
|
44
|
+
Logger.PREFIX = "[aDatabase]";
|
package/dist/SchemaManager.d.ts
CHANGED
package/dist/SchemaManager.js
CHANGED
|
@@ -14,21 +14,22 @@ 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
|
|
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* () {
|
|
27
|
+
Logger_1.Logger.schemaLog("Synchronizing database schema...");
|
|
27
28
|
for (const model of this.models) {
|
|
28
29
|
const table = (0, Table_1.getTableName)(model);
|
|
29
30
|
if (!table)
|
|
30
31
|
continue;
|
|
31
|
-
|
|
32
|
+
Logger_1.Logger.schemaLog("", { table: table, status: "started" });
|
|
32
33
|
const { columns, indexes } = this.getSchemaFromModel(model);
|
|
33
34
|
const existing = yield this.getExistingColumns(table);
|
|
34
35
|
if (Object.keys(existing).length === 0) {
|
|
@@ -52,7 +53,7 @@ class SchemaManager {
|
|
|
52
53
|
const conn = yield (0, Database_1.getConnection)();
|
|
53
54
|
try {
|
|
54
55
|
yield (0, Database_1.execute)(sql, [], conn);
|
|
55
|
-
|
|
56
|
+
Logger_1.Logger.schemaLog("", { table: table, status: "created" });
|
|
56
57
|
}
|
|
57
58
|
finally {
|
|
58
59
|
conn.release();
|
|
@@ -80,7 +81,7 @@ class SchemaManager {
|
|
|
80
81
|
const conn = yield (0, Database_1.getConnection)();
|
|
81
82
|
try {
|
|
82
83
|
yield (0, Database_1.execute)(sql, [], conn);
|
|
83
|
-
|
|
84
|
+
Logger_1.Logger.schemaLog("", { table: table, column: col, status: "added" });
|
|
84
85
|
}
|
|
85
86
|
finally {
|
|
86
87
|
conn.release();
|
|
@@ -96,7 +97,7 @@ class SchemaManager {
|
|
|
96
97
|
const nullableMeta = (0, Nullable_1.getNullableMeta)(model);
|
|
97
98
|
const adapter = (0, Database_1.getAdapter)();
|
|
98
99
|
if (!colMeta) {
|
|
99
|
-
|
|
100
|
+
Logger_1.Logger.log(`No @Column or @Id decorators found on model ${model.name}.`, 'warn');
|
|
100
101
|
return { columns, indexes };
|
|
101
102
|
}
|
|
102
103
|
for (const [prop, opts] of colMeta.entries()) {
|
|
@@ -116,7 +117,7 @@ class SchemaManager {
|
|
|
116
117
|
}
|
|
117
118
|
const type = opts.type;
|
|
118
119
|
if (!type) {
|
|
119
|
-
|
|
120
|
+
Logger_1.Logger.log(`Type not provided for property '${prop}' on model '${model.name}'.`, 'error');
|
|
120
121
|
continue;
|
|
121
122
|
}
|
|
122
123
|
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';
|
package/dist/Security.js
ADDED
|
@@ -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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aceitadev/adatabase",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
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",
|