@aceitadev/adatabase 0.1.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 (38) hide show
  1. package/README.md +66 -0
  2. package/dist/ActiveRecord.d.ts +12 -0
  3. package/dist/ActiveRecord.js +115 -0
  4. package/dist/ColumnAdapter.d.ts +8 -0
  5. package/dist/ColumnAdapter.js +12 -0
  6. package/dist/Database.d.ts +12 -0
  7. package/dist/Database.js +58 -0
  8. package/dist/MySQL.d.ts +18 -0
  9. package/dist/MySQL.js +72 -0
  10. package/dist/PersistenceException.d.ts +4 -0
  11. package/dist/PersistenceException.js +16 -0
  12. package/dist/QueryBuilder.d.ts +17 -0
  13. package/dist/QueryBuilder.js +180 -0
  14. package/dist/SchemaManager.d.ts +11 -0
  15. package/dist/SchemaManager.js +200 -0
  16. package/dist/adapters/IDatabaseAdapter.d.ts +12 -0
  17. package/dist/adapters/IDatabaseAdapter.js +2 -0
  18. package/dist/adapters/MySQLAdapter.d.ts +14 -0
  19. package/dist/adapters/MySQLAdapter.js +74 -0
  20. package/dist/adapters/PostgresAdapter.d.ts +16 -0
  21. package/dist/adapters/PostgresAdapter.js +85 -0
  22. package/dist/decorators/BelongsTo.d.ts +7 -0
  23. package/dist/decorators/BelongsTo.js +17 -0
  24. package/dist/decorators/Column.d.ts +12 -0
  25. package/dist/decorators/Column.js +17 -0
  26. package/dist/decorators/HasMany.d.ts +7 -0
  27. package/dist/decorators/HasMany.js +17 -0
  28. package/dist/decorators/HasOne.d.ts +7 -0
  29. package/dist/decorators/HasOne.js +17 -0
  30. package/dist/decorators/Id.d.ts +1 -0
  31. package/dist/decorators/Id.js +12 -0
  32. package/dist/decorators/Nullable.d.ts +2 -0
  33. package/dist/decorators/Nullable.js +16 -0
  34. package/dist/decorators/Table.d.ts +2 -0
  35. package/dist/decorators/Table.js +13 -0
  36. package/dist/index.d.ts +13 -0
  37. package/dist/index.js +29 -0
  38. package/package.json +41 -0
@@ -0,0 +1,200 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.SchemaManager = void 0;
13
+ const Table_1 = require("./decorators/Table");
14
+ const Column_1 = require("./decorators/Column");
15
+ const Nullable_1 = require("./decorators/Nullable");
16
+ const Database_1 = require("./Database");
17
+ function camelToSnake(s) {
18
+ return s.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
19
+ }
20
+ class SchemaManager {
21
+ constructor(models, logger = console) {
22
+ this.models = models;
23
+ this.logger = logger;
24
+ }
25
+ migrate() {
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ for (const model of this.models) {
28
+ const table = (0, Table_1.getTableName)(model);
29
+ if (!table)
30
+ continue;
31
+ this.logger.log(`[aMySQL] Syncing model ${model.name} to table ${table}`);
32
+ const { columns, indexes } = this.getSchemaFromModel(model);
33
+ const existing = yield this.getExistingColumns(table);
34
+ if (Object.keys(existing).length === 0) {
35
+ yield this.createTable(table, columns, indexes);
36
+ }
37
+ else {
38
+ yield this.updateTable(table, columns, indexes, existing);
39
+ }
40
+ }
41
+ });
42
+ }
43
+ createTable(table, columns, indexes) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ const adapter = (0, Database_1.getAdapter)();
46
+ const colsSql = Object.entries(columns).map(([k, v]) => `\`${k}\` ${v}`).join(", ");
47
+ let indexSql = "";
48
+ if (adapter.type === 'mysql' && indexes.length > 0) {
49
+ indexSql = ", " + indexes.map(col => `INDEX
50
+ ${col}
51
+ (
52
+ ${col}
53
+ )`).join(", ");
54
+ }
55
+ const sql = `CREATE TABLE
56
+ ${table}
57
+ (${colsSql}${indexSql});`;
58
+ const conn = yield (0, Database_1.getConnection)();
59
+ try {
60
+ yield (0, Database_1.execute)(sql, [], conn);
61
+ this.logger.log(`[aMySQL] Created table ${table}`);
62
+ }
63
+ finally {
64
+ conn.release();
65
+ }
66
+ if (adapter.type === 'postgres' && indexes.length > 0) {
67
+ for (const col of indexes) {
68
+ const indexName = `idx_${table}_${col}`;
69
+ const indexCreationSql = `CREATE INDEX
70
+ ${indexName}
71
+ ON
72
+ ${table}
73
+ (
74
+ ${col}
75
+ );`;
76
+ const indexConn = yield (0, Database_1.getConnection)();
77
+ try {
78
+ yield (0, Database_1.execute)(indexCreationSql, [], indexConn);
79
+ }
80
+ finally {
81
+ indexConn.release();
82
+ }
83
+ }
84
+ }
85
+ });
86
+ }
87
+ updateTable(table, desired, indexes, existing) {
88
+ return __awaiter(this, void 0, void 0, function* () {
89
+ for (const [col, type] of Object.entries(desired)) {
90
+ if (!existing.hasOwnProperty(col)) {
91
+ const sql = `ALTER TABLE
92
+ ${table}
93
+ ADD COLUMN
94
+ ${col}
95
+ ${type};`;
96
+ const conn = yield (0, Database_1.getConnection)();
97
+ try {
98
+ yield (0, Database_1.execute)(sql, [], conn);
99
+ this.logger.log(`[aMySQL] Added column ${col} to ${table}`);
100
+ }
101
+ finally {
102
+ conn.release();
103
+ }
104
+ }
105
+ }
106
+ });
107
+ }
108
+ getSchemaFromModel(model) {
109
+ const columns = {};
110
+ const indexes = [];
111
+ const colMeta = (0, Column_1.getColumnMeta)(model);
112
+ const nullableMeta = (0, Nullable_1.getNullableMeta)(model);
113
+ const adapter = (0, Database_1.getAdapter)();
114
+ if (!colMeta) {
115
+ this.logger.warn(`[aMySQL] No @Column or @Id decorators found on model ${model.name}.`);
116
+ return { columns, indexes };
117
+ }
118
+ for (const [prop, opts] of colMeta.entries()) {
119
+ const colName = opts && opts.name ? opts.name : camelToSnake(prop);
120
+ if (opts.index) {
121
+ indexes.push(colName);
122
+ }
123
+ if (opts && opts.id) {
124
+ const type = opts.type || Number;
125
+ if (adapter.type === 'postgres') {
126
+ columns[colName] = "SERIAL PRIMARY KEY";
127
+ }
128
+ else {
129
+ columns[colName] = this.getSqlTypeForClass(type) + " PRIMARY KEY AUTO_INCREMENT";
130
+ }
131
+ continue;
132
+ }
133
+ const type = opts.type;
134
+ if (!type) {
135
+ this.logger.error(`[aMySQL] Type not provided for property '${prop}' on model '${model.name}'.`);
136
+ continue;
137
+ }
138
+ let sqlType = this.getSqlTypeForClass(type);
139
+ if (type === String && opts && opts.limit) {
140
+ sqlType = `VARCHAR(${opts.limit})`;
141
+ }
142
+ sqlType += (nullableMeta === null || nullableMeta === void 0 ? void 0 : nullableMeta.get(prop)) ? " NULL" : " NOT NULL";
143
+ if (opts && opts.unique)
144
+ sqlType += " UNIQUE";
145
+ columns[colName] = sqlType;
146
+ }
147
+ return { columns, indexes };
148
+ }
149
+ getSqlTypeForClass(type) {
150
+ const adapterType = (0, Database_1.getAdapter)().type;
151
+ if (!type)
152
+ return "VARCHAR(255)";
153
+ switch (type) {
154
+ case Number:
155
+ return adapterType === 'postgres' ? "INTEGER" : "INT";
156
+ case String:
157
+ return "VARCHAR(255)";
158
+ case Boolean:
159
+ return adapterType === 'postgres' ? "BOOLEAN" : "TINYINT(1)";
160
+ case Date:
161
+ return "TIMESTAMP";
162
+ default:
163
+ if (type.name === "Array" || type.name === "Object")
164
+ return "TEXT";
165
+ return "VARCHAR(255)";
166
+ }
167
+ }
168
+ getExistingColumns(table) {
169
+ return __awaiter(this, void 0, void 0, function* () {
170
+ const conn = yield (0, Database_1.getConnection)();
171
+ const adapter = (0, Database_1.getAdapter)();
172
+ const columns = {};
173
+ try {
174
+ let sql;
175
+ if (adapter.type === 'postgres') {
176
+ sql = `SELECT column_name, data_type FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = ?`;
177
+ }
178
+ else { // mysql
179
+ sql = `SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?`;
180
+ }
181
+ const rows = yield (0, Database_1.query)(sql, [table], conn);
182
+ if (adapter.type === 'postgres') {
183
+ for (const r of rows) {
184
+ columns[r.column_name] = r.data_type;
185
+ }
186
+ }
187
+ else {
188
+ for (const r of rows) {
189
+ columns[r.COLUMN_NAME] = r.DATA_TYPE;
190
+ }
191
+ }
192
+ }
193
+ finally {
194
+ conn.release();
195
+ }
196
+ return columns;
197
+ });
198
+ }
199
+ }
200
+ exports.SchemaManager = SchemaManager;
@@ -0,0 +1,12 @@
1
+ export type GenericConnection = any;
2
+ export interface IDatabaseAdapter {
3
+ type: 'mysql' | 'postgres';
4
+ init(config: any): void;
5
+ query(sql: string, params: any[], connection?: GenericConnection): Promise<any[]>;
6
+ execute(sql: string, params: any[], connection?: GenericConnection): Promise<{
7
+ insertId?: number;
8
+ affectedRows?: number;
9
+ }>;
10
+ getConnection(): Promise<GenericConnection>;
11
+ close(): Promise<void>;
12
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,14 @@
1
+ import { IDatabaseAdapter, GenericConnection } from './IDatabaseAdapter';
2
+ export declare class MySQLAdapter implements IDatabaseAdapter {
3
+ readonly type = "mysql";
4
+ private pool;
5
+ init(config: any): void;
6
+ private getPool;
7
+ query(sql: string, params: any[], connection?: GenericConnection): Promise<any[]>;
8
+ execute(sql: string, params: any[], connection?: GenericConnection): Promise<{
9
+ insertId?: number;
10
+ affectedRows?: number;
11
+ }>;
12
+ getConnection(): Promise<GenericConnection>;
13
+ close(): Promise<void>;
14
+ }
@@ -0,0 +1,74 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.MySQLAdapter = void 0;
16
+ const promise_1 = __importDefault(require("mysql2/promise"));
17
+ class MySQLAdapter {
18
+ constructor() {
19
+ this.type = 'mysql';
20
+ this.pool = null;
21
+ }
22
+ init(config) {
23
+ var _a;
24
+ if (this.pool) {
25
+ console.warn("[MySQLAdapter] Pool já inicializado.");
26
+ return;
27
+ }
28
+ try {
29
+ this.pool = promise_1.default.createPool(Object.assign(Object.assign({}, config), { waitForConnections: true, connectionLimit: (_a = config.connectionLimit) !== null && _a !== void 0 ? _a : 10, queueLimit: 0 }));
30
+ console.log("[MySQLAdapter] ✅ Pool de conexões MySQL inicializado.");
31
+ }
32
+ catch (error) {
33
+ console.error("[MySQLAdapter] ❌ Falha ao inicializar pool MySQL:", error);
34
+ process.exit(1);
35
+ }
36
+ }
37
+ getPool() {
38
+ if (!this.pool) {
39
+ throw new Error("Pool MySQL não inicializado.");
40
+ }
41
+ return this.pool;
42
+ }
43
+ query(sql, params, connection) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ const conn = connection !== null && connection !== void 0 ? connection : this.getPool();
46
+ const [rows] = yield conn.query(sql, params);
47
+ return rows;
48
+ });
49
+ }
50
+ execute(sql, params, connection) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ const conn = connection !== null && connection !== void 0 ? connection : this.getPool();
53
+ const [result] = yield conn.execute(sql, params);
54
+ return {
55
+ insertId: result.insertId,
56
+ affectedRows: result.affectedRows
57
+ };
58
+ });
59
+ }
60
+ getConnection() {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ return this.getPool().getConnection();
63
+ });
64
+ }
65
+ close() {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ if (this.pool) {
68
+ yield this.pool.end();
69
+ this.pool = null;
70
+ }
71
+ });
72
+ }
73
+ }
74
+ exports.MySQLAdapter = MySQLAdapter;
@@ -0,0 +1,16 @@
1
+ import { IDatabaseAdapter, GenericConnection } from './IDatabaseAdapter';
2
+ export declare class PostgresAdapter implements IDatabaseAdapter {
3
+ readonly type = "postgres";
4
+ private pool;
5
+ init(config: any): void;
6
+ private getPool;
7
+ private formatSql;
8
+ private quoteIdentifiers;
9
+ query(sql: string, params: any[], connection?: GenericConnection): Promise<any[]>;
10
+ execute(sql: string, params: any[], connection?: GenericConnection): Promise<{
11
+ insertId?: number;
12
+ affectedRows?: number;
13
+ }>;
14
+ getConnection(): Promise<GenericConnection>;
15
+ close(): Promise<void>;
16
+ }
@@ -0,0 +1,85 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.PostgresAdapter = void 0;
13
+ const pg_1 = require("pg");
14
+ class PostgresAdapter {
15
+ constructor() {
16
+ this.type = 'postgres';
17
+ this.pool = null;
18
+ }
19
+ init(config) {
20
+ if (this.pool) {
21
+ console.warn("[PostgresAdapter] Pool já inicializado.");
22
+ return;
23
+ }
24
+ try {
25
+ this.pool = new pg_1.Pool({
26
+ connectionString: config.url,
27
+ });
28
+ console.log("[PostgresAdapter] ✅ Pool de conexões PostgreSQL inicializado.");
29
+ }
30
+ catch (error) {
31
+ console.error("[PostgresAdapter] ❌ Falha ao inicializar pool PostgreSQL:", error);
32
+ process.exit(1);
33
+ }
34
+ }
35
+ getPool() {
36
+ if (!this.pool) {
37
+ throw new Error("Pool PostgreSQL não inicializado.");
38
+ }
39
+ return this.pool;
40
+ }
41
+ formatSql(sql) {
42
+ let paramIndex = 1;
43
+ return sql.replace(/\?/g, () => `$${paramIndex++}`);
44
+ }
45
+ quoteIdentifiers(sql) {
46
+ return sql.replace(/`([^`]+)`/g, '"$1"');
47
+ }
48
+ query(sql, params, connection) {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ const conn = connection !== null && connection !== void 0 ? connection : this.getPool();
51
+ const pgSql = this.quoteIdentifiers(this.formatSql(sql));
52
+ const result = yield conn.query(pgSql, params);
53
+ return result.rows;
54
+ });
55
+ }
56
+ execute(sql, params, connection) {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ var _a;
59
+ const conn = connection !== null && connection !== void 0 ? connection : this.getPool();
60
+ let pgSql = this.quoteIdentifiers(this.formatSql(sql));
61
+ if (pgSql.trim().toUpperCase().startsWith('INSERT')) {
62
+ pgSql += ' RETURNING id';
63
+ }
64
+ const result = yield conn.query(pgSql, params);
65
+ return {
66
+ insertId: (_a = result.rows[0]) === null || _a === void 0 ? void 0 : _a.id,
67
+ affectedRows: result.rowCount
68
+ };
69
+ });
70
+ }
71
+ getConnection() {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ return this.getPool().connect();
74
+ });
75
+ }
76
+ close() {
77
+ return __awaiter(this, void 0, void 0, function* () {
78
+ if (this.pool) {
79
+ yield this.pool.end();
80
+ this.pool = null;
81
+ }
82
+ });
83
+ }
84
+ }
85
+ exports.PostgresAdapter = PostgresAdapter;
@@ -0,0 +1,7 @@
1
+ export declare const belongsToMap: Map<Function, Map<string, any>>;
2
+ export type BelongsToOptions = {
3
+ model: () => new (...args: any[]) => any;
4
+ foreignKey: string;
5
+ };
6
+ export declare function BelongsTo(options: BelongsToOptions): (target: any, propertyKey: string) => void;
7
+ export declare function getBelongsToMeta(ctor: Function): Map<string, any> | undefined;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.belongsToMap = void 0;
4
+ exports.BelongsTo = BelongsTo;
5
+ exports.getBelongsToMeta = getBelongsToMeta;
6
+ exports.belongsToMap = new Map();
7
+ function BelongsTo(options) {
8
+ return function (target, propertyKey) {
9
+ const ctor = target.constructor;
10
+ const relations = exports.belongsToMap.get(ctor) || new Map();
11
+ relations.set(propertyKey, options);
12
+ exports.belongsToMap.set(ctor, relations);
13
+ };
14
+ }
15
+ function getBelongsToMeta(ctor) {
16
+ return exports.belongsToMap.get(ctor);
17
+ }
@@ -0,0 +1,12 @@
1
+ import { ColumnAdapter } from "../ColumnAdapter";
2
+ export declare const columnMap: Map<Function, Map<string, any>>;
3
+ export type ColumnOptions = {
4
+ name?: string;
5
+ adapter?: new () => ColumnAdapter;
6
+ unique?: boolean;
7
+ limit?: number;
8
+ type?: any;
9
+ index?: boolean;
10
+ };
11
+ export declare function Column(options: ColumnOptions): (target: any, propertyKey: string) => void;
12
+ export declare function getColumnMeta(ctor: Function): Map<string, any> | undefined;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.columnMap = void 0;
4
+ exports.Column = Column;
5
+ exports.getColumnMeta = getColumnMeta;
6
+ exports.columnMap = new Map();
7
+ function Column(options) {
8
+ return function (target, propertyKey) {
9
+ const ctor = target.constructor;
10
+ const cols = getColumnMeta(ctor) || new Map();
11
+ cols.set(propertyKey, Object.assign(Object.assign({}, options), { propertyKey }));
12
+ exports.columnMap.set(ctor, cols);
13
+ };
14
+ }
15
+ function getColumnMeta(ctor) {
16
+ return exports.columnMap.get(ctor);
17
+ }
@@ -0,0 +1,7 @@
1
+ export declare const hasManyMap: Map<Function, Map<string, any>>;
2
+ export type HasManyOptions = {
3
+ model: () => new (...args: any[]) => any;
4
+ foreignKey: string;
5
+ };
6
+ export declare function HasMany(options: HasManyOptions): (target: any, propertyKey: string) => void;
7
+ export declare function getHasManyMeta(ctor: Function): Map<string, any> | undefined;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hasManyMap = void 0;
4
+ exports.HasMany = HasMany;
5
+ exports.getHasManyMeta = getHasManyMeta;
6
+ exports.hasManyMap = new Map();
7
+ function HasMany(options) {
8
+ return function (target, propertyKey) {
9
+ const ctor = target.constructor;
10
+ const relations = exports.hasManyMap.get(ctor) || new Map();
11
+ relations.set(propertyKey, options);
12
+ exports.hasManyMap.set(ctor, relations);
13
+ };
14
+ }
15
+ function getHasManyMeta(ctor) {
16
+ return exports.hasManyMap.get(ctor);
17
+ }
@@ -0,0 +1,7 @@
1
+ export declare const hasOneMap: Map<Function, Map<string, any>>;
2
+ export type HasOneOptions = {
3
+ model: () => new (...args: any[]) => any;
4
+ foreignKey: string;
5
+ };
6
+ export declare function HasOne(options: HasOneOptions): (target: any, propertyKey: string) => void;
7
+ export declare function getHasOneMeta(ctor: Function): Map<string, any> | undefined;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hasOneMap = void 0;
4
+ exports.HasOne = HasOne;
5
+ exports.getHasOneMeta = getHasOneMeta;
6
+ exports.hasOneMap = new Map();
7
+ function HasOne(options) {
8
+ return function (target, propertyKey) {
9
+ const ctor = target.constructor;
10
+ const relations = exports.hasOneMap.get(ctor) || new Map();
11
+ relations.set(propertyKey, options);
12
+ exports.hasOneMap.set(ctor, relations);
13
+ };
14
+ }
15
+ function getHasOneMeta(ctor) {
16
+ return exports.hasOneMap.get(ctor);
17
+ }
@@ -0,0 +1 @@
1
+ export declare function Id(): (target: any, propertyKey: string) => void;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Id = Id;
4
+ const Column_1 = require("./Column");
5
+ function Id() {
6
+ return function (target, propertyKey) {
7
+ const ctor = target.constructor;
8
+ const cols = Column_1.columnMap.get(ctor) || new Map();
9
+ cols.set(propertyKey, { id: true, propertyKey }); // Adiciona a propriedade com a flag 'id'
10
+ Column_1.columnMap.set(ctor, cols);
11
+ };
12
+ }
@@ -0,0 +1,2 @@
1
+ export declare function Nullable(value?: boolean): (target: any, propertyKey: string) => void;
2
+ export declare function getNullableMeta(ctor: Function): Map<string, boolean> | undefined;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Nullable = Nullable;
4
+ exports.getNullableMeta = getNullableMeta;
5
+ const nullableMap = new Map();
6
+ function Nullable(value = true) {
7
+ return function (target, propertyKey) {
8
+ const ctor = target.constructor;
9
+ const nulls = getNullableMeta(ctor) || new Map();
10
+ nulls.set(propertyKey, value);
11
+ nullableMap.set(ctor, nulls);
12
+ };
13
+ }
14
+ function getNullableMeta(ctor) {
15
+ return nullableMap.get(ctor);
16
+ }
@@ -0,0 +1,2 @@
1
+ export declare function Table(name: string): (constructor: Function) => void;
2
+ export declare function getTableName(target: Function): string | undefined;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Table = Table;
4
+ exports.getTableName = getTableName;
5
+ const tableMap = new Map();
6
+ function Table(name) {
7
+ return function (constructor) {
8
+ tableMap.set(constructor, name);
9
+ };
10
+ }
11
+ function getTableName(target) {
12
+ return tableMap.get(target);
13
+ }
@@ -0,0 +1,13 @@
1
+ export * from "./Database";
2
+ export * from "./ActiveRecord";
3
+ export * from "./QueryBuilder";
4
+ export * from "./SchemaManager";
5
+ export * from "./ColumnAdapter";
6
+ export * from "./decorators/Table";
7
+ export * from "./decorators/Column";
8
+ export * from "./decorators/Id";
9
+ export * from "./decorators/Nullable";
10
+ export * from "./decorators/HasMany";
11
+ export * from "./decorators/HasOne";
12
+ export * from "./decorators/BelongsTo";
13
+ export * from "./PersistenceException";
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Database"), exports);
18
+ __exportStar(require("./ActiveRecord"), exports);
19
+ __exportStar(require("./QueryBuilder"), exports);
20
+ __exportStar(require("./SchemaManager"), exports);
21
+ __exportStar(require("./ColumnAdapter"), exports);
22
+ __exportStar(require("./decorators/Table"), exports);
23
+ __exportStar(require("./decorators/Column"), exports);
24
+ __exportStar(require("./decorators/Id"), exports);
25
+ __exportStar(require("./decorators/Nullable"), exports);
26
+ __exportStar(require("./decorators/HasMany"), exports);
27
+ __exportStar(require("./decorators/HasOne"), exports);
28
+ __exportStar(require("./decorators/BelongsTo"), exports);
29
+ __exportStar(require("./PersistenceException"), exports);
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@aceitadev/adatabase",
3
+ "version": "0.1.0",
4
+ "description": "Uma biblioteca para facilitar a interação com bancos de dados MySQL e PostgreSQL em projetos TypeScript/Node.js.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "start": "node dist/example.js",
13
+ "dev": "ts-node-dev --respawn --transpile-only src/example.ts"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/aceitadev/aMySQL-Node.git"
18
+ },
19
+ "keywords": [
20
+ "mysql",
21
+ "mysql2",
22
+ "database",
23
+ "typescript",
24
+ "node"
25
+ ],
26
+ "author": "Murilo Ribeiro aceitadev@gmail.com",
27
+ "license": "MIT",
28
+ "bugs": {
29
+ "url": "https://github.com/aceitadev/aMySQL-Node/issues"
30
+ },
31
+ "homepage": "https://github.com/aceitadev/aMySQL-Node#readme",
32
+ "dependencies": {
33
+ "mysql2": "^3.2.0",
34
+ "pg": "^8.16.3"
35
+ },
36
+ "devDependencies": {
37
+ "@types/pg": "^8.15.5",
38
+ "ts-node-dev": "^2.0.0",
39
+ "typescript": "^5.0.0"
40
+ }
41
+ }