// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import { OkPacket, QueryError, RowDataPacket } from "mysql2";
import type { NextApiRequest, NextApiResponse } from 'next';
var pool = require("../../server/db/connect");

type Data = {
    row: string,
}

const queryString = "SELECT name, types FROM product_categories WHERE status=1";

export default function handler(
    req: NextApiRequest,
    res: NextApiResponse<Data>)
    {
        pool.getConnection(function(err: QueryError, rows: RowDataPacket[0]){
            if (err) {
                rows.release();
              throw err;
            }
            rows.query(query,function(err: QueryError, rows: RowDataPacket[0]){
                connection.release();
                if(!err) {
                    callback(null, {rows: rows});
                }
            });
            connection.on('error', function(err) {
                  throw err;
                  return;
            });
        });

        pool.query(
            queryString,
            (err: QueryError, rows: RowDataPacket[0]) => {
                res.status(200).json(rows);
            }
        );
        pool.releaseConnection(conn);
    };