-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathredshift.ts
More file actions
147 lines (133 loc) · 4.23 KB
/
redshift.ts
File metadata and controls
147 lines (133 loc) · 4.23 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
/**
* Redshift driver using the `pg` package (wire-compatible with PostgreSQL).
* Uses svv_ system views for introspection.
*/
import type { ConnectionConfig, Connector, ConnectorResult, SchemaColumn } from "./types"
export async function connect(config: ConnectionConfig): Promise<Connector> {
let pg: any
try {
pg = await import("pg")
} catch {
throw new Error(
"Redshift driver not installed (uses pg). Run: npm install pg @types/pg",
)
}
const Pool = pg.default?.Pool ?? pg.Pool
let pool: any
const connector: Connector = {
async connect() {
const poolConfig: Record<string, unknown> = {}
if (config.connection_string) {
poolConfig.connectionString = config.connection_string
} else {
// Validate password type to prevent cryptic SASL/SCRAM errors
if (config.password != null && typeof config.password !== "string") {
throw new Error(
"Redshift password must be a string. Check your warehouse configuration.",
)
}
poolConfig.host = config.host ?? "127.0.0.1"
poolConfig.port = config.port ?? 5439 // Redshift default
poolConfig.database = config.database ?? "dev"
poolConfig.user = config.user
poolConfig.password = config.password
poolConfig.ssl = config.ssl ?? { rejectUnauthorized: false }
}
poolConfig.max = 5
poolConfig.idleTimeoutMillis = 30000
poolConfig.connectionTimeoutMillis = 10000
pool = new Pool(poolConfig)
},
async execute(sql: string, limit?: number, _binds?: any[]): Promise<ConnectorResult> {
const client = await pool.connect()
try {
const effectiveLimit = limit ?? 1000
let query = sql
const isSelectLike = /^\s*(SELECT|WITH|VALUES)\b/i.test(sql)
if (
isSelectLike &&
effectiveLimit &&
!/\bLIMIT\b/i.test(sql)
) {
query = `${sql.replace(/;\s*$/, "")} LIMIT ${effectiveLimit + 1}`
}
const result = await client.query(query)
const columns = result.fields?.map((f: any) => f.name) ?? []
const truncated = result.rows.length > effectiveLimit
const rows = truncated
? result.rows.slice(0, effectiveLimit)
: result.rows
return {
columns,
rows: rows.map((row: any) =>
columns.map((col: string) => row[col]),
),
row_count: rows.length,
truncated,
}
} finally {
client.release()
}
},
async listSchemas(): Promise<string[]> {
const result = await connector.execute(
`SELECT DISTINCT schemaname
FROM svv_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema', 'pg_internal')
ORDER BY schemaname`,
10000,
)
return result.rows.map((r) => r[0] as string)
},
async listTables(
schema: string,
): Promise<Array<{ name: string; type: string }>> {
const client = await pool.connect()
try {
const result = await client.query(
`SELECT tablename, tabletype
FROM svv_tables
WHERE schemaname = $1
ORDER BY tablename`,
[schema],
)
return result.rows.map((r: any) => ({
name: r.tablename as string,
type: String(r.tabletype).toLowerCase() === "view" ? "view" : "table",
}))
} finally {
client.release()
}
},
async describeTable(
schema: string,
table: string,
): Promise<SchemaColumn[]> {
const client = await pool.connect()
try {
const result = await client.query(
`SELECT columnname, data_type, is_nullable
FROM svv_columns
WHERE schemaname = $1
AND tablename = $2
ORDER BY ordinal_position`,
[schema, table],
)
return result.rows.map((r: any) => ({
name: r.columnname as string,
data_type: r.data_type as string,
nullable: String(r.is_nullable).toUpperCase() === "YES",
}))
} finally {
client.release()
}
},
async close() {
if (pool) {
await pool.end()
pool = null
}
},
}
return connector
}