@ignisia/sql 0.2.2 → 0.4.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 (130) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/column/constants.d.cts +58 -5
  3. package/dist/cjs/column/constants.js +79 -22
  4. package/dist/cjs/column/index.d.cts +1 -0
  5. package/dist/cjs/column/index.js +31 -18
  6. package/dist/cjs/column/utils.d.cts +133 -0
  7. package/dist/cjs/column/utils.js +34 -0
  8. package/dist/cjs/database/alter.d.cts +3 -2
  9. package/dist/cjs/database/alter.js +15 -15
  10. package/dist/cjs/database/column.d.cts +3 -2
  11. package/dist/cjs/database/column.js +9 -7
  12. package/dist/cjs/database/contract.d.cts +3 -2
  13. package/dist/cjs/database/index.d.cts +3 -2
  14. package/dist/cjs/database/index.js +15 -2
  15. package/dist/cjs/database/table.d.cts +3 -2
  16. package/dist/cjs/database/table.js +12 -4
  17. package/dist/cjs/database/types.d.cts +2 -1
  18. package/dist/cjs/database/wrapper.d.cts +17 -21
  19. package/dist/cjs/database/wrapper.js +28 -52
  20. package/dist/cjs/index-CHxuUiO4.d.cts +472 -0
  21. package/dist/cjs/{index---zaMa69.d.cts → index-CZhrzE5r.d.cts} +5 -3
  22. package/dist/cjs/index.d.cts +3 -2
  23. package/dist/cjs/migration/index.d.cts +3 -2
  24. package/dist/cjs/migration/runner.js +1 -1
  25. package/dist/cjs/migration/type.d.cts +3 -2
  26. package/dist/cjs/query/builder.d.cts +3 -2
  27. package/dist/cjs/query/builder.js +1 -1
  28. package/dist/cjs/query/condition/common.d.cts +41 -0
  29. package/dist/cjs/query/condition/common.js +62 -0
  30. package/dist/cjs/query/condition/core.d.cts +8 -0
  31. package/dist/cjs/query/condition/core.js +57 -0
  32. package/dist/cjs/query/condition/index.d.cts +10 -0
  33. package/dist/cjs/query/condition/index.js +33 -0
  34. package/dist/cjs/query/condition/not.d.cts +34 -0
  35. package/dist/cjs/query/condition/not.js +51 -0
  36. package/dist/cjs/query/condition/raw.d.cts +8 -0
  37. package/dist/cjs/query/condition/raw.js +54 -0
  38. package/dist/cjs/query/constants.d.cts +27 -1
  39. package/dist/cjs/query/constants.js +28 -2
  40. package/dist/cjs/query/contract.d.cts +4 -3
  41. package/dist/cjs/query/explain.d.cts +12 -0
  42. package/dist/cjs/query/explain.js +65 -0
  43. package/dist/cjs/query/helper.d.cts +3 -2
  44. package/dist/cjs/query/helper.js +1 -1
  45. package/dist/cjs/query/index.d.cts +3 -2
  46. package/dist/cjs/query/index.js +69 -65
  47. package/dist/cjs/query/join.d.cts +18 -5
  48. package/dist/cjs/query/join.js +38 -9
  49. package/dist/cjs/query/sql.d.cts +9 -6
  50. package/dist/cjs/query/sql.js +80 -21
  51. package/dist/cjs/query/types.d.cts +2 -1
  52. package/dist/cjs/query/utilities.d.cts +7 -3
  53. package/dist/cjs/query/utilities.js +86 -5
  54. package/dist/cjs/table/constants.d.cts +1 -0
  55. package/dist/cjs/table/constants.js +1 -0
  56. package/dist/cjs/table/index.d.cts +3 -2
  57. package/dist/cjs/table/index.js +29 -9
  58. package/dist/cjs/table/types.d.cts +3 -2
  59. package/dist/cjs/table/utilities.d.cts +2 -1
  60. package/dist/cjs/types.d.cts +5 -1
  61. package/dist/cjs/utilities.d.cts +2 -1
  62. package/dist/cjs/utilities.js +22 -0
  63. package/dist/esm/column/constants.d.ts +58 -5
  64. package/dist/esm/column/constants.js +78 -23
  65. package/dist/esm/column/index.d.ts +1 -0
  66. package/dist/esm/column/index.js +31 -18
  67. package/dist/esm/column/utils.d.ts +133 -0
  68. package/dist/esm/column/utils.js +32 -0
  69. package/dist/esm/database/alter.d.ts +3 -2
  70. package/dist/esm/database/alter.js +15 -15
  71. package/dist/esm/database/column.d.ts +3 -2
  72. package/dist/esm/database/column.js +9 -7
  73. package/dist/esm/database/contract.d.ts +3 -2
  74. package/dist/esm/database/index.d.ts +3 -2
  75. package/dist/esm/database/index.js +17 -4
  76. package/dist/esm/database/table.d.ts +3 -2
  77. package/dist/esm/database/table.js +12 -4
  78. package/dist/esm/database/types.d.ts +2 -1
  79. package/dist/esm/database/wrapper.d.ts +17 -21
  80. package/dist/esm/database/wrapper.js +27 -53
  81. package/dist/esm/index-CjurLJdK.d.ts +472 -0
  82. package/dist/esm/{index-DFrpzXEn.d.ts → index-DgOs61lH.d.ts} +5 -3
  83. package/dist/esm/index.d.ts +3 -2
  84. package/dist/esm/migration/index.d.ts +3 -2
  85. package/dist/esm/migration/runner.js +1 -1
  86. package/dist/esm/migration/type.d.ts +3 -2
  87. package/dist/esm/query/builder.d.ts +3 -2
  88. package/dist/esm/query/builder.js +1 -1
  89. package/dist/esm/query/condition/common.d.ts +41 -0
  90. package/dist/esm/query/condition/common.js +56 -0
  91. package/dist/esm/query/condition/core.d.ts +8 -0
  92. package/dist/esm/query/condition/core.js +55 -0
  93. package/dist/esm/query/condition/index.d.ts +10 -0
  94. package/dist/esm/query/condition/index.js +4 -0
  95. package/dist/esm/query/condition/not.d.ts +34 -0
  96. package/dist/esm/query/condition/not.js +46 -0
  97. package/dist/esm/query/condition/raw.d.ts +8 -0
  98. package/dist/esm/query/condition/raw.js +50 -0
  99. package/dist/esm/query/constants.d.ts +27 -1
  100. package/dist/esm/query/constants.js +27 -3
  101. package/dist/esm/query/contract.d.ts +4 -3
  102. package/dist/esm/query/explain.d.ts +12 -0
  103. package/dist/esm/query/explain.js +64 -0
  104. package/dist/esm/query/helper.d.ts +3 -2
  105. package/dist/esm/query/helper.js +2 -2
  106. package/dist/esm/query/index.d.ts +3 -2
  107. package/dist/esm/query/index.js +72 -68
  108. package/dist/esm/query/join.d.ts +18 -5
  109. package/dist/esm/query/join.js +37 -9
  110. package/dist/esm/query/sql.d.ts +9 -6
  111. package/dist/esm/query/sql.js +80 -23
  112. package/dist/esm/query/types.d.ts +2 -1
  113. package/dist/esm/query/utilities.d.ts +7 -3
  114. package/dist/esm/query/utilities.js +86 -6
  115. package/dist/esm/table/constants.d.ts +1 -0
  116. package/dist/esm/table/constants.js +1 -0
  117. package/dist/esm/table/index.d.ts +3 -2
  118. package/dist/esm/table/index.js +29 -9
  119. package/dist/esm/table/types.d.ts +3 -2
  120. package/dist/esm/table/utilities.d.ts +2 -1
  121. package/dist/esm/types.d.ts +5 -1
  122. package/dist/esm/utilities.d.ts +2 -1
  123. package/dist/esm/utilities.js +22 -1
  124. package/package.json +8 -2
  125. package/dist/cjs/index-CwiFQh0I.d.cts +0 -358
  126. package/dist/cjs/query/condition.d.cts +0 -7
  127. package/dist/cjs/query/condition.js +0 -106
  128. package/dist/esm/index-FMT0YEO7.d.ts +0 -358
  129. package/dist/esm/query/condition.d.ts +0 -7
  130. package/dist/esm/query/condition.js +0 -98
@@ -0,0 +1,32 @@
1
+ import { ColumnPropertyMapping, ColumnProperties } from './constants.js';
2
+
3
+ // src/column/utils.ts
4
+ function getColumnProperty(dialect, property) {
5
+ return ColumnPropertyMapping[property][dialect];
6
+ }
7
+ function columnProperty(dialect) {
8
+ return {
9
+ [ColumnProperties.NOT_NULL]: getColumnProperty(
10
+ dialect,
11
+ ColumnProperties.NOT_NULL
12
+ ),
13
+ [ColumnProperties.UNIQUE]: getColumnProperty(
14
+ dialect,
15
+ ColumnProperties.UNIQUE
16
+ ),
17
+ [ColumnProperties.DEFAULT]: getColumnProperty(
18
+ dialect,
19
+ ColumnProperties.DEFAULT
20
+ ),
21
+ [ColumnProperties.AUTO_INCREMENT]: getColumnProperty(
22
+ dialect,
23
+ ColumnProperties.AUTO_INCREMENT
24
+ ),
25
+ [ColumnProperties.PRIMARY_KEY]: getColumnProperty(
26
+ dialect,
27
+ ColumnProperties.PRIMARY_KEY
28
+ )
29
+ };
30
+ }
31
+
32
+ export { columnProperty, getColumnProperty };
@@ -1,8 +1,9 @@
1
- import { D as Database } from '../index-DFrpzXEn.js';
1
+ import { D as Database } from '../index-DgOs61lH.js';
2
2
  import { Column } from '../column/index.js';
3
3
  import { AcceptedColumnTypes } from '../column/constants.js';
4
- import { T as Table, D as DatabaseDefinition } from '../index-FMT0YEO7.js';
4
+ import { T as Table, D as DatabaseDefinition } from '../index-CjurLJdK.js';
5
5
  import { Dialect } from '../table/constants.js';
6
+ import 'bun';
6
7
  import '../query/constants.js';
7
8
  import '../column/types.js';
8
9
  import '../types.js';
@@ -5,9 +5,9 @@ async function alterColumnType(tableName, columnName, newType) {
5
5
  if (this.dialect === Dialect.SQLITE) {
6
6
  throw new Error("SQLite does not support ALTER COLUMN TYPE directly.");
7
7
  }
8
- await this.client.exec(
9
- `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} TYPE ${newType}`
10
- );
8
+ await this.client.exec({
9
+ sql: `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} TYPE ${newType}`
10
+ });
11
11
  if (!this.tables[tableName]) return this;
12
12
  this.tables[tableName].columns[columnName].type = newType;
13
13
  return this;
@@ -16,9 +16,9 @@ async function setColumnDefault(tableName, columnName, value) {
16
16
  if (this.dialect === Dialect.SQLITE) {
17
17
  throw new Error("SQLite does not support ALTER COLUMN DEFAULT directly.");
18
18
  }
19
- await this.client.exec(
20
- `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} SET DEFAULT ${value}`
21
- );
19
+ await this.client.exec({
20
+ sql: `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} SET DEFAULT ${value}`
21
+ });
22
22
  if (!this.tables[tableName]) return this;
23
23
  if (
24
24
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -33,9 +33,9 @@ async function dropColumnDefault(tableName, columnName) {
33
33
  if (this.dialect === Dialect.SQLITE) {
34
34
  throw new Error("SQLite does not support DROP DEFAULT directly.");
35
35
  }
36
- await this.client.exec(
37
- `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} DROP DEFAULT`
38
- );
36
+ await this.client.exec({
37
+ sql: `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} DROP DEFAULT`
38
+ });
39
39
  if (!this.tables[tableName]) return this;
40
40
  if (
41
41
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -52,9 +52,9 @@ async function setColumnNotNull(tableName, columnName) {
52
52
  "SQLite does not support SET NOT NULL (requires table rebuild)"
53
53
  );
54
54
  }
55
- await this.client.exec(
56
- `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} SET NOT NULL`
57
- );
55
+ await this.client.exec({
56
+ sql: `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} SET NOT NULL`
57
+ });
58
58
  if (!this.tables[tableName]) return this;
59
59
  if (
60
60
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -71,9 +71,9 @@ async function dropColumnNotNull(tableName, columnName) {
71
71
  "SQLite does not support DROP NOT NULL (requires table rebuild)"
72
72
  );
73
73
  }
74
- await this.client.exec(
75
- `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} DROP NOT NULL`
76
- );
74
+ await this.client.exec({
75
+ sql: `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} DROP NOT NULL`
76
+ });
77
77
  if (!this.tables[tableName]) return this;
78
78
  if (
79
79
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -1,7 +1,8 @@
1
- import { D as Database } from '../index-DFrpzXEn.js';
1
+ import { D as Database } from '../index-DgOs61lH.js';
2
2
  import { Column } from '../column/index.js';
3
- import { T as Table, D as DatabaseDefinition } from '../index-FMT0YEO7.js';
3
+ import { T as Table, D as DatabaseDefinition } from '../index-CjurLJdK.js';
4
4
  import { Dialect } from '../table/constants.js';
5
+ import 'bun';
5
6
  import '../query/constants.js';
6
7
  import '../column/constants.js';
7
8
  import '../column/types.js';
@@ -2,9 +2,9 @@ import { Dialect } from '../table/constants.js';
2
2
 
3
3
  // src/database/column.ts
4
4
  async function addColumn(tableName, columnName, column) {
5
- await this.client.exec(
6
- `ALTER TABLE ${tableName} ADD COLUMN ${columnName} ${column.toString()};`
7
- );
5
+ await this.client.exec({
6
+ sql: `ALTER TABLE ${tableName} ADD COLUMN ${columnName} ${column.toString()};`
7
+ });
8
8
  if (!this.tables[tableName]) return this;
9
9
  this.tables[tableName].columns[columnName] = column;
10
10
  return this;
@@ -13,9 +13,9 @@ async function renameColumn(tableName, oldName, newName) {
13
13
  if (this.dialect === Dialect.SQLITE) {
14
14
  throw new Error("SQLite does not support RENAME COLUMN natively.");
15
15
  }
16
- await this.client.exec(
17
- `ALTER TABLE ${tableName} RENAME COLUMN ${oldName} TO ${newName};`
18
- );
16
+ await this.client.exec({
17
+ sql: `ALTER TABLE ${tableName} RENAME COLUMN ${oldName} TO ${newName};`
18
+ });
19
19
  if (!this.tables[tableName]) return this;
20
20
  this.tables[tableName].columns[newName] = this.tables[tableName].columns[oldName];
21
21
  delete this.tables[tableName].columns[oldName];
@@ -25,7 +25,9 @@ async function dropColumn(tableName, columnName) {
25
25
  if (this.dialect === Dialect.SQLITE) {
26
26
  throw new Error("SQLite does not support DROP COLUMN natively.");
27
27
  }
28
- await this.client.exec(`ALTER TABLE ${tableName} DROP COLUMN ${columnName};`);
28
+ await this.client.exec({
29
+ sql: `ALTER TABLE ${tableName} DROP COLUMN ${columnName};`
30
+ });
29
31
  if (!this.tables[tableName]) return this;
30
32
  delete this.tables[tableName].columns[columnName];
31
33
  return this;
@@ -1,8 +1,9 @@
1
- export { C as ColumnAlterationContract, T as TableAlterationContract } from '../index-DFrpzXEn.js';
1
+ export { C as ColumnAlterationContract, T as TableAlterationContract } from '../index-DgOs61lH.js';
2
2
  import '../column/index.js';
3
3
  import '../column/constants.js';
4
- import '../index-FMT0YEO7.js';
4
+ import '../index-CjurLJdK.js';
5
5
  import '../table/constants.js';
6
+ import 'bun';
6
7
  import '../query/constants.js';
7
8
  import '../column/types.js';
8
9
  import '../types.js';
@@ -1,8 +1,9 @@
1
+ import 'bun';
1
2
  import '../column/index.js';
2
- import '../index-FMT0YEO7.js';
3
+ import '../index-CjurLJdK.js';
3
4
  import '../query/constants.js';
4
5
  import '../table/constants.js';
5
- export { D as Database } from '../index-DFrpzXEn.js';
6
+ export { D as Database } from '../index-DgOs61lH.js';
6
7
  import '../column/constants.js';
7
8
  import '../column/types.js';
8
9
  import '../types.js';
@@ -1,9 +1,9 @@
1
1
  import '../table/index.js';
2
2
  import { Dialect } from '../table/constants.js';
3
- import { alterColumnType, setColumnDefault, dropColumnDefault, dropColumnNotNull } from './alter.js';
3
+ import { alterColumnType, setColumnDefault, dropColumnDefault, setColumnNotNull, dropColumnNotNull } from './alter.js';
4
4
  import { addColumn, renameColumn, dropColumn } from './column.js';
5
5
  import { createTable, renameTable, dropTable } from './table.js';
6
- import { DatabasePsql, DatabaseSqlite } from './wrapper.js';
6
+ import { DatabaseMysql, DatabasePsql, DatabaseSqlite } from './wrapper.js';
7
7
 
8
8
  // src/database/index.ts
9
9
  var Database = class _Database {
@@ -31,10 +31,11 @@ var Database = class _Database {
31
31
  dialect: options.dialect,
32
32
  config: options.config
33
33
  };
34
- this.client = options.dialect === Dialect.POSTGRES ? new DatabasePsql(options.config) : new DatabaseSqlite(options.config);
34
+ this.client = this.createClient(options);
35
35
  if (options.tables) {
36
36
  for (const tableName in options.tables) {
37
37
  options.tables[tableName].client = this.client;
38
+ options.tables[tableName].setDialect(this.dialect);
38
39
  }
39
40
  }
40
41
  this.createTable = createTable.bind(this);
@@ -52,13 +53,25 @@ var Database = class _Database {
52
53
  this.dropColumnDefault = dropColumnDefault.bind(
53
54
  this
54
55
  );
55
- this.setColumnNotNull = setColumnDefault.bind(
56
+ this.setColumnNotNull = setColumnNotNull.bind(
56
57
  this
57
58
  );
58
59
  this.dropColumnNotNull = dropColumnNotNull.bind(
59
60
  this
60
61
  );
61
62
  }
63
+ createClient(options) {
64
+ switch (options.dialect) {
65
+ case Dialect.SQLITE:
66
+ return new DatabaseSqlite(options.config);
67
+ case Dialect.POSTGRES:
68
+ return new DatabasePsql(options.config);
69
+ case Dialect.MYSQL:
70
+ return new DatabaseMysql(options.config);
71
+ default:
72
+ throw new Error(`Dialect ${options.dialect} is not supported`);
73
+ }
74
+ }
62
75
  table(tableName) {
63
76
  if (!this.tables[tableName]) {
64
77
  throw new Error(`Table ${tableName} does not exist`);
@@ -1,7 +1,8 @@
1
- import { D as Database } from '../index-DFrpzXEn.js';
1
+ import { D as Database } from '../index-DgOs61lH.js';
2
2
  import { Column } from '../column/index.js';
3
- import { T as Table, D as DatabaseDefinition, a as TimestampOptions, M as MergeTimestampParanoid } from '../index-FMT0YEO7.js';
3
+ import { T as Table, D as DatabaseDefinition, f as TimestampOptions, M as MergeTimestampParanoid } from '../index-CjurLJdK.js';
4
4
  import { Dialect } from '../table/constants.js';
5
+ import 'bun';
5
6
  import '../query/constants.js';
6
7
  import '../column/constants.js';
7
8
  import '../column/types.js';
@@ -16,21 +16,29 @@ async function createTable(tableName, columns, options) {
16
16
  while (this.client.status === "connecting") {
17
17
  await new Promise((resolve) => setTimeout(resolve, 100));
18
18
  }
19
- await table.create(this.client);
19
+ await table.create({
20
+ db: this.client
21
+ });
20
22
  return this;
21
23
  }
22
24
  async function renameTable(oldName, newName) {
23
- await this.client.exec(`ALTER TABLE ${oldName} RENAME TO ${newName};`);
25
+ await this.client.exec({
26
+ sql: `ALTER TABLE ${oldName} RENAME TO ${newName};`
27
+ });
24
28
  this.tables[newName] = this.tables[oldName];
25
29
  delete this.tables[oldName];
26
30
  return this;
27
31
  }
28
32
  async function dropTable(tableName) {
29
33
  if (!this.tables[tableName]) {
30
- await this.client.exec(`DROP TABLE IF EXISTS ${tableName};`);
34
+ await this.client.exec({
35
+ sql: `DROP TABLE IF EXISTS ${tableName};`
36
+ });
31
37
  return this;
32
38
  }
33
- await this.tables[tableName].drop(this.client);
39
+ await this.tables[tableName].drop({
40
+ db: this.client
41
+ });
34
42
  delete this.tables[tableName];
35
43
  return this;
36
44
  }
@@ -1,5 +1,6 @@
1
+ import 'bun';
1
2
  import '../column/index.js';
2
- export { D as DatabaseDefinition, b as DatabaseDialect, g as DatabaseOptions, P as PostgresConfig, S as SqliteConfig } from '../index-FMT0YEO7.js';
3
+ export { D as DatabaseDefinition, g as DatabaseDialect, h as DatabaseExecOptions, q as DatabaseOptions, i as MysqlConfig, P as PostgresConfig, J as SqlConfig, S as SqlConfigMapping, j as SqliteConfig } from '../index-CjurLJdK.js';
3
4
  import '../table/constants.js';
4
5
  import '../column/constants.js';
5
6
  import '../column/types.js';
@@ -1,36 +1,32 @@
1
- import { SQL } from 'bun';
2
- import { Database } from 'bun:sqlite';
1
+ import { SQL, TransactionSQL } from 'bun';
3
2
  import { Dialect } from '../table/constants.js';
4
- import { b as DatabaseDialect, P as PostgresConfig, S as SqliteConfig } from '../index-FMT0YEO7.js';
3
+ import { S as SqlConfigMapping, g as DatabaseDialect, h as DatabaseExecOptions, P as PostgresConfig, i as MysqlConfig, j as SqliteConfig } from '../index-CjurLJdK.js';
5
4
  import '../column/index.js';
6
5
  import '../column/constants.js';
7
6
  import '../column/types.js';
8
7
  import '../types.js';
9
8
  import '../query/constants.js';
10
9
 
11
- declare class DatabasePsql implements DatabaseDialect {
12
- readonly dialect: typeof Dialect.POSTGRES;
13
- readonly options: PostgresConfig;
10
+ declare class BaseSql<DbDialect extends Dialect, Options extends SqlConfigMapping[DbDialect]> implements DatabaseDialect {
11
+ readonly dialect: Dialect;
12
+ readonly options: Options;
14
13
  client: SQL;
15
14
  status: 'connecting' | 'connected' | 'disconnected';
16
- constructor(options: PostgresConfig);
15
+ constructor(dialect: Dialect, options: Options);
17
16
  connect(): Promise<this>;
18
17
  disconnect(): Promise<this>;
19
- exec<T>(sql: string): Promise<T>;
20
- exec<T>(sql: string, values: any[]): Promise<T>;
21
- transaction<T, U extends () => Promise<T>>(fn: U): Promise<T>;
18
+ exec<T>(options: DatabaseExecOptions): Promise<T>;
19
+ transaction<T, U extends (tx: TransactionSQL) => Promise<T>>(fn: U): Promise<T>;
20
+ distributed<T extends string, U, V extends (tx: TransactionSQL) => Promise<U>>(name: T, fn: V): Promise<U>;
22
21
  }
23
- declare class DatabaseSqlite implements DatabaseDialect {
24
- readonly dialect: typeof Dialect.SQLITE;
25
- readonly options: SqliteConfig;
26
- client: Database;
27
- status: 'connecting' | 'connected' | 'disconnected';
22
+ declare class DatabasePsql extends BaseSql<typeof Dialect.POSTGRES, PostgresConfig> {
23
+ constructor(options: PostgresConfig);
24
+ }
25
+ declare class DatabaseMysql extends BaseSql<typeof Dialect.MYSQL, MysqlConfig> {
26
+ constructor(options: MysqlConfig);
27
+ }
28
+ declare class DatabaseSqlite extends BaseSql<typeof Dialect.SQLITE, SqliteConfig> {
28
29
  constructor(options: SqliteConfig);
29
- connect(): Promise<this>;
30
- disconnect(): Promise<this>;
31
- exec<T>(sql: string): Promise<T>;
32
- exec<T>(sql: string, params: any): Promise<T>;
33
- transaction<T, U extends () => Promise<T>>(fn: U): Promise<T>;
34
30
  }
35
31
 
36
- export { DatabasePsql, DatabaseSqlite };
32
+ export { BaseSql, DatabaseMysql, DatabasePsql, DatabaseSqlite };
@@ -1,19 +1,19 @@
1
1
  import { SQL } from 'bun';
2
- import { Database } from 'bun:sqlite';
3
2
  import { Dialect } from '../table/constants.js';
4
3
 
5
4
  // src/database/wrapper.ts
6
- var DatabasePsql = class {
5
+ var BaseSql = class {
7
6
  dialect;
8
7
  options;
9
8
  client;
10
9
  status;
11
- constructor(options) {
12
- this.dialect = Dialect.POSTGRES;
10
+ constructor(dialect, options) {
11
+ this.dialect = dialect;
13
12
  this.options = options;
14
13
  this.status = "connecting";
15
14
  this.client = new SQL({
16
15
  ...options,
16
+ adapter: dialect,
17
17
  onconnect: () => {
18
18
  this.status = "connected";
19
19
  },
@@ -31,63 +31,37 @@ var DatabasePsql = class {
31
31
  await this.client.close();
32
32
  return this;
33
33
  }
34
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
- async exec(sql, values) {
36
- if (!values) {
37
- return this.client.unsafe(sql);
34
+ async exec(options) {
35
+ const client = options.tx || this.client;
36
+ if (!client) {
37
+ throw new Error("Database not connected");
38
+ }
39
+ if (!options.params) {
40
+ return client.unsafe(options.sql);
38
41
  }
39
- return this.client.unsafe(sql, values);
42
+ return client.unsafe(options.sql, options.params);
40
43
  }
41
44
  async transaction(fn) {
42
- try {
43
- await this.exec("BEGIN");
44
- const result = await fn();
45
- await this.exec("COMMIT");
46
- return result;
47
- } catch (err) {
48
- await this.exec("ROLLBACK");
49
- throw err;
50
- }
45
+ return this.client.transaction(fn);
46
+ }
47
+ async distributed(name, fn) {
48
+ return this.client.distributed(name, fn);
51
49
  }
52
50
  };
53
- var DatabaseSqlite = class {
54
- dialect;
55
- options;
56
- client;
57
- status;
51
+ var DatabasePsql = class extends BaseSql {
58
52
  constructor(options) {
59
- this.dialect = Dialect.SQLITE;
60
- this.options = options;
61
- this.status = "connecting";
62
- this.client = new Database(options.filename);
63
- this.status = "connected";
64
- }
65
- async connect() {
66
- this.client = new Database(this.options.filename);
67
- this.status = "connected";
68
- return this;
69
- }
70
- async disconnect() {
71
- this.client.close();
72
- this.status = "disconnected";
73
- return this;
53
+ super(Dialect.POSTGRES, options);
74
54
  }
75
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
76
- async exec(sql, params) {
77
- const query = this.client.prepare(sql, params);
78
- return query.all();
55
+ };
56
+ var DatabaseMysql = class extends BaseSql {
57
+ constructor(options) {
58
+ super(Dialect.MYSQL, options);
79
59
  }
80
- async transaction(fn) {
81
- try {
82
- await this.exec("BEGIN");
83
- const result = await fn();
84
- await this.exec("COMMIT");
85
- return result;
86
- } catch (err) {
87
- await this.exec("ROLLBACK");
88
- throw err;
89
- }
60
+ };
61
+ var DatabaseSqlite = class extends BaseSql {
62
+ constructor(options) {
63
+ super(Dialect.SQLITE, options);
90
64
  }
91
65
  };
92
66
 
93
- export { DatabasePsql, DatabaseSqlite };
67
+ export { BaseSql, DatabaseMysql, DatabasePsql, DatabaseSqlite };