@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.
- package/README.md +8 -8
- 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.d.ts +3 -3
- package/dist/database/alter.js +87 -15
- package/dist/database/column.d.ts +3 -3
- package/dist/database/column.js +33 -11
- package/dist/database/contract.d.ts +3 -3
- package/dist/database/contract.js +1 -0
- package/dist/database/index.d.ts +3 -3
- package/dist/database/index.js +92 -19
- package/dist/database/table.d.ts +3 -3
- package/dist/database/table.js +37 -19
- package/dist/database/types.d.ts +1 -1
- package/dist/database/types.js +1 -0
- package/dist/database/wrapper.d.ts +1 -1
- package/dist/database/wrapper.js +92 -9
- package/dist/{index-Dcm5xIpR.d.ts → index-DFrpzXEn.d.ts} +5 -1
- package/dist/{index-DJhQVUY3.d.ts → index-FMT0YEO7.d.ts} +17 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +5 -32
- package/dist/migration/index.d.ts +3 -3
- package/dist/migration/index.js +48 -6
- package/dist/migration/runner.js +8 -21
- package/dist/migration/type.d.ts +3 -3
- package/dist/migration/type.js +1 -0
- package/dist/query/builder.d.ts +1 -1
- package/dist/query/builder.js +66 -16
- package/dist/query/condition.d.ts +1 -1
- package/dist/query/condition.js +97 -24
- package/dist/query/constants.d.ts +6 -1
- package/dist/query/constants.js +54 -17
- package/dist/query/contract.d.ts +1 -1
- package/dist/query/contract.js +1 -0
- package/dist/query/helper.d.ts +1 -1
- package/dist/query/helper.js +30 -18
- package/dist/query/index.d.ts +1 -1
- package/dist/query/index.js +195 -10
- package/dist/query/join.d.ts +1 -1
- package/dist/query/join.js +16 -6
- package/dist/query/sql.d.ts +1 -1
- package/dist/query/sql.js +99 -16
- package/dist/query/types.d.ts +1 -1
- package/dist/query/types.js +1 -0
- package/dist/query/utilities.d.ts +1 -1
- package/dist/query/utilities.js +175 -24
- package/dist/table/constants.js +5 -5
- package/dist/table/index.d.ts +1 -1
- package/dist/table/index.js +52 -14
- package/dist/table/types.d.ts +1 -1
- package/dist/table/types.js +1 -0
- package/dist/table/utilities.d.ts +1 -1
- 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-4DQRB5XS.js +0 -94
- package/dist/chunk-CIWX3UCZ.js +0 -51
- package/dist/chunk-D2ASIT4Q.js +0 -44
- 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-MG2S4V4N.js +0 -60
- package/dist/chunk-TQ2GXAE7.js +0 -663
- package/dist/chunk-V4OMHVJN.js +0 -96
- package/dist/chunk-W2DR3ZVK.js +0 -59
- package/dist/chunk-WVJGTZFI.js +0 -60
- package/dist/chunk-Y7FSRHH3.js +0 -22
package/dist/query/utilities.js
CHANGED
|
@@ -1,24 +1,175 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
1
|
+
import '.';
|
|
2
|
+
import { Dialect } from '../table/constants';
|
|
3
|
+
import { quoteIdentifier } from '../utilities';
|
|
4
|
+
import { AcceptedOperator, QueryType } from './constants';
|
|
5
|
+
|
|
6
|
+
function getTableColumnNames(column, baseAlias, baseTable, joinedTables) {
|
|
7
|
+
const [tableAlias] = column.split(".");
|
|
8
|
+
const isOnBase = tableAlias === baseAlias;
|
|
9
|
+
const from = isOnBase ? baseAlias : tableAlias;
|
|
10
|
+
const columns = isOnBase ? Object.keys(baseTable.columns) : Object.keys(joinedTables?.[from]?.columns ?? {});
|
|
11
|
+
return {
|
|
12
|
+
from,
|
|
13
|
+
columns
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function getCondition(dialect, column, operator, value) {
|
|
17
|
+
switch (operator) {
|
|
18
|
+
case AcceptedOperator.EQ:
|
|
19
|
+
return `${column} = ?`;
|
|
20
|
+
case AcceptedOperator.NE:
|
|
21
|
+
return `${column} != ?`;
|
|
22
|
+
case AcceptedOperator.GT:
|
|
23
|
+
return `${column} > ?`;
|
|
24
|
+
case AcceptedOperator.LT:
|
|
25
|
+
return `${column} < ?`;
|
|
26
|
+
case AcceptedOperator.GTE:
|
|
27
|
+
return `${column} >= ?`;
|
|
28
|
+
case AcceptedOperator.LTE:
|
|
29
|
+
return `${column} <= ?`;
|
|
30
|
+
case AcceptedOperator.IN:
|
|
31
|
+
return `${column} IN (${value.map(() => "?").join(", ")})`;
|
|
32
|
+
case AcceptedOperator.NOT_IN:
|
|
33
|
+
return `${column} NOT IN (${value.map(() => "?").join(", ")})`;
|
|
34
|
+
case AcceptedOperator.LIKE:
|
|
35
|
+
return `${column} LIKE ?`;
|
|
36
|
+
case AcceptedOperator.NOT_LIKE:
|
|
37
|
+
return `${column} NOT LIKE ?`;
|
|
38
|
+
case AcceptedOperator.ILIKE:
|
|
39
|
+
if (dialect === Dialect.POSTGRES) {
|
|
40
|
+
return `${column} ILIKE ?`;
|
|
41
|
+
}
|
|
42
|
+
return `LOWER(${column}) LIKE LOWER(?)`;
|
|
43
|
+
case AcceptedOperator.NOT_ILIKE:
|
|
44
|
+
if (dialect === Dialect.POSTGRES) {
|
|
45
|
+
return `${column} NOT ILIKE ?`;
|
|
46
|
+
}
|
|
47
|
+
return `LOWER(${column}) NOT LIKE LOWER(?)`;
|
|
48
|
+
case AcceptedOperator.IS_NULL:
|
|
49
|
+
return `${column} IS NULL`;
|
|
50
|
+
case AcceptedOperator.IS_NOT_NULL:
|
|
51
|
+
return `${column} IS NOT NULL`;
|
|
52
|
+
case AcceptedOperator.BETWEEN:
|
|
53
|
+
return `${column} BETWEEN ? AND ?`;
|
|
54
|
+
case AcceptedOperator.NOT_BETWEEN:
|
|
55
|
+
return `${column} NOT BETWEEN ? AND ?`;
|
|
56
|
+
default:
|
|
57
|
+
throw new Error("Invalid operator");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function getTimestamp(table) {
|
|
61
|
+
const isWithTimestamp = !!table.timestamp;
|
|
62
|
+
const timestamp = /* @__PURE__ */ new Date();
|
|
63
|
+
let createdAt = "createdAt";
|
|
64
|
+
let updatedAt = "updatedAt";
|
|
65
|
+
if (isWithTimestamp) {
|
|
66
|
+
const isCustomTimestamp = typeof table.timestamp === "object";
|
|
67
|
+
if (isCustomTimestamp && table.timestamp.createdAt) {
|
|
68
|
+
createdAt = table.timestamp.createdAt;
|
|
69
|
+
}
|
|
70
|
+
if (isCustomTimestamp && table.timestamp.updatedAt) {
|
|
71
|
+
updatedAt = table.timestamp.updatedAt;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
isWithTimestamp,
|
|
76
|
+
timestamp,
|
|
77
|
+
createdAt,
|
|
78
|
+
updatedAt
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function getParanoid(table) {
|
|
82
|
+
const isWithParanoid = !!table.paranoid;
|
|
83
|
+
const timestamp = /* @__PURE__ */ new Date();
|
|
84
|
+
let deletedAt = "deletedAt";
|
|
85
|
+
if (isWithParanoid) {
|
|
86
|
+
if (typeof table.paranoid === "string") {
|
|
87
|
+
deletedAt = table.paranoid;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
isWithParanoid,
|
|
92
|
+
timestamp,
|
|
93
|
+
deletedAt
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function getWhereConditions(q) {
|
|
97
|
+
if (q.definition.queryType === QueryType.INSERT) return [];
|
|
98
|
+
const conditions = [];
|
|
99
|
+
const base = q.definition.baseAlias ?? q.table.name;
|
|
100
|
+
const { isWithParanoid, deletedAt } = getParanoid(q.table);
|
|
101
|
+
const withDeleted = !!q.definition.withDeleted;
|
|
102
|
+
const isHasConditions = !!q.definition.where?.length;
|
|
103
|
+
if (!withDeleted && isWithParanoid) {
|
|
104
|
+
const suffix = isHasConditions ? " AND" : "";
|
|
105
|
+
const column = `${base}.${quoteIdentifier(deletedAt)}`;
|
|
106
|
+
conditions.unshift(`${column} IS NULL${suffix}`);
|
|
107
|
+
}
|
|
108
|
+
if (q.definition.where?.length) {
|
|
109
|
+
conditions.push(...q.definition.where);
|
|
110
|
+
}
|
|
111
|
+
return conditions;
|
|
112
|
+
}
|
|
113
|
+
function getGroupByConditions(q) {
|
|
114
|
+
if (q.definition.queryType !== QueryType.SELECT) return [];
|
|
115
|
+
if (q.definition.groupBy?.length) return q.definition.groupBy;
|
|
116
|
+
if (q.definition.aggregates?.length) {
|
|
117
|
+
if (q.definition.select?.length)
|
|
118
|
+
return q.definition.select.map((col) => {
|
|
119
|
+
if (typeof col === "string" && col.endsWith("*")) {
|
|
120
|
+
const { from: from2, columns } = getTableColumnNames(
|
|
121
|
+
col,
|
|
122
|
+
q.definition.baseAlias ?? q.table.name,
|
|
123
|
+
q.table,
|
|
124
|
+
q.definition.joinedTables ?? {}
|
|
125
|
+
);
|
|
126
|
+
return columns.map((column) => `${from2}.${quoteIdentifier(column)}`).join(" ");
|
|
127
|
+
}
|
|
128
|
+
return col;
|
|
129
|
+
});
|
|
130
|
+
const from = q.definition.baseAlias ?? q.table.name;
|
|
131
|
+
return Object.keys(q.table.columns).map(
|
|
132
|
+
(col) => `${from}.${quoteIdentifier(col)}`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
function getTableSelectName(q) {
|
|
138
|
+
if (!q.definition.baseAlias) return q.table.name;
|
|
139
|
+
return `${q.table.name} AS ${q.definition.baseAlias}`;
|
|
140
|
+
}
|
|
141
|
+
function parseAliasedRow({
|
|
142
|
+
row,
|
|
143
|
+
selects,
|
|
144
|
+
root = null
|
|
145
|
+
}) {
|
|
146
|
+
let result = {};
|
|
147
|
+
for (const key in row) {
|
|
148
|
+
const [table, column] = key.split(".");
|
|
149
|
+
if (!column) {
|
|
150
|
+
const alias = selects.find(
|
|
151
|
+
(s) => typeof s === "object" && s.as === table
|
|
152
|
+
);
|
|
153
|
+
if (alias) {
|
|
154
|
+
const [oriTab] = alias.column.split(".");
|
|
155
|
+
if (!result[oriTab]) result[oriTab] = {};
|
|
156
|
+
result[oriTab][table] = row[key];
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
result[key] = row[key];
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (!result[table]) result[table] = {};
|
|
163
|
+
result[table][column] = row[key];
|
|
164
|
+
}
|
|
165
|
+
if (root) {
|
|
166
|
+
result = {
|
|
167
|
+
...result,
|
|
168
|
+
...result[root]
|
|
169
|
+
};
|
|
170
|
+
delete result[root];
|
|
171
|
+
}
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export { getCondition, getGroupByConditions, getParanoid, getTableColumnNames, getTableSelectName, getTimestamp, getWhereConditions, parseAliasedRow };
|
package/dist/table/constants.js
CHANGED
package/dist/table/index.d.ts
CHANGED
package/dist/table/index.js
CHANGED
|
@@ -1,14 +1,52 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { QueryBuilder } from '../query';
|
|
2
|
+
import { defineColumns } from './utilities';
|
|
3
|
+
|
|
4
|
+
class Table {
|
|
5
|
+
client;
|
|
6
|
+
dialect;
|
|
7
|
+
name;
|
|
8
|
+
columns;
|
|
9
|
+
timestamp;
|
|
10
|
+
paranoid;
|
|
11
|
+
_output;
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.dialect = options.dialect;
|
|
14
|
+
this.name = options.name;
|
|
15
|
+
this.columns = options.columns;
|
|
16
|
+
this.paranoid = options.paranoid || null;
|
|
17
|
+
this.timestamp = options.timestamp || null;
|
|
18
|
+
this.client = null;
|
|
19
|
+
for (const column of Object.values(this.columns)) {
|
|
20
|
+
column.dialect(options.dialect);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
infer() {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
static define(options) {
|
|
27
|
+
const columns = defineColumns(options);
|
|
28
|
+
return new Table({
|
|
29
|
+
...options,
|
|
30
|
+
columns
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async create(db = this.client) {
|
|
34
|
+
if (!db) throw new Error("Database client not defined");
|
|
35
|
+
const sql = `CREATE TABLE IF NOT EXISTS ${this.name} (${Object.entries(
|
|
36
|
+
this.columns
|
|
37
|
+
).map(([name, column]) => `${name} ${column.toQuery().query}`).join(", ")});`;
|
|
38
|
+
await db.exec(sql);
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
async drop(db = this.client) {
|
|
42
|
+
if (!db) throw new Error("Database client not defined");
|
|
43
|
+
const sql = `DROP TABLE IF EXISTS ${this.name};`;
|
|
44
|
+
await db.exec(sql);
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
query() {
|
|
48
|
+
return new QueryBuilder(this);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { Table };
|
package/dist/table/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { M as MergeTimestampParanoid,
|
|
1
|
+
export { M as MergeTimestampParanoid, s as TableOptions, t as TableOutput, a as TimestampOptions } from '../index-FMT0YEO7.js';
|
|
2
2
|
import '../column/index.js';
|
|
3
3
|
import './constants.js';
|
|
4
4
|
import '../column/constants.js';
|
package/dist/table/types.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import '../column/constants.js';
|
|
2
2
|
import '../column/index.js';
|
|
3
3
|
import './constants.js';
|
|
4
|
-
export {
|
|
4
|
+
export { p as createdAt, r as defineColumns, q as deletedAt, u as updatedAt } from '../index-FMT0YEO7.js';
|
|
5
5
|
import '../column/types.js';
|
|
6
6
|
import '../types.js';
|
|
7
7
|
import '../query/constants.js';
|
package/dist/table/utilities.js
CHANGED
|
@@ -1,15 +1,50 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
};
|
|
1
|
+
import { Column } from '../column';
|
|
2
|
+
|
|
3
|
+
const createdAt = Column.define({
|
|
4
|
+
type: "DATETIME"
|
|
5
|
+
}).default("CURRENT_TIMESTAMP");
|
|
6
|
+
const updatedAt = Column.define({
|
|
7
|
+
type: "DATETIME"
|
|
8
|
+
});
|
|
9
|
+
const deletedAt = Column.define({
|
|
10
|
+
type: "DATETIME"
|
|
11
|
+
});
|
|
12
|
+
function defineColumns(options) {
|
|
13
|
+
const columns = {
|
|
14
|
+
...options.columns
|
|
15
|
+
};
|
|
16
|
+
const tracker = {
|
|
17
|
+
deletedAt: "deletedAt"
|
|
18
|
+
};
|
|
19
|
+
if (options.timestamp) {
|
|
20
|
+
const timestamp = {
|
|
21
|
+
createdAt: "createdAt",
|
|
22
|
+
updatedAt: "updatedAt"
|
|
23
|
+
};
|
|
24
|
+
if (typeof options.timestamp === "object") {
|
|
25
|
+
if (typeof options.timestamp.createdAt === "string") {
|
|
26
|
+
timestamp.createdAt = options.timestamp.createdAt;
|
|
27
|
+
}
|
|
28
|
+
if (typeof options.timestamp.updatedAt === "string") {
|
|
29
|
+
timestamp.updatedAt = options.timestamp.updatedAt;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (!columns[timestamp.createdAt]) {
|
|
33
|
+
columns[timestamp.createdAt] = createdAt;
|
|
34
|
+
}
|
|
35
|
+
if (!columns[timestamp.updatedAt]) {
|
|
36
|
+
columns[timestamp.updatedAt] = updatedAt;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (options.paranoid) {
|
|
40
|
+
if (typeof options.paranoid !== "boolean") {
|
|
41
|
+
tracker.deletedAt = options.paranoid;
|
|
42
|
+
}
|
|
43
|
+
if (!columns[tracker.deletedAt]) {
|
|
44
|
+
columns[tracker.deletedAt] = deletedAt;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return columns;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { createdAt, defineColumns, deletedAt, updatedAt };
|
package/dist/types.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/dist/utilities.js
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
function deepClone(obj) {
|
|
2
|
+
if (Array.isArray(obj)) {
|
|
3
|
+
return obj.map((item) => deepClone(item));
|
|
4
|
+
}
|
|
5
|
+
if (obj && typeof obj === "object") {
|
|
6
|
+
const clonedObj = {};
|
|
7
|
+
for (const key in obj) {
|
|
8
|
+
clonedObj[key] = deepClone(obj[key]);
|
|
9
|
+
}
|
|
10
|
+
return clonedObj;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
function quoteIdentifier(identifier) {
|
|
15
|
+
return `"${identifier.replace(/"/g, '""')}"`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { deepClone, quoteIdentifier };
|
package/package.json
CHANGED
|
@@ -2,8 +2,43 @@
|
|
|
2
2
|
"name": "@ignisia/sql",
|
|
3
3
|
"module": "dist/index.js",
|
|
4
4
|
"types": "./dist/index.d.ts",
|
|
5
|
-
"version": "0.1
|
|
5
|
+
"version": "0.2.1",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts"
|
|
11
|
+
},
|
|
12
|
+
"./*": {
|
|
13
|
+
"import": "./dist/*.js",
|
|
14
|
+
"types": "./dist/*.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"./**/*": {
|
|
17
|
+
"import": "./dist/**/*.js",
|
|
18
|
+
"types": "./dist/**/*.d.ts"
|
|
19
|
+
},
|
|
20
|
+
"./package.json": "./package.json",
|
|
21
|
+
"./column": {
|
|
22
|
+
"import": "./dist/column/index.js",
|
|
23
|
+
"types": "./dist/column/index.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./database": {
|
|
26
|
+
"import": "./dist/database/index.js",
|
|
27
|
+
"types": "./dist/database/index.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./migration": {
|
|
30
|
+
"import": "./dist/migration/index.js",
|
|
31
|
+
"types": "./dist/migration/index.d.ts"
|
|
32
|
+
},
|
|
33
|
+
"./query": {
|
|
34
|
+
"import": "./dist/query/index.js",
|
|
35
|
+
"types": "./dist/query/index.d.ts"
|
|
36
|
+
},
|
|
37
|
+
"./table": {
|
|
38
|
+
"import": "./dist/table/index.js",
|
|
39
|
+
"types": "./dist/table/index.d.ts"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
7
42
|
"scripts": {
|
|
8
43
|
"build": "bun run build.ts"
|
|
9
44
|
},
|
|
@@ -17,4 +52,4 @@
|
|
|
17
52
|
"files": [
|
|
18
53
|
"dist"
|
|
19
54
|
]
|
|
20
|
-
}
|
|
55
|
+
}
|
package/dist/chunk-4DQRB5XS.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DatabasePsql,
|
|
3
|
-
DatabaseSqlite
|
|
4
|
-
} from "./chunk-HKTHKQLK.js";
|
|
5
|
-
import {
|
|
6
|
-
addColumn,
|
|
7
|
-
dropColumn,
|
|
8
|
-
renameColumn
|
|
9
|
-
} from "./chunk-JF7OSNH4.js";
|
|
10
|
-
import {
|
|
11
|
-
createTable,
|
|
12
|
-
dropTable,
|
|
13
|
-
renameTable
|
|
14
|
-
} from "./chunk-D2ASIT4Q.js";
|
|
15
|
-
import {
|
|
16
|
-
alterColumnType,
|
|
17
|
-
dropColumnDefault,
|
|
18
|
-
dropColumnNotNull,
|
|
19
|
-
setColumnDefault
|
|
20
|
-
} from "./chunk-V4OMHVJN.js";
|
|
21
|
-
import {
|
|
22
|
-
Dialect
|
|
23
|
-
} from "./chunk-GLOHF5CP.js";
|
|
24
|
-
|
|
25
|
-
// src/database/index.ts
|
|
26
|
-
var Database = class _Database {
|
|
27
|
-
dialect;
|
|
28
|
-
defintion;
|
|
29
|
-
tables;
|
|
30
|
-
client;
|
|
31
|
-
createTable;
|
|
32
|
-
renameTable;
|
|
33
|
-
dropTable;
|
|
34
|
-
addColumn;
|
|
35
|
-
renameColumn;
|
|
36
|
-
dropColumn;
|
|
37
|
-
alterColumnType;
|
|
38
|
-
setColumnDefault;
|
|
39
|
-
dropColumnDefault;
|
|
40
|
-
setColumnNotNull;
|
|
41
|
-
dropColumnNotNull;
|
|
42
|
-
constructor(options) {
|
|
43
|
-
this.dialect = options.dialect;
|
|
44
|
-
this.tables = options.tables ?? {};
|
|
45
|
-
this.defintion = {
|
|
46
|
-
dialect: options.dialect,
|
|
47
|
-
config: options.config
|
|
48
|
-
};
|
|
49
|
-
this.client = options.dialect === Dialect.POSTGRES ? new DatabasePsql(options.config) : new DatabaseSqlite(options.config);
|
|
50
|
-
if (options.tables) {
|
|
51
|
-
for (const tableName in options.tables) {
|
|
52
|
-
options.tables[tableName].database = this.client;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
this.createTable = createTable.bind(this);
|
|
56
|
-
this.renameTable = renameTable.bind(this);
|
|
57
|
-
this.dropTable = dropTable.bind(this);
|
|
58
|
-
this.addColumn = addColumn.bind(this);
|
|
59
|
-
this.renameColumn = renameColumn.bind(this);
|
|
60
|
-
this.dropColumn = dropColumn.bind(this);
|
|
61
|
-
this.alterColumnType = alterColumnType.bind(
|
|
62
|
-
this
|
|
63
|
-
);
|
|
64
|
-
this.setColumnDefault = setColumnDefault.bind(
|
|
65
|
-
this
|
|
66
|
-
);
|
|
67
|
-
this.dropColumnDefault = dropColumnDefault.bind(
|
|
68
|
-
this
|
|
69
|
-
);
|
|
70
|
-
this.setColumnNotNull = setColumnDefault.bind(
|
|
71
|
-
this
|
|
72
|
-
);
|
|
73
|
-
this.dropColumnNotNull = dropColumnNotNull.bind(
|
|
74
|
-
this
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
table(tableName) {
|
|
78
|
-
if (!this.tables[tableName]) {
|
|
79
|
-
throw new Error(`Table ${tableName} does not exist`);
|
|
80
|
-
}
|
|
81
|
-
const table = this.tables[tableName];
|
|
82
|
-
return table.query();
|
|
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 {
|
|
93
|
-
Database
|
|
94
|
-
};
|
package/dist/chunk-CIWX3UCZ.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
// src/migration/index.ts
|
|
2
|
-
var Migration = class _Migration {
|
|
3
|
-
db;
|
|
4
|
-
_up;
|
|
5
|
-
_down;
|
|
6
|
-
constructor(options) {
|
|
7
|
-
this.db = options.db;
|
|
8
|
-
this._up = options.up;
|
|
9
|
-
this._down = options.down;
|
|
10
|
-
}
|
|
11
|
-
get up() {
|
|
12
|
-
return this._up;
|
|
13
|
-
}
|
|
14
|
-
get down() {
|
|
15
|
-
return this._down;
|
|
16
|
-
}
|
|
17
|
-
static setUp(migrator, fn) {
|
|
18
|
-
migrator._up = () => fn(migrator.db);
|
|
19
|
-
return migrator;
|
|
20
|
-
}
|
|
21
|
-
static setDown(migrator, fn) {
|
|
22
|
-
migrator._down = () => fn(migrator.db);
|
|
23
|
-
return migrator;
|
|
24
|
-
}
|
|
25
|
-
static define(db, options) {
|
|
26
|
-
const migration = new _Migration({
|
|
27
|
-
db,
|
|
28
|
-
up: options?.up ? () => options.up(db) : null,
|
|
29
|
-
down: options?.down ? () => options.down(db) : null
|
|
30
|
-
});
|
|
31
|
-
return {
|
|
32
|
-
migration,
|
|
33
|
-
setUp(fn) {
|
|
34
|
-
return _Migration.setUp(
|
|
35
|
-
migration,
|
|
36
|
-
fn
|
|
37
|
-
);
|
|
38
|
-
},
|
|
39
|
-
setDown(fn) {
|
|
40
|
-
return _Migration.setDown(
|
|
41
|
-
migration,
|
|
42
|
-
fn
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export {
|
|
50
|
-
Migration
|
|
51
|
-
};
|
package/dist/chunk-D2ASIT4Q.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Table
|
|
3
|
-
} from "./chunk-W2DR3ZVK.js";
|
|
4
|
-
|
|
5
|
-
// src/database/table.ts
|
|
6
|
-
async function createTable(tableName, columns, options) {
|
|
7
|
-
const table = Table.define({
|
|
8
|
-
name: tableName,
|
|
9
|
-
dialect: this.dialect,
|
|
10
|
-
columns,
|
|
11
|
-
...options
|
|
12
|
-
});
|
|
13
|
-
table.database = this.client;
|
|
14
|
-
this.tables[tableName] = table;
|
|
15
|
-
if (!this?.client) {
|
|
16
|
-
throw new Error("Database not connected");
|
|
17
|
-
}
|
|
18
|
-
while (this.client.status === "connecting") {
|
|
19
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
20
|
-
}
|
|
21
|
-
await table.create(this.client);
|
|
22
|
-
return this;
|
|
23
|
-
}
|
|
24
|
-
async function renameTable(oldName, newName) {
|
|
25
|
-
await this.client.exec(`ALTER TABLE ${oldName} RENAME TO ${newName};`);
|
|
26
|
-
this.tables[newName] = this.tables[oldName];
|
|
27
|
-
delete this.tables[oldName];
|
|
28
|
-
return this;
|
|
29
|
-
}
|
|
30
|
-
async function dropTable(tableName) {
|
|
31
|
-
if (!this.tables[tableName]) {
|
|
32
|
-
await this.client.exec(`DROP TABLE IF EXISTS ${tableName};`);
|
|
33
|
-
return this;
|
|
34
|
-
}
|
|
35
|
-
await this.tables[tableName].drop(this.client);
|
|
36
|
-
delete this.tables[tableName];
|
|
37
|
-
return this;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export {
|
|
41
|
-
createTable,
|
|
42
|
-
renameTable,
|
|
43
|
-
dropTable
|
|
44
|
-
};
|
package/dist/chunk-FYSNJAGD.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
// src/query/join.ts
|
|
2
|
-
function addJoin(query, joinType, alias, joinTable, baseColumn, joinColumn) {
|
|
3
|
-
if (!query.definition.joins) query.definition.joins = [];
|
|
4
|
-
query.definition.joins.push(
|
|
5
|
-
`${joinType} JOIN ${joinTable.name} AS ${alias} ON ${baseColumn} = ${joinColumn}`
|
|
6
|
-
);
|
|
7
|
-
if (!query.definition.joinedTables) {
|
|
8
|
-
query.definition.joinedTables = {};
|
|
9
|
-
}
|
|
10
|
-
query.definition.joinedTables = {
|
|
11
|
-
...query.definition.joinedTables,
|
|
12
|
-
[alias]: joinTable
|
|
13
|
-
};
|
|
14
|
-
return query;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export {
|
|
18
|
-
addJoin
|
|
19
|
-
};
|