@aceitadev/adatabase 0.2.5 → 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.
@@ -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
  }
@@ -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.5",
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",