@ignisia/sql 0.2.0 → 0.2.1
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/column/constants.js +97 -8
- package/dist/column/index.js +105 -8
- package/dist/column/types.js +1 -2
- package/dist/database/alter.js +87 -15
- package/dist/database/column.js +33 -11
- package/dist/database/contract.js +1 -0
- package/dist/database/index.js +92 -19
- package/dist/database/table.js +37 -19
- package/dist/database/types.js +1 -0
- package/dist/database/wrapper.js +92 -9
- package/dist/index.js +5 -32
- package/dist/migration/index.js +48 -6
- package/dist/migration/runner.js +7 -20
- package/dist/migration/type.js +1 -0
- package/dist/query/builder.js +66 -16
- package/dist/query/condition.js +97 -24
- package/dist/query/constants.js +54 -19
- package/dist/query/contract.js +1 -0
- package/dist/query/helper.js +30 -18
- package/dist/query/index.js +195 -10
- package/dist/query/join.js +16 -6
- package/dist/query/sql.js +99 -16
- package/dist/query/types.js +1 -0
- package/dist/query/utilities.js +175 -24
- package/dist/table/constants.js +5 -5
- package/dist/table/index.js +52 -14
- package/dist/table/types.js +1 -0
- package/dist/table/utilities.js +50 -15
- package/dist/types.js +1 -0
- package/dist/utilities.js +18 -8
- package/package.json +37 -2
- package/dist/chunk-62FKD35V.js +0 -65
- package/dist/chunk-CIWX3UCZ.js +0 -51
- package/dist/chunk-EIUC7HJS.js +0 -686
- package/dist/chunk-FYSNJAGD.js +0 -19
- package/dist/chunk-G3LSCLIQ.js +0 -104
- package/dist/chunk-GLOHF5CP.js +0 -9
- package/dist/chunk-GY7R637S.js +0 -113
- package/dist/chunk-HKTHKQLK.js +0 -98
- package/dist/chunk-JF7OSNH4.js +0 -40
- package/dist/chunk-KVCIOW7L.js +0 -59
- package/dist/chunk-OYM2PNYZ.js +0 -44
- package/dist/chunk-UI7U54OT.js +0 -112
- package/dist/chunk-V4OMHVJN.js +0 -96
- package/dist/chunk-WVJGTZFI.js +0 -60
- package/dist/chunk-Y7FSRHH3.js +0 -22
package/dist/migration/index.js
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
class Migration {
|
|
2
|
+
db;
|
|
3
|
+
_up;
|
|
4
|
+
_down;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.db = options.db;
|
|
7
|
+
this._up = options.up;
|
|
8
|
+
this._down = options.down;
|
|
9
|
+
}
|
|
10
|
+
get up() {
|
|
11
|
+
return this._up;
|
|
12
|
+
}
|
|
13
|
+
get down() {
|
|
14
|
+
return this._down;
|
|
15
|
+
}
|
|
16
|
+
static setUp(migrator, fn) {
|
|
17
|
+
migrator._up = () => fn(migrator.db);
|
|
18
|
+
return migrator;
|
|
19
|
+
}
|
|
20
|
+
static setDown(migrator, fn) {
|
|
21
|
+
migrator._down = () => fn(migrator.db);
|
|
22
|
+
return migrator;
|
|
23
|
+
}
|
|
24
|
+
static define(db, options) {
|
|
25
|
+
const migration = new Migration({
|
|
26
|
+
db,
|
|
27
|
+
up: options?.up ? () => options.up(db) : null,
|
|
28
|
+
down: options?.down ? () => options.down(db) : null
|
|
29
|
+
});
|
|
30
|
+
return {
|
|
31
|
+
migration,
|
|
32
|
+
setUp(fn) {
|
|
33
|
+
return Migration.setUp(
|
|
34
|
+
migration,
|
|
35
|
+
fn
|
|
36
|
+
);
|
|
37
|
+
},
|
|
38
|
+
setDown(fn) {
|
|
39
|
+
return Migration.setDown(
|
|
40
|
+
migration,
|
|
41
|
+
fn
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { Migration };
|
package/dist/migration/runner.js
CHANGED
|
@@ -1,26 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from "../chunk-KVCIOW7L.js";
|
|
4
|
-
import "../chunk-EIUC7HJS.js";
|
|
5
|
-
import "../chunk-FYSNJAGD.js";
|
|
6
|
-
import "../chunk-62FKD35V.js";
|
|
7
|
-
import "../chunk-Y7FSRHH3.js";
|
|
8
|
-
import "../chunk-WVJGTZFI.js";
|
|
9
|
-
import {
|
|
10
|
-
Column
|
|
11
|
-
} from "../chunk-GY7R637S.js";
|
|
12
|
-
import "../chunk-G3LSCLIQ.js";
|
|
13
|
-
import "../chunk-GLOHF5CP.js";
|
|
1
|
+
import { Column } from '../column';
|
|
2
|
+
import { Table } from '../table';
|
|
14
3
|
|
|
15
|
-
|
|
16
|
-
var nameColumn = Column.define({
|
|
4
|
+
const nameColumn = Column.define({
|
|
17
5
|
type: "TEXT",
|
|
18
6
|
length: 255
|
|
19
7
|
}).primaryKey().notNull();
|
|
20
|
-
|
|
8
|
+
const runAtColumn = Column.define({
|
|
21
9
|
type: "DATETIME"
|
|
22
10
|
}).notNull();
|
|
23
|
-
|
|
11
|
+
let migrationTable = null;
|
|
24
12
|
async function runMigration(filePath, direction) {
|
|
25
13
|
const migrationFile = await import(filePath);
|
|
26
14
|
const fileName = filePath.split("/").pop();
|
|
@@ -64,6 +52,5 @@ async function runMigration(filePath, direction) {
|
|
|
64
52
|
return query.delete().where('ignisia_migration."name"', "eq", fileName).exec();
|
|
65
53
|
});
|
|
66
54
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
};
|
|
55
|
+
|
|
56
|
+
export { runMigration };
|
package/dist/migration/type.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/dist/query/builder.js
CHANGED
|
@@ -1,16 +1,66 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
};
|
|
1
|
+
import { quoteIdentifier } from '../utilities';
|
|
2
|
+
import { getTableSelectName } from './utilities';
|
|
3
|
+
|
|
4
|
+
function buildSelectQuery(q) {
|
|
5
|
+
const from = getTableSelectName(q);
|
|
6
|
+
const columns = [];
|
|
7
|
+
if (q.definition.select?.length) {
|
|
8
|
+
for (const col of q.definition.select) {
|
|
9
|
+
if (typeof col === "object") {
|
|
10
|
+
const alias = quoteIdentifier(col.as.replace(/"/g, ""));
|
|
11
|
+
columns.push(`${col.column} AS ${alias}`);
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
if (!col.endsWith("*")) {
|
|
15
|
+
const alias = quoteIdentifier(col.replace(/"/g, ""));
|
|
16
|
+
columns.push(`${col} AS ${alias}`);
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
columns.push(col);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (q.definition?.aggregates) {
|
|
23
|
+
for (const aggregate of q.definition.aggregates) {
|
|
24
|
+
columns.push(
|
|
25
|
+
`${aggregate.fn}(${aggregate.column}) AS ${quoteIdentifier(aggregate.as)}`
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const distinct = q.definition.distinct ? "DISTINCT " : "";
|
|
30
|
+
return `SELECT ${distinct}${columns.join(", ")} FROM ${from}`;
|
|
31
|
+
}
|
|
32
|
+
function buildInsertQuery(q) {
|
|
33
|
+
const rows = q.definition?.insertValues;
|
|
34
|
+
if (!rows?.length) {
|
|
35
|
+
throw new Error(`INSERT requires values`);
|
|
36
|
+
}
|
|
37
|
+
const keys = Object.keys(rows[0]);
|
|
38
|
+
const columns = keys.map(quoteIdentifier).join(", ");
|
|
39
|
+
const rowPlaceholders = `(${keys.map(() => "?").join(", ")})`;
|
|
40
|
+
const placeholders = rows.map(() => rowPlaceholders).join(", ");
|
|
41
|
+
q.definition.params = rows.flatMap(
|
|
42
|
+
(row) => keys.map((key) => row[key])
|
|
43
|
+
);
|
|
44
|
+
return `INSERT INTO ${q.table.name} (${columns}) VALUES ${placeholders} RETURNING *`;
|
|
45
|
+
}
|
|
46
|
+
function buildUpdateQuery(q) {
|
|
47
|
+
if (!q.definition?.updateValues) {
|
|
48
|
+
throw new Error(`UPDATE requires values`);
|
|
49
|
+
}
|
|
50
|
+
let keys = Object.keys(q.definition.updateValues);
|
|
51
|
+
const updateParams = keys.map(
|
|
52
|
+
(key) => q.definition.updateValues[key]
|
|
53
|
+
);
|
|
54
|
+
keys = keys.map(quoteIdentifier);
|
|
55
|
+
if (q.definition?.params) {
|
|
56
|
+
q.definition.params = [...updateParams, ...q.definition.params];
|
|
57
|
+
} else {
|
|
58
|
+
q.definition.params = updateParams;
|
|
59
|
+
}
|
|
60
|
+
return `UPDATE ${q.table.name} SET ${keys.map((key) => `${key} = ?`.trim()).join(", ")}`;
|
|
61
|
+
}
|
|
62
|
+
function buildDeleteQuery(q) {
|
|
63
|
+
return `DELETE FROM ${q.table.name}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export { buildDeleteQuery, buildInsertQuery, buildSelectQuery, buildUpdateQuery };
|
package/dist/query/condition.js
CHANGED
|
@@ -1,24 +1,97 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
1
|
+
import { LogicalOperator, ConditionClause, AcceptedOperator } from './constants';
|
|
2
|
+
import { rawCol } from './helper';
|
|
3
|
+
import { getCondition } from './utilities';
|
|
4
|
+
|
|
5
|
+
function addRawCondition(query, clause, column, logical, params) {
|
|
6
|
+
const validClause = clause.toLowerCase();
|
|
7
|
+
if (!query.definition[validClause]) query.definition[validClause] = [];
|
|
8
|
+
const condition = column(rawCol);
|
|
9
|
+
const logicalPrefix = query.definition[validClause].length > 0 ? logical : "";
|
|
10
|
+
query.definition[validClause].push(`${logicalPrefix} ${condition}`.trim());
|
|
11
|
+
if (!query.definition.params) query.definition.params = [];
|
|
12
|
+
if (typeof params === "undefined") {
|
|
13
|
+
return query;
|
|
14
|
+
}
|
|
15
|
+
if (Array.isArray(params)) {
|
|
16
|
+
query.definition.params.push(...params);
|
|
17
|
+
} else {
|
|
18
|
+
query.definition.params.push(params);
|
|
19
|
+
}
|
|
20
|
+
return query;
|
|
21
|
+
}
|
|
22
|
+
function rawWhere(column, params) {
|
|
23
|
+
return addRawCondition(
|
|
24
|
+
this,
|
|
25
|
+
ConditionClause.WHERE,
|
|
26
|
+
column,
|
|
27
|
+
LogicalOperator.AND,
|
|
28
|
+
params
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
function rawOr(column, params) {
|
|
32
|
+
return addRawCondition(
|
|
33
|
+
this,
|
|
34
|
+
ConditionClause.WHERE,
|
|
35
|
+
column,
|
|
36
|
+
LogicalOperator.OR,
|
|
37
|
+
params
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
function rawHaving(column, params) {
|
|
41
|
+
return addRawCondition(
|
|
42
|
+
this,
|
|
43
|
+
ConditionClause.HAVING,
|
|
44
|
+
column,
|
|
45
|
+
LogicalOperator.AND,
|
|
46
|
+
params
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
function addCondition(query, clause, column, operator, value, logical) {
|
|
50
|
+
const validClause = clause.toLowerCase();
|
|
51
|
+
const condition = getCondition(query.table.dialect, column, operator, value);
|
|
52
|
+
if (!query.definition[validClause]) query.definition[validClause] = [];
|
|
53
|
+
const logicalPrefix = query.definition[validClause].length > 0 ? logical : "";
|
|
54
|
+
query.definition[validClause].push(`${logicalPrefix} ${condition}`.trim());
|
|
55
|
+
if (operator === AcceptedOperator.IS_NULL || operator === AcceptedOperator.IS_NOT_NULL) {
|
|
56
|
+
return query;
|
|
57
|
+
}
|
|
58
|
+
if (!query.definition.params) query.definition.params = [];
|
|
59
|
+
if (Array.isArray(value)) {
|
|
60
|
+
query.definition.params.push(...value);
|
|
61
|
+
} else {
|
|
62
|
+
query.definition.params.push(value);
|
|
63
|
+
}
|
|
64
|
+
return query;
|
|
65
|
+
}
|
|
66
|
+
function where(column, operator, value) {
|
|
67
|
+
return addCondition(
|
|
68
|
+
this,
|
|
69
|
+
ConditionClause.WHERE,
|
|
70
|
+
column,
|
|
71
|
+
operator,
|
|
72
|
+
value,
|
|
73
|
+
LogicalOperator.AND
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
function or(column, operator, value) {
|
|
77
|
+
return addCondition(
|
|
78
|
+
this,
|
|
79
|
+
ConditionClause.WHERE,
|
|
80
|
+
column,
|
|
81
|
+
operator,
|
|
82
|
+
value,
|
|
83
|
+
LogicalOperator.OR
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
function having(column, operator, value) {
|
|
87
|
+
return addCondition(
|
|
88
|
+
this,
|
|
89
|
+
ConditionClause.HAVING,
|
|
90
|
+
column,
|
|
91
|
+
operator,
|
|
92
|
+
value,
|
|
93
|
+
LogicalOperator.AND
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export { addCondition, addRawCondition, having, or, rawHaving, rawOr, rawWhere, where };
|
package/dist/query/constants.js
CHANGED
|
@@ -1,20 +1,55 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
AggregationFunction,
|
|
5
|
-
ConditionClause,
|
|
6
|
-
LogicalOperator,
|
|
7
|
-
OrderBy,
|
|
8
|
-
QueryHooksType,
|
|
9
|
-
QueryType
|
|
10
|
-
} from "../chunk-62FKD35V.js";
|
|
11
|
-
export {
|
|
12
|
-
AcceptedJoin,
|
|
13
|
-
AcceptedOperator,
|
|
14
|
-
AggregationFunction,
|
|
15
|
-
ConditionClause,
|
|
16
|
-
LogicalOperator,
|
|
17
|
-
OrderBy,
|
|
18
|
-
QueryHooksType,
|
|
19
|
-
QueryType
|
|
1
|
+
const LogicalOperator = {
|
|
2
|
+
AND: "AND",
|
|
3
|
+
OR: "OR"
|
|
20
4
|
};
|
|
5
|
+
const ConditionClause = {
|
|
6
|
+
WHERE: "WHERE",
|
|
7
|
+
HAVING: "HAVING"
|
|
8
|
+
};
|
|
9
|
+
const AcceptedOperator = {
|
|
10
|
+
EQ: "eq",
|
|
11
|
+
NE: "ne",
|
|
12
|
+
GT: "gt",
|
|
13
|
+
LT: "lt",
|
|
14
|
+
GTE: "gte",
|
|
15
|
+
LTE: "lte",
|
|
16
|
+
IN: "in",
|
|
17
|
+
NOT_IN: "notIn",
|
|
18
|
+
LIKE: "like",
|
|
19
|
+
NOT_LIKE: "notLike",
|
|
20
|
+
ILIKE: "ilike",
|
|
21
|
+
NOT_ILIKE: "notILike",
|
|
22
|
+
IS_NULL: "isNull",
|
|
23
|
+
IS_NOT_NULL: "isNotNull",
|
|
24
|
+
BETWEEN: "between",
|
|
25
|
+
NOT_BETWEEN: "notBetween"
|
|
26
|
+
};
|
|
27
|
+
const QueryType = {
|
|
28
|
+
SELECT: "SELECT",
|
|
29
|
+
INSERT: "INSERT",
|
|
30
|
+
UPDATE: "UPDATE",
|
|
31
|
+
DELETE: "DELETE"
|
|
32
|
+
};
|
|
33
|
+
const OrderBy = {
|
|
34
|
+
ASC: "ASC",
|
|
35
|
+
DESC: "DESC"
|
|
36
|
+
};
|
|
37
|
+
const AggregationFunction = {
|
|
38
|
+
COUNT: "COUNT",
|
|
39
|
+
SUM: "SUM",
|
|
40
|
+
MIN: "MIN",
|
|
41
|
+
MAX: "MAX",
|
|
42
|
+
AVG: "AVG"
|
|
43
|
+
};
|
|
44
|
+
const AcceptedJoin = {
|
|
45
|
+
INNER: "INNER",
|
|
46
|
+
LEFT: "LEFT",
|
|
47
|
+
RIGHT: "RIGHT",
|
|
48
|
+
NATURAL: "NATURAL"
|
|
49
|
+
};
|
|
50
|
+
const QueryHooksType = {
|
|
51
|
+
AFTER: "after",
|
|
52
|
+
BEFORE: "before"
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export { AcceptedJoin, AcceptedOperator, AggregationFunction, ConditionClause, LogicalOperator, OrderBy, QueryHooksType, QueryType };
|
package/dist/query/contract.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/dist/query/helper.js
CHANGED
|
@@ -1,18 +1,30 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
import { QueryBuilder } from '.';
|
|
2
|
+
import { deepClone } from '../utilities';
|
|
3
|
+
|
|
4
|
+
function alias(alias2) {
|
|
5
|
+
this.definition.baseAlias = alias2;
|
|
6
|
+
return this;
|
|
7
|
+
}
|
|
8
|
+
function clone() {
|
|
9
|
+
const query = new QueryBuilder(this.table);
|
|
10
|
+
Object.assign(query.definition, deepClone(this.definition));
|
|
11
|
+
return query;
|
|
12
|
+
}
|
|
13
|
+
function rawCol(column) {
|
|
14
|
+
return column;
|
|
15
|
+
}
|
|
16
|
+
function col(column, alias2) {
|
|
17
|
+
return {
|
|
18
|
+
column,
|
|
19
|
+
as: alias2
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function aggregateCol(fn, column, alias2) {
|
|
23
|
+
return {
|
|
24
|
+
column,
|
|
25
|
+
as: alias2 ?? fn.toLowerCase(),
|
|
26
|
+
fn
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { aggregateCol, alias, clone, col, rawCol };
|
package/dist/query/index.js
CHANGED
|
@@ -1,10 +1,195 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { quoteIdentifier } from '../utilities';
|
|
2
|
+
import { rawWhere, rawHaving, rawOr, where, having, or } from './condition';
|
|
3
|
+
import { AcceptedJoin, QueryType } from './constants';
|
|
4
|
+
import { alias, clone, aggregateCol, col } from './helper';
|
|
5
|
+
import { addJoin } from './join';
|
|
6
|
+
import { toQuery, toString, exec } from './sql';
|
|
7
|
+
import { getTimestamp, getParanoid } from './utilities';
|
|
8
|
+
|
|
9
|
+
class QueryBuilder {
|
|
10
|
+
hooks;
|
|
11
|
+
table;
|
|
12
|
+
definition;
|
|
13
|
+
_output;
|
|
14
|
+
alias;
|
|
15
|
+
clone;
|
|
16
|
+
toQuery;
|
|
17
|
+
toString;
|
|
18
|
+
exec;
|
|
19
|
+
rawWhere;
|
|
20
|
+
rawAnd;
|
|
21
|
+
rawOr;
|
|
22
|
+
rawHaving;
|
|
23
|
+
where;
|
|
24
|
+
and;
|
|
25
|
+
or;
|
|
26
|
+
having;
|
|
27
|
+
constructor(table) {
|
|
28
|
+
this.hooks = {};
|
|
29
|
+
this.table = table;
|
|
30
|
+
this.definition = {
|
|
31
|
+
queryType: null,
|
|
32
|
+
select: null,
|
|
33
|
+
having: null,
|
|
34
|
+
where: null,
|
|
35
|
+
params: null,
|
|
36
|
+
limit: null,
|
|
37
|
+
offset: null,
|
|
38
|
+
groupBy: null,
|
|
39
|
+
insertValues: null,
|
|
40
|
+
updateValues: null,
|
|
41
|
+
orderBy: null,
|
|
42
|
+
aggregates: null,
|
|
43
|
+
joins: null,
|
|
44
|
+
distinct: null,
|
|
45
|
+
baseAlias: table.name,
|
|
46
|
+
joinedTables: null,
|
|
47
|
+
withDeleted: null
|
|
48
|
+
};
|
|
49
|
+
this.alias = alias.bind(this);
|
|
50
|
+
this.clone = clone.bind(this);
|
|
51
|
+
this.toQuery = toQuery.bind(this);
|
|
52
|
+
this.toString = toString.bind(this);
|
|
53
|
+
this.exec = exec.bind(this);
|
|
54
|
+
this.rawWhere = rawWhere.bind(this);
|
|
55
|
+
this.rawHaving = rawHaving.bind(this);
|
|
56
|
+
this.rawAnd = this.rawWhere;
|
|
57
|
+
this.rawOr = rawOr.bind(this);
|
|
58
|
+
this.where = where.bind(this);
|
|
59
|
+
this.having = having.bind(this);
|
|
60
|
+
this.and = this.where;
|
|
61
|
+
this.or = or.bind(this);
|
|
62
|
+
}
|
|
63
|
+
leftJoin(joinTable, alias2, baseColumn, joinColumn) {
|
|
64
|
+
return addJoin(
|
|
65
|
+
this,
|
|
66
|
+
AcceptedJoin.LEFT,
|
|
67
|
+
alias2,
|
|
68
|
+
joinTable,
|
|
69
|
+
baseColumn,
|
|
70
|
+
joinColumn
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
rightJoin(joinTable, alias2, baseColumn, joinColumn) {
|
|
74
|
+
return addJoin(
|
|
75
|
+
this,
|
|
76
|
+
AcceptedJoin.RIGHT,
|
|
77
|
+
alias2,
|
|
78
|
+
joinTable,
|
|
79
|
+
baseColumn,
|
|
80
|
+
joinColumn
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
innerJoin(joinTable, alias2, baseColumn, joinColumn) {
|
|
84
|
+
return addJoin(
|
|
85
|
+
this,
|
|
86
|
+
AcceptedJoin.INNER,
|
|
87
|
+
alias2,
|
|
88
|
+
joinTable,
|
|
89
|
+
baseColumn,
|
|
90
|
+
joinColumn
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
naturalJoin(joinTable, alias2, baseColumn, joinColumn) {
|
|
94
|
+
return addJoin(
|
|
95
|
+
this,
|
|
96
|
+
AcceptedJoin.NATURAL,
|
|
97
|
+
alias2,
|
|
98
|
+
joinTable,
|
|
99
|
+
baseColumn,
|
|
100
|
+
joinColumn
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
distinct() {
|
|
104
|
+
this.definition.distinct = true;
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
aggregate(...aggregates) {
|
|
108
|
+
this.definition.aggregates = aggregates.map(
|
|
109
|
+
(aggregate) => aggregate(aggregateCol)
|
|
110
|
+
);
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
groupBy(...columns) {
|
|
114
|
+
this.definition.groupBy = columns;
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
limit(limit) {
|
|
118
|
+
this.definition.limit = limit;
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
offset(offset) {
|
|
122
|
+
this.definition.offset = offset;
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
orderBy(...orderBy) {
|
|
126
|
+
if (!this.definition.orderBy) this.definition.orderBy = [];
|
|
127
|
+
this.definition.orderBy.push(...orderBy);
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
withDeleted() {
|
|
131
|
+
this.definition.withDeleted = true;
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
select(...columns) {
|
|
135
|
+
if (!columns.length) {
|
|
136
|
+
const base = this.definition.baseAlias ?? this.table.name;
|
|
137
|
+
columns = Object.keys(this.table.columns).map(
|
|
138
|
+
(colName) => `${base}.${quoteIdentifier(colName)}`
|
|
139
|
+
);
|
|
140
|
+
} else {
|
|
141
|
+
columns = columns.map((column) => {
|
|
142
|
+
if (typeof column === "function") {
|
|
143
|
+
return column(col);
|
|
144
|
+
}
|
|
145
|
+
return column;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
this.definition.select = columns;
|
|
149
|
+
this.definition.queryType = QueryType.SELECT;
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
insert(...values) {
|
|
153
|
+
this.definition.queryType = QueryType.INSERT;
|
|
154
|
+
if (!this.definition.insertValues) this.definition.insertValues = [];
|
|
155
|
+
const { isWithTimestamp, createdAt, updatedAt, timestamp } = getTimestamp(
|
|
156
|
+
this.table
|
|
157
|
+
);
|
|
158
|
+
if (isWithTimestamp) {
|
|
159
|
+
values = values.map((row) => ({
|
|
160
|
+
...row,
|
|
161
|
+
[createdAt]: row[createdAt] ?? timestamp,
|
|
162
|
+
[updatedAt]: row[updatedAt] ?? timestamp
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
this.definition.insertValues = values;
|
|
166
|
+
return this;
|
|
167
|
+
}
|
|
168
|
+
update(values) {
|
|
169
|
+
const { isWithTimestamp, updatedAt, timestamp } = getTimestamp(this.table);
|
|
170
|
+
if (isWithTimestamp) {
|
|
171
|
+
values = {
|
|
172
|
+
...values,
|
|
173
|
+
[updatedAt]: values[updatedAt] ?? timestamp
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
this.definition.queryType = QueryType.UPDATE;
|
|
177
|
+
this.definition.updateValues = values;
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
delete() {
|
|
181
|
+
const { isWithParanoid, deletedAt, timestamp } = getParanoid(this.table);
|
|
182
|
+
if (isWithParanoid) {
|
|
183
|
+
return this.update({
|
|
184
|
+
[deletedAt]: timestamp
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
this.definition.queryType = QueryType.DELETE;
|
|
188
|
+
return this;
|
|
189
|
+
}
|
|
190
|
+
infer() {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export { QueryBuilder };
|
package/dist/query/join.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
function addJoin(query, joinType, alias, joinTable, baseColumn, joinColumn) {
|
|
2
|
+
if (!query.definition.joins) query.definition.joins = [];
|
|
3
|
+
query.definition.joins.push(
|
|
4
|
+
`${joinType} JOIN ${joinTable.name} AS ${alias} ON ${baseColumn} = ${joinColumn}`
|
|
5
|
+
);
|
|
6
|
+
if (!query.definition.joinedTables) {
|
|
7
|
+
query.definition.joinedTables = {};
|
|
8
|
+
}
|
|
9
|
+
query.definition.joinedTables = {
|
|
10
|
+
...query.definition.joinedTables,
|
|
11
|
+
[alias]: joinTable
|
|
12
|
+
};
|
|
13
|
+
return query;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { addJoin };
|