@ignisia/sql 0.2.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.
- package/dist/column/constants.js +97 -8
- package/dist/column/index.js +105 -8
- package/dist/column/types.js +1 -2
- package/dist/database/alter.js +87 -15
- package/dist/database/column.js +33 -11
- package/dist/database/contract.js +1 -0
- package/dist/database/index.js +92 -19
- package/dist/database/table.js +37 -19
- package/dist/database/types.js +1 -0
- package/dist/database/wrapper.js +92 -9
- package/dist/index.js +5 -32
- package/dist/migration/index.js +48 -6
- package/dist/migration/runner.js +7 -20
- package/dist/migration/type.js +1 -0
- package/dist/query/builder.js +66 -16
- package/dist/query/condition.js +97 -24
- package/dist/query/constants.js +54 -19
- package/dist/query/contract.js +1 -0
- package/dist/query/helper.js +30 -18
- package/dist/query/index.js +195 -10
- package/dist/query/join.js +16 -6
- package/dist/query/sql.js +99 -16
- package/dist/query/types.js +1 -0
- package/dist/query/utilities.js +175 -24
- package/dist/table/constants.js +5 -5
- package/dist/table/index.js +52 -14
- package/dist/table/types.js +1 -0
- package/dist/table/utilities.js +50 -15
- package/dist/types.js +1 -0
- package/dist/utilities.js +18 -8
- package/package.json +37 -2
- package/dist/chunk-62FKD35V.js +0 -65
- package/dist/chunk-CIWX3UCZ.js +0 -51
- package/dist/chunk-EIUC7HJS.js +0 -686
- package/dist/chunk-FYSNJAGD.js +0 -19
- package/dist/chunk-G3LSCLIQ.js +0 -104
- package/dist/chunk-GLOHF5CP.js +0 -9
- package/dist/chunk-GY7R637S.js +0 -113
- package/dist/chunk-HKTHKQLK.js +0 -98
- package/dist/chunk-JF7OSNH4.js +0 -40
- package/dist/chunk-KVCIOW7L.js +0 -59
- package/dist/chunk-OYM2PNYZ.js +0 -44
- package/dist/chunk-UI7U54OT.js +0 -112
- package/dist/chunk-V4OMHVJN.js +0 -96
- package/dist/chunk-WVJGTZFI.js +0 -60
- package/dist/chunk-Y7FSRHH3.js +0 -22
package/dist/column/constants.js
CHANGED
|
@@ -1,9 +1,98 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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 };
|
package/dist/column/index.js
CHANGED
|
@@ -1,8 +1,105 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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 };
|
package/dist/column/types.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import "../chunk-GLOHF5CP.js";
|
|
1
|
+
import './constants';
|
package/dist/database/alter.js
CHANGED
|
@@ -1,15 +1,87 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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 };
|
package/dist/database/column.js
CHANGED
|
@@ -1,11 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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 };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/dist/database/index.js
CHANGED
|
@@ -1,19 +1,92 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
} from
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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 };
|
package/dist/database/table.js
CHANGED
|
@@ -1,19 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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 };
|
package/dist/database/types.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/dist/database/wrapper.js
CHANGED
|
@@ -1,9 +1,92 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { SQL } from 'bun';
|
|
2
|
+
import { Database } from 'bun:sqlite';
|
|
3
|
+
import { Dialect } from '../table/constants';
|
|
4
|
+
|
|
5
|
+
class DatabasePsql {
|
|
6
|
+
dialect;
|
|
7
|
+
options;
|
|
8
|
+
client;
|
|
9
|
+
status;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.dialect = Dialect.POSTGRES;
|
|
12
|
+
this.options = options;
|
|
13
|
+
this.status = "connecting";
|
|
14
|
+
this.client = new SQL({
|
|
15
|
+
...options,
|
|
16
|
+
onconnect: () => {
|
|
17
|
+
this.status = "connected";
|
|
18
|
+
},
|
|
19
|
+
onclose: () => {
|
|
20
|
+
this.status = "disconnected";
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
this.connect();
|
|
24
|
+
}
|
|
25
|
+
async connect() {
|
|
26
|
+
await this.client.connect();
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
async disconnect() {
|
|
30
|
+
await this.client.close();
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
34
|
+
async exec(sql, values) {
|
|
35
|
+
if (!values) {
|
|
36
|
+
return this.client.unsafe(sql);
|
|
37
|
+
}
|
|
38
|
+
return this.client.unsafe(sql, values);
|
|
39
|
+
}
|
|
40
|
+
async transaction(fn) {
|
|
41
|
+
try {
|
|
42
|
+
await this.exec("BEGIN");
|
|
43
|
+
const result = await fn();
|
|
44
|
+
await this.exec("COMMIT");
|
|
45
|
+
return result;
|
|
46
|
+
} catch (err) {
|
|
47
|
+
await this.exec("ROLLBACK");
|
|
48
|
+
throw err;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
class DatabaseSqlite {
|
|
53
|
+
dialect;
|
|
54
|
+
options;
|
|
55
|
+
client;
|
|
56
|
+
status;
|
|
57
|
+
constructor(options) {
|
|
58
|
+
this.dialect = Dialect.SQLITE;
|
|
59
|
+
this.options = options;
|
|
60
|
+
this.status = "connecting";
|
|
61
|
+
this.client = new Database(options.filename);
|
|
62
|
+
this.status = "connected";
|
|
63
|
+
}
|
|
64
|
+
async connect() {
|
|
65
|
+
this.client = new Database(this.options.filename);
|
|
66
|
+
this.status = "connected";
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
async disconnect() {
|
|
70
|
+
this.client.close();
|
|
71
|
+
this.status = "disconnected";
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
75
|
+
async exec(sql, params) {
|
|
76
|
+
const query = this.client.prepare(sql, params);
|
|
77
|
+
return query.all();
|
|
78
|
+
}
|
|
79
|
+
async transaction(fn) {
|
|
80
|
+
try {
|
|
81
|
+
await this.exec("BEGIN");
|
|
82
|
+
const result = await fn();
|
|
83
|
+
await this.exec("COMMIT");
|
|
84
|
+
return result;
|
|
85
|
+
} catch (err) {
|
|
86
|
+
await this.exec("ROLLBACK");
|
|
87
|
+
throw err;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export { DatabasePsql, DatabaseSqlite };
|
package/dist/index.js
CHANGED
|
@@ -1,32 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
} from
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from "./chunk-UI7U54OT.js";
|
|
7
|
-
import "./chunk-HKTHKQLK.js";
|
|
8
|
-
import "./chunk-JF7OSNH4.js";
|
|
9
|
-
import "./chunk-OYM2PNYZ.js";
|
|
10
|
-
import {
|
|
11
|
-
Table
|
|
12
|
-
} from "./chunk-KVCIOW7L.js";
|
|
13
|
-
import {
|
|
14
|
-
QueryBuilder
|
|
15
|
-
} from "./chunk-EIUC7HJS.js";
|
|
16
|
-
import "./chunk-FYSNJAGD.js";
|
|
17
|
-
import "./chunk-62FKD35V.js";
|
|
18
|
-
import "./chunk-V4OMHVJN.js";
|
|
19
|
-
import "./chunk-Y7FSRHH3.js";
|
|
20
|
-
import "./chunk-WVJGTZFI.js";
|
|
21
|
-
import {
|
|
22
|
-
Column
|
|
23
|
-
} from "./chunk-GY7R637S.js";
|
|
24
|
-
import "./chunk-G3LSCLIQ.js";
|
|
25
|
-
import "./chunk-GLOHF5CP.js";
|
|
26
|
-
export {
|
|
27
|
-
Column,
|
|
28
|
-
Database,
|
|
29
|
-
Migration,
|
|
30
|
-
QueryBuilder,
|
|
31
|
-
Table
|
|
32
|
-
};
|
|
1
|
+
export { Column } from './column';
|
|
2
|
+
export { Database } from './database';
|
|
3
|
+
export { Migration } from './migration';
|
|
4
|
+
export { QueryBuilder } from './query';
|
|
5
|
+
export { Table } from './table';
|