'Aggregates with Knex and Javascript

I have an application built using javascript, react, express, and postgreSQL. I have used knex to create and seed my database. The majority of the functions such as create, read, update, etc are all operating correctly however now I am trying to use knex to aggregate some of the data and failing to do so. For example, in this instance I want to find the max price listed in my database table. The error when visiting the route, I receive is...

{"error":"select * from "growthstocks" where "stock_id" = $1 limit $2 - invalid input syntax for type integer: "max""}

My code follows - relevant functions are max()

Service.js

const knex = require("../db/connection")

function create(newStock) {
    return knex("growthstocks")
      .insert(newStock)
      .returning("*")
      .then((createdRecords) => createdRecords[0]);
  }
  
  function read(stock_id) {
    return knex("growthstocks").select("*").where({ stock_id }).first();
  }
  
  function update(updatedStock) {
    return knex("growthstocks")
      .select("*")
      .where({ stock_id: updatedStock.stock_id })
      .update(updatedStock, "*")
      .then((updatedRecords) => updatedRecords[0]);
  }
  
  function destroy(stock_id) {
    return knex("growthstocks").where({ stock_id }).del();
  } 

  function list() {
    return knex("growthstocks").select("*");
  }

function max(){
  return knex("growthstocks")
  .select("stock_id")
  .max("price")
}
 

module.exports = {
  list,
  create,
  read,
  update,
  max,
  delete: destroy,
};

Controller.js

const knex = require("../db/connection")

function create(newStock) {
    return knex("growthstocks")
      .insert(newStock)
      .returning("*")
      .then((createdRecords) => createdRecords[0]);
  }
  
  function read(stock_id) {
    return knex("growthstocks").select("*").where({ stock_id }).first();
  }
  
  function update(updatedStock) {
    return knex("growthstocks")
      .select("*")
      .where({ stock_id: updatedStock.stock_id })
      .update(updatedStock, "*")
      .then((updatedRecords) => updatedRecords[0]);
  }
  
  function destroy(stock_id) {
    return knex("growthstocks").where({ stock_id }).del();
  } 

  function list() {
    return knex("growthstocks").select("*");
  }

function max(){
  return knex("growthstocks")
  .select("stock_id")
  .max("price")
}
 

module.exports = {
  list,
  create,
  read,
  update,
  max,
  delete: destroy,
};

Router.js

const router = require("express").Router({ mergeParams: true });
const controller = require("./growth.controller");
const methodNotAllowed = require("../errors/methodNotAllowed");

router.route("/").post(controller.create).get(controller.list).all(methodNotAllowed);

router.route("/:stock_id").put(controller.update).get(controller.read).delete(controller.delete).all(methodNotAllowed)


router.route("/max").get(controller.max).all(methodNotAllowed)

module.exports = router;



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source