@ignisia/sql 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/README.md +8 -8
  2. package/dist/column/constants.js +97 -8
  3. package/dist/column/index.js +105 -8
  4. package/dist/column/types.js +1 -2
  5. package/dist/database/alter.d.ts +3 -3
  6. package/dist/database/alter.js +87 -15
  7. package/dist/database/column.d.ts +3 -3
  8. package/dist/database/column.js +33 -11
  9. package/dist/database/contract.d.ts +3 -3
  10. package/dist/database/contract.js +1 -0
  11. package/dist/database/index.d.ts +3 -3
  12. package/dist/database/index.js +92 -19
  13. package/dist/database/table.d.ts +3 -3
  14. package/dist/database/table.js +37 -19
  15. package/dist/database/types.d.ts +1 -1
  16. package/dist/database/types.js +1 -0
  17. package/dist/database/wrapper.d.ts +1 -1
  18. package/dist/database/wrapper.js +92 -9
  19. package/dist/{index-Dcm5xIpR.d.ts → index-DFrpzXEn.d.ts} +5 -1
  20. package/dist/{index-DJhQVUY3.d.ts → index-FMT0YEO7.d.ts} +17 -3
  21. package/dist/index.d.ts +3 -3
  22. package/dist/index.js +5 -32
  23. package/dist/migration/index.d.ts +3 -3
  24. package/dist/migration/index.js +48 -6
  25. package/dist/migration/runner.js +8 -21
  26. package/dist/migration/type.d.ts +3 -3
  27. package/dist/migration/type.js +1 -0
  28. package/dist/query/builder.d.ts +1 -1
  29. package/dist/query/builder.js +66 -16
  30. package/dist/query/condition.d.ts +1 -1
  31. package/dist/query/condition.js +97 -24
  32. package/dist/query/constants.d.ts +6 -1
  33. package/dist/query/constants.js +54 -17
  34. package/dist/query/contract.d.ts +1 -1
  35. package/dist/query/contract.js +1 -0
  36. package/dist/query/helper.d.ts +1 -1
  37. package/dist/query/helper.js +30 -18
  38. package/dist/query/index.d.ts +1 -1
  39. package/dist/query/index.js +195 -10
  40. package/dist/query/join.d.ts +1 -1
  41. package/dist/query/join.js +16 -6
  42. package/dist/query/sql.d.ts +1 -1
  43. package/dist/query/sql.js +99 -16
  44. package/dist/query/types.d.ts +1 -1
  45. package/dist/query/types.js +1 -0
  46. package/dist/query/utilities.d.ts +1 -1
  47. package/dist/query/utilities.js +175 -24
  48. package/dist/table/constants.js +5 -5
  49. package/dist/table/index.d.ts +1 -1
  50. package/dist/table/index.js +52 -14
  51. package/dist/table/types.d.ts +1 -1
  52. package/dist/table/types.js +1 -0
  53. package/dist/table/utilities.d.ts +1 -1
  54. package/dist/table/utilities.js +50 -15
  55. package/dist/types.js +1 -0
  56. package/dist/utilities.js +18 -8
  57. package/package.json +37 -2
  58. package/dist/chunk-4DQRB5XS.js +0 -94
  59. package/dist/chunk-CIWX3UCZ.js +0 -51
  60. package/dist/chunk-D2ASIT4Q.js +0 -44
  61. package/dist/chunk-FYSNJAGD.js +0 -19
  62. package/dist/chunk-G3LSCLIQ.js +0 -104
  63. package/dist/chunk-GLOHF5CP.js +0 -9
  64. package/dist/chunk-GY7R637S.js +0 -113
  65. package/dist/chunk-HKTHKQLK.js +0 -98
  66. package/dist/chunk-JF7OSNH4.js +0 -40
  67. package/dist/chunk-MG2S4V4N.js +0 -60
  68. package/dist/chunk-TQ2GXAE7.js +0 -663
  69. package/dist/chunk-V4OMHVJN.js +0 -96
  70. package/dist/chunk-W2DR3ZVK.js +0 -59
  71. package/dist/chunk-WVJGTZFI.js +0 -60
  72. package/dist/chunk-Y7FSRHH3.js +0 -22
package/README.md CHANGED
@@ -19,14 +19,14 @@ bun add @ignisia/sql
19
19
  import { Table, Column, Database } from '@ignisia/sql';
20
20
 
21
21
  // Define a table
22
- const users = Table.define(
22
+ const users = Table.define({
23
23
  name: 'users',
24
+ dialect: 'postgres',
24
25
  columns: {
25
26
  // Define a column called "id"
26
27
  id: Column.define({
27
28
  type: 'SERIAL',
28
- })
29
- .primaryKey(),
29
+ }).primaryKey(),
30
30
  name: Column.define({
31
31
  type: 'TEXT',
32
32
  length: 255,
@@ -47,7 +47,7 @@ const users = Table.define(
47
47
  // By default the name of the created at column called "createdAt" and the name of the updated at column called "updatedAt"
48
48
  // Pass an object with `createdAt` and `updatedAt` to rename them
49
49
  timestamp: true,
50
- );
50
+ });
51
51
 
52
52
  // Define a database
53
53
  const db = Database.define({
@@ -57,7 +57,7 @@ const db = Database.define({
57
57
  config: {
58
58
  host: 'localhost',
59
59
  port: 5432,
60
- username: 'postgres',
60
+ user: 'postgres',
61
61
  password: 'postgres',
62
62
  database: 'my_database',
63
63
  },
@@ -65,13 +65,13 @@ const db = Database.define({
65
65
  // This will automatically assign db properties to each table so it can do query
66
66
  tables: {
67
67
  users,
68
- }
68
+ },
69
69
  });
70
70
 
71
71
  // Using the database
72
72
 
73
73
  // Fetch all users
74
- const listUsers = await db.table('users').select().query()
74
+ const listUsers = await db.table('users').select().query();
75
75
  // ^ Add .limit before the .query() to limit the number of results
76
76
  // ^ Add .offset before the .query() to offset the results
77
77
  // ^ Add .orderBy before the .query() to order the results
@@ -93,7 +93,7 @@ const newUsers = await db.table('users').insert(
93
93
  );
94
94
 
95
95
  // Delete a user
96
- const deletedUsers = await db.table('users').delete()
96
+ const deletedUsers = await db.table('users').delete();
97
97
  // ^ Add .where before the .query() to filter the deleted rows
98
98
 
99
99
  // Update a user
@@ -1,9 +1,98 @@
1
- import {
2
- AcceptedColumnTypes,
3
- ColumnTypeMapping
4
- } from "../chunk-G3LSCLIQ.js";
5
- import "../chunk-GLOHF5CP.js";
6
- export {
7
- AcceptedColumnTypes,
8
- ColumnTypeMapping
1
+ import { Dialect } from '../table/constants';
2
+
3
+ const AcceptedColumnTypes = {
4
+ INTEGER: "INTEGER",
5
+ STRING: "STRING",
6
+ BOOLEAN: "BOOLEAN",
7
+ DATE: "DATE",
8
+ FLOAT: "FLOAT",
9
+ DECIMAL: "DECIMAL",
10
+ BIGINT: "BIGINT",
11
+ TEXT: "TEXT",
12
+ BLOB: "BLOB",
13
+ JSON: "JSON",
14
+ VARCHAR: "VARCHAR",
15
+ TIME: "TIME",
16
+ TIMESTAMP: "TIMESTAMP",
17
+ DOUBLE: "DOUBLE",
18
+ DATETIME: "DATETIME",
19
+ DATEONLY: "DATEONLY",
20
+ ENUM: "ENUM",
21
+ SERIAL: "SERIAL"
9
22
  };
23
+ const ColumnTypeMapping = {
24
+ [AcceptedColumnTypes.INTEGER]: {
25
+ [Dialect.SQLITE]: "INTEGER",
26
+ [Dialect.POSTGRES]: "INTEGER"
27
+ },
28
+ [AcceptedColumnTypes.STRING]: {
29
+ [Dialect.SQLITE]: "TEXT",
30
+ [Dialect.POSTGRES]: "VARCHAR"
31
+ },
32
+ [AcceptedColumnTypes.BOOLEAN]: {
33
+ [Dialect.SQLITE]: "INTEGER",
34
+ [Dialect.POSTGRES]: "BOOLEAN"
35
+ },
36
+ [AcceptedColumnTypes.DATE]: {
37
+ [Dialect.SQLITE]: "TEXT",
38
+ [Dialect.POSTGRES]: "DATE"
39
+ },
40
+ [AcceptedColumnTypes.FLOAT]: {
41
+ [Dialect.SQLITE]: "REAL",
42
+ [Dialect.POSTGRES]: "FLOAT"
43
+ },
44
+ [AcceptedColumnTypes.DECIMAL]: {
45
+ [Dialect.SQLITE]: "TEXT",
46
+ [Dialect.POSTGRES]: "DECIMAL"
47
+ },
48
+ [AcceptedColumnTypes.BIGINT]: {
49
+ [Dialect.SQLITE]: "TEXT",
50
+ [Dialect.POSTGRES]: "BIGINT"
51
+ },
52
+ [AcceptedColumnTypes.TEXT]: {
53
+ [Dialect.SQLITE]: "TEXT",
54
+ [Dialect.POSTGRES]: "TEXT"
55
+ },
56
+ [AcceptedColumnTypes.BLOB]: {
57
+ [Dialect.SQLITE]: "BLOB",
58
+ [Dialect.POSTGRES]: "BYTEA"
59
+ },
60
+ [AcceptedColumnTypes.JSON]: {
61
+ [Dialect.SQLITE]: "TEXT",
62
+ [Dialect.POSTGRES]: "JSONB"
63
+ },
64
+ [AcceptedColumnTypes.VARCHAR]: {
65
+ [Dialect.SQLITE]: "TEXT",
66
+ [Dialect.POSTGRES]: "VARCHAR"
67
+ },
68
+ [AcceptedColumnTypes.TIME]: {
69
+ [Dialect.SQLITE]: "TEXT",
70
+ [Dialect.POSTGRES]: "TIME"
71
+ },
72
+ [AcceptedColumnTypes.TIMESTAMP]: {
73
+ [Dialect.SQLITE]: "TEXT",
74
+ [Dialect.POSTGRES]: "TIMESTAMP"
75
+ },
76
+ [AcceptedColumnTypes.DOUBLE]: {
77
+ [Dialect.SQLITE]: "REAL",
78
+ [Dialect.POSTGRES]: "DOUBLE PRECISION"
79
+ },
80
+ [AcceptedColumnTypes.DATETIME]: {
81
+ [Dialect.SQLITE]: "TEXT",
82
+ [Dialect.POSTGRES]: "TIMESTAMP"
83
+ },
84
+ [AcceptedColumnTypes.DATEONLY]: {
85
+ [Dialect.SQLITE]: "TEXT",
86
+ [Dialect.POSTGRES]: "DATE"
87
+ },
88
+ [AcceptedColumnTypes.ENUM]: {
89
+ [Dialect.SQLITE]: "TEXT",
90
+ [Dialect.POSTGRES]: "TEXT"
91
+ },
92
+ [AcceptedColumnTypes.SERIAL]: {
93
+ [Dialect.SQLITE]: "INTEGER AUTOINCREMENT",
94
+ [Dialect.POSTGRES]: "SERIAL"
95
+ }
96
+ };
97
+
98
+ export { AcceptedColumnTypes, ColumnTypeMapping };
@@ -1,8 +1,105 @@
1
- import {
2
- Column
3
- } from "../chunk-GY7R637S.js";
4
- import "../chunk-G3LSCLIQ.js";
5
- import "../chunk-GLOHF5CP.js";
6
- export {
7
- Column
8
- };
1
+ import { Dialect } from '../table/constants';
2
+ import { ColumnTypeMapping, AcceptedColumnTypes } from './constants';
3
+
4
+ class Column {
5
+ definition;
6
+ type;
7
+ length;
8
+ enums;
9
+ _output;
10
+ constructor(options) {
11
+ this.type = options.type;
12
+ this.enums = [];
13
+ this.length = null;
14
+ if ("length" in options) {
15
+ this.length = options.length;
16
+ }
17
+ if ("values" in options) {
18
+ this.enums = options.values;
19
+ }
20
+ this.definition = {
21
+ autoIncrement: false,
22
+ primaryKey: false,
23
+ notNull: false,
24
+ unique: false,
25
+ comment: null,
26
+ default: void 0
27
+ };
28
+ }
29
+ static define(options) {
30
+ return new Column(options);
31
+ }
32
+ autoIncrement() {
33
+ this.definition.autoIncrement = true;
34
+ return this;
35
+ }
36
+ primaryKey() {
37
+ this.definition.primaryKey = true;
38
+ return this;
39
+ }
40
+ notNull() {
41
+ this.definition.notNull = true;
42
+ return this;
43
+ }
44
+ unique() {
45
+ this.definition.unique = true;
46
+ return this;
47
+ }
48
+ comment(value) {
49
+ this.definition.comment = value;
50
+ return this;
51
+ }
52
+ default(value) {
53
+ this.definition.default = value;
54
+ return this;
55
+ }
56
+ dialect(dialect) {
57
+ this.definition.dialect = dialect;
58
+ return this;
59
+ }
60
+ toQuery() {
61
+ if (!this.definition.dialect) {
62
+ throw new Error("No DB Dialect defined");
63
+ }
64
+ const correctType = ColumnTypeMapping[this.type][this.definition.dialect];
65
+ let sql = correctType + (this.length ? `(${this.length})` : "");
66
+ if (this.definition.primaryKey) {
67
+ sql += " PRIMARY KEY";
68
+ }
69
+ if (this.definition.autoIncrement || this.type === AcceptedColumnTypes.SERIAL) {
70
+ const isPrimaryKey = !!this.definition.primaryKey;
71
+ if (this.definition.dialect === Dialect.POSTGRES) {
72
+ sql = `SERIAL${isPrimaryKey ? " PRIMARY KEY" : ""}`;
73
+ } else {
74
+ if (this.type !== AcceptedColumnTypes.SERIAL) {
75
+ sql += " AUTOINCREMENT";
76
+ } else {
77
+ const sqls = ["INTEGER", "PRIMARY KEY", "AUTOINCREMENT"];
78
+ if (!isPrimaryKey) sqls.splice(1, 1);
79
+ sql = sqls.join(" ");
80
+ }
81
+ }
82
+ }
83
+ if (this.definition.notNull) {
84
+ sql += " NOT NULL";
85
+ }
86
+ if (this.definition.unique) {
87
+ sql += " UNIQUE";
88
+ }
89
+ if (this.definition.default !== void 0) {
90
+ const value = this.definition.default;
91
+ const isString = typeof this.definition.default === "string";
92
+ const finalValue = isString ? `'${value}'` : value;
93
+ sql += ` DEFAULT ${finalValue}`;
94
+ }
95
+ return { query: sql, params: [] };
96
+ }
97
+ toString() {
98
+ return this.toQuery().query;
99
+ }
100
+ infer() {
101
+ return null;
102
+ }
103
+ }
104
+
105
+ export { Column };
@@ -1,2 +1 @@
1
- import "../chunk-G3LSCLIQ.js";
2
- import "../chunk-GLOHF5CP.js";
1
+ import './constants';
@@ -1,11 +1,11 @@
1
- import { D as Database } from '../index-Dcm5xIpR.js';
1
+ import { D as Database } from '../index-DFrpzXEn.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-DJhQVUY3.js';
4
+ import { T as Table, D as DatabaseDefinition } from '../index-FMT0YEO7.js';
5
5
  import { Dialect } from '../table/constants.js';
6
+ import '../query/constants.js';
6
7
  import '../column/types.js';
7
8
  import '../types.js';
8
- import '../query/constants.js';
9
9
 
10
10
  declare function alterColumnType<DbDialect extends Dialect, Tables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<DatabaseDefinition<DbDialect>>, TableName extends (keyof Tables & string) | (string & {}), ColName extends (keyof Tables[TableName]['columns'] & string) | (string & {}), Type extends Omit<AcceptedColumnTypes, typeof AcceptedColumnTypes.ENUM>, NewTables extends Omit<Tables, TableName> & {
11
11
  [K in TableName]: Table<TableName, Omit<Tables[TableName]['columns'], ColName> & {
@@ -1,15 +1,87 @@
1
- import {
2
- alterColumnType,
3
- dropColumnDefault,
4
- dropColumnNotNull,
5
- setColumnDefault,
6
- setColumnNotNull
7
- } from "../chunk-V4OMHVJN.js";
8
- import "../chunk-GLOHF5CP.js";
9
- export {
10
- alterColumnType,
11
- dropColumnDefault,
12
- dropColumnNotNull,
13
- setColumnDefault,
14
- setColumnNotNull
15
- };
1
+ import { Dialect } from '../table/constants';
2
+
3
+ async function alterColumnType(tableName, columnName, newType) {
4
+ if (this.dialect === Dialect.SQLITE) {
5
+ throw new Error("SQLite does not support ALTER COLUMN TYPE directly.");
6
+ }
7
+ await this.client.exec(
8
+ `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} TYPE ${newType}`
9
+ );
10
+ if (!this.tables[tableName]) return this;
11
+ this.tables[tableName].columns[columnName].type = newType;
12
+ return this;
13
+ }
14
+ async function setColumnDefault(tableName, columnName, value) {
15
+ if (this.dialect === Dialect.SQLITE) {
16
+ throw new Error("SQLite does not support ALTER COLUMN DEFAULT directly.");
17
+ }
18
+ await this.client.exec(
19
+ `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} SET DEFAULT ${value}`
20
+ );
21
+ if (!this.tables[tableName]) return this;
22
+ if (
23
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
+ !this.tables[tableName].columns[columnName].definition
25
+ ) {
26
+ this.tables[tableName].columns[columnName].definition = {};
27
+ }
28
+ this.tables[tableName].columns[columnName].definition.default = value;
29
+ return this;
30
+ }
31
+ async function dropColumnDefault(tableName, columnName) {
32
+ if (this.dialect === Dialect.SQLITE) {
33
+ throw new Error("SQLite does not support DROP DEFAULT directly.");
34
+ }
35
+ await this.client.exec(
36
+ `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} DROP DEFAULT`
37
+ );
38
+ if (!this.tables[tableName]) return this;
39
+ if (
40
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
+ !this.tables[tableName].columns[columnName].definition
42
+ ) {
43
+ this.tables[tableName].columns[columnName].definition = {};
44
+ }
45
+ delete this.tables[tableName].columns[columnName].definition.default;
46
+ return this;
47
+ }
48
+ async function setColumnNotNull(tableName, columnName) {
49
+ if (this.dialect === Dialect.SQLITE) {
50
+ throw new Error(
51
+ "SQLite does not support SET NOT NULL (requires table rebuild)"
52
+ );
53
+ }
54
+ await this.client.exec(
55
+ `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} SET NOT NULL`
56
+ );
57
+ if (!this.tables[tableName]) return this;
58
+ if (
59
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
+ !this.tables[tableName].columns[columnName].definition
61
+ ) {
62
+ this.tables[tableName].columns[columnName].definition = {};
63
+ }
64
+ this.tables[tableName].columns[columnName].definition.notNull = true;
65
+ return this;
66
+ }
67
+ async function dropColumnNotNull(tableName, columnName) {
68
+ if (this.dialect === Dialect.SQLITE) {
69
+ throw new Error(
70
+ "SQLite does not support DROP NOT NULL (requires table rebuild)"
71
+ );
72
+ }
73
+ await this.client.exec(
74
+ `ALTER TABLE ${tableName} ALTER COLUMN ${columnName} DROP NOT NULL`
75
+ );
76
+ if (!this.tables[tableName]) return this;
77
+ if (
78
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
79
+ !this.tables[tableName].columns[columnName].definition
80
+ ) {
81
+ this.tables[tableName].columns[columnName].definition = {};
82
+ }
83
+ this.tables[tableName].columns[columnName].definition.notNull = null;
84
+ return this;
85
+ }
86
+
87
+ export { alterColumnType, dropColumnDefault, dropColumnNotNull, setColumnDefault, setColumnNotNull };
@@ -1,11 +1,11 @@
1
- import { D as Database } from '../index-Dcm5xIpR.js';
1
+ import { D as Database } from '../index-DFrpzXEn.js';
2
2
  import { Column } from '../column/index.js';
3
- import { T as Table, D as DatabaseDefinition } from '../index-DJhQVUY3.js';
3
+ import { T as Table, D as DatabaseDefinition } from '../index-FMT0YEO7.js';
4
4
  import { Dialect } from '../table/constants.js';
5
+ import '../query/constants.js';
5
6
  import '../column/constants.js';
6
7
  import '../column/types.js';
7
8
  import '../types.js';
8
- import '../query/constants.js';
9
9
 
10
10
  declare function addColumn<DbDialect extends Dialect, Tables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<DatabaseDefinition<DbDialect>>, TableName extends (keyof Tables & string) | (string & {}), ColName extends (keyof Tables[TableName]['columns'] & string) | (string & {}), NewTables extends Omit<Tables, TableName> & {
11
11
  [K in TableName]: Table<TableName, Tables[TableName]['columns'] & {
@@ -1,11 +1,33 @@
1
- import {
2
- addColumn,
3
- dropColumn,
4
- renameColumn
5
- } from "../chunk-JF7OSNH4.js";
6
- import "../chunk-GLOHF5CP.js";
7
- export {
8
- addColumn,
9
- dropColumn,
10
- renameColumn
11
- };
1
+ import { Dialect } from '../table/constants';
2
+
3
+ async function addColumn(tableName, columnName, column) {
4
+ await this.client.exec(
5
+ `ALTER TABLE ${tableName} ADD COLUMN ${columnName} ${column.toString()};`
6
+ );
7
+ if (!this.tables[tableName]) return this;
8
+ this.tables[tableName].columns[columnName] = column;
9
+ return this;
10
+ }
11
+ async function renameColumn(tableName, oldName, newName) {
12
+ if (this.dialect === Dialect.SQLITE) {
13
+ throw new Error("SQLite does not support RENAME COLUMN natively.");
14
+ }
15
+ await this.client.exec(
16
+ `ALTER TABLE ${tableName} RENAME COLUMN ${oldName} TO ${newName};`
17
+ );
18
+ if (!this.tables[tableName]) return this;
19
+ this.tables[tableName].columns[newName] = this.tables[tableName].columns[oldName];
20
+ delete this.tables[tableName].columns[oldName];
21
+ return this;
22
+ }
23
+ async function dropColumn(tableName, columnName) {
24
+ if (this.dialect === Dialect.SQLITE) {
25
+ throw new Error("SQLite does not support DROP COLUMN natively.");
26
+ }
27
+ await this.client.exec(`ALTER TABLE ${tableName} DROP COLUMN ${columnName};`);
28
+ if (!this.tables[tableName]) return this;
29
+ delete this.tables[tableName].columns[columnName];
30
+ return this;
31
+ }
32
+
33
+ export { addColumn, dropColumn, renameColumn };
@@ -1,8 +1,8 @@
1
- export { C as ColumnAlterationContract, T as TableAlterationContract } from '../index-Dcm5xIpR.js';
1
+ export { C as ColumnAlterationContract, T as TableAlterationContract } from '../index-DFrpzXEn.js';
2
2
  import '../column/index.js';
3
3
  import '../column/constants.js';
4
- import '../index-DJhQVUY3.js';
4
+ import '../index-FMT0YEO7.js';
5
5
  import '../table/constants.js';
6
+ import '../query/constants.js';
6
7
  import '../column/types.js';
7
8
  import '../types.js';
8
- import '../query/constants.js';
@@ -0,0 +1 @@
1
+
@@ -1,8 +1,8 @@
1
1
  import '../column/index.js';
2
- import '../index-DJhQVUY3.js';
2
+ import '../index-FMT0YEO7.js';
3
+ import '../query/constants.js';
3
4
  import '../table/constants.js';
4
- export { D as Database } from '../index-Dcm5xIpR.js';
5
+ export { D as Database } from '../index-DFrpzXEn.js';
5
6
  import '../column/constants.js';
6
7
  import '../column/types.js';
7
8
  import '../types.js';
8
- import '../query/constants.js';
@@ -1,19 +1,92 @@
1
- import {
2
- Database
3
- } from "../chunk-4DQRB5XS.js";
4
- import "../chunk-HKTHKQLK.js";
5
- import "../chunk-JF7OSNH4.js";
6
- import "../chunk-D2ASIT4Q.js";
7
- import "../chunk-W2DR3ZVK.js";
8
- import "../chunk-TQ2GXAE7.js";
9
- import "../chunk-MG2S4V4N.js";
10
- import "../chunk-FYSNJAGD.js";
11
- import "../chunk-WVJGTZFI.js";
12
- import "../chunk-GY7R637S.js";
13
- import "../chunk-G3LSCLIQ.js";
14
- import "../chunk-Y7FSRHH3.js";
15
- import "../chunk-V4OMHVJN.js";
16
- import "../chunk-GLOHF5CP.js";
17
- export {
18
- Database
19
- };
1
+ import '../table';
2
+ import { Dialect } from '../table/constants';
3
+ import { alterColumnType, setColumnDefault, dropColumnDefault, dropColumnNotNull } from './alter';
4
+ import { addColumn, renameColumn, dropColumn } from './column';
5
+ import { createTable, renameTable, dropTable } from './table';
6
+ import { DatabasePsql, DatabaseSqlite } from './wrapper';
7
+
8
+ class Database {
9
+ hooks;
10
+ dialect;
11
+ defintion;
12
+ tables;
13
+ client;
14
+ createTable;
15
+ renameTable;
16
+ dropTable;
17
+ addColumn;
18
+ renameColumn;
19
+ dropColumn;
20
+ alterColumnType;
21
+ setColumnDefault;
22
+ dropColumnDefault;
23
+ setColumnNotNull;
24
+ dropColumnNotNull;
25
+ constructor(options) {
26
+ this.hooks = {};
27
+ this.dialect = options.dialect;
28
+ this.tables = options.tables ?? {};
29
+ this.defintion = {
30
+ dialect: options.dialect,
31
+ config: options.config
32
+ };
33
+ this.client = options.dialect === Dialect.POSTGRES ? new DatabasePsql(options.config) : new DatabaseSqlite(options.config);
34
+ if (options.tables) {
35
+ for (const tableName in options.tables) {
36
+ options.tables[tableName].client = this.client;
37
+ }
38
+ }
39
+ this.createTable = createTable.bind(this);
40
+ this.renameTable = renameTable.bind(this);
41
+ this.dropTable = dropTable.bind(this);
42
+ this.addColumn = addColumn.bind(this);
43
+ this.renameColumn = renameColumn.bind(this);
44
+ this.dropColumn = dropColumn.bind(this);
45
+ this.alterColumnType = alterColumnType.bind(
46
+ this
47
+ );
48
+ this.setColumnDefault = setColumnDefault.bind(
49
+ this
50
+ );
51
+ this.dropColumnDefault = dropColumnDefault.bind(
52
+ this
53
+ );
54
+ this.setColumnNotNull = setColumnDefault.bind(
55
+ this
56
+ );
57
+ this.dropColumnNotNull = dropColumnNotNull.bind(
58
+ this
59
+ );
60
+ }
61
+ table(tableName) {
62
+ if (!this.tables[tableName]) {
63
+ throw new Error(`Table ${tableName} does not exist`);
64
+ }
65
+ const table = this.tables[tableName];
66
+ const query = table.query();
67
+ query.hooks.before = this.hooks.before;
68
+ query.hooks.after = this.hooks.after;
69
+ return query;
70
+ }
71
+ addHook(type, fn) {
72
+ if (!this.hooks[type]) {
73
+ this.hooks[type] = /* @__PURE__ */ new Set();
74
+ }
75
+ this.hooks[type].add(fn);
76
+ return this;
77
+ }
78
+ removeHook(type, fn) {
79
+ if (this.hooks[type]) {
80
+ this.hooks[type].delete(fn);
81
+ }
82
+ return this;
83
+ }
84
+ async transaction(fn) {
85
+ return this.client.transaction(fn);
86
+ }
87
+ static define(options) {
88
+ return new Database(options);
89
+ }
90
+ }
91
+
92
+ export { Database };
@@ -1,11 +1,11 @@
1
- import { D as Database } from '../index-Dcm5xIpR.js';
1
+ import { D as Database } from '../index-DFrpzXEn.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-DJhQVUY3.js';
3
+ import { T as Table, D as DatabaseDefinition, a as TimestampOptions, M as MergeTimestampParanoid } from '../index-FMT0YEO7.js';
4
4
  import { Dialect } from '../table/constants.js';
5
+ import '../query/constants.js';
5
6
  import '../column/constants.js';
6
7
  import '../column/types.js';
7
8
  import '../types.js';
8
- import '../query/constants.js';
9
9
 
10
10
  declare function createTable<DbDialect extends Dialect, Tables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<DatabaseDefinition<DbDialect>>, TableName extends string, Columns extends Record<string, Column>, CreatedAt extends string, UpdatedAt extends string, Timestamp extends TimestampOptions<CreatedAt, UpdatedAt> | boolean, Paranoid extends string | boolean, FinalColumns extends MergeTimestampParanoid<Columns, CreatedAt, UpdatedAt, Timestamp, Paranoid>, NewTables extends Tables & {
11
11
  [K in TableName]: Table<TableName, FinalColumns, DbDialect, CreatedAt, UpdatedAt, Timestamp, Paranoid>;
@@ -1,19 +1,37 @@
1
- import {
2
- createTable,
3
- dropTable,
4
- renameTable
5
- } from "../chunk-D2ASIT4Q.js";
6
- import "../chunk-W2DR3ZVK.js";
7
- import "../chunk-TQ2GXAE7.js";
8
- import "../chunk-MG2S4V4N.js";
9
- import "../chunk-FYSNJAGD.js";
10
- import "../chunk-WVJGTZFI.js";
11
- import "../chunk-GY7R637S.js";
12
- import "../chunk-G3LSCLIQ.js";
13
- import "../chunk-Y7FSRHH3.js";
14
- import "../chunk-GLOHF5CP.js";
15
- export {
16
- createTable,
17
- dropTable,
18
- renameTable
19
- };
1
+ import { Table } from '../table';
2
+
3
+ async function createTable(tableName, columns, options) {
4
+ const table = Table.define({
5
+ name: tableName,
6
+ dialect: this.dialect,
7
+ columns,
8
+ ...options
9
+ });
10
+ table.client = this.client;
11
+ this.tables[tableName] = table;
12
+ if (!this?.client) {
13
+ throw new Error("Database not connected");
14
+ }
15
+ while (this.client.status === "connecting") {
16
+ await new Promise((resolve) => setTimeout(resolve, 100));
17
+ }
18
+ await table.create(this.client);
19
+ return this;
20
+ }
21
+ async function renameTable(oldName, newName) {
22
+ await this.client.exec(`ALTER TABLE ${oldName} RENAME TO ${newName};`);
23
+ this.tables[newName] = this.tables[oldName];
24
+ delete this.tables[oldName];
25
+ return this;
26
+ }
27
+ async function dropTable(tableName) {
28
+ if (!this.tables[tableName]) {
29
+ await this.client.exec(`DROP TABLE IF EXISTS ${tableName};`);
30
+ return this;
31
+ }
32
+ await this.tables[tableName].drop(this.client);
33
+ delete this.tables[tableName];
34
+ return this;
35
+ }
36
+
37
+ export { createTable, dropTable, renameTable };