aboutsummaryrefslogtreecommitdiff
path: root/query-executor.js
blob: 7bd836dece4f4ff436b718492d6592750f1ae347 (plain)
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
"use strict";

const { ipcRenderer } = require("electron");
const { Pool } = require("pg");

const connectionPool = new Pool({
  user: "postgres",
  host: "localhost",
  database: "postgres",
  password: "",
  port: 5432
});

ipcRenderer.on("queryExecutor.runQuery", (event, payload) => {

  connectionPool.query(payload, (err, res) => {

    console.log(err, res)

    ipcRenderer.send("queryExecutor.runQueryComplete", {
      "error": err,
      "result": res
    });

  });

});

ipcRenderer.on("queryExecutor.queryTableMetadata", (event, payload) => {

  let tableMetadata = {};

  let tableDataQuery = 
    "SELECT c.table_schema || '.' || c.table_name identifier, c.column_name " +
    "FROM information_schema.columns c " +
    "WHERE c.table_schema != 'pg_catalog'";

  connectionPool.query(tableDataQuery, (err, res) => {

    console.log(err, res)

    res.rows.forEach((row) => {
      if(tableMetadata.hasOwnProperty(row.identifier)) {
        tableMetadata[row.identifier].push(row.column_name);
      }
      else {
        tableMetadata[row.identifier] = [row.column_name];
      }
    })

    ipcRenderer.send("queryExecutor.queryTableMetadataComplete", {
      "error": err,
      "result": tableMetadata
    });

  });

});