@ignisia/sql 0.2.1 → 0.3.0

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 (138) hide show
  1. package/dist/cjs/column/constants.d.cts +150 -0
  2. package/dist/cjs/column/constants.js +158 -0
  3. package/dist/cjs/column/index.d.cts +43 -0
  4. package/dist/cjs/column/index.js +127 -0
  5. package/dist/cjs/column/types.d.cts +43 -0
  6. package/dist/cjs/column/types.js +4 -0
  7. package/dist/cjs/column/utils.d.cts +133 -0
  8. package/dist/cjs/column/utils.js +34 -0
  9. package/dist/cjs/database/alter.d.cts +53 -0
  10. package/dist/cjs/database/alter.js +93 -0
  11. package/dist/cjs/database/column.d.cts +25 -0
  12. package/dist/cjs/database/column.js +39 -0
  13. package/dist/cjs/database/contract.d.cts +9 -0
  14. package/dist/cjs/database/contract.js +2 -0
  15. package/dist/cjs/database/index.d.cts +9 -0
  16. package/dist/cjs/database/index.js +107 -0
  17. package/dist/cjs/database/table.d.cts +22 -0
  18. package/dist/cjs/database/table.js +49 -0
  19. package/dist/cjs/database/types.d.cts +8 -0
  20. package/dist/cjs/database/types.js +2 -0
  21. package/dist/cjs/database/wrapper.d.cts +32 -0
  22. package/dist/cjs/database/wrapper.js +87 -0
  23. package/dist/cjs/index-2jl8MRfX.d.cts +105 -0
  24. package/dist/cjs/index-CnQVnCEI.d.cts +392 -0
  25. package/dist/cjs/index.d.cts +11 -0
  26. package/dist/cjs/index.js +30 -0
  27. package/dist/cjs/migration/index.d.cts +31 -0
  28. package/dist/{migration → cjs/migration}/index.js +3 -1
  29. package/dist/cjs/migration/runner.js +58 -0
  30. package/dist/cjs/migration/type.d.cts +20 -0
  31. package/dist/cjs/migration/type.js +2 -0
  32. package/dist/cjs/package.json +3 -0
  33. package/dist/cjs/query/builder.d.cts +15 -0
  34. package/dist/cjs/query/builder.js +71 -0
  35. package/dist/cjs/query/condition.d.cts +8 -0
  36. package/dist/cjs/query/condition.js +109 -0
  37. package/dist/cjs/query/constants.js +64 -0
  38. package/dist/cjs/query/contract.d.cts +8 -0
  39. package/dist/cjs/query/contract.js +2 -0
  40. package/dist/cjs/query/helper.d.cts +8 -0
  41. package/dist/cjs/query/helper.js +36 -0
  42. package/dist/cjs/query/index.d.cts +8 -0
  43. package/dist/cjs/query/index.js +206 -0
  44. package/dist/cjs/query/join.d.cts +17 -0
  45. package/dist/cjs/query/join.js +18 -0
  46. package/dist/cjs/query/sql.d.cts +18 -0
  47. package/dist/cjs/query/sql.js +132 -0
  48. package/dist/cjs/query/types.d.cts +8 -0
  49. package/dist/cjs/query/types.js +2 -0
  50. package/dist/cjs/query/utilities.d.cts +37 -0
  51. package/dist/cjs/query/utilities.js +194 -0
  52. package/dist/cjs/table/constants.d.cts +8 -0
  53. package/dist/cjs/table/constants.js +9 -0
  54. package/dist/cjs/table/index.d.cts +8 -0
  55. package/dist/cjs/table/index.js +74 -0
  56. package/dist/cjs/table/types.d.cts +8 -0
  57. package/dist/cjs/table/types.js +2 -0
  58. package/dist/cjs/table/utilities.d.cts +8 -0
  59. package/dist/cjs/table/utilities.js +55 -0
  60. package/dist/cjs/types.js +2 -0
  61. package/dist/cjs/utilities.js +21 -0
  62. package/dist/{column → esm/column}/constants.d.ts +58 -5
  63. package/dist/esm/column/constants.js +154 -0
  64. package/dist/{column → esm/column}/index.d.ts +1 -0
  65. package/dist/{column → esm/column}/index.js +36 -15
  66. package/dist/esm/column/types.js +1 -0
  67. package/dist/esm/column/utils.d.ts +133 -0
  68. package/dist/esm/column/utils.js +32 -0
  69. package/dist/{database → esm/database}/alter.d.ts +3 -2
  70. package/dist/{database → esm/database}/alter.js +17 -16
  71. package/dist/{database → esm/database}/column.d.ts +3 -2
  72. package/dist/{database → esm/database}/column.js +11 -8
  73. package/dist/{database → esm/database}/contract.d.ts +3 -2
  74. package/dist/{database → esm/database}/index.d.ts +3 -2
  75. package/dist/{database → esm/database}/index.js +24 -10
  76. package/dist/{database → esm/database}/table.d.ts +3 -2
  77. package/dist/{database → esm/database}/table.js +14 -5
  78. package/dist/esm/database/types.d.ts +8 -0
  79. package/dist/esm/database/wrapper.d.ts +32 -0
  80. package/dist/esm/database/wrapper.js +83 -0
  81. package/dist/{index-DFrpzXEn.d.ts → esm/index-BXOAxB_h.d.ts} +4 -2
  82. package/dist/{index-FMT0YEO7.d.ts → esm/index-BdpoD4zk.d.ts} +80 -46
  83. package/dist/{index.d.ts → esm/index.d.ts} +3 -2
  84. package/dist/esm/index.js +5 -0
  85. package/dist/{migration → esm/migration}/index.d.ts +3 -2
  86. package/dist/esm/migration/index.js +49 -0
  87. package/dist/esm/migration/runner.d.ts +3 -0
  88. package/dist/{migration → esm/migration}/runner.js +7 -6
  89. package/dist/{migration → esm/migration}/type.d.ts +3 -2
  90. package/dist/{query → esm/query}/builder.d.ts +2 -1
  91. package/dist/{query → esm/query}/builder.js +3 -2
  92. package/dist/esm/query/condition.d.ts +8 -0
  93. package/dist/{query → esm/query}/condition.js +9 -5
  94. package/dist/esm/query/constants.d.ts +63 -0
  95. package/dist/{query → esm/query}/constants.js +9 -8
  96. package/dist/{query → esm/query}/contract.d.ts +3 -2
  97. package/dist/{query → esm/query}/helper.d.ts +2 -1
  98. package/dist/{query → esm/query}/helper.js +3 -2
  99. package/dist/{query → esm/query}/index.d.ts +2 -1
  100. package/dist/{query → esm/query}/index.js +26 -16
  101. package/dist/{query → esm/query}/join.d.ts +2 -1
  102. package/dist/{query → esm/query}/join.js +1 -0
  103. package/dist/{query → esm/query}/sql.d.ts +5 -4
  104. package/dist/{query → esm/query}/sql.js +40 -11
  105. package/dist/esm/query/types.d.ts +8 -0
  106. package/dist/{query → esm/query}/utilities.d.ts +4 -1
  107. package/dist/{query → esm/query}/utilities.js +20 -9
  108. package/dist/{table → esm/table}/constants.d.ts +1 -0
  109. package/dist/{table → esm/table}/constants.js +3 -1
  110. package/dist/{table → esm/table}/index.d.ts +2 -1
  111. package/dist/{table → esm/table}/index.js +35 -14
  112. package/dist/{table → esm/table}/types.d.ts +2 -1
  113. package/dist/{table → esm/table}/utilities.d.ts +2 -1
  114. package/dist/{table → esm/table}/utilities.js +5 -4
  115. package/dist/esm/types.d.ts +3 -0
  116. package/dist/esm/utilities.d.ts +4 -0
  117. package/dist/{utilities.js → esm/utilities.js} +1 -0
  118. package/package.json +42 -19
  119. package/dist/column/constants.js +0 -98
  120. package/dist/column/types.js +0 -1
  121. package/dist/database/types.d.ts +0 -7
  122. package/dist/database/wrapper.d.ts +0 -36
  123. package/dist/database/wrapper.js +0 -92
  124. package/dist/index.js +0 -5
  125. package/dist/query/condition.d.ts +0 -7
  126. package/dist/query/types.d.ts +0 -7
  127. /package/dist/{migration/runner.d.ts → cjs/migration/runner.d.cts} +0 -0
  128. /package/dist/{query/constants.d.ts → cjs/query/constants.d.cts} +0 -0
  129. /package/dist/{types.d.ts → cjs/types.d.cts} +0 -0
  130. /package/dist/{utilities.d.ts → cjs/utilities.d.cts} +0 -0
  131. /package/dist/{column → esm/column}/types.d.ts +0 -0
  132. /package/dist/{database → esm/database}/contract.js +0 -0
  133. /package/dist/{database → esm/database}/types.js +0 -0
  134. /package/dist/{migration → esm/migration}/type.js +0 -0
  135. /package/dist/{query → esm/query}/contract.js +0 -0
  136. /package/dist/{query → esm/query}/types.js +0 -0
  137. /package/dist/{table → esm/table}/types.js +0 -0
  138. /package/dist/{types.js → esm/types.js} +0 -0
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ var column = require('../column');
4
+ var table = require('../table');
5
+
6
+ const nameColumn = column.Column.define({
7
+ type: "TEXT",
8
+ length: 255
9
+ }).primaryKey().notNull();
10
+ const runAtColumn = column.Column.define({
11
+ type: "DATETIME"
12
+ }).notNull();
13
+ let migrationTable = null;
14
+ async function runMigration(filePath, direction) {
15
+ const migrationFile = await import(filePath);
16
+ const fileName = filePath.split("/").pop();
17
+ if (!migrationFile || !migrationFile.migration) {
18
+ throw new Error(
19
+ 'Invalid migration file! A migration file must export a migration as "migration"'
20
+ );
21
+ }
22
+ const { migration } = migrationFile;
23
+ if (!migrationTable) {
24
+ migrationTable = table.Table.define({
25
+ name: "ignisia_migration",
26
+ columns: {
27
+ name: nameColumn,
28
+ runAt: runAtColumn
29
+ },
30
+ dialect: migration.db.dialect
31
+ });
32
+ migrationTable.client = migration.db.client;
33
+ await migrationTable.create({});
34
+ }
35
+ const fn = migration[direction];
36
+ if (!fn)
37
+ throw new Error("No migration function found for direction: " + direction);
38
+ const [lastRun] = await migrationTable.query().select("ignisia_migration.*").limit(1).where('ignisia_migration."name"', "eq", fileName).exec();
39
+ if (lastRun) {
40
+ console.log("Migration already run!");
41
+ console.log("Run At : " + new Date(lastRun.runAt).toDateString());
42
+ console.log("Name : " + fileName);
43
+ return;
44
+ }
45
+ const query = migrationTable.query();
46
+ await migration.db.client.transaction(async () => {
47
+ await fn();
48
+ if (direction === "up") {
49
+ return query.insert({
50
+ name: fileName,
51
+ runAt: (/* @__PURE__ */ new Date()).toISOString()
52
+ }).exec();
53
+ }
54
+ return query.delete().where('ignisia_migration."name"', "eq", fileName).exec();
55
+ });
56
+ }
57
+
58
+ exports.runMigration = runMigration;
@@ -0,0 +1,20 @@
1
+ import { Column } from '../column/index.cjs';
2
+ import { D as Database } from '../index-2jl8MRfX.cjs';
3
+ import { T as Table } from '../index-CnQVnCEI.cjs';
4
+ import { Dialect } from '../table/constants.cjs';
5
+ import '../column/constants.cjs';
6
+ import '../column/types.cjs';
7
+ import 'bun';
8
+ import '../query/constants.cjs';
9
+ import '../types.cjs';
10
+
11
+ interface MigrationOptions<DbDialect extends Dialect, Tables extends Record<string, Table<string, Record<string, Column>>>> {
12
+ db: Database<DbDialect, Tables>;
13
+ up: (() => Promise<void>) | null;
14
+ down: (() => Promise<void>) | null;
15
+ }
16
+ interface MigrationFn<DbDialect extends Dialect, Tables extends Record<string, Table<string, Record<string, Column>>>> {
17
+ (db: Database<DbDialect, Tables>): Promise<void>;
18
+ }
19
+
20
+ export type { MigrationFn, MigrationOptions };
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,15 @@
1
+ import { T as Table, Q as QueryDefinition, C as ColumnSelector, e as StrictColumnSelector, f as QueryBuilder } from '../index-CnQVnCEI.cjs';
2
+ import { Column } from '../column/index.cjs';
3
+ import '../column/constants.cjs';
4
+ import '../types.cjs';
5
+ import './constants.cjs';
6
+ import 'bun';
7
+ import '../table/constants.cjs';
8
+ import '../column/types.cjs';
9
+
10
+ declare function buildSelectQuery<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>>(q: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>): string;
11
+ declare function buildInsertQuery<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>>(q: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>): string;
12
+ declare function buildUpdateQuery<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>>(q: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>): string;
13
+ declare function buildDeleteQuery<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>>(q: QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>): string;
14
+
15
+ export { buildDeleteQuery, buildInsertQuery, buildSelectQuery, buildUpdateQuery };
@@ -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,8 @@
1
+ export { N as addCondition, I as addRawCondition, V as having, U as or, L as rawHaving, K as rawOr, J as rawWhere, O as where } from '../index-CnQVnCEI.cjs';
2
+ import '../column/index.cjs';
3
+ import './constants.cjs';
4
+ import '../column/constants.cjs';
5
+ import '../types.cjs';
6
+ import 'bun';
7
+ import '../table/constants.cjs';
8
+ import '../column/types.cjs';
@@ -0,0 +1,109 @@
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
+ if (!query.table.dialect) {
53
+ throw new Error("No DB Dialect defined");
54
+ }
55
+ const validClause = clause.toLowerCase();
56
+ const condition = utilities.getCondition(query.table.dialect, column, operator, value);
57
+ if (!query.definition[validClause]) query.definition[validClause] = [];
58
+ const logicalPrefix = query.definition[validClause].length > 0 ? logical : "";
59
+ query.definition[validClause].push(`${logicalPrefix} ${condition}`.trim());
60
+ if (operator === constants.AcceptedOperator.IS_NULL || operator === constants.AcceptedOperator.IS_NOT_NULL) {
61
+ return query;
62
+ }
63
+ if (!query.definition.params) query.definition.params = [];
64
+ if (Array.isArray(value)) {
65
+ query.definition.params.push(...value);
66
+ } else {
67
+ query.definition.params.push(value);
68
+ }
69
+ return query;
70
+ }
71
+ function where(column, operator, value) {
72
+ return addCondition(
73
+ this,
74
+ constants.ConditionClause.WHERE,
75
+ column,
76
+ operator,
77
+ value || null,
78
+ constants.LogicalOperator.AND
79
+ );
80
+ }
81
+ function or(column, operator, value) {
82
+ return addCondition(
83
+ this,
84
+ constants.ConditionClause.WHERE,
85
+ column,
86
+ operator,
87
+ value || null,
88
+ constants.LogicalOperator.OR
89
+ );
90
+ }
91
+ function having(column, operator, value) {
92
+ return addCondition(
93
+ this,
94
+ constants.ConditionClause.HAVING,
95
+ column,
96
+ operator,
97
+ value,
98
+ constants.LogicalOperator.AND
99
+ );
100
+ }
101
+
102
+ exports.addCondition = addCondition;
103
+ exports.addRawCondition = addRawCondition;
104
+ exports.having = having;
105
+ exports.or = or;
106
+ exports.rawHaving = rawHaving;
107
+ exports.rawOr = rawOr;
108
+ exports.rawWhere = rawWhere;
109
+ 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,8 @@
1
+ import 'bun';
2
+ export { Y as QueryConditionContract, X as QueryTransformerContract } from '../index-CnQVnCEI.cjs';
3
+ import '../column/index.cjs';
4
+ import '../table/constants.cjs';
5
+ import './constants.cjs';
6
+ import '../column/constants.cjs';
7
+ import '../types.cjs';
8
+ import '../column/types.cjs';
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,8 @@
1
+ export { H as aggregateCol, z as alias, B as clone, G as col, F as rawCol } from '../index-CnQVnCEI.cjs';
2
+ import '../column/index.cjs';
3
+ import './constants.cjs';
4
+ import '../column/constants.cjs';
5
+ import '../types.cjs';
6
+ import 'bun';
7
+ import '../table/constants.cjs';
8
+ 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,8 @@
1
+ import '../column/index.cjs';
2
+ export { f as QueryBuilder } from '../index-CnQVnCEI.cjs';
3
+ import './constants.cjs';
4
+ import '../table/constants.cjs';
5
+ import '../column/constants.cjs';
6
+ import '../column/types.cjs';
7
+ import '../types.cjs';
8
+ import 'bun';
@@ -0,0 +1,206 @@
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 {
158
+ isWithTimestamp,
159
+ isHasCreatedAt,
160
+ isHasUpdatedAt,
161
+ createdAt,
162
+ updatedAt,
163
+ timestamp
164
+ } = utilities$1.getTimestamp(this.table);
165
+ if (isWithTimestamp) {
166
+ values = values.map((row) => ({
167
+ ...row,
168
+ ...isHasCreatedAt && {
169
+ [createdAt]: row[createdAt] ?? timestamp
170
+ },
171
+ ...isHasUpdatedAt && {
172
+ [updatedAt]: row[updatedAt] ?? timestamp
173
+ }
174
+ }));
175
+ }
176
+ this.definition.insertValues = values;
177
+ return this;
178
+ }
179
+ update(values) {
180
+ const { isWithTimestamp, isHasUpdatedAt, updatedAt, timestamp } = utilities$1.getTimestamp(this.table);
181
+ if (isWithTimestamp && isHasUpdatedAt) {
182
+ values = {
183
+ ...values,
184
+ [updatedAt]: values[updatedAt] ?? timestamp
185
+ };
186
+ }
187
+ this.definition.queryType = constants.QueryType.UPDATE;
188
+ this.definition.updateValues = values;
189
+ return this;
190
+ }
191
+ delete() {
192
+ const { isWithParanoid, deletedAt, timestamp } = utilities$1.getParanoid(this.table);
193
+ if (isWithParanoid) {
194
+ return this.update({
195
+ [deletedAt]: timestamp
196
+ });
197
+ }
198
+ this.definition.queryType = constants.QueryType.DELETE;
199
+ return this;
200
+ }
201
+ infer() {
202
+ return null;
203
+ }
204
+ }
205
+
206
+ exports.QueryBuilder = QueryBuilder;
@@ -0,0 +1,17 @@
1
+ import { T as Table, Q as QueryDefinition, C as ColumnSelector, e as StrictColumnSelector, f as QueryBuilder } from '../index-CnQVnCEI.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 'bun';
7
+ import '../table/constants.cjs';
8
+ import '../column/types.cjs';
9
+
10
+ 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 & {
11
+ [K in JoinAlias]: JoinTable;
12
+ }>(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"> & {
13
+ joins: string[];
14
+ joinedTables: FinalJoinedTables;
15
+ }>;
16
+
17
+ 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;