// 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(queryString,function(err: QueryError, rows: RowDataPacket[0]){
                rows.release();
                if(!err) {
                    res.status(200).json(rows);
                }
            });
            rows.on('error', function(err: QueryError,) {
                  throw err;
                  return;
            });
        });
    };