@devbro/neko-sql 0.1.3 → 0.1.5
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/Blueprint.d.mts +1 -1
- package/dist/Blueprint.mjs +12 -1
- package/dist/Blueprint.mjs.map +1 -1
- package/dist/{Connection-D4SY9JPV.d.mts → Connection-DUifmM2I.d.mts} +1 -1
- package/dist/Connection.d.mts +2 -2
- package/dist/Connection.mjs +5 -0
- package/dist/Connection.mjs.map +1 -1
- package/dist/Expression.d.mts +1 -1
- package/dist/Expression.mjs +5 -0
- package/dist/Expression.mjs.map +1 -1
- package/dist/Migration.d.mts +2 -2
- package/dist/Migration.mjs +5 -0
- package/dist/Migration.mjs.map +1 -1
- package/dist/Query.d.mts +2 -2
- package/dist/Query.mjs +5 -0
- package/dist/Query.mjs.map +1 -1
- package/dist/QueryGrammar.d.mts +2 -2
- package/dist/QueryGrammar.mjs +12 -0
- package/dist/QueryGrammar.mjs.map +1 -1
- package/dist/Schema.d.mts +2 -2
- package/dist/Schema.mjs +7 -2
- package/dist/Schema.mjs.map +1 -1
- package/dist/SchemaGrammar.d.mts +1 -1
- package/dist/SchemaGrammar.mjs +9 -1
- package/dist/SchemaGrammar.mjs.map +1 -1
- package/dist/databases/index.d.mts +2 -2
- package/dist/databases/index.mjs +1 -1
- package/dist/databases/index.mjs.map +1 -1
- package/dist/databases/postgresql/PostgresqlConnection.d.mts +2 -2
- package/dist/databases/postgresql/PostgresqlConnection.mjs +10 -5
- package/dist/databases/postgresql/PostgresqlConnection.mjs.map +1 -1
- package/dist/databases/postgresql/PostgresqlQueryGrammar.d.mts +2 -2
- package/dist/databases/postgresql/PostgresqlQueryGrammar.mjs +6 -1
- package/dist/databases/postgresql/PostgresqlQueryGrammar.mjs.map +1 -1
- package/dist/databases/postgresql/PostgresqlSchemaGrammar.d.mts +1 -1
- package/dist/databases/postgresql/PostgresqlSchemaGrammar.mjs +6 -1
- package/dist/databases/postgresql/PostgresqlSchemaGrammar.mjs.map +1 -1
- package/dist/databases/postgresql/index.d.mts +2 -2
- package/dist/databases/postgresql/index.mjs +3 -3
- package/dist/databases/postgresql/index.mjs.map +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.js +938 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +9 -9
- package/dist/index.mjs.map +1 -1
- package/dist/{types-C_aDrXJN.d.mts → types-BEFQrxTG.d.mts} +1 -1
- package/dist/types.d.mts +1 -1
- package/package.json +6 -7
- package/dist/Blueprint.d.ts +0 -57
- package/dist/Blueprint.js +0 -169
- package/dist/Blueprint.js.map +0 -1
- package/dist/Connection-DklX9Kzq.d.ts +0 -106
- package/dist/Connection.d.ts +0 -4
- package/dist/Connection.js +0 -30
- package/dist/Connection.js.map +0 -1
- package/dist/Expression.d.ts +0 -1
- package/dist/Expression.js +0 -37
- package/dist/Expression.js.map +0 -1
- package/dist/Migration.d.ts +0 -11
- package/dist/Migration.js +0 -30
- package/dist/Migration.js.map +0 -1
- package/dist/Query.d.ts +0 -4
- package/dist/Query.js +0 -171
- package/dist/Query.js.map +0 -1
- package/dist/QueryGrammar.d.ts +0 -4
- package/dist/QueryGrammar.js +0 -260
- package/dist/QueryGrammar.js.map +0 -1
- package/dist/Schema.d.ts +0 -4
- package/dist/Schema.js +0 -70
- package/dist/Schema.js.map +0 -1
- package/dist/SchemaGrammar.d.ts +0 -20
- package/dist/SchemaGrammar.js +0 -174
- package/dist/SchemaGrammar.js.map +0 -1
- package/dist/databases/index.d.ts +0 -8
- package/dist/databases/index.js +0 -23
- package/dist/databases/index.js.map +0 -1
- package/dist/databases/postgresql/PostgresqlConnection.d.ts +0 -28
- package/dist/databases/postgresql/PostgresqlConnection.js +0 -114
- package/dist/databases/postgresql/PostgresqlConnection.js.map +0 -1
- package/dist/databases/postgresql/PostgresqlQueryGrammar.d.ts +0 -21
- package/dist/databases/postgresql/PostgresqlQueryGrammar.js +0 -69
- package/dist/databases/postgresql/PostgresqlQueryGrammar.js.map +0 -1
- package/dist/databases/postgresql/PostgresqlSchemaGrammar.d.ts +0 -8
- package/dist/databases/postgresql/PostgresqlSchemaGrammar.js +0 -31
- package/dist/databases/postgresql/PostgresqlSchemaGrammar.js.map +0 -1
- package/dist/databases/postgresql/index.d.ts +0 -8
- package/dist/databases/postgresql/index.js +0 -27
- package/dist/databases/postgresql/index.js.map +0 -1
- package/dist/index.d.ts +0 -9
- package/dist/types-C_aDrXJN.d.ts +0 -48
- package/dist/types.d.ts +0 -1
- package/dist/types.js +0 -17
- package/dist/types.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
6
13
|
var __copyProps = (to, from, except, desc) => {
|
|
7
14
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
15
|
for (let key of __getOwnPropNames(from))
|
|
@@ -11,29 +18,941 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
11
18
|
}
|
|
12
19
|
return to;
|
|
13
20
|
};
|
|
14
|
-
var
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
15
29
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
16
32
|
var index_exports = {};
|
|
33
|
+
__export(index_exports, {
|
|
34
|
+
Blueprint: () => Blueprint,
|
|
35
|
+
Column: () => Column,
|
|
36
|
+
Connection: () => Connection,
|
|
37
|
+
ForeignKeyConstraint: () => ForeignKeyConstraint,
|
|
38
|
+
Migration: () => Migration,
|
|
39
|
+
PostgresqlConnection: () => PostgresqlConnection,
|
|
40
|
+
PostgresqlQueryGrammar: () => PostgresqlQueryGrammar,
|
|
41
|
+
PostgresqlSchemaGrammar: () => PostgresqlSchemaGrammar,
|
|
42
|
+
Query: () => Query,
|
|
43
|
+
QueryGrammar: () => QueryGrammar,
|
|
44
|
+
Schema: () => Schema,
|
|
45
|
+
SchemaGrammar: () => SchemaGrammar
|
|
46
|
+
});
|
|
17
47
|
module.exports = __toCommonJS(index_exports);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
48
|
+
|
|
49
|
+
// src/Connection.mts
|
|
50
|
+
var Connection = class {
|
|
51
|
+
static {
|
|
52
|
+
__name(this, "Connection");
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// src/databases/postgresql/PostgresqlConnection.mts
|
|
57
|
+
var import_pg = require("pg");
|
|
58
|
+
|
|
59
|
+
// src/Query.mts
|
|
60
|
+
var Query = class {
|
|
61
|
+
constructor(connection, grammar) {
|
|
62
|
+
this.connection = connection;
|
|
63
|
+
this.grammar = grammar;
|
|
64
|
+
}
|
|
65
|
+
static {
|
|
66
|
+
__name(this, "Query");
|
|
67
|
+
}
|
|
68
|
+
allowedOperations = ["=", ">", "<", "!=", "like", "ilike"];
|
|
69
|
+
parts = {
|
|
70
|
+
select: ["*"],
|
|
71
|
+
table: "",
|
|
72
|
+
join: [],
|
|
73
|
+
where: [],
|
|
74
|
+
groupBy: [],
|
|
75
|
+
having: [],
|
|
76
|
+
orderBy: [],
|
|
77
|
+
limit: null,
|
|
78
|
+
offset: null
|
|
79
|
+
};
|
|
80
|
+
table(tableName) {
|
|
81
|
+
this.parts.table = tableName;
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
whereOp(column, operation, value, joinCondition = "and", negateCondition = false) {
|
|
85
|
+
this.parts.where.push({
|
|
86
|
+
type: "operation",
|
|
87
|
+
column,
|
|
88
|
+
operation,
|
|
89
|
+
value,
|
|
90
|
+
joinCondition,
|
|
91
|
+
negateCondition
|
|
92
|
+
});
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
whereColumn(column1, operation, column2, joinCondition = "and", negateCondition = false) {
|
|
96
|
+
this.parts.where.push({
|
|
97
|
+
type: "operationColumn",
|
|
98
|
+
column1,
|
|
99
|
+
operation,
|
|
100
|
+
column2,
|
|
101
|
+
joinCondition,
|
|
102
|
+
negateCondition
|
|
103
|
+
});
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
whereNull(column, joinCondition = "and", negateCondition = false) {
|
|
107
|
+
this.parts.where.push({ type: "null", column, joinCondition, negateCondition });
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
clearWhere() {
|
|
111
|
+
this.parts.where = [];
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
select(selects) {
|
|
115
|
+
this.parts.select = [...selects];
|
|
116
|
+
return this;
|
|
117
|
+
}
|
|
118
|
+
groupBy(columns) {
|
|
119
|
+
this.parts.groupBy = [...columns];
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
havingOp(column, operation, value, joinCondition = "and", negateCondition = false) {
|
|
123
|
+
this.parts.having.push({
|
|
124
|
+
type: "operation",
|
|
125
|
+
column,
|
|
126
|
+
operation,
|
|
127
|
+
value,
|
|
128
|
+
joinCondition,
|
|
129
|
+
negateCondition
|
|
130
|
+
});
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
havingRaw(sql, bindings, joinCondition = "and", negateCondition = false) {
|
|
134
|
+
this.parts.having.push({ type: "raw", sql, bindings, joinCondition, negateCondition });
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
orderBy(column, direction = "asc") {
|
|
138
|
+
this.parts.orderBy.push(`${column} ${direction}`);
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
limit(limit) {
|
|
142
|
+
this.parts.limit = limit;
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
offset(offset) {
|
|
146
|
+
this.parts.offset = offset;
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
toSql() {
|
|
150
|
+
return this.grammar.toSql(this);
|
|
151
|
+
}
|
|
152
|
+
async get() {
|
|
153
|
+
return await this.connection?.runQuery(this.toSql());
|
|
154
|
+
}
|
|
155
|
+
async count() {
|
|
156
|
+
const csql = this.grammar.compileCount(this);
|
|
157
|
+
const result = await this.connection?.runQuery(csql);
|
|
158
|
+
if (result && Array.isArray(result) && result.length > 0) {
|
|
159
|
+
return parseInt(result[0]["count"], 10);
|
|
160
|
+
}
|
|
161
|
+
return 0;
|
|
162
|
+
}
|
|
163
|
+
async getCursor() {
|
|
164
|
+
return await this.connection?.runCursor(this.toSql());
|
|
165
|
+
}
|
|
166
|
+
getConnection() {
|
|
167
|
+
return this.connection;
|
|
168
|
+
}
|
|
169
|
+
async insert(data) {
|
|
170
|
+
const csql = this.grammar.compileInsert(this, data);
|
|
171
|
+
return await this.connection?.runQuery(csql);
|
|
172
|
+
}
|
|
173
|
+
async insertGetId(data, options = { primaryKey: ["id"] }) {
|
|
174
|
+
const csql = this.grammar.compileInsertGetId(this, data, options);
|
|
175
|
+
return await this.connection?.runQuery(csql);
|
|
176
|
+
}
|
|
177
|
+
async update(data) {
|
|
178
|
+
const csql = this.grammar.compileUpdate(this, data);
|
|
179
|
+
return await this.connection?.runQuery(csql);
|
|
180
|
+
}
|
|
181
|
+
async upsert(data, uniqueColumns, updateColumns) {
|
|
182
|
+
const csql = this.grammar.compileUpsert(this, data, uniqueColumns, updateColumns);
|
|
183
|
+
return await this.connection?.runQuery(csql);
|
|
184
|
+
}
|
|
185
|
+
async delete() {
|
|
186
|
+
const csql = this.grammar.compileDelete(this);
|
|
187
|
+
return await this.connection?.runQuery(csql);
|
|
188
|
+
}
|
|
189
|
+
innerJoin(table, condtions) {
|
|
190
|
+
this.parts.join.push({ type: "inner", table, conditions: condtions });
|
|
191
|
+
return this;
|
|
192
|
+
}
|
|
193
|
+
leftJoin(table, condtions) {
|
|
194
|
+
this.parts.join.push({ type: "left", table, conditions: condtions });
|
|
195
|
+
return this;
|
|
196
|
+
}
|
|
197
|
+
rightJoin(table, condtions) {
|
|
198
|
+
this.parts.join.push({ type: "right", table, conditions: condtions });
|
|
199
|
+
return this;
|
|
200
|
+
}
|
|
201
|
+
fullJoin(table, condtions) {
|
|
202
|
+
this.parts.join.push({ type: "full", table, conditions: condtions });
|
|
203
|
+
return this;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// src/QueryGrammar.mts
|
|
208
|
+
function toUpperFirst(str) {
|
|
209
|
+
return str.substring(0, 1).toUpperCase() + str.substring(1);
|
|
210
|
+
}
|
|
211
|
+
__name(toUpperFirst, "toUpperFirst");
|
|
212
|
+
var QueryGrammar = class {
|
|
213
|
+
static {
|
|
214
|
+
__name(this, "QueryGrammar");
|
|
215
|
+
}
|
|
216
|
+
sqlParts = [
|
|
217
|
+
"select",
|
|
218
|
+
"table",
|
|
219
|
+
"join",
|
|
220
|
+
"where",
|
|
221
|
+
"groupBy",
|
|
222
|
+
"having",
|
|
223
|
+
"orderBy",
|
|
224
|
+
"limit",
|
|
225
|
+
"offset"
|
|
226
|
+
];
|
|
227
|
+
toSql(query) {
|
|
228
|
+
let sql = "";
|
|
229
|
+
let bindings = [];
|
|
230
|
+
for (const part of this.sqlParts) {
|
|
231
|
+
const funcName = "compile" + toUpperFirst(part);
|
|
232
|
+
const r = this[funcName](query.parts[part]);
|
|
233
|
+
if (!sql) {
|
|
234
|
+
sql = r.sql;
|
|
235
|
+
} else if (r.sql) {
|
|
236
|
+
sql += " " + r.sql;
|
|
237
|
+
}
|
|
238
|
+
bindings = [...bindings, ...r.bindings];
|
|
239
|
+
}
|
|
240
|
+
return { sql, bindings };
|
|
241
|
+
}
|
|
242
|
+
compileCount(query) {
|
|
243
|
+
let sql = "";
|
|
244
|
+
let bindings = [];
|
|
245
|
+
for (const part of this.sqlParts) {
|
|
246
|
+
let parts = query.parts[part];
|
|
247
|
+
if (part === "select") {
|
|
248
|
+
parts = ["count(*) as count"];
|
|
249
|
+
}
|
|
250
|
+
const funcName = "compile" + toUpperFirst(part);
|
|
251
|
+
const r = this[funcName](parts);
|
|
252
|
+
if (!sql) {
|
|
253
|
+
sql = r.sql;
|
|
254
|
+
} else if (r.sql) {
|
|
255
|
+
sql += " " + r.sql;
|
|
256
|
+
}
|
|
257
|
+
bindings = [...bindings, ...r.bindings];
|
|
258
|
+
}
|
|
259
|
+
return { sql, bindings };
|
|
260
|
+
}
|
|
261
|
+
compileSelect(selects) {
|
|
262
|
+
const rc = selects.map((v) => {
|
|
263
|
+
return v;
|
|
264
|
+
}).join(", ");
|
|
265
|
+
return { sql: "select " + rc, bindings: [] };
|
|
266
|
+
}
|
|
267
|
+
compileTable(tableName) {
|
|
268
|
+
let rc = "";
|
|
269
|
+
if (tableName.length) {
|
|
270
|
+
rc = "from " + tableName;
|
|
271
|
+
}
|
|
272
|
+
return { sql: rc, bindings: [] };
|
|
273
|
+
}
|
|
274
|
+
compileJoin(joins) {
|
|
275
|
+
let sql = "";
|
|
276
|
+
let bindings = [];
|
|
277
|
+
for (const j of joins) {
|
|
278
|
+
sql += " " + j.type + " join " + j.table + " on ";
|
|
279
|
+
const where = this.compileWhere(j.conditions);
|
|
280
|
+
if (where.sql.startsWith("where ")) {
|
|
281
|
+
where.sql = where.sql.substring("where ".length);
|
|
282
|
+
}
|
|
283
|
+
sql += "(";
|
|
284
|
+
sql += where.sql;
|
|
285
|
+
sql += ")";
|
|
286
|
+
bindings = [...bindings, ...where.bindings];
|
|
287
|
+
}
|
|
288
|
+
return { sql, bindings };
|
|
289
|
+
}
|
|
290
|
+
compileWhere(wheres) {
|
|
291
|
+
let sql = "";
|
|
292
|
+
let bindings = [];
|
|
293
|
+
for (const w of wheres) {
|
|
294
|
+
sql += " " + w.joinCondition + " ";
|
|
295
|
+
if (w.negateCondition) {
|
|
296
|
+
sql += "not ";
|
|
297
|
+
}
|
|
298
|
+
const funcName = "compileWhere" + toUpperFirst(w.type);
|
|
299
|
+
const wh = this[funcName](w);
|
|
300
|
+
sql += wh.sql;
|
|
301
|
+
bindings = [...bindings, ...wh.bindings];
|
|
302
|
+
}
|
|
303
|
+
if (sql.startsWith(" and ")) {
|
|
304
|
+
sql = "where " + sql.substring(" and ".length);
|
|
305
|
+
} else if (sql.startsWith(" or ")) {
|
|
306
|
+
sql = "where " + sql.substring(" or ".length);
|
|
307
|
+
}
|
|
308
|
+
return { sql, bindings };
|
|
309
|
+
}
|
|
310
|
+
compileWhereOperation(w) {
|
|
311
|
+
if (w.operation.toLowerCase() === "in" && Array.isArray(w.value)) {
|
|
312
|
+
return {
|
|
313
|
+
sql: `${w.column} = ANY(${this.getVariablePlaceholder()})`,
|
|
314
|
+
bindings: [w.value]
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
return {
|
|
318
|
+
sql: `${w.column} ${w.operation} ${this.getVariablePlaceholder()}`,
|
|
319
|
+
bindings: [w.value]
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
compileWhereOperationColumn(w) {
|
|
323
|
+
return {
|
|
324
|
+
sql: `${w.column1} ${w.operation} ${w.column2}`,
|
|
325
|
+
bindings: []
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
compileOrderBy(orderBy) {
|
|
329
|
+
let rc = "";
|
|
330
|
+
if (orderBy.length) {
|
|
331
|
+
rc = "order by " + orderBy.join(", ");
|
|
332
|
+
}
|
|
333
|
+
return { sql: rc, bindings: [] };
|
|
334
|
+
}
|
|
335
|
+
compileLimit(limit) {
|
|
336
|
+
let rc = "";
|
|
337
|
+
if (limit !== null) {
|
|
338
|
+
rc = "limit " + limit;
|
|
339
|
+
}
|
|
340
|
+
return { sql: rc, bindings: [] };
|
|
341
|
+
}
|
|
342
|
+
compileOffset(offset) {
|
|
343
|
+
let rc = "";
|
|
344
|
+
if (offset !== null) {
|
|
345
|
+
rc = "offset " + offset;
|
|
346
|
+
}
|
|
347
|
+
return { sql: rc, bindings: [] };
|
|
348
|
+
}
|
|
349
|
+
compileWhereNull(w) {
|
|
350
|
+
return {
|
|
351
|
+
sql: `${w.column} is null`,
|
|
352
|
+
bindings: []
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
compileInsert(query, data) {
|
|
356
|
+
let sql = "insert into " + query.parts.table + " (";
|
|
357
|
+
const columns = [];
|
|
358
|
+
const bindings = [];
|
|
359
|
+
const values = [];
|
|
360
|
+
for (const [k, v] of Object.entries(data)) {
|
|
361
|
+
columns.push(k);
|
|
362
|
+
bindings.push(v);
|
|
363
|
+
values.push(this.getVariablePlaceholder());
|
|
364
|
+
}
|
|
365
|
+
sql += columns.join(", ") + ") values (" + values + ")";
|
|
366
|
+
return { sql, bindings };
|
|
367
|
+
}
|
|
368
|
+
compileUpdate(query, data) {
|
|
369
|
+
let sql = "update " + query.parts.table + " set ";
|
|
370
|
+
const bindings = [];
|
|
371
|
+
const setParts = [];
|
|
372
|
+
for (const [k, v] of Object.entries(data)) {
|
|
373
|
+
setParts.push(`${k} = ${this.getVariablePlaceholder()}`);
|
|
374
|
+
bindings.push(v);
|
|
375
|
+
}
|
|
376
|
+
sql += setParts.join(", ");
|
|
377
|
+
const where_csql = this.compileWhere(query.parts.where);
|
|
378
|
+
sql += " " + where_csql.sql;
|
|
379
|
+
bindings.push(...where_csql.bindings);
|
|
380
|
+
return { sql, bindings };
|
|
381
|
+
}
|
|
382
|
+
compileDelete(query) {
|
|
383
|
+
let sql = "delete from " + query.parts.table;
|
|
384
|
+
const where_csql = this.compileWhere(query.parts.where);
|
|
385
|
+
sql += " " + where_csql.sql;
|
|
386
|
+
return { sql, bindings: where_csql.bindings };
|
|
387
|
+
}
|
|
388
|
+
compileUpsert(query, data, conflictFields, updateFields) {
|
|
389
|
+
let sql = "insert into " + query.parts.table + " (";
|
|
390
|
+
const columns = [];
|
|
391
|
+
const bindings = [];
|
|
392
|
+
const values = [];
|
|
393
|
+
for (const [k, v] of Object.entries(data)) {
|
|
394
|
+
columns.push(k);
|
|
395
|
+
bindings.push(v);
|
|
396
|
+
values.push(this.getVariablePlaceholder());
|
|
397
|
+
}
|
|
398
|
+
sql += columns.join(", ") + ") values (" + values + ")";
|
|
399
|
+
sql += " on conflict (" + conflictFields.join(", ") + ") do update set ";
|
|
400
|
+
const setParts = [];
|
|
401
|
+
for (const f of updateFields) {
|
|
402
|
+
setParts.push(`${f} = excluded.${f}`);
|
|
403
|
+
}
|
|
404
|
+
sql += setParts.join(", ");
|
|
405
|
+
const where_csql = this.compileWhere(query.parts.where);
|
|
406
|
+
sql += " " + where_csql.sql;
|
|
407
|
+
bindings.push(...where_csql.bindings);
|
|
408
|
+
return { sql, bindings };
|
|
409
|
+
}
|
|
410
|
+
compileGroupBy(groupBy) {
|
|
411
|
+
let rc = "";
|
|
412
|
+
if (groupBy.length) {
|
|
413
|
+
rc = "group by " + groupBy.join(", ");
|
|
414
|
+
}
|
|
415
|
+
return { sql: rc, bindings: [] };
|
|
416
|
+
}
|
|
417
|
+
compileHaving(having) {
|
|
418
|
+
let sql = "";
|
|
419
|
+
let bindings = [];
|
|
420
|
+
for (const w of having) {
|
|
421
|
+
sql += " " + w.joinCondition + " ";
|
|
422
|
+
if (w.negateCondition) {
|
|
423
|
+
sql += "not ";
|
|
424
|
+
}
|
|
425
|
+
const funcName = "compileHaving" + toUpperFirst(w.type);
|
|
426
|
+
const wh = this[funcName](w);
|
|
427
|
+
sql += wh.sql;
|
|
428
|
+
bindings = [...bindings, ...wh.bindings];
|
|
429
|
+
}
|
|
430
|
+
if (sql.startsWith(" and ")) {
|
|
431
|
+
sql = "having " + sql.substring(" and ".length);
|
|
432
|
+
} else if (sql.startsWith(" or ")) {
|
|
433
|
+
sql = "having " + sql.substring(" or ".length);
|
|
434
|
+
}
|
|
435
|
+
return { sql, bindings };
|
|
436
|
+
}
|
|
437
|
+
compileHavingOperation(w) {
|
|
438
|
+
return {
|
|
439
|
+
sql: `${w.column} ${w.operation} ${this.getVariablePlaceholder()}`,
|
|
440
|
+
bindings: [w.value]
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
compileHavingRaw(w) {
|
|
444
|
+
return {
|
|
445
|
+
sql: w.sql,
|
|
446
|
+
bindings: w.bindings
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
// src/databases/postgresql/PostgresqlQueryGrammar.mts
|
|
452
|
+
var PostgresqlQueryGrammar = class extends QueryGrammar {
|
|
453
|
+
static {
|
|
454
|
+
__name(this, "PostgresqlQueryGrammar");
|
|
455
|
+
}
|
|
456
|
+
parameterIndex;
|
|
457
|
+
constructor() {
|
|
458
|
+
super();
|
|
459
|
+
this.parameterIndex = 1;
|
|
460
|
+
}
|
|
461
|
+
toSql(query) {
|
|
462
|
+
this.parameterIndex = 1;
|
|
463
|
+
return super.toSql(query);
|
|
464
|
+
}
|
|
465
|
+
getVariablePlaceholder() {
|
|
466
|
+
return "$" + this.parameterIndex++;
|
|
467
|
+
}
|
|
468
|
+
compileInsert(query, data) {
|
|
469
|
+
this.parameterIndex = 1;
|
|
470
|
+
return super.compileInsert(query, data);
|
|
471
|
+
}
|
|
472
|
+
compileInsertGetId(query, data, options = { primaryKey: ["id"] }) {
|
|
473
|
+
this.parameterIndex = 1;
|
|
474
|
+
const rc = super.compileInsert(query, data);
|
|
475
|
+
rc.sql += ` RETURNING ${options.primaryKey.join(", ")}`;
|
|
476
|
+
return rc;
|
|
477
|
+
}
|
|
478
|
+
compileUpdate(query, data) {
|
|
479
|
+
this.parameterIndex = 1;
|
|
480
|
+
return super.compileUpdate(query, data);
|
|
481
|
+
}
|
|
482
|
+
compileDelete(query) {
|
|
483
|
+
this.parameterIndex = 1;
|
|
484
|
+
return super.compileDelete(query);
|
|
485
|
+
}
|
|
486
|
+
compileUpsert(query, data, conflictFields, updateFields) {
|
|
487
|
+
this.parameterIndex = 1;
|
|
488
|
+
return super.compileUpsert(query, data, conflictFields, updateFields);
|
|
489
|
+
}
|
|
490
|
+
compileCount(query) {
|
|
491
|
+
this.parameterIndex = 1;
|
|
492
|
+
return super.compileCount(query);
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
// src/Expression.mts
|
|
497
|
+
var Expression = class {
|
|
498
|
+
constructor(sql = "", bindings = []) {
|
|
499
|
+
this.sql = sql;
|
|
500
|
+
this.bindings = bindings;
|
|
501
|
+
}
|
|
502
|
+
static {
|
|
503
|
+
__name(this, "Expression");
|
|
504
|
+
}
|
|
505
|
+
toCompiledSql() {
|
|
506
|
+
return { sql: this.sql, bindings: this.bindings };
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
// src/Blueprint.mts
|
|
511
|
+
var Column = class {
|
|
512
|
+
static {
|
|
513
|
+
__name(this, "Column");
|
|
514
|
+
}
|
|
515
|
+
columnName = "";
|
|
516
|
+
properties = {
|
|
517
|
+
type: "string",
|
|
518
|
+
length: 255,
|
|
519
|
+
nullable: false,
|
|
520
|
+
unique: false,
|
|
521
|
+
default: null
|
|
522
|
+
};
|
|
523
|
+
constructor(columnName, type) {
|
|
524
|
+
this.columnName = columnName;
|
|
525
|
+
this.properties.type = type;
|
|
526
|
+
}
|
|
527
|
+
length(length) {
|
|
528
|
+
this.properties.length = length;
|
|
529
|
+
return this;
|
|
530
|
+
}
|
|
531
|
+
nullable(nullable = true) {
|
|
532
|
+
this.properties.nullable = nullable;
|
|
533
|
+
return this;
|
|
534
|
+
}
|
|
535
|
+
unique(unique = true) {
|
|
536
|
+
this.properties.unique = unique;
|
|
537
|
+
return this;
|
|
538
|
+
}
|
|
539
|
+
default(value) {
|
|
540
|
+
this.properties.default = value;
|
|
541
|
+
return this;
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
var ForeignKeyConstraint = class {
|
|
545
|
+
static {
|
|
546
|
+
__name(this, "ForeignKeyConstraint");
|
|
547
|
+
}
|
|
548
|
+
column;
|
|
549
|
+
reference_table;
|
|
550
|
+
onUpdateAction = "restrict";
|
|
551
|
+
onDeleteAction = "restrict";
|
|
552
|
+
constructor(column) {
|
|
553
|
+
this.column = column;
|
|
554
|
+
this.reference_table = { table: "", column: "" };
|
|
555
|
+
}
|
|
556
|
+
on(table) {
|
|
557
|
+
this.reference_table.table = table;
|
|
558
|
+
return this;
|
|
559
|
+
}
|
|
560
|
+
references(column) {
|
|
561
|
+
this.reference_table.column = column;
|
|
562
|
+
return this;
|
|
563
|
+
}
|
|
564
|
+
onDelete(action) {
|
|
565
|
+
this.onDeleteAction = action;
|
|
566
|
+
return this;
|
|
567
|
+
}
|
|
568
|
+
onUpdate(action) {
|
|
569
|
+
this.onUpdateAction = action;
|
|
570
|
+
return this;
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
var Blueprint = class {
|
|
574
|
+
static {
|
|
575
|
+
__name(this, "Blueprint");
|
|
576
|
+
}
|
|
577
|
+
tableName = "";
|
|
578
|
+
columns = [];
|
|
579
|
+
drop_coumns = [];
|
|
580
|
+
foreignKeys = [];
|
|
581
|
+
existingTable = false;
|
|
582
|
+
primaryKeys = [];
|
|
583
|
+
constructor() {
|
|
584
|
+
}
|
|
585
|
+
setTableName(tableName, existingTable = false) {
|
|
586
|
+
this.tableName = tableName;
|
|
587
|
+
this.existingTable = existingTable;
|
|
588
|
+
}
|
|
589
|
+
Boolean(columnName) {
|
|
590
|
+
const rc = new Column(columnName, "boolean");
|
|
591
|
+
this.columns.push(rc);
|
|
592
|
+
return rc;
|
|
593
|
+
}
|
|
594
|
+
char(columnName) {
|
|
595
|
+
const rc = new Column(columnName, "char");
|
|
596
|
+
this.columns.push(rc);
|
|
597
|
+
return rc;
|
|
598
|
+
}
|
|
599
|
+
string(columnName, length = 255) {
|
|
600
|
+
const rc = new Column(columnName, "string");
|
|
601
|
+
rc.length(length);
|
|
602
|
+
this.columns.push(rc);
|
|
603
|
+
return rc;
|
|
604
|
+
}
|
|
605
|
+
text(columnName) {
|
|
606
|
+
const rc = new Column(columnName, "text");
|
|
607
|
+
this.columns.push(rc);
|
|
608
|
+
return rc;
|
|
609
|
+
}
|
|
610
|
+
integer(columnName) {
|
|
611
|
+
const rc = new Column(columnName, "integer");
|
|
612
|
+
this.columns.push(rc);
|
|
613
|
+
return rc;
|
|
614
|
+
}
|
|
615
|
+
float(columnName) {
|
|
616
|
+
const rc = new Column(columnName, "float");
|
|
617
|
+
this.columns.push(rc);
|
|
618
|
+
return rc;
|
|
619
|
+
}
|
|
620
|
+
double(columnName) {
|
|
621
|
+
const rc = new Column(columnName, "double");
|
|
622
|
+
this.columns.push(rc);
|
|
623
|
+
return rc;
|
|
624
|
+
}
|
|
625
|
+
id() {
|
|
626
|
+
const rc = new Column("id", "serial");
|
|
627
|
+
this.columns.push(rc);
|
|
628
|
+
this.primaryKeys.push("id");
|
|
629
|
+
return rc;
|
|
630
|
+
}
|
|
631
|
+
timestamps() {
|
|
632
|
+
this.columns.push(
|
|
633
|
+
new Column("created_at", "timestamp").default(new Expression("CURRENT_TIMESTAMP"))
|
|
634
|
+
);
|
|
635
|
+
this.columns.push(
|
|
636
|
+
new Column("updated_at", "timestamp").default(new Expression("CURRENT_TIMESTAMP"))
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
date(columnName) {
|
|
640
|
+
const rc = new Column(columnName, "date");
|
|
641
|
+
this.columns.push(rc);
|
|
642
|
+
return rc;
|
|
643
|
+
}
|
|
644
|
+
primary(keys) {
|
|
645
|
+
this.primaryKeys = keys;
|
|
646
|
+
}
|
|
647
|
+
foreign(columnName) {
|
|
648
|
+
const rc = new ForeignKeyConstraint(columnName);
|
|
649
|
+
this.foreignKeys.push(rc);
|
|
650
|
+
return rc;
|
|
651
|
+
}
|
|
652
|
+
dropColumn(columnName) {
|
|
653
|
+
this.drop_coumns.push(columnName);
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
// src/SchemaGrammar.mts
|
|
658
|
+
var SchemaGrammar = class {
|
|
659
|
+
static {
|
|
660
|
+
__name(this, "SchemaGrammar");
|
|
661
|
+
}
|
|
662
|
+
toSql(blueprint) {
|
|
663
|
+
if (!blueprint.existingTable) {
|
|
664
|
+
return this.compileCreateTable(blueprint).sql;
|
|
665
|
+
} else if (blueprint.existingTable) {
|
|
666
|
+
return this.compileAlterTable(blueprint).sql;
|
|
667
|
+
}
|
|
668
|
+
throw new Error("bad blueprint to compile: " + blueprint.tableName);
|
|
669
|
+
}
|
|
670
|
+
compileCreateTable(blueprint) {
|
|
671
|
+
let sql = "create table " + blueprint.tableName + " (";
|
|
672
|
+
const columns = blueprint.columns.map((v) => {
|
|
673
|
+
return this.compileColumn(v);
|
|
674
|
+
}).join(", ");
|
|
675
|
+
const primaryKeys = this.compilePrimaryKeys(blueprint.primaryKeys);
|
|
676
|
+
let foreignKeys = [];
|
|
677
|
+
if (blueprint.foreignKeys.length > 0) {
|
|
678
|
+
foreignKeys = blueprint.foreignKeys.map((v) => {
|
|
679
|
+
return this.compileForeignKey(v);
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
sql += [columns, primaryKeys, ...foreignKeys].join(",") + ")";
|
|
683
|
+
return { sql, bindings: [] };
|
|
684
|
+
}
|
|
685
|
+
compileAlterTable(blueprint) {
|
|
686
|
+
let sql = ["alter table " + blueprint.tableName];
|
|
687
|
+
const add_columns = blueprint.columns.map((v) => {
|
|
688
|
+
return "add column " + this.compileColumn(v);
|
|
689
|
+
});
|
|
690
|
+
const drop_columns = blueprint.drop_coumns.map((v) => {
|
|
691
|
+
return "drop column " + v;
|
|
692
|
+
});
|
|
693
|
+
sql = sql.concat([[...add_columns, ...drop_columns].join(", ")]);
|
|
694
|
+
return { sql: sql.join(" "), bindings: [] };
|
|
695
|
+
}
|
|
696
|
+
compileColumn(column) {
|
|
697
|
+
const rc = [`${column.columnName}`];
|
|
698
|
+
if (column.properties.type === "string") {
|
|
699
|
+
rc.push("varchar(" + column.properties.length + ")");
|
|
700
|
+
} else if (column.properties.type === "char") {
|
|
701
|
+
rc.push("char");
|
|
702
|
+
} else if (column.properties.type === "boolean") {
|
|
703
|
+
rc.push("boolean");
|
|
704
|
+
} else if (column.properties.type === "integer") {
|
|
705
|
+
rc.push("integer");
|
|
706
|
+
} else if (column.properties.type === "text") {
|
|
707
|
+
rc.push("text");
|
|
708
|
+
} else if (column.properties.type === "timestamp") {
|
|
709
|
+
rc.push("timestamp");
|
|
710
|
+
} else if (column.properties.type === "serial") {
|
|
711
|
+
rc.push("serial");
|
|
712
|
+
} else if (column.properties.type === "float") {
|
|
713
|
+
rc.push("float");
|
|
714
|
+
} else if (column.properties.type === "double") {
|
|
715
|
+
rc.push("double precision");
|
|
716
|
+
} else if (column.properties.type === "date") {
|
|
717
|
+
rc.push("date");
|
|
718
|
+
} else {
|
|
719
|
+
throw new Error("Unknown column type: " + column.properties.type);
|
|
720
|
+
}
|
|
721
|
+
if (column.properties.nullable) {
|
|
722
|
+
rc.push("null");
|
|
723
|
+
} else {
|
|
724
|
+
rc.push("not null");
|
|
725
|
+
}
|
|
726
|
+
if (column.properties.unique) {
|
|
727
|
+
rc.push("unique");
|
|
728
|
+
}
|
|
729
|
+
if (column.properties.default !== null) {
|
|
730
|
+
rc.push("default " + this.escape(column.properties.default));
|
|
731
|
+
}
|
|
732
|
+
return rc.join(" ");
|
|
733
|
+
}
|
|
734
|
+
escape(value) {
|
|
735
|
+
if (value === null || value === void 0) {
|
|
736
|
+
return "null";
|
|
737
|
+
}
|
|
738
|
+
if (typeof value === "number") {
|
|
739
|
+
return value.toString();
|
|
740
|
+
}
|
|
741
|
+
if (typeof value === "boolean") {
|
|
742
|
+
return value ? "true" : "false";
|
|
743
|
+
}
|
|
744
|
+
if (value instanceof Date) {
|
|
745
|
+
return "'" + value.toISOString() + "'";
|
|
746
|
+
}
|
|
747
|
+
if (value instanceof Expression) {
|
|
748
|
+
return value.toCompiledSql().sql;
|
|
749
|
+
}
|
|
750
|
+
if (Array.isArray(value)) {
|
|
751
|
+
return "'{" + value.join(",") + "}'";
|
|
752
|
+
}
|
|
753
|
+
return "'" + value.replace("'", "\\'") + "'";
|
|
754
|
+
}
|
|
755
|
+
compilePrimaryKeys(primaryKeys) {
|
|
756
|
+
if (!primaryKeys.length) {
|
|
757
|
+
return "";
|
|
758
|
+
}
|
|
759
|
+
return "primary key (" + primaryKeys.join(", ") + ")";
|
|
760
|
+
}
|
|
761
|
+
compileTables(schema = void 0) {
|
|
762
|
+
return {
|
|
763
|
+
sql: "select c.relname as name, n.nspname as schema, pg_total_relation_size(c.oid) as size, obj_description(c.oid, 'pg_class') as comment from pg_class c, pg_namespace n where c.relkind in ('r', 'p') and n.oid = c.relnamespace and " + this.compileSchemaWhereClause(schema, "n.nspname") + " order by n.nspname, c.relname",
|
|
764
|
+
bindings: []
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
compileTableExists(tableName, schema = "") {
|
|
768
|
+
return {
|
|
769
|
+
sql: "select exists (select 1 from pg_class c, pg_namespace n where n.nspname = " + (schema ? this.escape(schema) : "current_schema()") + " and c.relname = $1 and c.relkind in ('r', 'p') and n.oid = c.relnamespace)",
|
|
770
|
+
bindings: [tableName]
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
compileDropTable(tableName) {
|
|
774
|
+
return { sql: `drop table ${this.doubleQuoteString(tableName)}`, bindings: [] };
|
|
775
|
+
}
|
|
776
|
+
compileSchemaWhereClause(schema, column) {
|
|
777
|
+
if (Array.isArray(schema) && schema.length > 0) {
|
|
778
|
+
return `${column} in (${this.quoteString(schema)})`;
|
|
779
|
+
} else if (schema && typeof schema === "string") {
|
|
780
|
+
return `${column} = ${this.quoteString(schema)}`;
|
|
781
|
+
} else {
|
|
782
|
+
return `${column} <> 'information_schema' and ${column} not like 'pg\\_%'`;
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
quoteString(value) {
|
|
786
|
+
if (Array.isArray(value)) {
|
|
787
|
+
return value.map((v) => `'${v.replace(/'/g, "\\'")}'`).join(", ");
|
|
788
|
+
}
|
|
789
|
+
return `'${value.replace(/'/g, "\\'")}'`;
|
|
790
|
+
}
|
|
791
|
+
doubleQuoteString(value) {
|
|
792
|
+
if (Array.isArray(value)) {
|
|
793
|
+
return value.map((v) => this.doubleQuoteString(v)).join(", ");
|
|
794
|
+
}
|
|
795
|
+
return `"${value.replace(/"/g, '\\"')}"`;
|
|
796
|
+
}
|
|
797
|
+
compileForeignKey(foreignKey) {
|
|
798
|
+
const rc = [`FOREIGN KEY (${foreignKey.column})`];
|
|
799
|
+
rc.push(`references ${foreignKey.reference_table.table}(${foreignKey.reference_table.column})`);
|
|
800
|
+
if (foreignKey.onDeleteAction) {
|
|
801
|
+
rc.push(`on delete ${foreignKey.onDeleteAction}`);
|
|
802
|
+
}
|
|
803
|
+
if (foreignKey.onUpdateAction) {
|
|
804
|
+
rc.push(`on update ${foreignKey.onUpdateAction}`);
|
|
805
|
+
}
|
|
806
|
+
return rc.join(" ");
|
|
807
|
+
}
|
|
808
|
+
};
|
|
809
|
+
|
|
810
|
+
// src/Schema.mts
|
|
811
|
+
var Schema = class {
|
|
812
|
+
constructor(connection, grammar) {
|
|
813
|
+
this.connection = connection;
|
|
814
|
+
this.grammar = grammar;
|
|
815
|
+
}
|
|
816
|
+
static {
|
|
817
|
+
__name(this, "Schema");
|
|
818
|
+
}
|
|
819
|
+
async createTable(tableName, structMethod) {
|
|
820
|
+
const blueprint = new Blueprint();
|
|
821
|
+
blueprint.setTableName(tableName, false);
|
|
822
|
+
structMethod(blueprint);
|
|
823
|
+
const grammar = new SchemaGrammar();
|
|
824
|
+
const sql = grammar.toSql(blueprint);
|
|
825
|
+
await this.connection?.runQuery({ sql, bindings: [] });
|
|
826
|
+
}
|
|
827
|
+
async alterTable(tableName, structMethod) {
|
|
828
|
+
const blueprint = new Blueprint();
|
|
829
|
+
blueprint.setTableName(tableName, true);
|
|
830
|
+
structMethod(blueprint);
|
|
831
|
+
const grammar = new SchemaGrammar();
|
|
832
|
+
const sql = grammar.toSql(blueprint);
|
|
833
|
+
await this.connection?.runQuery({ sql, bindings: [] });
|
|
834
|
+
}
|
|
835
|
+
async dropTable(tableName) {
|
|
836
|
+
const grammar = new SchemaGrammar();
|
|
837
|
+
await this.connection?.runQuery(grammar.compileDropTable(tableName));
|
|
838
|
+
}
|
|
839
|
+
async tables() {
|
|
840
|
+
const grammar = new SchemaGrammar();
|
|
841
|
+
return await this.connection?.runQuery(grammar.compileTables());
|
|
842
|
+
}
|
|
843
|
+
async tableExists(table_name) {
|
|
844
|
+
const grammar = new SchemaGrammar();
|
|
845
|
+
return (await this.connection?.runQuery(grammar.compileTableExists(table_name)))[0]["exists"];
|
|
846
|
+
}
|
|
847
|
+
async dropTableIfExists(tableName) {
|
|
848
|
+
if (await this.tableExists(tableName)) {
|
|
849
|
+
await this.dropTable(tableName);
|
|
850
|
+
}
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
};
|
|
854
|
+
|
|
855
|
+
// src/databases/postgresql/PostgresqlSchemaGrammar.mts
|
|
856
|
+
var PostgresqlSchemaGrammar = class extends SchemaGrammar {
|
|
857
|
+
static {
|
|
858
|
+
__name(this, "PostgresqlSchemaGrammar");
|
|
859
|
+
}
|
|
860
|
+
};
|
|
861
|
+
|
|
862
|
+
// src/databases/postgresql/PostgresqlConnection.mts
|
|
863
|
+
var import_pg_cursor = __toESM(require("pg-cursor"), 1);
|
|
864
|
+
var PostgresqlConnection = class _PostgresqlConnection extends Connection {
|
|
865
|
+
static {
|
|
866
|
+
__name(this, "PostgresqlConnection");
|
|
867
|
+
}
|
|
868
|
+
connection;
|
|
869
|
+
static pool;
|
|
870
|
+
static defaults = {
|
|
871
|
+
port: 5432,
|
|
872
|
+
ssl: false,
|
|
873
|
+
max: 20,
|
|
874
|
+
idleTimeoutMillis: 1,
|
|
875
|
+
// wait X milli seconds before closing an idle/released connection
|
|
876
|
+
connectionTimeoutMillis: 3e4,
|
|
877
|
+
// wait up to 30 seconds to obtain a new connection
|
|
878
|
+
maxUses: 7500
|
|
879
|
+
};
|
|
880
|
+
constructor(params) {
|
|
881
|
+
super();
|
|
882
|
+
if (!_PostgresqlConnection.pool) {
|
|
883
|
+
_PostgresqlConnection.pool = new import_pg.Pool({ ..._PostgresqlConnection.defaults, ...params });
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
async connect() {
|
|
887
|
+
this.connection = await _PostgresqlConnection.pool.connect();
|
|
888
|
+
return true;
|
|
889
|
+
}
|
|
890
|
+
async runQuery(sql) {
|
|
891
|
+
const result = await this.connection?.query(sql.sql, sql.bindings);
|
|
892
|
+
return result?.rows;
|
|
893
|
+
}
|
|
894
|
+
async runCursor(sql) {
|
|
895
|
+
return this.connection?.query(new import_pg_cursor.default(sql.sql, sql.bindings));
|
|
896
|
+
}
|
|
897
|
+
async disconnect() {
|
|
898
|
+
await this.connection?.release();
|
|
899
|
+
return true;
|
|
900
|
+
}
|
|
901
|
+
getQuery() {
|
|
902
|
+
return new Query(this, new PostgresqlQueryGrammar());
|
|
903
|
+
}
|
|
904
|
+
getSchema() {
|
|
905
|
+
return new Schema(this, new PostgresqlSchemaGrammar());
|
|
906
|
+
}
|
|
907
|
+
getQueryGrammar() {
|
|
908
|
+
return new PostgresqlQueryGrammar();
|
|
909
|
+
}
|
|
910
|
+
getSchemaGrammar() {
|
|
911
|
+
return new PostgresqlSchemaGrammar();
|
|
912
|
+
}
|
|
913
|
+
async beginTransaction() {
|
|
914
|
+
if (!this.connection) {
|
|
915
|
+
throw new Error("No active connection to begin a transaction.");
|
|
916
|
+
}
|
|
917
|
+
await this.connection.query("BEGIN");
|
|
918
|
+
}
|
|
919
|
+
async commit() {
|
|
920
|
+
if (!this.connection) {
|
|
921
|
+
throw new Error("No active connection to commit a transaction.");
|
|
922
|
+
}
|
|
923
|
+
await this.connection.query("COMMIT");
|
|
924
|
+
}
|
|
925
|
+
async rollback() {
|
|
926
|
+
if (!this.connection) {
|
|
927
|
+
throw new Error("No active connection to rollback a transaction.");
|
|
928
|
+
}
|
|
929
|
+
await this.connection.query("ROLLBACK");
|
|
930
|
+
}
|
|
931
|
+
static async destroy() {
|
|
932
|
+
_PostgresqlConnection.pool.end();
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
};
|
|
936
|
+
|
|
937
|
+
// src/Migration.mts
|
|
938
|
+
var Migration = class {
|
|
939
|
+
static {
|
|
940
|
+
__name(this, "Migration");
|
|
941
|
+
}
|
|
942
|
+
};
|
|
27
943
|
// Annotate the CommonJS export names for ESM import in node:
|
|
28
944
|
0 && (module.exports = {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
945
|
+
Blueprint,
|
|
946
|
+
Column,
|
|
947
|
+
Connection,
|
|
948
|
+
ForeignKeyConstraint,
|
|
949
|
+
Migration,
|
|
950
|
+
PostgresqlConnection,
|
|
951
|
+
PostgresqlQueryGrammar,
|
|
952
|
+
PostgresqlSchemaGrammar,
|
|
953
|
+
Query,
|
|
954
|
+
QueryGrammar,
|
|
955
|
+
Schema,
|
|
956
|
+
SchemaGrammar
|
|
38
957
|
});
|
|
39
958
|
//# sourceMappingURL=index.js.map
|