-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres.photon.ts
More file actions
269 lines (243 loc) · 8.39 KB
/
postgres.photon.ts
File metadata and controls
269 lines (243 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* PostgreSQL - Powerful relational database
* @version 1.1.0
* @author Portel
* @license MIT
* @icon 🐘
* @tags postgresql, database, sql
* @dependencies pg@^8.11.0
*/
import pg from 'pg';
const { Pool } = pg;
interface QueryResult {
rows: any[];
rowCount: number;
fields: Array<{ name: string; dataTypeID: number }>;
}
export default class PostgreSQL {
private pool?: pg.Pool;
constructor(
private database: string,
private user: string,
private password: string,
private host: string = 'localhost',
private port: number = 5432,
private ssl: boolean = false
) {
if (!database || !user || !password) {
throw new Error('Database, user, and password are required');
}
}
async onInitialize() {
this.pool = new Pool({
host: this.host,
port: this.port,
database: this.database,
user: this.user,
password: this.password,
ssl: this.ssl ? { rejectUnauthorized: false } : false,
max: 10,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 5000,
});
const client = await this.pool.connect();
client.release();
}
async onShutdown() {
await this.pool?.end();
}
/**
* Execute a SQL query
* @param sql SQL query {@field textarea} {@example SELECT * FROM users WHERE active = $1}
* @param params Query parameters (use $1, $2, etc.)
* @format table
* @timeout 30s
* @retryable 2 1s
*/
async query(params: { sql: string; params?: any[] }): Promise<QueryResult> {
if (!this.pool) throw new Error('Database not initialized');
const result = await this.pool.query(params.sql, params.params);
return {
rows: result.rows,
rowCount: result.rowCount ?? 0,
fields: result.fields.map((f) => ({ name: f.name, dataTypeID: f.dataTypeID })),
};
}
/**
* Execute multiple statements in a transaction
* @param statements Array of SQL statements with optional parameters
* @timeout 60s
*/
async transaction(params: { statements: Array<{ sql: string; params?: any[] }> }) {
if (!this.pool) throw new Error('Database not initialized');
const client = await this.pool.connect();
try {
await client.query('BEGIN');
const results = [];
for (const stmt of params.statements) {
const result = await client.query(stmt.sql, stmt.params);
results.push({ rowCount: result.rowCount, rows: result.rows });
}
await client.query('COMMIT');
return results;
} catch (error) {
await client.query('ROLLBACK');
throw error;
} finally {
client.release();
}
}
/**
* List all tables in database
* @param schema Schema name (default: public)
* @autorun
* @format table
* @timeout 10s
* @cached 5m
*/
async tables(params?: { schema?: string }) {
if (!this.pool) throw new Error('Database not initialized');
const schema = params?.schema ?? 'public';
const result = await this.pool.query(
`SELECT table_name, table_type FROM information_schema.tables
WHERE table_schema = $1 ORDER BY table_name`,
[schema]
);
return result.rows.map((row) => ({ name: row.table_name, type: row.table_type }));
}
/**
* Get schema information for a table
* @param table Table name {@example users}
* @param schema Schema name (default: public)
* @format table
* @timeout 10s
* @cached 5m
*/
async describe(params: { table: string; schema?: string }) {
if (!this.pool) throw new Error('Database not initialized');
const schema = params.schema ?? 'public';
const result = await this.pool.query(
`SELECT column_name, data_type, character_maximum_length, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = $1 AND table_name = $2
ORDER BY ordinal_position`,
[schema, params.table]
);
if (result.rowCount === 0) throw new Error(`Table ${params.table} not found`);
return {
table: params.table,
schema,
columns: result.rows.map((row) => ({
name: row.column_name,
type: row.data_type,
maxLength: row.character_maximum_length,
nullable: row.is_nullable === 'YES',
default: row.column_default,
})),
};
}
/**
* List all indexes on a table
* @param table Table name {@example users}
* @param schema Schema name (default: public)
* @format table
* @timeout 10s
* @cached 5m
*/
async indexes(params: { table: string; schema?: string }) {
if (!this.pool) throw new Error('Database not initialized');
const schema = params.schema ?? 'public';
const result = await this.pool.query(
`SELECT i.relname AS index_name, a.attname AS column_name, ix.indisunique AS is_unique, ix.indisprimary AS is_primary
FROM pg_class t
JOIN pg_index ix ON t.oid = ix.indrelid
JOIN pg_class i ON i.oid = ix.indexrelid
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(ix.indkey)
JOIN pg_namespace n ON n.oid = t.relnamespace
WHERE t.relname = $1 AND n.nspname = $2
ORDER BY i.relname, a.attnum`,
[params.table, schema]
);
const indexMap = new Map<string, any>();
for (const row of result.rows) {
if (!indexMap.has(row.index_name)) {
indexMap.set(row.index_name, {
name: row.index_name,
columns: [],
unique: row.is_unique,
primary: row.is_primary,
});
}
indexMap.get(row.index_name).columns.push(row.column_name);
}
return Array.from(indexMap.values());
}
/**
* Insert a document
* @param table Table name {@example users}
* @param data Object with column names as keys
* @param returning Column names to return
*/
async insert(params: { table: string; data: Record<string, any>; returning?: string[] }) {
if (!this.pool) throw new Error('Database not initialized');
const columns = Object.keys(params.data);
const values = Object.values(params.data);
const placeholders = values.map((_, i) => `$${i + 1}`).join(', ');
const returning = params.returning ? `RETURNING ${params.returning.join(', ')}` : '';
const sql = `INSERT INTO ${params.table} (${columns.join(', ')}) VALUES (${placeholders}) ${returning}`;
const result = await this.pool.query(sql, values);
return { rowCount: result.rowCount, rows: result.rows };
}
/**
* Get database statistics
* @autorun
* @format card
* @timeout 10s
* @cached 1m
*/
async stats() {
if (!this.pool) throw new Error('Database not initialized');
const sizeResult = await this.pool.query(`SELECT pg_size_pretty(pg_database_size($1)) as size`, [this.database]);
const tableCountResult = await this.pool.query(
`SELECT count(*) as count FROM information_schema.tables WHERE table_schema = 'public'`
);
const connectionResult = await this.pool.query(`SELECT count(*) as count FROM pg_stat_activity WHERE datname = $1`, [
this.database,
]);
return {
database: this.database,
size: sizeResult.rows[0].size,
tableCount: parseInt(tableCountResult.rows[0].count),
activeConnections: parseInt(connectionResult.rows[0].count),
};
}
// ========== TESTS ==========
private isConnected(): boolean {
return this.pool !== undefined;
}
async testQuery() {
if (!this.isConnected()) return { skipped: true, reason: 'Postgres not connected' };
const result = await this.query({ sql: 'SELECT 1 as test' });
if (!result.rows || result.rows.length === 0) throw new Error('No rows returned');
if (result.rows[0].test !== 1) throw new Error('Wrong value');
return { passed: true };
}
async testQueryWithParams() {
if (!this.isConnected()) return { skipped: true, reason: 'Postgres not connected' };
const result = await this.query({ sql: 'SELECT $1::int + $2::int as sum', params: [2, 3] });
if (result.rows[0].sum !== 5) throw new Error('Wrong calculation');
return { passed: true };
}
async testTables() {
if (!this.isConnected()) return { skipped: true, reason: 'Postgres not connected' };
const result = await this.tables();
if (!Array.isArray(result)) throw new Error('Tables should be array');
return { passed: true };
}
async testStats() {
if (!this.isConnected()) return { skipped: true, reason: 'Postgres not connected' };
const result = await this.stats();
if (!result.database) throw new Error('Missing database name');
return { passed: true };
}
}