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