@ignisia/sql 0.2.1 → 0.2.2
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/cjs/column/constants.js +101 -0
- package/dist/cjs/column/index.d.cts +42 -0
- package/dist/cjs/column/index.js +107 -0
- package/dist/cjs/column/types.d.cts +43 -0
- package/dist/cjs/column/types.js +4 -0
- package/dist/cjs/database/alter.d.cts +52 -0
- package/dist/cjs/database/alter.js +93 -0
- package/dist/cjs/database/column.d.cts +24 -0
- package/dist/cjs/database/column.js +37 -0
- package/dist/cjs/database/contract.d.cts +8 -0
- package/dist/cjs/database/contract.js +2 -0
- package/dist/cjs/database/index.d.cts +8 -0
- package/dist/cjs/database/index.js +94 -0
- package/dist/cjs/database/table.d.cts +21 -0
- package/dist/cjs/database/table.js +41 -0
- package/dist/cjs/database/types.d.cts +7 -0
- package/dist/cjs/database/types.js +2 -0
- package/dist/cjs/database/wrapper.d.cts +36 -0
- package/dist/cjs/database/wrapper.js +95 -0
- package/dist/cjs/index---zaMa69.d.cts +103 -0
- package/dist/cjs/index-CwiFQh0I.d.cts +358 -0
- package/dist/cjs/index.d.cts +10 -0
- package/dist/cjs/index.js +30 -0
- package/dist/cjs/migration/index.d.cts +30 -0
- package/dist/{migration → cjs/migration}/index.js +3 -1
- package/dist/cjs/migration/runner.js +58 -0
- package/dist/cjs/migration/type.d.cts +19 -0
- package/dist/cjs/migration/type.js +2 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/query/builder.d.cts +14 -0
- package/dist/cjs/query/builder.js +71 -0
- package/dist/cjs/query/condition.d.cts +7 -0
- package/dist/cjs/query/condition.js +106 -0
- package/dist/cjs/query/constants.js +64 -0
- package/dist/cjs/query/contract.d.cts +7 -0
- package/dist/cjs/query/contract.js +2 -0
- package/dist/cjs/query/helper.d.cts +7 -0
- package/dist/cjs/query/helper.js +36 -0
- package/dist/cjs/query/index.d.cts +7 -0
- package/dist/cjs/query/index.js +197 -0
- package/dist/cjs/query/join.d.cts +16 -0
- package/dist/cjs/query/join.js +18 -0
- package/dist/cjs/query/sql.d.cts +17 -0
- package/dist/cjs/query/sql.js +104 -0
- package/dist/cjs/query/types.d.cts +7 -0
- package/dist/cjs/query/types.js +2 -0
- package/dist/cjs/query/utilities.d.cts +34 -0
- package/dist/cjs/query/utilities.js +184 -0
- package/dist/cjs/table/constants.js +8 -0
- package/dist/cjs/table/index.d.cts +7 -0
- package/dist/cjs/table/index.js +54 -0
- package/dist/cjs/table/types.d.cts +7 -0
- package/dist/cjs/table/types.js +2 -0
- package/dist/cjs/table/utilities.d.cts +7 -0
- package/dist/cjs/table/utilities.js +55 -0
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/utilities.js +21 -0
- package/dist/esm/column/constants.d.ts +97 -0
- package/dist/{column → esm/column}/constants.js +4 -3
- package/dist/{column → esm/column}/index.js +6 -5
- package/dist/esm/column/types.js +1 -0
- package/dist/{database → esm/database}/alter.js +2 -1
- package/dist/{database → esm/database}/column.js +2 -1
- package/dist/{database → esm/database}/index.js +10 -9
- package/dist/{database → esm/database}/table.js +2 -1
- package/dist/{database → esm/database}/wrapper.js +6 -5
- package/dist/esm/index.js +5 -0
- package/dist/esm/migration/index.js +49 -0
- package/dist/esm/migration/runner.d.ts +3 -0
- package/dist/{migration → esm/migration}/runner.js +6 -5
- package/dist/{query → esm/query}/builder.js +3 -2
- package/dist/{query → esm/query}/condition.js +4 -3
- package/dist/esm/query/constants.d.ts +63 -0
- package/dist/{query → esm/query}/constants.js +9 -8
- package/dist/{query → esm/query}/helper.js +3 -2
- package/dist/{query → esm/query}/index.js +10 -9
- package/dist/{query → esm/query}/join.js +1 -0
- package/dist/{query → esm/query}/sql.js +4 -3
- package/dist/{query → esm/query}/utilities.js +5 -4
- package/dist/esm/table/constants.d.ts +7 -0
- package/dist/{table → esm/table}/constants.js +2 -1
- package/dist/{table → esm/table}/index.js +6 -5
- package/dist/{table → esm/table}/utilities.js +5 -4
- package/dist/esm/types.d.ts +3 -0
- package/dist/esm/utilities.d.ts +4 -0
- package/dist/{utilities.js → esm/utilities.js} +1 -0
- package/package.json +42 -19
- package/dist/column/types.js +0 -1
- package/dist/index.js +0 -5
- /package/dist/{column/constants.d.ts → cjs/column/constants.d.cts} +0 -0
- /package/dist/{migration/runner.d.ts → cjs/migration/runner.d.cts} +0 -0
- /package/dist/{query/constants.d.ts → cjs/query/constants.d.cts} +0 -0
- /package/dist/{table/constants.d.ts → cjs/table/constants.d.cts} +0 -0
- /package/dist/{types.d.ts → cjs/types.d.cts} +0 -0
- /package/dist/{utilities.d.ts → cjs/utilities.d.cts} +0 -0
- /package/dist/{column → esm/column}/index.d.ts +0 -0
- /package/dist/{column → esm/column}/types.d.ts +0 -0
- /package/dist/{database → esm/database}/alter.d.ts +0 -0
- /package/dist/{database → esm/database}/column.d.ts +0 -0
- /package/dist/{database → esm/database}/contract.d.ts +0 -0
- /package/dist/{database → esm/database}/contract.js +0 -0
- /package/dist/{database → esm/database}/index.d.ts +0 -0
- /package/dist/{database → esm/database}/table.d.ts +0 -0
- /package/dist/{database → esm/database}/types.d.ts +0 -0
- /package/dist/{database → esm/database}/types.js +0 -0
- /package/dist/{database → esm/database}/wrapper.d.ts +0 -0
- /package/dist/{index-DFrpzXEn.d.ts → esm/index-DFrpzXEn.d.ts} +0 -0
- /package/dist/{index-FMT0YEO7.d.ts → esm/index-FMT0YEO7.d.ts} +0 -0
- /package/dist/{index.d.ts → esm/index.d.ts} +0 -0
- /package/dist/{migration → esm/migration}/index.d.ts +0 -0
- /package/dist/{migration → esm/migration}/type.d.ts +0 -0
- /package/dist/{migration → esm/migration}/type.js +0 -0
- /package/dist/{query → esm/query}/builder.d.ts +0 -0
- /package/dist/{query → esm/query}/condition.d.ts +0 -0
- /package/dist/{query → esm/query}/contract.d.ts +0 -0
- /package/dist/{query → esm/query}/contract.js +0 -0
- /package/dist/{query → esm/query}/helper.d.ts +0 -0
- /package/dist/{query → esm/query}/index.d.ts +0 -0
- /package/dist/{query → esm/query}/join.d.ts +0 -0
- /package/dist/{query → esm/query}/sql.d.ts +0 -0
- /package/dist/{query → esm/query}/types.d.ts +0 -0
- /package/dist/{query → esm/query}/types.js +0 -0
- /package/dist/{query → esm/query}/utilities.d.ts +0 -0
- /package/dist/{table → esm/table}/index.d.ts +0 -0
- /package/dist/{table → esm/table}/types.d.ts +0 -0
- /package/dist/{table → esm/table}/types.js +0 -0
- /package/dist/{table → esm/table}/utilities.d.ts +0 -0
- /package/dist/{types.js → esm/types.js} +0 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { T as Table, W as WhereValue, Q as QueryDefinition, C as ColumnSelector, c as StrictColumnSelector, d as QueryBuilder, A as AliasedColumn, e as SelectableColumn } from '../index-CwiFQh0I.cjs';
|
|
2
|
+
import { Column } from '../column/index.cjs';
|
|
3
|
+
import { Dialect } from '../table/constants.cjs';
|
|
4
|
+
import { AcceptedOperator } from './constants.cjs';
|
|
5
|
+
import '../column/constants.cjs';
|
|
6
|
+
import '../types.cjs';
|
|
7
|
+
import '../column/types.cjs';
|
|
8
|
+
|
|
9
|
+
declare function getTableColumnNames<ColName extends string, BaseAlias extends string, BaseTable extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>>(column: ColName, baseAlias: BaseAlias, baseTable: BaseTable, joinedTables: JoinedTables): {
|
|
10
|
+
from: string;
|
|
11
|
+
columns: string[];
|
|
12
|
+
};
|
|
13
|
+
declare function getCondition<DbDialect extends Dialect, ColName extends string, Operator extends AcceptedOperator, Value extends WhereValue<Column>[Operator]>(dialect: DbDialect, column: ColName, operator: Operator, value: Value): string;
|
|
14
|
+
declare function getTimestamp<TableRef extends Table<string, Record<string, Column>>>(table: TableRef): {
|
|
15
|
+
isWithTimestamp: boolean;
|
|
16
|
+
timestamp: Date;
|
|
17
|
+
createdAt: string;
|
|
18
|
+
updatedAt: string;
|
|
19
|
+
};
|
|
20
|
+
declare function getParanoid<TableRef extends Table<string, Record<string, Column>>>(table: TableRef): {
|
|
21
|
+
isWithParanoid: boolean;
|
|
22
|
+
timestamp: Date;
|
|
23
|
+
deletedAt: string;
|
|
24
|
+
};
|
|
25
|
+
declare function getWhereConditions<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>(q: Query): string[];
|
|
26
|
+
declare function getGroupByConditions<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>(q: Query): (string | AliasedColumn<ColumnSelector<Alias, TableRef, JoinedTables>, string>)[];
|
|
27
|
+
declare function getTableSelectName<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables>, Query extends QueryBuilder<Alias, TableRef, JoinedTables, Definition, AllowedColumn, StrictAllowedColumn>>(q: Query): string;
|
|
28
|
+
declare function parseAliasedRow({ row, selects, root, }: {
|
|
29
|
+
row: Record<string, unknown>;
|
|
30
|
+
selects: SelectableColumn<string>[];
|
|
31
|
+
root?: string | null;
|
|
32
|
+
}): Record<string, any>;
|
|
33
|
+
|
|
34
|
+
export { getCondition, getGroupByConditions, getParanoid, getTableColumnNames, getTableSelectName, getTimestamp, getWhereConditions, parseAliasedRow };
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('.');
|
|
4
|
+
var constants$1 = require('../table/constants');
|
|
5
|
+
var utilities = require('../utilities');
|
|
6
|
+
var constants = require('./constants');
|
|
7
|
+
|
|
8
|
+
function getTableColumnNames(column, baseAlias, baseTable, joinedTables) {
|
|
9
|
+
const [tableAlias] = column.split(".");
|
|
10
|
+
const isOnBase = tableAlias === baseAlias;
|
|
11
|
+
const from = isOnBase ? baseAlias : tableAlias;
|
|
12
|
+
const columns = isOnBase ? Object.keys(baseTable.columns) : Object.keys(joinedTables?.[from]?.columns ?? {});
|
|
13
|
+
return {
|
|
14
|
+
from,
|
|
15
|
+
columns
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function getCondition(dialect, column, operator, value) {
|
|
19
|
+
switch (operator) {
|
|
20
|
+
case constants.AcceptedOperator.EQ:
|
|
21
|
+
return `${column} = ?`;
|
|
22
|
+
case constants.AcceptedOperator.NE:
|
|
23
|
+
return `${column} != ?`;
|
|
24
|
+
case constants.AcceptedOperator.GT:
|
|
25
|
+
return `${column} > ?`;
|
|
26
|
+
case constants.AcceptedOperator.LT:
|
|
27
|
+
return `${column} < ?`;
|
|
28
|
+
case constants.AcceptedOperator.GTE:
|
|
29
|
+
return `${column} >= ?`;
|
|
30
|
+
case constants.AcceptedOperator.LTE:
|
|
31
|
+
return `${column} <= ?`;
|
|
32
|
+
case constants.AcceptedOperator.IN:
|
|
33
|
+
return `${column} IN (${value.map(() => "?").join(", ")})`;
|
|
34
|
+
case constants.AcceptedOperator.NOT_IN:
|
|
35
|
+
return `${column} NOT IN (${value.map(() => "?").join(", ")})`;
|
|
36
|
+
case constants.AcceptedOperator.LIKE:
|
|
37
|
+
return `${column} LIKE ?`;
|
|
38
|
+
case constants.AcceptedOperator.NOT_LIKE:
|
|
39
|
+
return `${column} NOT LIKE ?`;
|
|
40
|
+
case constants.AcceptedOperator.ILIKE:
|
|
41
|
+
if (dialect === constants$1.Dialect.POSTGRES) {
|
|
42
|
+
return `${column} ILIKE ?`;
|
|
43
|
+
}
|
|
44
|
+
return `LOWER(${column}) LIKE LOWER(?)`;
|
|
45
|
+
case constants.AcceptedOperator.NOT_ILIKE:
|
|
46
|
+
if (dialect === constants$1.Dialect.POSTGRES) {
|
|
47
|
+
return `${column} NOT ILIKE ?`;
|
|
48
|
+
}
|
|
49
|
+
return `LOWER(${column}) NOT LIKE LOWER(?)`;
|
|
50
|
+
case constants.AcceptedOperator.IS_NULL:
|
|
51
|
+
return `${column} IS NULL`;
|
|
52
|
+
case constants.AcceptedOperator.IS_NOT_NULL:
|
|
53
|
+
return `${column} IS NOT NULL`;
|
|
54
|
+
case constants.AcceptedOperator.BETWEEN:
|
|
55
|
+
return `${column} BETWEEN ? AND ?`;
|
|
56
|
+
case constants.AcceptedOperator.NOT_BETWEEN:
|
|
57
|
+
return `${column} NOT BETWEEN ? AND ?`;
|
|
58
|
+
default:
|
|
59
|
+
throw new Error("Invalid operator");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function getTimestamp(table) {
|
|
63
|
+
const isWithTimestamp = !!table.timestamp;
|
|
64
|
+
const timestamp = /* @__PURE__ */ new Date();
|
|
65
|
+
let createdAt = "createdAt";
|
|
66
|
+
let updatedAt = "updatedAt";
|
|
67
|
+
if (isWithTimestamp) {
|
|
68
|
+
const isCustomTimestamp = typeof table.timestamp === "object";
|
|
69
|
+
if (isCustomTimestamp && table.timestamp.createdAt) {
|
|
70
|
+
createdAt = table.timestamp.createdAt;
|
|
71
|
+
}
|
|
72
|
+
if (isCustomTimestamp && table.timestamp.updatedAt) {
|
|
73
|
+
updatedAt = table.timestamp.updatedAt;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
isWithTimestamp,
|
|
78
|
+
timestamp,
|
|
79
|
+
createdAt,
|
|
80
|
+
updatedAt
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function getParanoid(table) {
|
|
84
|
+
const isWithParanoid = !!table.paranoid;
|
|
85
|
+
const timestamp = /* @__PURE__ */ new Date();
|
|
86
|
+
let deletedAt = "deletedAt";
|
|
87
|
+
if (isWithParanoid) {
|
|
88
|
+
if (typeof table.paranoid === "string") {
|
|
89
|
+
deletedAt = table.paranoid;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
isWithParanoid,
|
|
94
|
+
timestamp,
|
|
95
|
+
deletedAt
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function getWhereConditions(q) {
|
|
99
|
+
if (q.definition.queryType === constants.QueryType.INSERT) return [];
|
|
100
|
+
const conditions = [];
|
|
101
|
+
const base = q.definition.baseAlias ?? q.table.name;
|
|
102
|
+
const { isWithParanoid, deletedAt } = getParanoid(q.table);
|
|
103
|
+
const withDeleted = !!q.definition.withDeleted;
|
|
104
|
+
const isHasConditions = !!q.definition.where?.length;
|
|
105
|
+
if (!withDeleted && isWithParanoid) {
|
|
106
|
+
const suffix = isHasConditions ? " AND" : "";
|
|
107
|
+
const column = `${base}.${utilities.quoteIdentifier(deletedAt)}`;
|
|
108
|
+
conditions.unshift(`${column} IS NULL${suffix}`);
|
|
109
|
+
}
|
|
110
|
+
if (q.definition.where?.length) {
|
|
111
|
+
conditions.push(...q.definition.where);
|
|
112
|
+
}
|
|
113
|
+
return conditions;
|
|
114
|
+
}
|
|
115
|
+
function getGroupByConditions(q) {
|
|
116
|
+
if (q.definition.queryType !== constants.QueryType.SELECT) return [];
|
|
117
|
+
if (q.definition.groupBy?.length) return q.definition.groupBy;
|
|
118
|
+
if (q.definition.aggregates?.length) {
|
|
119
|
+
if (q.definition.select?.length)
|
|
120
|
+
return q.definition.select.map((col) => {
|
|
121
|
+
if (typeof col === "string" && col.endsWith("*")) {
|
|
122
|
+
const { from: from2, columns } = getTableColumnNames(
|
|
123
|
+
col,
|
|
124
|
+
q.definition.baseAlias ?? q.table.name,
|
|
125
|
+
q.table,
|
|
126
|
+
q.definition.joinedTables ?? {}
|
|
127
|
+
);
|
|
128
|
+
return columns.map((column) => `${from2}.${utilities.quoteIdentifier(column)}`).join(" ");
|
|
129
|
+
}
|
|
130
|
+
return col;
|
|
131
|
+
});
|
|
132
|
+
const from = q.definition.baseAlias ?? q.table.name;
|
|
133
|
+
return Object.keys(q.table.columns).map(
|
|
134
|
+
(col) => `${from}.${utilities.quoteIdentifier(col)}`
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
return [];
|
|
138
|
+
}
|
|
139
|
+
function getTableSelectName(q) {
|
|
140
|
+
if (!q.definition.baseAlias) return q.table.name;
|
|
141
|
+
return `${q.table.name} AS ${q.definition.baseAlias}`;
|
|
142
|
+
}
|
|
143
|
+
function parseAliasedRow({
|
|
144
|
+
row,
|
|
145
|
+
selects,
|
|
146
|
+
root = null
|
|
147
|
+
}) {
|
|
148
|
+
let result = {};
|
|
149
|
+
for (const key in row) {
|
|
150
|
+
const [table, column] = key.split(".");
|
|
151
|
+
if (!column) {
|
|
152
|
+
const alias = selects.find(
|
|
153
|
+
(s) => typeof s === "object" && s.as === table
|
|
154
|
+
);
|
|
155
|
+
if (alias) {
|
|
156
|
+
const [oriTab] = alias.column.split(".");
|
|
157
|
+
if (!result[oriTab]) result[oriTab] = {};
|
|
158
|
+
result[oriTab][table] = row[key];
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
result[key] = row[key];
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (!result[table]) result[table] = {};
|
|
165
|
+
result[table][column] = row[key];
|
|
166
|
+
}
|
|
167
|
+
if (root) {
|
|
168
|
+
result = {
|
|
169
|
+
...result,
|
|
170
|
+
...result[root]
|
|
171
|
+
};
|
|
172
|
+
delete result[root];
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
exports.getCondition = getCondition;
|
|
178
|
+
exports.getGroupByConditions = getGroupByConditions;
|
|
179
|
+
exports.getParanoid = getParanoid;
|
|
180
|
+
exports.getTableColumnNames = getTableColumnNames;
|
|
181
|
+
exports.getTableSelectName = getTableSelectName;
|
|
182
|
+
exports.getTimestamp = getTimestamp;
|
|
183
|
+
exports.getWhereConditions = getWhereConditions;
|
|
184
|
+
exports.parseAliasedRow = parseAliasedRow;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var query = require('../query');
|
|
4
|
+
var utilities = require('./utilities');
|
|
5
|
+
|
|
6
|
+
class Table {
|
|
7
|
+
client;
|
|
8
|
+
dialect;
|
|
9
|
+
name;
|
|
10
|
+
columns;
|
|
11
|
+
timestamp;
|
|
12
|
+
paranoid;
|
|
13
|
+
_output;
|
|
14
|
+
constructor(options) {
|
|
15
|
+
this.dialect = options.dialect;
|
|
16
|
+
this.name = options.name;
|
|
17
|
+
this.columns = options.columns;
|
|
18
|
+
this.paranoid = options.paranoid || null;
|
|
19
|
+
this.timestamp = options.timestamp || null;
|
|
20
|
+
this.client = null;
|
|
21
|
+
for (const column of Object.values(this.columns)) {
|
|
22
|
+
column.dialect(options.dialect);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
infer() {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
static define(options) {
|
|
29
|
+
const columns = utilities.defineColumns(options);
|
|
30
|
+
return new Table({
|
|
31
|
+
...options,
|
|
32
|
+
columns
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
async create(db = this.client) {
|
|
36
|
+
if (!db) throw new Error("Database client not defined");
|
|
37
|
+
const sql = `CREATE TABLE IF NOT EXISTS ${this.name} (${Object.entries(
|
|
38
|
+
this.columns
|
|
39
|
+
).map(([name, column]) => `${name} ${column.toQuery().query}`).join(", ")});`;
|
|
40
|
+
await db.exec(sql);
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
async drop(db = this.client) {
|
|
44
|
+
if (!db) throw new Error("Database client not defined");
|
|
45
|
+
const sql = `DROP TABLE IF EXISTS ${this.name};`;
|
|
46
|
+
await db.exec(sql);
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
query() {
|
|
50
|
+
return new query.QueryBuilder(this);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
exports.Table = Table;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { M as MergeTimestampParanoid, s as TableOptions, t as TableOutput, a as TimestampOptions } from '../index-CwiFQh0I.cjs';
|
|
2
|
+
import '../column/index.cjs';
|
|
3
|
+
import './constants.cjs';
|
|
4
|
+
import '../column/constants.cjs';
|
|
5
|
+
import '../types.cjs';
|
|
6
|
+
import '../query/constants.cjs';
|
|
7
|
+
import '../column/types.cjs';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import '../column/constants.cjs';
|
|
2
|
+
import '../column/index.cjs';
|
|
3
|
+
import './constants.cjs';
|
|
4
|
+
export { p as createdAt, r as defineColumns, q as deletedAt, u as updatedAt } from '../index-CwiFQh0I.cjs';
|
|
5
|
+
import '../column/types.cjs';
|
|
6
|
+
import '../types.cjs';
|
|
7
|
+
import '../query/constants.cjs';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var column = require('../column');
|
|
4
|
+
|
|
5
|
+
const createdAt = column.Column.define({
|
|
6
|
+
type: "DATETIME"
|
|
7
|
+
}).default("CURRENT_TIMESTAMP");
|
|
8
|
+
const updatedAt = column.Column.define({
|
|
9
|
+
type: "DATETIME"
|
|
10
|
+
});
|
|
11
|
+
const deletedAt = column.Column.define({
|
|
12
|
+
type: "DATETIME"
|
|
13
|
+
});
|
|
14
|
+
function defineColumns(options) {
|
|
15
|
+
const columns = {
|
|
16
|
+
...options.columns
|
|
17
|
+
};
|
|
18
|
+
const tracker = {
|
|
19
|
+
deletedAt: "deletedAt"
|
|
20
|
+
};
|
|
21
|
+
if (options.timestamp) {
|
|
22
|
+
const timestamp = {
|
|
23
|
+
createdAt: "createdAt",
|
|
24
|
+
updatedAt: "updatedAt"
|
|
25
|
+
};
|
|
26
|
+
if (typeof options.timestamp === "object") {
|
|
27
|
+
if (typeof options.timestamp.createdAt === "string") {
|
|
28
|
+
timestamp.createdAt = options.timestamp.createdAt;
|
|
29
|
+
}
|
|
30
|
+
if (typeof options.timestamp.updatedAt === "string") {
|
|
31
|
+
timestamp.updatedAt = options.timestamp.updatedAt;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (!columns[timestamp.createdAt]) {
|
|
35
|
+
columns[timestamp.createdAt] = createdAt;
|
|
36
|
+
}
|
|
37
|
+
if (!columns[timestamp.updatedAt]) {
|
|
38
|
+
columns[timestamp.updatedAt] = updatedAt;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (options.paranoid) {
|
|
42
|
+
if (typeof options.paranoid !== "boolean") {
|
|
43
|
+
tracker.deletedAt = options.paranoid;
|
|
44
|
+
}
|
|
45
|
+
if (!columns[tracker.deletedAt]) {
|
|
46
|
+
columns[tracker.deletedAt] = deletedAt;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return columns;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
exports.createdAt = createdAt;
|
|
53
|
+
exports.defineColumns = defineColumns;
|
|
54
|
+
exports.deletedAt = deletedAt;
|
|
55
|
+
exports.updatedAt = updatedAt;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function deepClone(obj) {
|
|
4
|
+
if (Array.isArray(obj)) {
|
|
5
|
+
return obj.map((item) => deepClone(item));
|
|
6
|
+
}
|
|
7
|
+
if (obj && typeof obj === "object") {
|
|
8
|
+
const clonedObj = {};
|
|
9
|
+
for (const key in obj) {
|
|
10
|
+
clonedObj[key] = deepClone(obj[key]);
|
|
11
|
+
}
|
|
12
|
+
return clonedObj;
|
|
13
|
+
}
|
|
14
|
+
return obj;
|
|
15
|
+
}
|
|
16
|
+
function quoteIdentifier(identifier) {
|
|
17
|
+
return `"${identifier.replace(/"/g, '""')}"`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
exports.deepClone = deepClone;
|
|
21
|
+
exports.quoteIdentifier = quoteIdentifier;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
declare const AcceptedColumnTypes: {
|
|
2
|
+
readonly INTEGER: "INTEGER";
|
|
3
|
+
readonly STRING: "STRING";
|
|
4
|
+
readonly BOOLEAN: "BOOLEAN";
|
|
5
|
+
readonly DATE: "DATE";
|
|
6
|
+
readonly FLOAT: "FLOAT";
|
|
7
|
+
readonly DECIMAL: "DECIMAL";
|
|
8
|
+
readonly BIGINT: "BIGINT";
|
|
9
|
+
readonly TEXT: "TEXT";
|
|
10
|
+
readonly BLOB: "BLOB";
|
|
11
|
+
readonly JSON: "JSON";
|
|
12
|
+
readonly VARCHAR: "VARCHAR";
|
|
13
|
+
readonly TIME: "TIME";
|
|
14
|
+
readonly TIMESTAMP: "TIMESTAMP";
|
|
15
|
+
readonly DOUBLE: "DOUBLE";
|
|
16
|
+
readonly DATETIME: "DATETIME";
|
|
17
|
+
readonly DATEONLY: "DATEONLY";
|
|
18
|
+
readonly ENUM: "ENUM";
|
|
19
|
+
readonly SERIAL: "SERIAL";
|
|
20
|
+
};
|
|
21
|
+
type AcceptedColumnTypes = (typeof AcceptedColumnTypes)[keyof typeof AcceptedColumnTypes];
|
|
22
|
+
declare const ColumnTypeMapping: {
|
|
23
|
+
readonly INTEGER: {
|
|
24
|
+
readonly sqlite: "INTEGER";
|
|
25
|
+
readonly postgres: "INTEGER";
|
|
26
|
+
};
|
|
27
|
+
readonly STRING: {
|
|
28
|
+
readonly sqlite: "TEXT";
|
|
29
|
+
readonly postgres: "VARCHAR";
|
|
30
|
+
};
|
|
31
|
+
readonly BOOLEAN: {
|
|
32
|
+
readonly sqlite: "INTEGER";
|
|
33
|
+
readonly postgres: "BOOLEAN";
|
|
34
|
+
};
|
|
35
|
+
readonly DATE: {
|
|
36
|
+
readonly sqlite: "TEXT";
|
|
37
|
+
readonly postgres: "DATE";
|
|
38
|
+
};
|
|
39
|
+
readonly FLOAT: {
|
|
40
|
+
readonly sqlite: "REAL";
|
|
41
|
+
readonly postgres: "FLOAT";
|
|
42
|
+
};
|
|
43
|
+
readonly DECIMAL: {
|
|
44
|
+
readonly sqlite: "TEXT";
|
|
45
|
+
readonly postgres: "DECIMAL";
|
|
46
|
+
};
|
|
47
|
+
readonly BIGINT: {
|
|
48
|
+
readonly sqlite: "TEXT";
|
|
49
|
+
readonly postgres: "BIGINT";
|
|
50
|
+
};
|
|
51
|
+
readonly TEXT: {
|
|
52
|
+
readonly sqlite: "TEXT";
|
|
53
|
+
readonly postgres: "TEXT";
|
|
54
|
+
};
|
|
55
|
+
readonly BLOB: {
|
|
56
|
+
readonly sqlite: "BLOB";
|
|
57
|
+
readonly postgres: "BYTEA";
|
|
58
|
+
};
|
|
59
|
+
readonly JSON: {
|
|
60
|
+
readonly sqlite: "TEXT";
|
|
61
|
+
readonly postgres: "JSONB";
|
|
62
|
+
};
|
|
63
|
+
readonly VARCHAR: {
|
|
64
|
+
readonly sqlite: "TEXT";
|
|
65
|
+
readonly postgres: "VARCHAR";
|
|
66
|
+
};
|
|
67
|
+
readonly TIME: {
|
|
68
|
+
readonly sqlite: "TEXT";
|
|
69
|
+
readonly postgres: "TIME";
|
|
70
|
+
};
|
|
71
|
+
readonly TIMESTAMP: {
|
|
72
|
+
readonly sqlite: "TEXT";
|
|
73
|
+
readonly postgres: "TIMESTAMP";
|
|
74
|
+
};
|
|
75
|
+
readonly DOUBLE: {
|
|
76
|
+
readonly sqlite: "REAL";
|
|
77
|
+
readonly postgres: "DOUBLE PRECISION";
|
|
78
|
+
};
|
|
79
|
+
readonly DATETIME: {
|
|
80
|
+
readonly sqlite: "TEXT";
|
|
81
|
+
readonly postgres: "TIMESTAMP";
|
|
82
|
+
};
|
|
83
|
+
readonly DATEONLY: {
|
|
84
|
+
readonly sqlite: "TEXT";
|
|
85
|
+
readonly postgres: "DATE";
|
|
86
|
+
};
|
|
87
|
+
readonly ENUM: {
|
|
88
|
+
readonly sqlite: "TEXT";
|
|
89
|
+
readonly postgres: "TEXT";
|
|
90
|
+
};
|
|
91
|
+
readonly SERIAL: {
|
|
92
|
+
readonly sqlite: "INTEGER AUTOINCREMENT";
|
|
93
|
+
readonly postgres: "SERIAL";
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export { AcceptedColumnTypes, ColumnTypeMapping };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { Dialect } from '../table/constants';
|
|
1
|
+
import { Dialect } from '../table/constants.js';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
// src/column/constants.ts
|
|
4
|
+
var AcceptedColumnTypes = {
|
|
4
5
|
INTEGER: "INTEGER",
|
|
5
6
|
STRING: "STRING",
|
|
6
7
|
BOOLEAN: "BOOLEAN",
|
|
@@ -20,7 +21,7 @@ const AcceptedColumnTypes = {
|
|
|
20
21
|
ENUM: "ENUM",
|
|
21
22
|
SERIAL: "SERIAL"
|
|
22
23
|
};
|
|
23
|
-
|
|
24
|
+
var ColumnTypeMapping = {
|
|
24
25
|
[AcceptedColumnTypes.INTEGER]: {
|
|
25
26
|
[Dialect.SQLITE]: "INTEGER",
|
|
26
27
|
[Dialect.POSTGRES]: "INTEGER"
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { Dialect } from '../table/constants';
|
|
2
|
-
import { ColumnTypeMapping, AcceptedColumnTypes } from './constants';
|
|
1
|
+
import { Dialect } from '../table/constants.js';
|
|
2
|
+
import { ColumnTypeMapping, AcceptedColumnTypes } from './constants.js';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
// src/column/index.ts
|
|
5
|
+
var Column = class _Column {
|
|
5
6
|
definition;
|
|
6
7
|
type;
|
|
7
8
|
length;
|
|
@@ -27,7 +28,7 @@ class Column {
|
|
|
27
28
|
};
|
|
28
29
|
}
|
|
29
30
|
static define(options) {
|
|
30
|
-
return new
|
|
31
|
+
return new _Column(options);
|
|
31
32
|
}
|
|
32
33
|
autoIncrement() {
|
|
33
34
|
this.definition.autoIncrement = true;
|
|
@@ -100,6 +101,6 @@ class Column {
|
|
|
100
101
|
infer() {
|
|
101
102
|
return null;
|
|
102
103
|
}
|
|
103
|
-
}
|
|
104
|
+
};
|
|
104
105
|
|
|
105
106
|
export { Column };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './constants.js';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Dialect } from '../table/constants';
|
|
1
|
+
import { Dialect } from '../table/constants.js';
|
|
2
2
|
|
|
3
|
+
// src/database/alter.ts
|
|
3
4
|
async function alterColumnType(tableName, columnName, newType) {
|
|
4
5
|
if (this.dialect === Dialect.SQLITE) {
|
|
5
6
|
throw new Error("SQLite does not support ALTER COLUMN TYPE directly.");
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Dialect } from '../table/constants';
|
|
1
|
+
import { Dialect } from '../table/constants.js';
|
|
2
2
|
|
|
3
|
+
// src/database/column.ts
|
|
3
4
|
async function addColumn(tableName, columnName, column) {
|
|
4
5
|
await this.client.exec(
|
|
5
6
|
`ALTER TABLE ${tableName} ADD COLUMN ${columnName} ${column.toString()};`
|
|
@@ -1,11 +1,12 @@
|
|
|
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';
|
|
1
|
+
import '../table/index.js';
|
|
2
|
+
import { Dialect } from '../table/constants.js';
|
|
3
|
+
import { alterColumnType, setColumnDefault, dropColumnDefault, dropColumnNotNull } from './alter.js';
|
|
4
|
+
import { addColumn, renameColumn, dropColumn } from './column.js';
|
|
5
|
+
import { createTable, renameTable, dropTable } from './table.js';
|
|
6
|
+
import { DatabasePsql, DatabaseSqlite } from './wrapper.js';
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
// src/database/index.ts
|
|
9
|
+
var Database = class _Database {
|
|
9
10
|
hooks;
|
|
10
11
|
dialect;
|
|
11
12
|
defintion;
|
|
@@ -85,8 +86,8 @@ class Database {
|
|
|
85
86
|
return this.client.transaction(fn);
|
|
86
87
|
}
|
|
87
88
|
static define(options) {
|
|
88
|
-
return new
|
|
89
|
+
return new _Database(options);
|
|
89
90
|
}
|
|
90
|
-
}
|
|
91
|
+
};
|
|
91
92
|
|
|
92
93
|
export { Database };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { SQL } from 'bun';
|
|
2
2
|
import { Database } from 'bun:sqlite';
|
|
3
|
-
import { Dialect } from '../table/constants';
|
|
3
|
+
import { Dialect } from '../table/constants.js';
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
// src/database/wrapper.ts
|
|
6
|
+
var DatabasePsql = class {
|
|
6
7
|
dialect;
|
|
7
8
|
options;
|
|
8
9
|
client;
|
|
@@ -48,8 +49,8 @@ class DatabasePsql {
|
|
|
48
49
|
throw err;
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
|
-
}
|
|
52
|
-
|
|
52
|
+
};
|
|
53
|
+
var DatabaseSqlite = class {
|
|
53
54
|
dialect;
|
|
54
55
|
options;
|
|
55
56
|
client;
|
|
@@ -87,6 +88,6 @@ class DatabaseSqlite {
|
|
|
87
88
|
throw err;
|
|
88
89
|
}
|
|
89
90
|
}
|
|
90
|
-
}
|
|
91
|
+
};
|
|
91
92
|
|
|
92
93
|
export { DatabasePsql, DatabaseSqlite };
|