@aceitadev/adatabase 0.2.0 → 0.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.
@@ -4,6 +4,7 @@ type Constructor<T> = {
4
4
  new (...args: any[]): T;
5
5
  };
6
6
  export declare abstract class ActiveRecord {
7
+ private static idFieldCache;
7
8
  private static getIdField;
8
9
  save<T extends ActiveRecord>(this: T, tx?: GenericConnection): Promise<T>;
9
10
  static find<T extends ActiveRecord>(this: Constructor<T>): QueryBuilder<T>;
@@ -21,11 +21,15 @@ function camelToSnake(s) {
21
21
  class ActiveRecord {
22
22
  static getIdField(ctor) {
23
23
  return __awaiter(this, void 0, void 0, function* () {
24
+ if (this.idFieldCache.has(ctor)) {
25
+ return this.idFieldCache.get(ctor);
26
+ }
24
27
  const columns = (0, Column_1.getColumnMeta)(ctor);
25
28
  if (!columns)
26
29
  throw new PersistenceException_1.PersistenceException("Model has no @Column decorators", null);
27
30
  for (const [prop, opts] of columns.entries()) {
28
31
  if (opts === null || opts === void 0 ? void 0 : opts.id) {
32
+ this.idFieldCache.set(ctor, prop);
29
33
  return prop;
30
34
  }
31
35
  }
@@ -119,3 +123,4 @@ ${idColName}
119
123
  }
120
124
  }
121
125
  exports.ActiveRecord = ActiveRecord;
126
+ ActiveRecord.idFieldCache = new Map();
@@ -46,15 +46,9 @@ class SchemaManager {
46
46
  const colsSql = Object.entries(columns).map(([k, v]) => `\`${k}\` ${v}`).join(", ");
47
47
  let indexSql = "";
48
48
  if (adapter.type === 'mysql' && indexes.length > 0) {
49
- indexSql = ", " + indexes.map(col => `INDEX
50
- ${col}
51
- (
52
- ${col}
53
- )`).join(", ");
49
+ indexSql = ", " + indexes.map(col => `INDEX \`${col}\` (\`${col}\`)`).join(", ");
54
50
  }
55
- const sql = `CREATE TABLE
56
- ${table}
57
- (${colsSql}${indexSql});`;
51
+ const sql = `CREATE TABLE \`${table}\` (${colsSql}${indexSql});`;
58
52
  const conn = yield (0, Database_1.getConnection)();
59
53
  try {
60
54
  yield (0, Database_1.execute)(sql, [], conn);
@@ -66,13 +60,7 @@ ${table}
66
60
  if (adapter.type === 'postgres' && indexes.length > 0) {
67
61
  for (const col of indexes) {
68
62
  const indexName = `idx_${table}_${col}`;
69
- const indexCreationSql = `CREATE INDEX
70
- ${indexName}
71
- ON
72
- ${table}
73
- (
74
- ${col}
75
- );`;
63
+ const indexCreationSql = `CREATE INDEX \`${indexName}\` ON \`${table}\` (\`${col}\`);`;
76
64
  const indexConn = yield (0, Database_1.getConnection)();
77
65
  try {
78
66
  yield (0, Database_1.execute)(indexCreationSql, [], indexConn);
@@ -88,11 +76,7 @@ ${col}
88
76
  return __awaiter(this, void 0, void 0, function* () {
89
77
  for (const [col, type] of Object.entries(desired)) {
90
78
  if (!existing.hasOwnProperty(col)) {
91
- const sql = `ALTER TABLE
92
- ${table}
93
- ADD COLUMN
94
- ${col}
95
- ${type};`;
79
+ const sql = `ALTER TABLE \`${table}\` ADD COLUMN \`${col}\` ${type};`;
96
80
  const conn = yield (0, Database_1.getConnection)();
97
81
  try {
98
82
  yield (0, Database_1.execute)(sql, [], conn);
@@ -136,6 +120,14 @@ ${col}
136
120
  continue;
137
121
  }
138
122
  let sqlType = this.getSqlTypeForClass(type);
123
+ if (type === Number && opts.decimal) {
124
+ if (Array.isArray(opts.decimal) && opts.decimal.length === 2) {
125
+ sqlType = `DECIMAL(${opts.decimal[0]}, ${opts.decimal[1]})`;
126
+ }
127
+ else {
128
+ sqlType = 'DECIMAL(10, 2)';
129
+ }
130
+ }
139
131
  if (type === String && opts && opts.limit) {
140
132
  sqlType = `VARCHAR(${opts.limit})`;
141
133
  }
@@ -59,6 +59,10 @@ class PostgresAdapter {
59
59
  const conn = connection !== null && connection !== void 0 ? connection : this.getPool();
60
60
  let pgSql = this.quoteIdentifiers(this.formatSql(sql));
61
61
  if (pgSql.trim().toUpperCase().startsWith('INSERT')) {
62
+ pgSql = pgSql.trim();
63
+ if (pgSql.endsWith(';')) {
64
+ pgSql = pgSql.slice(0, -1);
65
+ }
62
66
  pgSql += ' RETURNING id';
63
67
  }
64
68
  const result = yield conn.query(pgSql, params);
@@ -7,6 +7,7 @@ export type ColumnOptions = {
7
7
  limit?: number;
8
8
  type?: any;
9
9
  index?: boolean;
10
+ decimal?: boolean | [number, number];
10
11
  };
11
12
  export declare function Column(options: ColumnOptions): (target: any, propertyKey: string) => void;
12
13
  export declare function getColumnMeta(ctor: Function): Map<string, any> | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aceitadev/adatabase",
3
- "version": "0.2.0",
3
+ "version": "0.3.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",