@aceitadev/adatabase 0.1.0 → 0.1.3

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.
@@ -58,8 +58,10 @@ class ActiveRecord {
58
58
  if (idValue == null || idValue === 0) {
59
59
  const cols = colEntries.map(c => `\`${c.colName}\``).join(", ");
60
60
  const placeholders = colEntries.map(() => "?").join(", ");
61
- const params = colEntries.map(c => c.value);
62
- const sql = `INSERT INTO \`${table}\` (${cols}) VALUES (${placeholders});`;
61
+ const params = colEntries.map(c => c.value === undefined ? null : c.value);
62
+ const sql = `INSERT INTO
63
+ ${table}
64
+ (${cols}) VALUES (${placeholders});`;
63
65
  const res = yield (0, Database_1.execute)(sql, params, conn);
64
66
  if (res.insertId) {
65
67
  this[idField] = res.insertId;
@@ -67,9 +69,13 @@ class ActiveRecord {
67
69
  }
68
70
  else {
69
71
  const setClause = colEntries.map(c => `\`${c.colName}\` = ?`).join(", ");
70
- const params = [...colEntries.map(c => c.value), idValue];
72
+ const params = [...colEntries.map(c => c.value === undefined ? null : c.value), idValue];
71
73
  const idColName = camelToSnake(idField);
72
- const sql = `UPDATE \`${table}\` SET ${setClause} WHERE \`${idColName}\` = ?;`;
74
+ const sql = `UPDATE
75
+ ${table}
76
+ SET ${setClause} WHERE
77
+ ${idColName}
78
+ = ?;`;
73
79
  yield (0, Database_1.execute)(sql, params, conn);
74
80
  }
75
81
  }
@@ -139,7 +139,19 @@ ${col}
139
139
  if (type === String && opts && opts.limit) {
140
140
  sqlType = `VARCHAR(${opts.limit})`;
141
141
  }
142
- sqlType += (nullableMeta === null || nullableMeta === void 0 ? void 0 : nullableMeta.get(prop)) ? " NULL" : " NOT NULL";
142
+ const isNullable = nullableMeta === null || nullableMeta === void 0 ? void 0 : nullableMeta.get(prop);
143
+ sqlType += isNullable ? " NULL" : " NOT NULL";
144
+ if (type === Date && !isNullable) {
145
+ if (adapter.type === 'mysql') {
146
+ sqlType += " DEFAULT CURRENT_TIMESTAMP";
147
+ if (colName === 'updated_at') {
148
+ sqlType += " ON UPDATE CURRENT_TIMESTAMP";
149
+ }
150
+ }
151
+ else if (adapter.type === 'postgres') {
152
+ sqlType += " DEFAULT CURRENT_TIMESTAMP";
153
+ }
154
+ }
143
155
  if (opts && opts.unique)
144
156
  sqlType += " UNIQUE";
145
157
  columns[colName] = sqlType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aceitadev/adatabase",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
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",