@ignisia/sql 0.1.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.
Files changed (72) hide show
  1. package/README.md +8 -8
  2. package/dist/column/constants.js +97 -8
  3. package/dist/column/index.js +105 -8
  4. package/dist/column/types.js +1 -2
  5. package/dist/database/alter.d.ts +3 -3
  6. package/dist/database/alter.js +87 -15
  7. package/dist/database/column.d.ts +3 -3
  8. package/dist/database/column.js +33 -11
  9. package/dist/database/contract.d.ts +3 -3
  10. package/dist/database/contract.js +1 -0
  11. package/dist/database/index.d.ts +3 -3
  12. package/dist/database/index.js +92 -19
  13. package/dist/database/table.d.ts +3 -3
  14. package/dist/database/table.js +37 -19
  15. package/dist/database/types.d.ts +1 -1
  16. package/dist/database/types.js +1 -0
  17. package/dist/database/wrapper.d.ts +1 -1
  18. package/dist/database/wrapper.js +92 -9
  19. package/dist/{index-Dcm5xIpR.d.ts → index-DFrpzXEn.d.ts} +5 -1
  20. package/dist/{index-DJhQVUY3.d.ts → index-FMT0YEO7.d.ts} +17 -3
  21. package/dist/index.d.ts +3 -3
  22. package/dist/index.js +5 -32
  23. package/dist/migration/index.d.ts +3 -3
  24. package/dist/migration/index.js +48 -6
  25. package/dist/migration/runner.js +8 -21
  26. package/dist/migration/type.d.ts +3 -3
  27. package/dist/migration/type.js +1 -0
  28. package/dist/query/builder.d.ts +1 -1
  29. package/dist/query/builder.js +66 -16
  30. package/dist/query/condition.d.ts +1 -1
  31. package/dist/query/condition.js +97 -24
  32. package/dist/query/constants.d.ts +6 -1
  33. package/dist/query/constants.js +54 -17
  34. package/dist/query/contract.d.ts +1 -1
  35. package/dist/query/contract.js +1 -0
  36. package/dist/query/helper.d.ts +1 -1
  37. package/dist/query/helper.js +30 -18
  38. package/dist/query/index.d.ts +1 -1
  39. package/dist/query/index.js +195 -10
  40. package/dist/query/join.d.ts +1 -1
  41. package/dist/query/join.js +16 -6
  42. package/dist/query/sql.d.ts +1 -1
  43. package/dist/query/sql.js +99 -16
  44. package/dist/query/types.d.ts +1 -1
  45. package/dist/query/types.js +1 -0
  46. package/dist/query/utilities.d.ts +1 -1
  47. package/dist/query/utilities.js +175 -24
  48. package/dist/table/constants.js +5 -5
  49. package/dist/table/index.d.ts +1 -1
  50. package/dist/table/index.js +52 -14
  51. package/dist/table/types.d.ts +1 -1
  52. package/dist/table/types.js +1 -0
  53. package/dist/table/utilities.d.ts +1 -1
  54. package/dist/table/utilities.js +50 -15
  55. package/dist/types.js +1 -0
  56. package/dist/utilities.js +18 -8
  57. package/package.json +37 -2
  58. package/dist/chunk-4DQRB5XS.js +0 -94
  59. package/dist/chunk-CIWX3UCZ.js +0 -51
  60. package/dist/chunk-D2ASIT4Q.js +0 -44
  61. package/dist/chunk-FYSNJAGD.js +0 -19
  62. package/dist/chunk-G3LSCLIQ.js +0 -104
  63. package/dist/chunk-GLOHF5CP.js +0 -9
  64. package/dist/chunk-GY7R637S.js +0 -113
  65. package/dist/chunk-HKTHKQLK.js +0 -98
  66. package/dist/chunk-JF7OSNH4.js +0 -40
  67. package/dist/chunk-MG2S4V4N.js +0 -60
  68. package/dist/chunk-TQ2GXAE7.js +0 -663
  69. package/dist/chunk-V4OMHVJN.js +0 -96
  70. package/dist/chunk-W2DR3ZVK.js +0 -59
  71. package/dist/chunk-WVJGTZFI.js +0 -60
  72. package/dist/chunk-Y7FSRHH3.js +0 -22
@@ -1,18 +1,55 @@
1
- import {
2
- AcceptedJoin,
3
- AcceptedOperator,
4
- AggregationFunction,
5
- ConditionClause,
6
- LogicalOperator,
7
- OrderBy,
8
- QueryType
9
- } from "../chunk-MG2S4V4N.js";
10
- export {
11
- AcceptedJoin,
12
- AcceptedOperator,
13
- AggregationFunction,
14
- ConditionClause,
15
- LogicalOperator,
16
- OrderBy,
17
- QueryType
1
+ const LogicalOperator = {
2
+ AND: "AND",
3
+ OR: "OR"
18
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 };
@@ -1,4 +1,4 @@
1
- export { J as QueryConditionContract, I as QueryTransformerContract } from '../index-DJhQVUY3.js';
1
+ export { N as QueryConditionContract, L as QueryTransformerContract } from '../index-FMT0YEO7.js';
2
2
  import '../column/index.js';
3
3
  import './constants.js';
4
4
  import '../column/constants.js';
@@ -0,0 +1 @@
1
+
@@ -1,4 +1,4 @@
1
- export { w as aggregateCol, r as alias, s as clone, v as col, t as rawCol } from '../index-DJhQVUY3.js';
1
+ export { z as aggregateCol, v as alias, w as clone, y as col, x as rawCol } from '../index-FMT0YEO7.js';
2
2
  import '../column/index.js';
3
3
  import './constants.js';
4
4
  import '../column/constants.js';
@@ -1,18 +1,30 @@
1
- import {
2
- aggregateCol,
3
- alias,
4
- clone,
5
- col,
6
- rawCol
7
- } from "../chunk-TQ2GXAE7.js";
8
- import "../chunk-MG2S4V4N.js";
9
- import "../chunk-FYSNJAGD.js";
10
- import "../chunk-Y7FSRHH3.js";
11
- import "../chunk-GLOHF5CP.js";
12
- export {
13
- aggregateCol,
14
- alias,
15
- clone,
16
- col,
17
- rawCol
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 };
@@ -1,5 +1,5 @@
1
1
  import '../column/index.js';
2
- export { d as QueryBuilder } from '../index-DJhQVUY3.js';
2
+ export { d as QueryBuilder } from '../index-FMT0YEO7.js';
3
3
  import './constants.js';
4
4
  import '../table/constants.js';
5
5
  import '../column/constants.js';
@@ -1,10 +1,195 @@
1
- import {
2
- QueryBuilder
3
- } from "../chunk-TQ2GXAE7.js";
4
- import "../chunk-MG2S4V4N.js";
5
- import "../chunk-FYSNJAGD.js";
6
- import "../chunk-Y7FSRHH3.js";
7
- import "../chunk-GLOHF5CP.js";
8
- export {
9
- QueryBuilder
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 };
@@ -1,4 +1,4 @@
1
- import { T as Table, Q as QueryDefinition, C as ColumnSelector, c as StrictColumnSelector, d as QueryBuilder } from '../index-DJhQVUY3.js';
1
+ import { T as Table, Q as QueryDefinition, C as ColumnSelector, c as StrictColumnSelector, d as QueryBuilder } from '../index-FMT0YEO7.js';
2
2
  import { Column } from '../column/index.js';
3
3
  import { AcceptedJoin } from './constants.js';
4
4
  import '../column/constants.js';
@@ -1,6 +1,16 @@
1
- import {
2
- addJoin
3
- } from "../chunk-FYSNJAGD.js";
4
- export {
5
- addJoin
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 };
@@ -1,4 +1,4 @@
1
- import { T as Table, Q as QueryDefinition, C as ColumnSelector, c as StrictColumnSelector, d as QueryBuilder } from '../index-DJhQVUY3.js';
1
+ import { T as Table, Q as QueryDefinition, C as ColumnSelector, c as StrictColumnSelector, d as QueryBuilder } from '../index-FMT0YEO7.js';
2
2
  import { Column } from '../column/index.js';
3
3
  import '../column/constants.js';
4
4
  import '../types.js';
package/dist/query/sql.js CHANGED
@@ -1,16 +1,99 @@
1
- import {
2
- buildQuery,
3
- exec,
4
- toQuery,
5
- toString
6
- } from "../chunk-TQ2GXAE7.js";
7
- import "../chunk-MG2S4V4N.js";
8
- import "../chunk-FYSNJAGD.js";
9
- import "../chunk-Y7FSRHH3.js";
10
- import "../chunk-GLOHF5CP.js";
11
- export {
12
- buildQuery,
13
- exec,
14
- toQuery,
15
- toString
16
- };
1
+ import { buildDeleteQuery, buildUpdateQuery, buildInsertQuery, buildSelectQuery } from './builder';
2
+ import { QueryType, QueryHooksType } from './constants';
3
+ import { getWhereConditions, getGroupByConditions, parseAliasedRow } from './utilities';
4
+
5
+ function buildQuery(query) {
6
+ let index = 0;
7
+ return query.replace(/\?/g, () => {
8
+ index++;
9
+ return `$${index}`;
10
+ });
11
+ }
12
+ function toQuery() {
13
+ let sql = "";
14
+ switch (this.definition.queryType) {
15
+ case QueryType.SELECT:
16
+ sql = buildSelectQuery(this);
17
+ break;
18
+ case QueryType.INSERT:
19
+ sql = buildInsertQuery(this);
20
+ break;
21
+ case QueryType.UPDATE:
22
+ sql = buildUpdateQuery(this);
23
+ break;
24
+ case QueryType.DELETE:
25
+ sql = buildDeleteQuery(this);
26
+ break;
27
+ default:
28
+ throw new Error("No query type defined");
29
+ }
30
+ if (this.definition?.joins?.length) {
31
+ sql += ` ${this.definition.joins.join(" ")}`;
32
+ }
33
+ const whereConditions = getWhereConditions(this);
34
+ if (whereConditions.length) {
35
+ sql += ` WHERE ${whereConditions.join(" ")}`;
36
+ }
37
+ const groupByConditions = getGroupByConditions(this);
38
+ if (groupByConditions.length) {
39
+ sql += ` GROUP BY ${groupByConditions.join(", ")}`;
40
+ }
41
+ if (this.definition?.having?.length) {
42
+ sql += ` HAVING ${this.definition.having.join(" ")}`;
43
+ }
44
+ if (this.definition?.orderBy?.length) {
45
+ sql += ` ORDER BY ${this.definition.orderBy.map((order) => [order.column, order.direction].join(" ")).join(", ")}`;
46
+ }
47
+ if (this.definition?.limit !== null) {
48
+ sql += ` LIMIT ?`;
49
+ if (!this.definition.params) this.definition.params = [];
50
+ this.definition.params.push(this.definition.limit);
51
+ }
52
+ if (this.definition?.offset !== null) {
53
+ sql += ` OFFSET ?`;
54
+ if (!this.definition.params) this.definition.params = [];
55
+ this.definition.params.push(this.definition.offset);
56
+ }
57
+ if (this.definition.queryType === QueryType.UPDATE || this.definition.queryType === QueryType.DELETE) {
58
+ sql += ` RETURNING *`;
59
+ }
60
+ sql = buildQuery(sql);
61
+ return { query: sql + ";", params: this.definition.params };
62
+ }
63
+ function toString() {
64
+ return this.toQuery().query;
65
+ }
66
+ async function exec() {
67
+ if (!this.table.client) throw new Error("Database client not defined");
68
+ const { query, params } = this.toQuery();
69
+ if (this.hooks?.before?.size) {
70
+ for (const hook of this.hooks.before.values()) {
71
+ hook({
72
+ query,
73
+ params,
74
+ type: this.definition.queryType,
75
+ hook: QueryHooksType.BEFORE
76
+ });
77
+ }
78
+ }
79
+ const result = await this.table.client.exec(query, params);
80
+ if (this.hooks?.after?.size) {
81
+ for (const hook of this.hooks.after.values()) {
82
+ hook({
83
+ query,
84
+ params,
85
+ type: this.definition.queryType,
86
+ hook: QueryHooksType.AFTER
87
+ });
88
+ }
89
+ }
90
+ return result.map(
91
+ (r) => parseAliasedRow({
92
+ row: r,
93
+ selects: this.definition.select ?? [],
94
+ root: this.definition?.baseAlias ?? this.table.name
95
+ })
96
+ );
97
+ }
98
+
99
+ export { buildQuery, exec, toQuery, toString };
@@ -1,6 +1,6 @@
1
1
  import '../column/index.js';
2
2
  import '../column/constants.js';
3
- export { h as AcceptedInsertValues, g as AcceptedOrderBy, i as AcceptedUpdateValues, j as AggregateColumn, A as AliasedColumn, C as ColumnSelector, Q as QueryDefinition, l as QueryOutput, R as RawColumn, k as SelectQueryOutput, e as SelectableColumn, c as StrictColumnSelector, W as WhereValue } from '../index-DJhQVUY3.js';
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-FMT0YEO7.js';
4
4
  import '../types.js';
5
5
  import './constants.js';
6
6
  import '../table/constants.js';
@@ -0,0 +1 @@
1
+
@@ -1,4 +1,4 @@
1
- import { T as Table, W as WhereValue, Q as QueryDefinition, C as ColumnSelector, c as StrictColumnSelector, d as QueryBuilder, A as AliasedColumn, e as SelectableColumn } from '../index-DJhQVUY3.js';
1
+ import { T as Table, W as WhereValue, Q as QueryDefinition, C as ColumnSelector, c as StrictColumnSelector, d as QueryBuilder, A as AliasedColumn, e as SelectableColumn } from '../index-FMT0YEO7.js';
2
2
  import { Column } from '../column/index.js';
3
3
  import { Dialect } from '../table/constants.js';
4
4
  import { AcceptedOperator } from './constants.js';