@ignisia/sql 0.2.2 → 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 (87) hide show
  1. package/dist/cjs/column/constants.d.cts +58 -5
  2. package/dist/cjs/column/constants.js +79 -22
  3. package/dist/cjs/column/index.d.cts +1 -0
  4. package/dist/cjs/column/index.js +30 -10
  5. package/dist/cjs/column/utils.d.cts +133 -0
  6. package/dist/cjs/column/utils.js +34 -0
  7. package/dist/cjs/database/alter.d.cts +3 -2
  8. package/dist/cjs/database/alter.js +15 -15
  9. package/dist/cjs/database/column.d.cts +3 -2
  10. package/dist/cjs/database/column.js +9 -7
  11. package/dist/cjs/database/contract.d.cts +3 -2
  12. package/dist/cjs/database/index.d.cts +3 -2
  13. package/dist/cjs/database/index.js +14 -1
  14. package/dist/cjs/database/table.d.cts +3 -2
  15. package/dist/cjs/database/table.js +12 -4
  16. package/dist/cjs/database/types.d.cts +2 -1
  17. package/dist/cjs/database/wrapper.d.cts +17 -21
  18. package/dist/cjs/database/wrapper.js +44 -52
  19. package/dist/cjs/{index---zaMa69.d.cts → index-2jl8MRfX.d.cts} +4 -2
  20. package/dist/cjs/{index-CwiFQh0I.d.cts → index-CnQVnCEI.d.cts} +80 -46
  21. package/dist/cjs/index.d.cts +3 -2
  22. package/dist/cjs/migration/index.d.cts +3 -2
  23. package/dist/cjs/migration/runner.js +1 -1
  24. package/dist/cjs/migration/type.d.cts +3 -2
  25. package/dist/cjs/query/builder.d.cts +2 -1
  26. package/dist/cjs/query/condition.d.cts +2 -1
  27. package/dist/cjs/query/condition.js +5 -2
  28. package/dist/cjs/query/contract.d.cts +3 -2
  29. package/dist/cjs/query/helper.d.cts +2 -1
  30. package/dist/cjs/query/index.d.cts +2 -1
  31. package/dist/cjs/query/index.js +16 -7
  32. package/dist/cjs/query/join.d.cts +2 -1
  33. package/dist/cjs/query/sql.d.cts +5 -4
  34. package/dist/cjs/query/sql.js +36 -8
  35. package/dist/cjs/query/types.d.cts +2 -1
  36. package/dist/cjs/query/utilities.d.cts +4 -1
  37. package/dist/cjs/query/utilities.js +15 -5
  38. package/dist/cjs/table/constants.d.cts +1 -0
  39. package/dist/cjs/table/constants.js +1 -0
  40. package/dist/cjs/table/index.d.cts +2 -1
  41. package/dist/cjs/table/index.js +29 -9
  42. package/dist/cjs/table/types.d.cts +2 -1
  43. package/dist/cjs/table/utilities.d.cts +2 -1
  44. package/dist/esm/column/constants.d.ts +58 -5
  45. package/dist/esm/column/constants.js +78 -23
  46. package/dist/esm/column/index.d.ts +1 -0
  47. package/dist/esm/column/index.js +30 -10
  48. package/dist/esm/column/utils.d.ts +133 -0
  49. package/dist/esm/column/utils.js +32 -0
  50. package/dist/esm/database/alter.d.ts +3 -2
  51. package/dist/esm/database/alter.js +15 -15
  52. package/dist/esm/database/column.d.ts +3 -2
  53. package/dist/esm/database/column.js +9 -7
  54. package/dist/esm/database/contract.d.ts +3 -2
  55. package/dist/esm/database/index.d.ts +3 -2
  56. package/dist/esm/database/index.js +15 -2
  57. package/dist/esm/database/table.d.ts +3 -2
  58. package/dist/esm/database/table.js +12 -4
  59. package/dist/esm/database/types.d.ts +2 -1
  60. package/dist/esm/database/wrapper.d.ts +17 -21
  61. package/dist/esm/database/wrapper.js +43 -53
  62. package/dist/esm/{index-DFrpzXEn.d.ts → index-BXOAxB_h.d.ts} +4 -2
  63. package/dist/esm/{index-FMT0YEO7.d.ts → index-BdpoD4zk.d.ts} +80 -46
  64. package/dist/esm/index.d.ts +3 -2
  65. package/dist/esm/migration/index.d.ts +3 -2
  66. package/dist/esm/migration/runner.js +1 -1
  67. package/dist/esm/migration/type.d.ts +3 -2
  68. package/dist/esm/query/builder.d.ts +2 -1
  69. package/dist/esm/query/condition.d.ts +2 -1
  70. package/dist/esm/query/condition.js +5 -2
  71. package/dist/esm/query/contract.d.ts +3 -2
  72. package/dist/esm/query/helper.d.ts +2 -1
  73. package/dist/esm/query/index.d.ts +2 -1
  74. package/dist/esm/query/index.js +16 -7
  75. package/dist/esm/query/join.d.ts +2 -1
  76. package/dist/esm/query/sql.d.ts +5 -4
  77. package/dist/esm/query/sql.js +36 -8
  78. package/dist/esm/query/types.d.ts +2 -1
  79. package/dist/esm/query/utilities.d.ts +4 -1
  80. package/dist/esm/query/utilities.js +15 -5
  81. package/dist/esm/table/constants.d.ts +1 -0
  82. package/dist/esm/table/constants.js +1 -0
  83. package/dist/esm/table/index.d.ts +2 -1
  84. package/dist/esm/table/index.js +29 -9
  85. package/dist/esm/table/types.d.ts +2 -1
  86. package/dist/esm/table/utilities.d.ts +2 -1
  87. package/package.json +1 -1
@@ -153,22 +153,31 @@ var QueryBuilder = class {
153
153
  insert(...values) {
154
154
  this.definition.queryType = QueryType.INSERT;
155
155
  if (!this.definition.insertValues) this.definition.insertValues = [];
156
- const { isWithTimestamp, createdAt, updatedAt, timestamp } = getTimestamp(
157
- this.table
158
- );
156
+ const {
157
+ isWithTimestamp,
158
+ isHasCreatedAt,
159
+ isHasUpdatedAt,
160
+ createdAt,
161
+ updatedAt,
162
+ timestamp
163
+ } = getTimestamp(this.table);
159
164
  if (isWithTimestamp) {
160
165
  values = values.map((row) => ({
161
166
  ...row,
162
- [createdAt]: row[createdAt] ?? timestamp,
163
- [updatedAt]: row[updatedAt] ?? timestamp
167
+ ...isHasCreatedAt && {
168
+ [createdAt]: row[createdAt] ?? timestamp
169
+ },
170
+ ...isHasUpdatedAt && {
171
+ [updatedAt]: row[updatedAt] ?? timestamp
172
+ }
164
173
  }));
165
174
  }
166
175
  this.definition.insertValues = values;
167
176
  return this;
168
177
  }
169
178
  update(values) {
170
- const { isWithTimestamp, updatedAt, timestamp } = getTimestamp(this.table);
171
- if (isWithTimestamp) {
179
+ const { isWithTimestamp, isHasUpdatedAt, updatedAt, timestamp } = getTimestamp(this.table);
180
+ if (isWithTimestamp && isHasUpdatedAt) {
172
181
  values = {
173
182
  ...values,
174
183
  [updatedAt]: values[updatedAt] ?? timestamp
@@ -1,8 +1,9 @@
1
- import { T as Table, Q as QueryDefinition, C as ColumnSelector, c as StrictColumnSelector, d as QueryBuilder } from '../index-FMT0YEO7.js';
1
+ import { T as Table, Q as QueryDefinition, C as ColumnSelector, e as StrictColumnSelector, f as QueryBuilder } from '../index-BdpoD4zk.js';
2
2
  import { Column } from '../column/index.js';
3
3
  import { AcceptedJoin } from './constants.js';
4
4
  import '../column/constants.js';
5
5
  import '../types.js';
6
+ import 'bun';
6
7
  import '../table/constants.js';
7
8
  import '../column/types.js';
8
9
 
@@ -1,17 +1,18 @@
1
- import { T as Table, Q as QueryDefinition, C as ColumnSelector, c as StrictColumnSelector, d as QueryBuilder } from '../index-FMT0YEO7.js';
1
+ import { TransactionSQL } from 'bun';
2
+ import { T as Table, Q as QueryDefinition, C as ColumnSelector, e as StrictColumnSelector, f as QueryBuilder } from '../index-BdpoD4zk.js';
2
3
  import { Column } from '../column/index.js';
4
+ import { Dialect } from '../table/constants.js';
3
5
  import '../column/constants.js';
4
6
  import '../types.js';
5
7
  import './constants.js';
6
- import '../table/constants.js';
7
8
  import '../column/types.js';
8
9
 
9
10
  declare function buildQuery(query: string): string;
10
- declare function toQuery<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>(this: Query): {
11
+ 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): {
11
12
  query: string;
12
13
  params: unknown[] | null | undefined;
13
14
  };
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;
15
- declare function exec<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>, Output extends Query['_output'] = Query['_output']>(this: Query): Promise<Output>;
16
+ 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>;
16
17
 
17
18
  export { buildQuery, exec, toQuery, toString };
@@ -1,3 +1,4 @@
1
+ import { Dialect } from '../table/constants.js';
1
2
  import { buildDeleteQuery, buildUpdateQuery, buildInsertQuery, buildSelectQuery } from './builder.js';
2
3
  import { QueryType, QueryHooksType } from './constants.js';
3
4
  import { getWhereConditions, getGroupByConditions, parseAliasedRow } from './utilities.js';
@@ -10,7 +11,7 @@ function buildQuery(query) {
10
11
  return `$${index}`;
11
12
  });
12
13
  }
13
- function toQuery() {
14
+ function toQuery(dialect) {
14
15
  let sql = "";
15
16
  switch (this.definition.queryType) {
16
17
  case QueryType.SELECT:
@@ -56,7 +57,9 @@ function toQuery() {
56
57
  this.definition.params.push(this.definition.offset);
57
58
  }
58
59
  if (this.definition.queryType === QueryType.UPDATE || this.definition.queryType === QueryType.DELETE) {
59
- sql += ` RETURNING *`;
60
+ if (dialect !== Dialect.MYSQL) {
61
+ sql += ` RETURNING *`;
62
+ }
60
63
  }
61
64
  sql = buildQuery(sql);
62
65
  return { query: sql + ";", params: this.definition.params };
@@ -64,26 +67,51 @@ function toQuery() {
64
67
  function toString() {
65
68
  return this.toQuery().query;
66
69
  }
67
- async function exec() {
68
- if (!this.table.client) throw new Error("Database client not defined");
69
- const { query, params } = this.toQuery();
70
+ async function exec(tx) {
71
+ const client = this.table.client;
72
+ const dialect = this.table.dialect;
73
+ const queryType = this.definition.queryType;
74
+ const isUpdate = queryType === QueryType.UPDATE;
75
+ const isDelete = queryType === QueryType.DELETE;
76
+ const isReturning = isUpdate || isDelete;
77
+ const isMySQL = dialect === Dialect.MYSQL;
78
+ if (!client) {
79
+ throw new Error("Database client not defined");
80
+ }
81
+ if (!queryType) {
82
+ throw new Error("No query type defined");
83
+ }
84
+ const { query, params } = this.toQuery(dialect);
70
85
  if (this.hooks?.before?.size) {
71
86
  for (const hook of this.hooks.before.values()) {
72
87
  hook({
73
88
  query,
74
89
  params,
75
- type: this.definition.queryType,
90
+ type: queryType,
76
91
  hook: QueryHooksType.BEFORE
77
92
  });
78
93
  }
79
94
  }
80
- const result = await this.table.client.exec(query, params);
95
+ let result = await client.exec({
96
+ sql: query,
97
+ params,
98
+ tx
99
+ });
100
+ if (isMySQL && isReturning) {
101
+ const query2 = this.clone();
102
+ query2.definition.queryType = QueryType.SELECT;
103
+ result = await client.exec({
104
+ sql: query2.toQuery().query,
105
+ params,
106
+ tx
107
+ });
108
+ }
81
109
  if (this.hooks?.after?.size) {
82
110
  for (const hook of this.hooks.after.values()) {
83
111
  hook({
84
112
  query,
85
113
  params,
86
- type: this.definition.queryType,
114
+ type: queryType,
87
115
  hook: QueryHooksType.AFTER
88
116
  });
89
117
  }
@@ -1,7 +1,8 @@
1
1
  import '../column/index.js';
2
2
  import '../column/constants.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';
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-BdpoD4zk.js';
4
4
  import '../types.js';
5
5
  import './constants.js';
6
6
  import '../table/constants.js';
7
7
  import '../column/types.js';
8
+ import 'bun';
@@ -1,9 +1,10 @@
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';
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-BdpoD4zk.js';
2
2
  import { Column } from '../column/index.js';
3
3
  import { Dialect } from '../table/constants.js';
4
4
  import { AcceptedOperator } from './constants.js';
5
5
  import '../column/constants.js';
6
6
  import '../types.js';
7
+ import 'bun';
7
8
  import '../column/types.js';
8
9
 
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): {
@@ -16,6 +17,8 @@ declare function getTimestamp<TableRef extends Table<string, Record<string, Colu
16
17
  timestamp: Date;
17
18
  createdAt: string;
18
19
  updatedAt: string;
20
+ isHasUpdatedAt: boolean;
21
+ isHasCreatedAt: boolean;
19
22
  };
20
23
  declare function getParanoid<TableRef extends Table<string, Record<string, Column>>>(table: TableRef): {
21
24
  isWithParanoid: boolean;
@@ -61,22 +61,32 @@ function getCondition(dialect, column, operator, value) {
61
61
  function getTimestamp(table) {
62
62
  const isWithTimestamp = !!table.timestamp;
63
63
  const timestamp = /* @__PURE__ */ new Date();
64
+ let isHasCreatedAt = true;
65
+ let isHasUpdatedAt = true;
64
66
  let createdAt = "createdAt";
65
67
  let updatedAt = "updatedAt";
66
68
  if (isWithTimestamp) {
67
69
  const isCustomTimestamp = typeof table.timestamp === "object";
68
- if (isCustomTimestamp && table.timestamp.createdAt) {
69
- createdAt = table.timestamp.createdAt;
70
+ if (isCustomTimestamp) {
71
+ if (typeof table.timestamp.createdAt === "string") {
72
+ createdAt = table.timestamp.createdAt;
73
+ }
74
+ isHasCreatedAt = table.timestamp.createdAt === false;
70
75
  }
71
- if (isCustomTimestamp && table.timestamp.updatedAt) {
72
- updatedAt = table.timestamp.updatedAt;
76
+ if (isCustomTimestamp) {
77
+ if (typeof table.timestamp.updatedAt === "string") {
78
+ updatedAt = table.timestamp.updatedAt;
79
+ }
80
+ isHasUpdatedAt = table.timestamp.updatedAt === false;
73
81
  }
74
82
  }
75
83
  return {
76
84
  isWithTimestamp,
77
85
  timestamp,
78
86
  createdAt,
79
- updatedAt
87
+ updatedAt,
88
+ isHasUpdatedAt,
89
+ isHasCreatedAt
80
90
  };
81
91
  }
82
92
  function getParanoid(table) {
@@ -1,5 +1,6 @@
1
1
  declare const Dialect: {
2
2
  readonly POSTGRES: "postgres";
3
+ readonly MYSQL: "mysql";
3
4
  readonly SQLITE: "sqlite";
4
5
  };
5
6
  type Dialect = (typeof Dialect)[keyof typeof Dialect];
@@ -1,6 +1,7 @@
1
1
  // src/table/constants.ts
2
2
  var Dialect = {
3
3
  POSTGRES: "postgres",
4
+ MYSQL: "mysql",
4
5
  SQLITE: "sqlite"
5
6
  };
6
7
 
@@ -1,7 +1,8 @@
1
- export { T as Table } from '../index-FMT0YEO7.js';
1
+ export { T as Table } from '../index-BdpoD4zk.js';
2
2
  import '../column/index.js';
3
3
  import './constants.js';
4
4
  import '../column/constants.js';
5
5
  import '../types.js';
6
6
  import '../query/constants.js';
7
+ import 'bun';
7
8
  import '../column/types.js';
@@ -4,22 +4,21 @@ import { defineColumns } from './utilities.js';
4
4
  // src/table/index.ts
5
5
  var Table = class _Table {
6
6
  client;
7
- dialect;
7
+ _dialect;
8
8
  name;
9
9
  columns;
10
10
  timestamp;
11
11
  paranoid;
12
12
  _output;
13
13
  constructor(options) {
14
- this.dialect = options.dialect;
14
+ this._dialect = options.dialect || null;
15
15
  this.name = options.name;
16
16
  this.columns = options.columns;
17
17
  this.paranoid = options.paranoid || null;
18
18
  this.timestamp = options.timestamp || null;
19
19
  this.client = null;
20
- for (const column of Object.values(this.columns)) {
21
- column.dialect(options.dialect);
22
- }
20
+ if (!this._dialect) return;
21
+ this.setColumnDialect(this._dialect);
23
22
  }
24
23
  infer() {
25
24
  return null;
@@ -31,18 +30,39 @@ var Table = class _Table {
31
30
  columns
32
31
  });
33
32
  }
34
- async create(db = this.client) {
33
+ setColumnDialect(dialect) {
34
+ for (const column of Object.values(this.columns)) {
35
+ column.dialect(dialect);
36
+ }
37
+ }
38
+ get dialect() {
39
+ return this._dialect;
40
+ }
41
+ setDialect(dialect) {
42
+ this._dialect = dialect;
43
+ this.setColumnDialect(dialect);
44
+ return this;
45
+ }
46
+ async create(options = {}) {
47
+ const db = options.db || this.client;
35
48
  if (!db) throw new Error("Database client not defined");
36
49
  const sql = `CREATE TABLE IF NOT EXISTS ${this.name} (${Object.entries(
37
50
  this.columns
38
51
  ).map(([name, column]) => `${name} ${column.toQuery().query}`).join(", ")});`;
39
- await db.exec(sql);
52
+ await db.exec({
53
+ sql,
54
+ tx: options.tx
55
+ });
40
56
  return this;
41
57
  }
42
- async drop(db = this.client) {
58
+ async drop(options = {}) {
59
+ const db = options.db || this.client;
43
60
  if (!db) throw new Error("Database client not defined");
44
61
  const sql = `DROP TABLE IF EXISTS ${this.name};`;
45
- await db.exec(sql);
62
+ await db.exec({
63
+ sql,
64
+ tx: options.tx
65
+ });
46
66
  return this;
47
67
  }
48
68
  query() {
@@ -1,4 +1,5 @@
1
- export { M as MergeTimestampParanoid, s as TableOptions, t as TableOutput, a as TimestampOptions } from '../index-FMT0YEO7.js';
1
+ import 'bun';
2
+ export { E as ExecOptions, M as MergeTimestampParanoid, x as TableOptions, y as TableOutput, a as TimestampOptions } from '../index-BdpoD4zk.js';
2
3
  import '../column/index.js';
3
4
  import './constants.js';
4
5
  import '../column/constants.js';
@@ -1,7 +1,8 @@
1
1
  import '../column/constants.js';
2
2
  import '../column/index.js';
3
3
  import './constants.js';
4
- export { p as createdAt, r as defineColumns, q as deletedAt, u as updatedAt } from '../index-FMT0YEO7.js';
4
+ export { t as createdAt, w as defineColumns, v as deletedAt, u as updatedAt } from '../index-BdpoD4zk.js';
5
5
  import '../column/types.js';
6
6
  import '../types.js';
7
7
  import '../query/constants.js';
8
+ import 'bun';
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "dist/cjs/index.js",
4
4
  "module": "dist/esm/index.js",
5
5
  "types": "./dist/esm/index.d.ts",
6
- "version": "0.2.2",
6
+ "version": "0.3.0",
7
7
  "type": "module",
8
8
  "exports": {
9
9
  ".": {