@ignisia/sql 0.2.1 → 0.2.2
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/cjs/column/constants.js +101 -0
- package/dist/cjs/column/index.d.cts +42 -0
- package/dist/cjs/column/index.js +107 -0
- package/dist/cjs/column/types.d.cts +43 -0
- package/dist/cjs/column/types.js +4 -0
- package/dist/cjs/database/alter.d.cts +52 -0
- package/dist/cjs/database/alter.js +93 -0
- package/dist/cjs/database/column.d.cts +24 -0
- package/dist/cjs/database/column.js +37 -0
- package/dist/cjs/database/contract.d.cts +8 -0
- package/dist/cjs/database/contract.js +2 -0
- package/dist/cjs/database/index.d.cts +8 -0
- package/dist/cjs/database/index.js +94 -0
- package/dist/cjs/database/table.d.cts +21 -0
- package/dist/cjs/database/table.js +41 -0
- package/dist/cjs/database/types.d.cts +7 -0
- package/dist/cjs/database/types.js +2 -0
- package/dist/cjs/database/wrapper.d.cts +36 -0
- package/dist/cjs/database/wrapper.js +95 -0
- package/dist/cjs/index---zaMa69.d.cts +103 -0
- package/dist/cjs/index-CwiFQh0I.d.cts +358 -0
- package/dist/cjs/index.d.cts +10 -0
- package/dist/cjs/index.js +30 -0
- package/dist/cjs/migration/index.d.cts +30 -0
- package/dist/{migration → cjs/migration}/index.js +3 -1
- package/dist/cjs/migration/runner.js +58 -0
- package/dist/cjs/migration/type.d.cts +19 -0
- package/dist/cjs/migration/type.js +2 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/query/builder.d.cts +14 -0
- package/dist/cjs/query/builder.js +71 -0
- package/dist/cjs/query/condition.d.cts +7 -0
- package/dist/cjs/query/condition.js +106 -0
- package/dist/cjs/query/constants.js +64 -0
- package/dist/cjs/query/contract.d.cts +7 -0
- package/dist/cjs/query/contract.js +2 -0
- package/dist/cjs/query/helper.d.cts +7 -0
- package/dist/cjs/query/helper.js +36 -0
- package/dist/cjs/query/index.d.cts +7 -0
- package/dist/cjs/query/index.js +197 -0
- package/dist/cjs/query/join.d.cts +16 -0
- package/dist/cjs/query/join.js +18 -0
- package/dist/cjs/query/sql.d.cts +17 -0
- package/dist/cjs/query/sql.js +104 -0
- package/dist/cjs/query/types.d.cts +7 -0
- package/dist/cjs/query/types.js +2 -0
- package/dist/cjs/query/utilities.d.cts +34 -0
- package/dist/cjs/query/utilities.js +184 -0
- package/dist/cjs/table/constants.js +8 -0
- package/dist/cjs/table/index.d.cts +7 -0
- package/dist/cjs/table/index.js +54 -0
- package/dist/cjs/table/types.d.cts +7 -0
- package/dist/cjs/table/types.js +2 -0
- package/dist/cjs/table/utilities.d.cts +7 -0
- package/dist/cjs/table/utilities.js +55 -0
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/utilities.js +21 -0
- package/dist/esm/column/constants.d.ts +97 -0
- package/dist/{column → esm/column}/constants.js +4 -3
- package/dist/{column → esm/column}/index.js +6 -5
- package/dist/esm/column/types.js +1 -0
- package/dist/{database → esm/database}/alter.js +2 -1
- package/dist/{database → esm/database}/column.js +2 -1
- package/dist/{database → esm/database}/index.js +10 -9
- package/dist/{database → esm/database}/table.js +2 -1
- package/dist/{database → esm/database}/wrapper.js +6 -5
- package/dist/esm/index.js +5 -0
- package/dist/esm/migration/index.js +49 -0
- package/dist/esm/migration/runner.d.ts +3 -0
- package/dist/{migration → esm/migration}/runner.js +6 -5
- package/dist/{query → esm/query}/builder.js +3 -2
- package/dist/{query → esm/query}/condition.js +4 -3
- package/dist/esm/query/constants.d.ts +63 -0
- package/dist/{query → esm/query}/constants.js +9 -8
- package/dist/{query → esm/query}/helper.js +3 -2
- package/dist/{query → esm/query}/index.js +10 -9
- package/dist/{query → esm/query}/join.js +1 -0
- package/dist/{query → esm/query}/sql.js +4 -3
- package/dist/{query → esm/query}/utilities.js +5 -4
- package/dist/esm/table/constants.d.ts +7 -0
- package/dist/{table → esm/table}/constants.js +2 -1
- package/dist/{table → esm/table}/index.js +6 -5
- package/dist/{table → esm/table}/utilities.js +5 -4
- package/dist/esm/types.d.ts +3 -0
- package/dist/esm/utilities.d.ts +4 -0
- package/dist/{utilities.js → esm/utilities.js} +1 -0
- package/package.json +42 -19
- package/dist/column/types.js +0 -1
- package/dist/index.js +0 -5
- /package/dist/{column/constants.d.ts → cjs/column/constants.d.cts} +0 -0
- /package/dist/{migration/runner.d.ts → cjs/migration/runner.d.cts} +0 -0
- /package/dist/{query/constants.d.ts → cjs/query/constants.d.cts} +0 -0
- /package/dist/{table/constants.d.ts → cjs/table/constants.d.cts} +0 -0
- /package/dist/{types.d.ts → cjs/types.d.cts} +0 -0
- /package/dist/{utilities.d.ts → cjs/utilities.d.cts} +0 -0
- /package/dist/{column → esm/column}/index.d.ts +0 -0
- /package/dist/{column → esm/column}/types.d.ts +0 -0
- /package/dist/{database → esm/database}/alter.d.ts +0 -0
- /package/dist/{database → esm/database}/column.d.ts +0 -0
- /package/dist/{database → esm/database}/contract.d.ts +0 -0
- /package/dist/{database → esm/database}/contract.js +0 -0
- /package/dist/{database → esm/database}/index.d.ts +0 -0
- /package/dist/{database → esm/database}/table.d.ts +0 -0
- /package/dist/{database → esm/database}/types.d.ts +0 -0
- /package/dist/{database → esm/database}/types.js +0 -0
- /package/dist/{database → esm/database}/wrapper.d.ts +0 -0
- /package/dist/{index-DFrpzXEn.d.ts → esm/index-DFrpzXEn.d.ts} +0 -0
- /package/dist/{index-FMT0YEO7.d.ts → esm/index-FMT0YEO7.d.ts} +0 -0
- /package/dist/{index.d.ts → esm/index.d.ts} +0 -0
- /package/dist/{migration → esm/migration}/index.d.ts +0 -0
- /package/dist/{migration → esm/migration}/type.d.ts +0 -0
- /package/dist/{migration → esm/migration}/type.js +0 -0
- /package/dist/{query → esm/query}/builder.d.ts +0 -0
- /package/dist/{query → esm/query}/condition.d.ts +0 -0
- /package/dist/{query → esm/query}/contract.d.ts +0 -0
- /package/dist/{query → esm/query}/contract.js +0 -0
- /package/dist/{query → esm/query}/helper.d.ts +0 -0
- /package/dist/{query → esm/query}/index.d.ts +0 -0
- /package/dist/{query → esm/query}/join.d.ts +0 -0
- /package/dist/{query → esm/query}/sql.d.ts +0 -0
- /package/dist/{query → esm/query}/types.d.ts +0 -0
- /package/dist/{query → esm/query}/types.js +0 -0
- /package/dist/{query → esm/query}/utilities.d.ts +0 -0
- /package/dist/{table → esm/table}/index.d.ts +0 -0
- /package/dist/{table → esm/table}/types.d.ts +0 -0
- /package/dist/{table → esm/table}/types.js +0 -0
- /package/dist/{table → esm/table}/utilities.d.ts +0 -0
- /package/dist/{types.js → esm/types.js} +0 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utilities$1 = require('../utilities');
|
|
4
|
+
var utilities = require('./utilities');
|
|
5
|
+
|
|
6
|
+
function buildSelectQuery(q) {
|
|
7
|
+
const from = utilities.getTableSelectName(q);
|
|
8
|
+
const columns = [];
|
|
9
|
+
if (q.definition.select?.length) {
|
|
10
|
+
for (const col of q.definition.select) {
|
|
11
|
+
if (typeof col === "object") {
|
|
12
|
+
const alias = utilities$1.quoteIdentifier(col.as.replace(/"/g, ""));
|
|
13
|
+
columns.push(`${col.column} AS ${alias}`);
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
if (!col.endsWith("*")) {
|
|
17
|
+
const alias = utilities$1.quoteIdentifier(col.replace(/"/g, ""));
|
|
18
|
+
columns.push(`${col} AS ${alias}`);
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
columns.push(col);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (q.definition?.aggregates) {
|
|
25
|
+
for (const aggregate of q.definition.aggregates) {
|
|
26
|
+
columns.push(
|
|
27
|
+
`${aggregate.fn}(${aggregate.column}) AS ${utilities$1.quoteIdentifier(aggregate.as)}`
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const distinct = q.definition.distinct ? "DISTINCT " : "";
|
|
32
|
+
return `SELECT ${distinct}${columns.join(", ")} FROM ${from}`;
|
|
33
|
+
}
|
|
34
|
+
function buildInsertQuery(q) {
|
|
35
|
+
const rows = q.definition?.insertValues;
|
|
36
|
+
if (!rows?.length) {
|
|
37
|
+
throw new Error(`INSERT requires values`);
|
|
38
|
+
}
|
|
39
|
+
const keys = Object.keys(rows[0]);
|
|
40
|
+
const columns = keys.map(utilities$1.quoteIdentifier).join(", ");
|
|
41
|
+
const rowPlaceholders = `(${keys.map(() => "?").join(", ")})`;
|
|
42
|
+
const placeholders = rows.map(() => rowPlaceholders).join(", ");
|
|
43
|
+
q.definition.params = rows.flatMap(
|
|
44
|
+
(row) => keys.map((key) => row[key])
|
|
45
|
+
);
|
|
46
|
+
return `INSERT INTO ${q.table.name} (${columns}) VALUES ${placeholders} RETURNING *`;
|
|
47
|
+
}
|
|
48
|
+
function buildUpdateQuery(q) {
|
|
49
|
+
if (!q.definition?.updateValues) {
|
|
50
|
+
throw new Error(`UPDATE requires values`);
|
|
51
|
+
}
|
|
52
|
+
let keys = Object.keys(q.definition.updateValues);
|
|
53
|
+
const updateParams = keys.map(
|
|
54
|
+
(key) => q.definition.updateValues[key]
|
|
55
|
+
);
|
|
56
|
+
keys = keys.map(utilities$1.quoteIdentifier);
|
|
57
|
+
if (q.definition?.params) {
|
|
58
|
+
q.definition.params = [...updateParams, ...q.definition.params];
|
|
59
|
+
} else {
|
|
60
|
+
q.definition.params = updateParams;
|
|
61
|
+
}
|
|
62
|
+
return `UPDATE ${q.table.name} SET ${keys.map((key) => `${key} = ?`.trim()).join(", ")}`;
|
|
63
|
+
}
|
|
64
|
+
function buildDeleteQuery(q) {
|
|
65
|
+
return `DELETE FROM ${q.table.name}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
exports.buildDeleteQuery = buildDeleteQuery;
|
|
69
|
+
exports.buildInsertQuery = buildInsertQuery;
|
|
70
|
+
exports.buildSelectQuery = buildSelectQuery;
|
|
71
|
+
exports.buildUpdateQuery = buildUpdateQuery;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { H as addCondition, B as addRawCondition, K as having, J as or, G as rawHaving, F as rawOr, E as rawWhere, I as where } from '../index-CwiFQh0I.cjs';
|
|
2
|
+
import '../column/index.cjs';
|
|
3
|
+
import './constants.cjs';
|
|
4
|
+
import '../column/constants.cjs';
|
|
5
|
+
import '../types.cjs';
|
|
6
|
+
import '../table/constants.cjs';
|
|
7
|
+
import '../column/types.cjs';
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var constants = require('./constants');
|
|
4
|
+
var helper = require('./helper');
|
|
5
|
+
var utilities = require('./utilities');
|
|
6
|
+
|
|
7
|
+
function addRawCondition(query, clause, column, logical, params) {
|
|
8
|
+
const validClause = clause.toLowerCase();
|
|
9
|
+
if (!query.definition[validClause]) query.definition[validClause] = [];
|
|
10
|
+
const condition = column(helper.rawCol);
|
|
11
|
+
const logicalPrefix = query.definition[validClause].length > 0 ? logical : "";
|
|
12
|
+
query.definition[validClause].push(`${logicalPrefix} ${condition}`.trim());
|
|
13
|
+
if (!query.definition.params) query.definition.params = [];
|
|
14
|
+
if (typeof params === "undefined") {
|
|
15
|
+
return query;
|
|
16
|
+
}
|
|
17
|
+
if (Array.isArray(params)) {
|
|
18
|
+
query.definition.params.push(...params);
|
|
19
|
+
} else {
|
|
20
|
+
query.definition.params.push(params);
|
|
21
|
+
}
|
|
22
|
+
return query;
|
|
23
|
+
}
|
|
24
|
+
function rawWhere(column, params) {
|
|
25
|
+
return addRawCondition(
|
|
26
|
+
this,
|
|
27
|
+
constants.ConditionClause.WHERE,
|
|
28
|
+
column,
|
|
29
|
+
constants.LogicalOperator.AND,
|
|
30
|
+
params
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
function rawOr(column, params) {
|
|
34
|
+
return addRawCondition(
|
|
35
|
+
this,
|
|
36
|
+
constants.ConditionClause.WHERE,
|
|
37
|
+
column,
|
|
38
|
+
constants.LogicalOperator.OR,
|
|
39
|
+
params
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
function rawHaving(column, params) {
|
|
43
|
+
return addRawCondition(
|
|
44
|
+
this,
|
|
45
|
+
constants.ConditionClause.HAVING,
|
|
46
|
+
column,
|
|
47
|
+
constants.LogicalOperator.AND,
|
|
48
|
+
params
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
function addCondition(query, clause, column, operator, value, logical) {
|
|
52
|
+
const validClause = clause.toLowerCase();
|
|
53
|
+
const condition = utilities.getCondition(query.table.dialect, column, operator, value);
|
|
54
|
+
if (!query.definition[validClause]) query.definition[validClause] = [];
|
|
55
|
+
const logicalPrefix = query.definition[validClause].length > 0 ? logical : "";
|
|
56
|
+
query.definition[validClause].push(`${logicalPrefix} ${condition}`.trim());
|
|
57
|
+
if (operator === constants.AcceptedOperator.IS_NULL || operator === constants.AcceptedOperator.IS_NOT_NULL) {
|
|
58
|
+
return query;
|
|
59
|
+
}
|
|
60
|
+
if (!query.definition.params) query.definition.params = [];
|
|
61
|
+
if (Array.isArray(value)) {
|
|
62
|
+
query.definition.params.push(...value);
|
|
63
|
+
} else {
|
|
64
|
+
query.definition.params.push(value);
|
|
65
|
+
}
|
|
66
|
+
return query;
|
|
67
|
+
}
|
|
68
|
+
function where(column, operator, value) {
|
|
69
|
+
return addCondition(
|
|
70
|
+
this,
|
|
71
|
+
constants.ConditionClause.WHERE,
|
|
72
|
+
column,
|
|
73
|
+
operator,
|
|
74
|
+
value,
|
|
75
|
+
constants.LogicalOperator.AND
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
function or(column, operator, value) {
|
|
79
|
+
return addCondition(
|
|
80
|
+
this,
|
|
81
|
+
constants.ConditionClause.WHERE,
|
|
82
|
+
column,
|
|
83
|
+
operator,
|
|
84
|
+
value,
|
|
85
|
+
constants.LogicalOperator.OR
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
function having(column, operator, value) {
|
|
89
|
+
return addCondition(
|
|
90
|
+
this,
|
|
91
|
+
constants.ConditionClause.HAVING,
|
|
92
|
+
column,
|
|
93
|
+
operator,
|
|
94
|
+
value,
|
|
95
|
+
constants.LogicalOperator.AND
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
exports.addCondition = addCondition;
|
|
100
|
+
exports.addRawCondition = addRawCondition;
|
|
101
|
+
exports.having = having;
|
|
102
|
+
exports.or = or;
|
|
103
|
+
exports.rawHaving = rawHaving;
|
|
104
|
+
exports.rawOr = rawOr;
|
|
105
|
+
exports.rawWhere = rawWhere;
|
|
106
|
+
exports.where = where;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const LogicalOperator = {
|
|
4
|
+
AND: "AND",
|
|
5
|
+
OR: "OR"
|
|
6
|
+
};
|
|
7
|
+
const ConditionClause = {
|
|
8
|
+
WHERE: "WHERE",
|
|
9
|
+
HAVING: "HAVING"
|
|
10
|
+
};
|
|
11
|
+
const AcceptedOperator = {
|
|
12
|
+
EQ: "eq",
|
|
13
|
+
NE: "ne",
|
|
14
|
+
GT: "gt",
|
|
15
|
+
LT: "lt",
|
|
16
|
+
GTE: "gte",
|
|
17
|
+
LTE: "lte",
|
|
18
|
+
IN: "in",
|
|
19
|
+
NOT_IN: "notIn",
|
|
20
|
+
LIKE: "like",
|
|
21
|
+
NOT_LIKE: "notLike",
|
|
22
|
+
ILIKE: "ilike",
|
|
23
|
+
NOT_ILIKE: "notILike",
|
|
24
|
+
IS_NULL: "isNull",
|
|
25
|
+
IS_NOT_NULL: "isNotNull",
|
|
26
|
+
BETWEEN: "between",
|
|
27
|
+
NOT_BETWEEN: "notBetween"
|
|
28
|
+
};
|
|
29
|
+
const QueryType = {
|
|
30
|
+
SELECT: "SELECT",
|
|
31
|
+
INSERT: "INSERT",
|
|
32
|
+
UPDATE: "UPDATE",
|
|
33
|
+
DELETE: "DELETE"
|
|
34
|
+
};
|
|
35
|
+
const OrderBy = {
|
|
36
|
+
ASC: "ASC",
|
|
37
|
+
DESC: "DESC"
|
|
38
|
+
};
|
|
39
|
+
const AggregationFunction = {
|
|
40
|
+
COUNT: "COUNT",
|
|
41
|
+
SUM: "SUM",
|
|
42
|
+
MIN: "MIN",
|
|
43
|
+
MAX: "MAX",
|
|
44
|
+
AVG: "AVG"
|
|
45
|
+
};
|
|
46
|
+
const AcceptedJoin = {
|
|
47
|
+
INNER: "INNER",
|
|
48
|
+
LEFT: "LEFT",
|
|
49
|
+
RIGHT: "RIGHT",
|
|
50
|
+
NATURAL: "NATURAL"
|
|
51
|
+
};
|
|
52
|
+
const QueryHooksType = {
|
|
53
|
+
AFTER: "after",
|
|
54
|
+
BEFORE: "before"
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
exports.AcceptedJoin = AcceptedJoin;
|
|
58
|
+
exports.AcceptedOperator = AcceptedOperator;
|
|
59
|
+
exports.AggregationFunction = AggregationFunction;
|
|
60
|
+
exports.ConditionClause = ConditionClause;
|
|
61
|
+
exports.LogicalOperator = LogicalOperator;
|
|
62
|
+
exports.OrderBy = OrderBy;
|
|
63
|
+
exports.QueryHooksType = QueryHooksType;
|
|
64
|
+
exports.QueryType = QueryType;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { N as QueryConditionContract, L as QueryTransformerContract } from '../index-CwiFQh0I.cjs';
|
|
2
|
+
import '../column/index.cjs';
|
|
3
|
+
import './constants.cjs';
|
|
4
|
+
import '../column/constants.cjs';
|
|
5
|
+
import '../types.cjs';
|
|
6
|
+
import '../table/constants.cjs';
|
|
7
|
+
import '../column/types.cjs';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { z as aggregateCol, v as alias, w as clone, y as col, x as rawCol } from '../index-CwiFQh0I.cjs';
|
|
2
|
+
import '../column/index.cjs';
|
|
3
|
+
import './constants.cjs';
|
|
4
|
+
import '../column/constants.cjs';
|
|
5
|
+
import '../types.cjs';
|
|
6
|
+
import '../table/constants.cjs';
|
|
7
|
+
import '../column/types.cjs';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('.');
|
|
4
|
+
var utilities = require('../utilities');
|
|
5
|
+
|
|
6
|
+
function alias(alias2) {
|
|
7
|
+
this.definition.baseAlias = alias2;
|
|
8
|
+
return this;
|
|
9
|
+
}
|
|
10
|
+
function clone() {
|
|
11
|
+
const query = new _.QueryBuilder(this.table);
|
|
12
|
+
Object.assign(query.definition, utilities.deepClone(this.definition));
|
|
13
|
+
return query;
|
|
14
|
+
}
|
|
15
|
+
function rawCol(column) {
|
|
16
|
+
return column;
|
|
17
|
+
}
|
|
18
|
+
function col(column, alias2) {
|
|
19
|
+
return {
|
|
20
|
+
column,
|
|
21
|
+
as: alias2
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function aggregateCol(fn, column, alias2) {
|
|
25
|
+
return {
|
|
26
|
+
column,
|
|
27
|
+
as: alias2 ?? fn.toLowerCase(),
|
|
28
|
+
fn
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
exports.aggregateCol = aggregateCol;
|
|
33
|
+
exports.alias = alias;
|
|
34
|
+
exports.clone = clone;
|
|
35
|
+
exports.col = col;
|
|
36
|
+
exports.rawCol = rawCol;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utilities = require('../utilities');
|
|
4
|
+
var condition = require('./condition');
|
|
5
|
+
var constants = require('./constants');
|
|
6
|
+
var helper = require('./helper');
|
|
7
|
+
var join = require('./join');
|
|
8
|
+
var sql = require('./sql');
|
|
9
|
+
var utilities$1 = require('./utilities');
|
|
10
|
+
|
|
11
|
+
class QueryBuilder {
|
|
12
|
+
hooks;
|
|
13
|
+
table;
|
|
14
|
+
definition;
|
|
15
|
+
_output;
|
|
16
|
+
alias;
|
|
17
|
+
clone;
|
|
18
|
+
toQuery;
|
|
19
|
+
toString;
|
|
20
|
+
exec;
|
|
21
|
+
rawWhere;
|
|
22
|
+
rawAnd;
|
|
23
|
+
rawOr;
|
|
24
|
+
rawHaving;
|
|
25
|
+
where;
|
|
26
|
+
and;
|
|
27
|
+
or;
|
|
28
|
+
having;
|
|
29
|
+
constructor(table) {
|
|
30
|
+
this.hooks = {};
|
|
31
|
+
this.table = table;
|
|
32
|
+
this.definition = {
|
|
33
|
+
queryType: null,
|
|
34
|
+
select: null,
|
|
35
|
+
having: null,
|
|
36
|
+
where: null,
|
|
37
|
+
params: null,
|
|
38
|
+
limit: null,
|
|
39
|
+
offset: null,
|
|
40
|
+
groupBy: null,
|
|
41
|
+
insertValues: null,
|
|
42
|
+
updateValues: null,
|
|
43
|
+
orderBy: null,
|
|
44
|
+
aggregates: null,
|
|
45
|
+
joins: null,
|
|
46
|
+
distinct: null,
|
|
47
|
+
baseAlias: table.name,
|
|
48
|
+
joinedTables: null,
|
|
49
|
+
withDeleted: null
|
|
50
|
+
};
|
|
51
|
+
this.alias = helper.alias.bind(this);
|
|
52
|
+
this.clone = helper.clone.bind(this);
|
|
53
|
+
this.toQuery = sql.toQuery.bind(this);
|
|
54
|
+
this.toString = sql.toString.bind(this);
|
|
55
|
+
this.exec = sql.exec.bind(this);
|
|
56
|
+
this.rawWhere = condition.rawWhere.bind(this);
|
|
57
|
+
this.rawHaving = condition.rawHaving.bind(this);
|
|
58
|
+
this.rawAnd = this.rawWhere;
|
|
59
|
+
this.rawOr = condition.rawOr.bind(this);
|
|
60
|
+
this.where = condition.where.bind(this);
|
|
61
|
+
this.having = condition.having.bind(this);
|
|
62
|
+
this.and = this.where;
|
|
63
|
+
this.or = condition.or.bind(this);
|
|
64
|
+
}
|
|
65
|
+
leftJoin(joinTable, alias2, baseColumn, joinColumn) {
|
|
66
|
+
return join.addJoin(
|
|
67
|
+
this,
|
|
68
|
+
constants.AcceptedJoin.LEFT,
|
|
69
|
+
alias2,
|
|
70
|
+
joinTable,
|
|
71
|
+
baseColumn,
|
|
72
|
+
joinColumn
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
rightJoin(joinTable, alias2, baseColumn, joinColumn) {
|
|
76
|
+
return join.addJoin(
|
|
77
|
+
this,
|
|
78
|
+
constants.AcceptedJoin.RIGHT,
|
|
79
|
+
alias2,
|
|
80
|
+
joinTable,
|
|
81
|
+
baseColumn,
|
|
82
|
+
joinColumn
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
innerJoin(joinTable, alias2, baseColumn, joinColumn) {
|
|
86
|
+
return join.addJoin(
|
|
87
|
+
this,
|
|
88
|
+
constants.AcceptedJoin.INNER,
|
|
89
|
+
alias2,
|
|
90
|
+
joinTable,
|
|
91
|
+
baseColumn,
|
|
92
|
+
joinColumn
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
naturalJoin(joinTable, alias2, baseColumn, joinColumn) {
|
|
96
|
+
return join.addJoin(
|
|
97
|
+
this,
|
|
98
|
+
constants.AcceptedJoin.NATURAL,
|
|
99
|
+
alias2,
|
|
100
|
+
joinTable,
|
|
101
|
+
baseColumn,
|
|
102
|
+
joinColumn
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
distinct() {
|
|
106
|
+
this.definition.distinct = true;
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
aggregate(...aggregates) {
|
|
110
|
+
this.definition.aggregates = aggregates.map(
|
|
111
|
+
(aggregate) => aggregate(helper.aggregateCol)
|
|
112
|
+
);
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
groupBy(...columns) {
|
|
116
|
+
this.definition.groupBy = columns;
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
limit(limit) {
|
|
120
|
+
this.definition.limit = limit;
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
offset(offset) {
|
|
124
|
+
this.definition.offset = offset;
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
orderBy(...orderBy) {
|
|
128
|
+
if (!this.definition.orderBy) this.definition.orderBy = [];
|
|
129
|
+
this.definition.orderBy.push(...orderBy);
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
withDeleted() {
|
|
133
|
+
this.definition.withDeleted = true;
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
select(...columns) {
|
|
137
|
+
if (!columns.length) {
|
|
138
|
+
const base = this.definition.baseAlias ?? this.table.name;
|
|
139
|
+
columns = Object.keys(this.table.columns).map(
|
|
140
|
+
(colName) => `${base}.${utilities.quoteIdentifier(colName)}`
|
|
141
|
+
);
|
|
142
|
+
} else {
|
|
143
|
+
columns = columns.map((column) => {
|
|
144
|
+
if (typeof column === "function") {
|
|
145
|
+
return column(helper.col);
|
|
146
|
+
}
|
|
147
|
+
return column;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
this.definition.select = columns;
|
|
151
|
+
this.definition.queryType = constants.QueryType.SELECT;
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
insert(...values) {
|
|
155
|
+
this.definition.queryType = constants.QueryType.INSERT;
|
|
156
|
+
if (!this.definition.insertValues) this.definition.insertValues = [];
|
|
157
|
+
const { isWithTimestamp, createdAt, updatedAt, timestamp } = utilities$1.getTimestamp(
|
|
158
|
+
this.table
|
|
159
|
+
);
|
|
160
|
+
if (isWithTimestamp) {
|
|
161
|
+
values = values.map((row) => ({
|
|
162
|
+
...row,
|
|
163
|
+
[createdAt]: row[createdAt] ?? timestamp,
|
|
164
|
+
[updatedAt]: row[updatedAt] ?? timestamp
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
this.definition.insertValues = values;
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
update(values) {
|
|
171
|
+
const { isWithTimestamp, updatedAt, timestamp } = utilities$1.getTimestamp(this.table);
|
|
172
|
+
if (isWithTimestamp) {
|
|
173
|
+
values = {
|
|
174
|
+
...values,
|
|
175
|
+
[updatedAt]: values[updatedAt] ?? timestamp
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
this.definition.queryType = constants.QueryType.UPDATE;
|
|
179
|
+
this.definition.updateValues = values;
|
|
180
|
+
return this;
|
|
181
|
+
}
|
|
182
|
+
delete() {
|
|
183
|
+
const { isWithParanoid, deletedAt, timestamp } = utilities$1.getParanoid(this.table);
|
|
184
|
+
if (isWithParanoid) {
|
|
185
|
+
return this.update({
|
|
186
|
+
[deletedAt]: timestamp
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
this.definition.queryType = constants.QueryType.DELETE;
|
|
190
|
+
return this;
|
|
191
|
+
}
|
|
192
|
+
infer() {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
exports.QueryBuilder = QueryBuilder;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { T as Table, Q as QueryDefinition, C as ColumnSelector, c as StrictColumnSelector, d as QueryBuilder } from '../index-CwiFQh0I.cjs';
|
|
2
|
+
import { Column } from '../column/index.cjs';
|
|
3
|
+
import { AcceptedJoin } from './constants.cjs';
|
|
4
|
+
import '../column/constants.cjs';
|
|
5
|
+
import '../types.cjs';
|
|
6
|
+
import '../table/constants.cjs';
|
|
7
|
+
import '../column/types.cjs';
|
|
8
|
+
|
|
9
|
+
declare function addJoin<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, JoinType extends AcceptedJoin, JoinTable extends Table<string, Record<string, Column>>, JoinAlias extends string, BaseColName extends `${Alias}."${keyof TableRef['columns'] & string}"`, JoinColName extends `${JoinAlias}."${keyof JoinTable['columns'] & string}"`, FinalJoinedTables extends JoinedTables & {
|
|
10
|
+
[K in JoinAlias]: JoinTable;
|
|
11
|
+
}>(query: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, joinType: JoinType, alias: JoinAlias, joinTable: JoinTable, baseColumn: BaseColName, joinColumn: JoinColName): QueryBuilder<Alias, TableRef, FinalJoinedTables, Omit<Definition, "joins" | "joinedTables"> & {
|
|
12
|
+
joins: string[];
|
|
13
|
+
joinedTables: FinalJoinedTables;
|
|
14
|
+
}>;
|
|
15
|
+
|
|
16
|
+
export { addJoin };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function addJoin(query, joinType, alias, joinTable, baseColumn, joinColumn) {
|
|
4
|
+
if (!query.definition.joins) query.definition.joins = [];
|
|
5
|
+
query.definition.joins.push(
|
|
6
|
+
`${joinType} JOIN ${joinTable.name} AS ${alias} ON ${baseColumn} = ${joinColumn}`
|
|
7
|
+
);
|
|
8
|
+
if (!query.definition.joinedTables) {
|
|
9
|
+
query.definition.joinedTables = {};
|
|
10
|
+
}
|
|
11
|
+
query.definition.joinedTables = {
|
|
12
|
+
...query.definition.joinedTables,
|
|
13
|
+
[alias]: joinTable
|
|
14
|
+
};
|
|
15
|
+
return query;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
exports.addJoin = addJoin;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { T as Table, Q as QueryDefinition, C as ColumnSelector, c as StrictColumnSelector, d as QueryBuilder } from '../index-CwiFQh0I.cjs';
|
|
2
|
+
import { Column } from '../column/index.cjs';
|
|
3
|
+
import '../column/constants.cjs';
|
|
4
|
+
import '../types.cjs';
|
|
5
|
+
import './constants.cjs';
|
|
6
|
+
import '../table/constants.cjs';
|
|
7
|
+
import '../column/types.cjs';
|
|
8
|
+
|
|
9
|
+
declare function buildQuery(query: string): string;
|
|
10
|
+
declare function toQuery<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>(this: Query): {
|
|
11
|
+
query: string;
|
|
12
|
+
params: unknown[] | null | undefined;
|
|
13
|
+
};
|
|
14
|
+
declare function toString<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>(this: Query): string;
|
|
15
|
+
declare function exec<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, Output extends Query['_output'] = Query['_output']>(this: Query): Promise<Output>;
|
|
16
|
+
|
|
17
|
+
export { buildQuery, exec, toQuery, toString };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var builder = require('./builder');
|
|
4
|
+
var constants = require('./constants');
|
|
5
|
+
var utilities = require('./utilities');
|
|
6
|
+
|
|
7
|
+
function buildQuery(query) {
|
|
8
|
+
let index = 0;
|
|
9
|
+
return query.replace(/\?/g, () => {
|
|
10
|
+
index++;
|
|
11
|
+
return `$${index}`;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function toQuery() {
|
|
15
|
+
let sql = "";
|
|
16
|
+
switch (this.definition.queryType) {
|
|
17
|
+
case constants.QueryType.SELECT:
|
|
18
|
+
sql = builder.buildSelectQuery(this);
|
|
19
|
+
break;
|
|
20
|
+
case constants.QueryType.INSERT:
|
|
21
|
+
sql = builder.buildInsertQuery(this);
|
|
22
|
+
break;
|
|
23
|
+
case constants.QueryType.UPDATE:
|
|
24
|
+
sql = builder.buildUpdateQuery(this);
|
|
25
|
+
break;
|
|
26
|
+
case constants.QueryType.DELETE:
|
|
27
|
+
sql = builder.buildDeleteQuery(this);
|
|
28
|
+
break;
|
|
29
|
+
default:
|
|
30
|
+
throw new Error("No query type defined");
|
|
31
|
+
}
|
|
32
|
+
if (this.definition?.joins?.length) {
|
|
33
|
+
sql += ` ${this.definition.joins.join(" ")}`;
|
|
34
|
+
}
|
|
35
|
+
const whereConditions = utilities.getWhereConditions(this);
|
|
36
|
+
if (whereConditions.length) {
|
|
37
|
+
sql += ` WHERE ${whereConditions.join(" ")}`;
|
|
38
|
+
}
|
|
39
|
+
const groupByConditions = utilities.getGroupByConditions(this);
|
|
40
|
+
if (groupByConditions.length) {
|
|
41
|
+
sql += ` GROUP BY ${groupByConditions.join(", ")}`;
|
|
42
|
+
}
|
|
43
|
+
if (this.definition?.having?.length) {
|
|
44
|
+
sql += ` HAVING ${this.definition.having.join(" ")}`;
|
|
45
|
+
}
|
|
46
|
+
if (this.definition?.orderBy?.length) {
|
|
47
|
+
sql += ` ORDER BY ${this.definition.orderBy.map((order) => [order.column, order.direction].join(" ")).join(", ")}`;
|
|
48
|
+
}
|
|
49
|
+
if (this.definition?.limit !== null) {
|
|
50
|
+
sql += ` LIMIT ?`;
|
|
51
|
+
if (!this.definition.params) this.definition.params = [];
|
|
52
|
+
this.definition.params.push(this.definition.limit);
|
|
53
|
+
}
|
|
54
|
+
if (this.definition?.offset !== null) {
|
|
55
|
+
sql += ` OFFSET ?`;
|
|
56
|
+
if (!this.definition.params) this.definition.params = [];
|
|
57
|
+
this.definition.params.push(this.definition.offset);
|
|
58
|
+
}
|
|
59
|
+
if (this.definition.queryType === constants.QueryType.UPDATE || this.definition.queryType === constants.QueryType.DELETE) {
|
|
60
|
+
sql += ` RETURNING *`;
|
|
61
|
+
}
|
|
62
|
+
sql = buildQuery(sql);
|
|
63
|
+
return { query: sql + ";", params: this.definition.params };
|
|
64
|
+
}
|
|
65
|
+
function toString() {
|
|
66
|
+
return this.toQuery().query;
|
|
67
|
+
}
|
|
68
|
+
async function exec() {
|
|
69
|
+
if (!this.table.client) throw new Error("Database client not defined");
|
|
70
|
+
const { query, params } = this.toQuery();
|
|
71
|
+
if (this.hooks?.before?.size) {
|
|
72
|
+
for (const hook of this.hooks.before.values()) {
|
|
73
|
+
hook({
|
|
74
|
+
query,
|
|
75
|
+
params,
|
|
76
|
+
type: this.definition.queryType,
|
|
77
|
+
hook: constants.QueryHooksType.BEFORE
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const result = await this.table.client.exec(query, params);
|
|
82
|
+
if (this.hooks?.after?.size) {
|
|
83
|
+
for (const hook of this.hooks.after.values()) {
|
|
84
|
+
hook({
|
|
85
|
+
query,
|
|
86
|
+
params,
|
|
87
|
+
type: this.definition.queryType,
|
|
88
|
+
hook: constants.QueryHooksType.AFTER
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return result.map(
|
|
93
|
+
(r) => utilities.parseAliasedRow({
|
|
94
|
+
row: r,
|
|
95
|
+
selects: this.definition.select ?? [],
|
|
96
|
+
root: this.definition?.baseAlias ?? this.table.name
|
|
97
|
+
})
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
exports.buildQuery = buildQuery;
|
|
102
|
+
exports.exec = exec;
|
|
103
|
+
exports.toQuery = toQuery;
|
|
104
|
+
exports.toString = toString;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import '../column/index.cjs';
|
|
2
|
+
import '../column/constants.cjs';
|
|
3
|
+
export { j as AcceptedInsertValues, i as AcceptedOrderBy, k as AcceptedUpdateValues, l as AggregateColumn, A as AliasedColumn, C as ColumnSelector, f as QuerHooks, Q as QueryDefinition, n as QueryOutput, h as QueryRunHooks, o as QueryRunHooksOptions, R as RawColumn, m as SelectQueryOutput, e as SelectableColumn, c as StrictColumnSelector, W as WhereValue } from '../index-CwiFQh0I.cjs';
|
|
4
|
+
import '../types.cjs';
|
|
5
|
+
import './constants.cjs';
|
|
6
|
+
import '../table/constants.cjs';
|
|
7
|
+
import '../column/types.cjs';
|