1

I'm trying to translate my old mysql app to Postgresql, it was very difficult to connect to the server, but when i think it worked, this message appears on insomnia. The message I tried using different methods that i found on Google but they didn't work for me. I think that the problem is how i'm connecting to the server. I'm new using postgresql.

const { Pool, Client } = require("pg")

const config = require("../config")

const connection = new Pool({
  host: config.postgresql.host,
  user: config.postgresql.user,
  password: config.postgresql.password,
  database: config.postgresql.database,
  port: "5432",
  ssl: true
})

function list(table) {
  return new Promise((resolve, reject) => {
    connection.query(`SELECT * FROM ${table}`, (err, data) => {
      if (err) return reject(err)
      resolve(data)
    })
  })
}

function get(table, id) {
  return new Promise((resolve, reject) => {
    connection.query(`SELECT * FROM ${table} WHERE id=${id}`, (err, data) => {
      if (err) return reject(err)
      resolve(data)
    })
  })
}

function insert(table, data) {
  return new Promise((resolve, reject) => {
    connection.query(`INSERT INTO ${table} SET ${data}`, (err, result) => {
      if (err) return reject(err)
      resolve(result)
    })
  })
}

function update(table, data) {
  return new Promise((resolve, reject) => {
    connection.query(
      `UPDATE ${table} SET ${data} WHERE id=${data.id}`,
      (err, result) => {
        if (err) return reject(err)
        resolve(result)
      }
    )
  })
}

const upsert = async (table, payload) =>
  new Promise((resolve, reject) => {
    connection.query(
      `INSERT INTO ${table} SET ${payload} ON DUPLICATE KEY UPDATE ${payload}`,
      (error, data) => {
        console.log("UPDATE DATA: ", data)
        if (error) {
          return reject(error)
        }
        resolve(data)
      }
    )
  })

function query(table, query, join) {
  let joinQuery = ""
  if (join) {
    const key = Object.keys(join)[0]
    const val = join[key]
    joinQuery = `JOIN ${key} ON ${table}.${val} = ${key}.id`
  }

  return new Promise((resolve, reject) => {
    connection.query(
      `SELECT * FROM ${table} ${joinQuery} WHERE ${table}.${query}`,
      (err, res) => {
        if (err) return reject(err)
        resolve(res[0] || null)
      }
    )
  })
}

module.exports = {
  list,
  get,
  upsert,
  query
}
0

1 Answer 1

2

The insert query is wrong, Please change it to the below syntax. You cannot use SET in insert. SET should be used in update.

Wrong:

connection.query(`INSERT INTO ${table} SET ${data}`, (err, result) 

Insert query syntax:

INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.