@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,18 @@
1
+ import { TransactionSQL } from 'bun';
2
+ import { T as Table, Q as QueryDefinition, C as ColumnSelector, e as StrictColumnSelector, f as QueryBuilder } from '../index-CnQVnCEI.cjs';
3
+ import { Column } from '../column/index.cjs';
4
+ import { Dialect } from '../table/constants.cjs';
5
+ import '../column/constants.cjs';
6
+ import '../types.cjs';
7
+ import './constants.cjs';
8
+ import '../column/types.cjs';
9
+
10
+ declare function buildQuery(query: string): string;
11
+ 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, dialect?: Dialect | null): {
12
+ query: string;
13
+ params: unknown[] | null | undefined;
14
+ };
15
+ 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;
16
+ 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, tx?: TransactionSQL | null): Promise<Output>;
17
+
18
+ export { buildQuery, exec, toQuery, toString };
@@ -0,0 +1,132 @@
1
+ 'use strict';
2
+
3
+ var constants$1 = require('../table/constants');
4
+ var builder = require('./builder');
5
+ var constants = require('./constants');
6
+ var utilities = require('./utilities');
7
+
8
+ function buildQuery(query) {
9
+ let index = 0;
10
+ return query.replace(/\?/g, () => {
11
+ index++;
12
+ return `$${index}`;
13
+ });
14
+ }
15
+ function toQuery(dialect) {
16
+ let sql = "";
17
+ switch (this.definition.queryType) {
18
+ case constants.QueryType.SELECT:
19
+ sql = builder.buildSelectQuery(this);
20
+ break;
21
+ case constants.QueryType.INSERT:
22
+ sql = builder.buildInsertQuery(this);
23
+ break;
24
+ case constants.QueryType.UPDATE:
25
+ sql = builder.buildUpdateQuery(this);
26
+ break;
27
+ case constants.QueryType.DELETE:
28
+ sql = builder.buildDeleteQuery(this);
29
+ break;
30
+ default:
31
+ throw new Error("No query type defined");
32
+ }
33
+ if (this.definition?.joins?.length) {
34
+ sql += ` ${this.definition.joins.join(" ")}`;
35
+ }
36
+ const whereConditions = utilities.getWhereConditions(this);
37
+ if (whereConditions.length) {
38
+ sql += ` WHERE ${whereConditions.join(" ")}`;
39
+ }
40
+ const groupByConditions = utilities.getGroupByConditions(this);
41
+ if (groupByConditions.length) {
42
+ sql += ` GROUP BY ${groupByConditions.join(", ")}`;
43
+ }
44
+ if (this.definition?.having?.length) {
45
+ sql += ` HAVING ${this.definition.having.join(" ")}`;
46
+ }
47
+ if (this.definition?.orderBy?.length) {
48
+ sql += ` ORDER BY ${this.definition.orderBy.map((order) => [order.column, order.direction].join(" ")).join(", ")}`;
49
+ }
50
+ if (this.definition?.limit !== null) {
51
+ sql += ` LIMIT ?`;
52
+ if (!this.definition.params) this.definition.params = [];
53
+ this.definition.params.push(this.definition.limit);
54
+ }
55
+ if (this.definition?.offset !== null) {
56
+ sql += ` OFFSET ?`;
57
+ if (!this.definition.params) this.definition.params = [];
58
+ this.definition.params.push(this.definition.offset);
59
+ }
60
+ if (this.definition.queryType === constants.QueryType.UPDATE || this.definition.queryType === constants.QueryType.DELETE) {
61
+ if (dialect !== constants$1.Dialect.MYSQL) {
62
+ sql += ` RETURNING *`;
63
+ }
64
+ }
65
+ sql = buildQuery(sql);
66
+ return { query: sql + ";", params: this.definition.params };
67
+ }
68
+ function toString() {
69
+ return this.toQuery().query;
70
+ }
71
+ async function exec(tx) {
72
+ const client = this.table.client;
73
+ const dialect = this.table.dialect;
74
+ const queryType = this.definition.queryType;
75
+ const isUpdate = queryType === constants.QueryType.UPDATE;
76
+ const isDelete = queryType === constants.QueryType.DELETE;
77
+ const isReturning = isUpdate || isDelete;
78
+ const isMySQL = dialect === constants$1.Dialect.MYSQL;
79
+ if (!client) {
80
+ throw new Error("Database client not defined");
81
+ }
82
+ if (!queryType) {
83
+ throw new Error("No query type defined");
84
+ }
85
+ const { query, params } = this.toQuery(dialect);
86
+ if (this.hooks?.before?.size) {
87
+ for (const hook of this.hooks.before.values()) {
88
+ hook({
89
+ query,
90
+ params,
91
+ type: queryType,
92
+ hook: constants.QueryHooksType.BEFORE
93
+ });
94
+ }
95
+ }
96
+ let result = await client.exec({
97
+ sql: query,
98
+ params,
99
+ tx
100
+ });
101
+ if (isMySQL && isReturning) {
102
+ const query2 = this.clone();
103
+ query2.definition.queryType = constants.QueryType.SELECT;
104
+ result = await client.exec({
105
+ sql: query2.toQuery().query,
106
+ params,
107
+ tx
108
+ });
109
+ }
110
+ if (this.hooks?.after?.size) {
111
+ for (const hook of this.hooks.after.values()) {
112
+ hook({
113
+ query,
114
+ params,
115
+ type: queryType,
116
+ hook: constants.QueryHooksType.AFTER
117
+ });
118
+ }
119
+ }
120
+ return result.map(
121
+ (r) => utilities.parseAliasedRow({
122
+ row: r,
123
+ selects: this.definition.select ?? [],
124
+ root: this.definition?.baseAlias ?? this.table.name
125
+ })
126
+ );
127
+ }
128
+
129
+ exports.buildQuery = buildQuery;
130
+ exports.exec = exec;
131
+ exports.toQuery = toQuery;
132
+ exports.toString = toString;
@@ -0,0 +1,8 @@
1
+ import '../column/index.cjs';
2
+ import '../column/constants.cjs';
3
+ export { l as AcceptedInsertValues, k as AcceptedOrderBy, m as AcceptedUpdateValues, n as AggregateColumn, A as AliasedColumn, C as ColumnSelector, h as QuerHooks, Q as QueryDefinition, p as QueryOutput, j as QueryRunHooks, q as QueryRunHooksOptions, R as RawColumn, o as SelectQueryOutput, g as SelectableColumn, e as StrictColumnSelector, W as WhereValue } from '../index-CnQVnCEI.cjs';
4
+ import '../types.cjs';
5
+ import './constants.cjs';
6
+ import '../table/constants.cjs';
7
+ import '../column/types.cjs';
8
+ import 'bun';
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,37 @@
1
+ import { T as Table, W as WhereValue, Q as QueryDefinition, C as ColumnSelector, e as StrictColumnSelector, f as QueryBuilder, A as AliasedColumn, g as SelectableColumn } from '../index-CnQVnCEI.cjs';
2
+ import { Column } from '../column/index.cjs';
3
+ import { Dialect } from '../table/constants.cjs';
4
+ import { AcceptedOperator } from './constants.cjs';
5
+ import '../column/constants.cjs';
6
+ import '../types.cjs';
7
+ import 'bun';
8
+ import '../column/types.cjs';
9
+
10
+ declare function getTableColumnNames<ColName extends string, BaseAlias extends string, BaseTable extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>>(column: ColName, baseAlias: BaseAlias, baseTable: BaseTable, joinedTables: JoinedTables): {
11
+ from: string;
12
+ columns: string[];
13
+ };
14
+ declare function getCondition<DbDialect extends Dialect, ColName extends string, Operator extends AcceptedOperator, Value extends WhereValue<Column>[Operator]>(dialect: DbDialect, column: ColName, operator: Operator, value: Value): string;
15
+ declare function getTimestamp<TableRef extends Table<string, Record<string, Column>>>(table: TableRef): {
16
+ isWithTimestamp: boolean;
17
+ timestamp: Date;
18
+ createdAt: string;
19
+ updatedAt: string;
20
+ isHasUpdatedAt: boolean;
21
+ isHasCreatedAt: boolean;
22
+ };
23
+ declare function getParanoid<TableRef extends Table<string, Record<string, Column>>>(table: TableRef): {
24
+ isWithParanoid: boolean;
25
+ timestamp: Date;
26
+ deletedAt: string;
27
+ };
28
+ declare function getWhereConditions<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>>(q: Query): string[];
29
+ declare function getGroupByConditions<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>>(q: Query): (string | AliasedColumn<ColumnSelector<Alias, TableRef, JoinedTables>, string>)[];
30
+ declare function getTableSelectName<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>>(q: Query): string;
31
+ declare function parseAliasedRow({ row, selects, root, }: {
32
+ row: Record<string, unknown>;
33
+ selects: SelectableColumn<string>[];
34
+ root?: string | null;
35
+ }): Record<string, any>;
36
+
37
+ export { getCondition, getGroupByConditions, getParanoid, getTableColumnNames, getTableSelectName, getTimestamp, getWhereConditions, parseAliasedRow };
@@ -0,0 +1,194 @@
1
+ 'use strict';
2
+
3
+ require('.');
4
+ var constants$1 = require('../table/constants');
5
+ var utilities = require('../utilities');
6
+ var constants = require('./constants');
7
+
8
+ function getTableColumnNames(column, baseAlias, baseTable, joinedTables) {
9
+ const [tableAlias] = column.split(".");
10
+ const isOnBase = tableAlias === baseAlias;
11
+ const from = isOnBase ? baseAlias : tableAlias;
12
+ const columns = isOnBase ? Object.keys(baseTable.columns) : Object.keys(joinedTables?.[from]?.columns ?? {});
13
+ return {
14
+ from,
15
+ columns
16
+ };
17
+ }
18
+ function getCondition(dialect, column, operator, value) {
19
+ switch (operator) {
20
+ case constants.AcceptedOperator.EQ:
21
+ return `${column} = ?`;
22
+ case constants.AcceptedOperator.NE:
23
+ return `${column} != ?`;
24
+ case constants.AcceptedOperator.GT:
25
+ return `${column} > ?`;
26
+ case constants.AcceptedOperator.LT:
27
+ return `${column} < ?`;
28
+ case constants.AcceptedOperator.GTE:
29
+ return `${column} >= ?`;
30
+ case constants.AcceptedOperator.LTE:
31
+ return `${column} <= ?`;
32
+ case constants.AcceptedOperator.IN:
33
+ return `${column} IN (${value.map(() => "?").join(", ")})`;
34
+ case constants.AcceptedOperator.NOT_IN:
35
+ return `${column} NOT IN (${value.map(() => "?").join(", ")})`;
36
+ case constants.AcceptedOperator.LIKE:
37
+ return `${column} LIKE ?`;
38
+ case constants.AcceptedOperator.NOT_LIKE:
39
+ return `${column} NOT LIKE ?`;
40
+ case constants.AcceptedOperator.ILIKE:
41
+ if (dialect === constants$1.Dialect.POSTGRES) {
42
+ return `${column} ILIKE ?`;
43
+ }
44
+ return `LOWER(${column}) LIKE LOWER(?)`;
45
+ case constants.AcceptedOperator.NOT_ILIKE:
46
+ if (dialect === constants$1.Dialect.POSTGRES) {
47
+ return `${column} NOT ILIKE ?`;
48
+ }
49
+ return `LOWER(${column}) NOT LIKE LOWER(?)`;
50
+ case constants.AcceptedOperator.IS_NULL:
51
+ return `${column} IS NULL`;
52
+ case constants.AcceptedOperator.IS_NOT_NULL:
53
+ return `${column} IS NOT NULL`;
54
+ case constants.AcceptedOperator.BETWEEN:
55
+ return `${column} BETWEEN ? AND ?`;
56
+ case constants.AcceptedOperator.NOT_BETWEEN:
57
+ return `${column} NOT BETWEEN ? AND ?`;
58
+ default:
59
+ throw new Error("Invalid operator");
60
+ }
61
+ }
62
+ function getTimestamp(table) {
63
+ const isWithTimestamp = !!table.timestamp;
64
+ const timestamp = /* @__PURE__ */ new Date();
65
+ let isHasCreatedAt = true;
66
+ let isHasUpdatedAt = true;
67
+ let createdAt = "createdAt";
68
+ let updatedAt = "updatedAt";
69
+ if (isWithTimestamp) {
70
+ const isCustomTimestamp = typeof table.timestamp === "object";
71
+ if (isCustomTimestamp) {
72
+ if (typeof table.timestamp.createdAt === "string") {
73
+ createdAt = table.timestamp.createdAt;
74
+ }
75
+ isHasCreatedAt = table.timestamp.createdAt === false;
76
+ }
77
+ if (isCustomTimestamp) {
78
+ if (typeof table.timestamp.updatedAt === "string") {
79
+ updatedAt = table.timestamp.updatedAt;
80
+ }
81
+ isHasUpdatedAt = table.timestamp.updatedAt === false;
82
+ }
83
+ }
84
+ return {
85
+ isWithTimestamp,
86
+ timestamp,
87
+ createdAt,
88
+ updatedAt,
89
+ isHasUpdatedAt,
90
+ isHasCreatedAt
91
+ };
92
+ }
93
+ function getParanoid(table) {
94
+ const isWithParanoid = !!table.paranoid;
95
+ const timestamp = /* @__PURE__ */ new Date();
96
+ let deletedAt = "deletedAt";
97
+ if (isWithParanoid) {
98
+ if (typeof table.paranoid === "string") {
99
+ deletedAt = table.paranoid;
100
+ }
101
+ }
102
+ return {
103
+ isWithParanoid,
104
+ timestamp,
105
+ deletedAt
106
+ };
107
+ }
108
+ function getWhereConditions(q) {
109
+ if (q.definition.queryType === constants.QueryType.INSERT) return [];
110
+ const conditions = [];
111
+ const base = q.definition.baseAlias ?? q.table.name;
112
+ const { isWithParanoid, deletedAt } = getParanoid(q.table);
113
+ const withDeleted = !!q.definition.withDeleted;
114
+ const isHasConditions = !!q.definition.where?.length;
115
+ if (!withDeleted && isWithParanoid) {
116
+ const suffix = isHasConditions ? " AND" : "";
117
+ const column = `${base}.${utilities.quoteIdentifier(deletedAt)}`;
118
+ conditions.unshift(`${column} IS NULL${suffix}`);
119
+ }
120
+ if (q.definition.where?.length) {
121
+ conditions.push(...q.definition.where);
122
+ }
123
+ return conditions;
124
+ }
125
+ function getGroupByConditions(q) {
126
+ if (q.definition.queryType !== constants.QueryType.SELECT) return [];
127
+ if (q.definition.groupBy?.length) return q.definition.groupBy;
128
+ if (q.definition.aggregates?.length) {
129
+ if (q.definition.select?.length)
130
+ return q.definition.select.map((col) => {
131
+ if (typeof col === "string" && col.endsWith("*")) {
132
+ const { from: from2, columns } = getTableColumnNames(
133
+ col,
134
+ q.definition.baseAlias ?? q.table.name,
135
+ q.table,
136
+ q.definition.joinedTables ?? {}
137
+ );
138
+ return columns.map((column) => `${from2}.${utilities.quoteIdentifier(column)}`).join(" ");
139
+ }
140
+ return col;
141
+ });
142
+ const from = q.definition.baseAlias ?? q.table.name;
143
+ return Object.keys(q.table.columns).map(
144
+ (col) => `${from}.${utilities.quoteIdentifier(col)}`
145
+ );
146
+ }
147
+ return [];
148
+ }
149
+ function getTableSelectName(q) {
150
+ if (!q.definition.baseAlias) return q.table.name;
151
+ return `${q.table.name} AS ${q.definition.baseAlias}`;
152
+ }
153
+ function parseAliasedRow({
154
+ row,
155
+ selects,
156
+ root = null
157
+ }) {
158
+ let result = {};
159
+ for (const key in row) {
160
+ const [table, column] = key.split(".");
161
+ if (!column) {
162
+ const alias = selects.find(
163
+ (s) => typeof s === "object" && s.as === table
164
+ );
165
+ if (alias) {
166
+ const [oriTab] = alias.column.split(".");
167
+ if (!result[oriTab]) result[oriTab] = {};
168
+ result[oriTab][table] = row[key];
169
+ continue;
170
+ }
171
+ result[key] = row[key];
172
+ continue;
173
+ }
174
+ if (!result[table]) result[table] = {};
175
+ result[table][column] = row[key];
176
+ }
177
+ if (root) {
178
+ result = {
179
+ ...result,
180
+ ...result[root]
181
+ };
182
+ delete result[root];
183
+ }
184
+ return result;
185
+ }
186
+
187
+ exports.getCondition = getCondition;
188
+ exports.getGroupByConditions = getGroupByConditions;
189
+ exports.getParanoid = getParanoid;
190
+ exports.getTableColumnNames = getTableColumnNames;
191
+ exports.getTableSelectName = getTableSelectName;
192
+ exports.getTimestamp = getTimestamp;
193
+ exports.getWhereConditions = getWhereConditions;
194
+ exports.parseAliasedRow = parseAliasedRow;
@@ -0,0 +1,8 @@
1
+ declare const Dialect: {
2
+ readonly POSTGRES: "postgres";
3
+ readonly MYSQL: "mysql";
4
+ readonly SQLITE: "sqlite";
5
+ };
6
+ type Dialect = (typeof Dialect)[keyof typeof Dialect];
7
+
8
+ export { Dialect };
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const Dialect = {
4
+ POSTGRES: "postgres",
5
+ MYSQL: "mysql",
6
+ SQLITE: "sqlite"
7
+ };
8
+
9
+ exports.Dialect = Dialect;
@@ -0,0 +1,8 @@
1
+ export { T as Table } from '../index-CnQVnCEI.cjs';
2
+ import '../column/index.cjs';
3
+ import './constants.cjs';
4
+ import '../column/constants.cjs';
5
+ import '../types.cjs';
6
+ import '../query/constants.cjs';
7
+ import 'bun';
8
+ import '../column/types.cjs';
@@ -0,0 +1,74 @@
1
+ 'use strict';
2
+
3
+ var query = require('../query');
4
+ var utilities = require('./utilities');
5
+
6
+ class Table {
7
+ client;
8
+ _dialect;
9
+ name;
10
+ columns;
11
+ timestamp;
12
+ paranoid;
13
+ _output;
14
+ constructor(options) {
15
+ this._dialect = options.dialect || null;
16
+ this.name = options.name;
17
+ this.columns = options.columns;
18
+ this.paranoid = options.paranoid || null;
19
+ this.timestamp = options.timestamp || null;
20
+ this.client = null;
21
+ if (!this._dialect) return;
22
+ this.setColumnDialect(this._dialect);
23
+ }
24
+ infer() {
25
+ return null;
26
+ }
27
+ static define(options) {
28
+ const columns = utilities.defineColumns(options);
29
+ return new Table({
30
+ ...options,
31
+ columns
32
+ });
33
+ }
34
+ setColumnDialect(dialect) {
35
+ for (const column of Object.values(this.columns)) {
36
+ column.dialect(dialect);
37
+ }
38
+ }
39
+ get dialect() {
40
+ return this._dialect;
41
+ }
42
+ setDialect(dialect) {
43
+ this._dialect = dialect;
44
+ this.setColumnDialect(dialect);
45
+ return this;
46
+ }
47
+ async create(options = {}) {
48
+ const db = options.db || this.client;
49
+ if (!db) throw new Error("Database client not defined");
50
+ const sql = `CREATE TABLE IF NOT EXISTS ${this.name} (${Object.entries(
51
+ this.columns
52
+ ).map(([name, column]) => `${name} ${column.toQuery().query}`).join(", ")});`;
53
+ await db.exec({
54
+ sql,
55
+ tx: options.tx
56
+ });
57
+ return this;
58
+ }
59
+ async drop(options = {}) {
60
+ const db = options.db || this.client;
61
+ if (!db) throw new Error("Database client not defined");
62
+ const sql = `DROP TABLE IF EXISTS ${this.name};`;
63
+ await db.exec({
64
+ sql,
65
+ tx: options.tx
66
+ });
67
+ return this;
68
+ }
69
+ query() {
70
+ return new query.QueryBuilder(this);
71
+ }
72
+ }
73
+
74
+ exports.Table = Table;
@@ -0,0 +1,8 @@
1
+ import 'bun';
2
+ export { E as ExecOptions, M as MergeTimestampParanoid, x as TableOptions, y as TableOutput, a as TimestampOptions } from '../index-CnQVnCEI.cjs';
3
+ import '../column/index.cjs';
4
+ import './constants.cjs';
5
+ import '../column/constants.cjs';
6
+ import '../types.cjs';
7
+ import '../query/constants.cjs';
8
+ import '../column/types.cjs';
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,8 @@
1
+ import '../column/constants.cjs';
2
+ import '../column/index.cjs';
3
+ import './constants.cjs';
4
+ export { t as createdAt, w as defineColumns, v as deletedAt, u as updatedAt } from '../index-CnQVnCEI.cjs';
5
+ import '../column/types.cjs';
6
+ import '../types.cjs';
7
+ import '../query/constants.cjs';
8
+ import 'bun';
@@ -0,0 +1,55 @@
1
+ 'use strict';
2
+
3
+ var column = require('../column');
4
+
5
+ const createdAt = column.Column.define({
6
+ type: "DATETIME"
7
+ }).default("CURRENT_TIMESTAMP");
8
+ const updatedAt = column.Column.define({
9
+ type: "DATETIME"
10
+ });
11
+ const deletedAt = column.Column.define({
12
+ type: "DATETIME"
13
+ });
14
+ function defineColumns(options) {
15
+ const columns = {
16
+ ...options.columns
17
+ };
18
+ const tracker = {
19
+ deletedAt: "deletedAt"
20
+ };
21
+ if (options.timestamp) {
22
+ const timestamp = {
23
+ createdAt: "createdAt",
24
+ updatedAt: "updatedAt"
25
+ };
26
+ if (typeof options.timestamp === "object") {
27
+ if (typeof options.timestamp.createdAt === "string") {
28
+ timestamp.createdAt = options.timestamp.createdAt;
29
+ }
30
+ if (typeof options.timestamp.updatedAt === "string") {
31
+ timestamp.updatedAt = options.timestamp.updatedAt;
32
+ }
33
+ }
34
+ if (!columns[timestamp.createdAt]) {
35
+ columns[timestamp.createdAt] = createdAt;
36
+ }
37
+ if (!columns[timestamp.updatedAt]) {
38
+ columns[timestamp.updatedAt] = updatedAt;
39
+ }
40
+ }
41
+ if (options.paranoid) {
42
+ if (typeof options.paranoid !== "boolean") {
43
+ tracker.deletedAt = options.paranoid;
44
+ }
45
+ if (!columns[tracker.deletedAt]) {
46
+ columns[tracker.deletedAt] = deletedAt;
47
+ }
48
+ }
49
+ return columns;
50
+ }
51
+
52
+ exports.createdAt = createdAt;
53
+ exports.defineColumns = defineColumns;
54
+ exports.deletedAt = deletedAt;
55
+ exports.updatedAt = updatedAt;
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ function deepClone(obj) {
4
+ if (Array.isArray(obj)) {
5
+ return obj.map((item) => deepClone(item));
6
+ }
7
+ if (obj && typeof obj === "object") {
8
+ const clonedObj = {};
9
+ for (const key in obj) {
10
+ clonedObj[key] = deepClone(obj[key]);
11
+ }
12
+ return clonedObj;
13
+ }
14
+ return obj;
15
+ }
16
+ function quoteIdentifier(identifier) {
17
+ return `"${identifier.replace(/"/g, '""')}"`;
18
+ }
19
+
20
+ exports.deepClone = deepClone;
21
+ exports.quoteIdentifier = quoteIdentifier;