@aceitadev/adatabase 0.1.2 → 0.1.5
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/ActiveRecord.js +10 -4
- package/dist/QueryBuilder.d.ts +5 -0
- package/dist/QueryBuilder.js +22 -2
- package/package.json +1 -1
package/dist/ActiveRecord.js
CHANGED
|
@@ -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
|
|
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
|
|
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
|
}
|
package/dist/QueryBuilder.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ export declare class QueryBuilder<T extends ActiveRecord> {
|
|
|
3
3
|
private model;
|
|
4
4
|
private whereClauses;
|
|
5
5
|
private _limit?;
|
|
6
|
+
private _offset?;
|
|
7
|
+
private _orderBy?;
|
|
6
8
|
private _includes;
|
|
7
9
|
private primaryKey;
|
|
8
10
|
constructor(model: {
|
|
@@ -10,6 +12,9 @@ export declare class QueryBuilder<T extends ActiveRecord> {
|
|
|
10
12
|
});
|
|
11
13
|
where(field: keyof T | string, operator: string, value: any): this;
|
|
12
14
|
include(...models: (new (...args: any[]) => any)[]): this;
|
|
15
|
+
orderBy(column: keyof T | string, direction?: 'asc' | 'desc'): this;
|
|
16
|
+
limit(count: number): this;
|
|
17
|
+
offset(count: number): this;
|
|
13
18
|
first(): Promise<T | null>;
|
|
14
19
|
get(): Promise<T[]>;
|
|
15
20
|
private getColumnName;
|
package/dist/QueryBuilder.js
CHANGED
|
@@ -18,7 +18,7 @@ const BelongsTo_1 = require("./decorators/BelongsTo");
|
|
|
18
18
|
const HasMany_1 = require("./decorators/HasMany");
|
|
19
19
|
const HasOne_1 = require("./decorators/HasOne");
|
|
20
20
|
function camelToSnake(s) {
|
|
21
|
-
return s.replace(/([a-z0-
|
|
21
|
+
return s.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
22
22
|
}
|
|
23
23
|
class QueryBuilder {
|
|
24
24
|
constructor(model) {
|
|
@@ -44,6 +44,18 @@ class QueryBuilder {
|
|
|
44
44
|
this._includes.push(...models);
|
|
45
45
|
return this;
|
|
46
46
|
}
|
|
47
|
+
orderBy(column, direction = 'asc') {
|
|
48
|
+
this._orderBy = { column: column, direction };
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
limit(count) {
|
|
52
|
+
this._limit = count;
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
offset(count) {
|
|
56
|
+
this._offset = count;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
47
59
|
first() {
|
|
48
60
|
return __awaiter(this, void 0, void 0, function* () {
|
|
49
61
|
this._limit = 1;
|
|
@@ -117,8 +129,16 @@ class QueryBuilder {
|
|
|
117
129
|
params.push(c.value);
|
|
118
130
|
});
|
|
119
131
|
}
|
|
120
|
-
if (this.
|
|
132
|
+
if (this._orderBy) {
|
|
133
|
+
const colName = this.getColumnName(this._orderBy.column, columnsMeta);
|
|
134
|
+
sql += ` ORDER BY ${baseAlias}.\`${colName}\` ${this._orderBy.direction.toUpperCase()}`;
|
|
135
|
+
}
|
|
136
|
+
if (this._limit) {
|
|
121
137
|
sql += ` LIMIT ${this._limit}`;
|
|
138
|
+
}
|
|
139
|
+
if (this._offset) {
|
|
140
|
+
sql += ` OFFSET ${this._offset}`;
|
|
141
|
+
}
|
|
122
142
|
sql += ";";
|
|
123
143
|
const rows = yield (0, Database_1.query)(sql, params);
|
|
124
144
|
return this.mapRowsToEntities(rows);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aceitadev/adatabase",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
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",
|