I am writing middleware to return database queries using SQLite3 and Next.js. Here is a snippet of code that I wrote to return the contents of a DB table. It appears to work, and my checks of sources appear solid, with mixed reviews about the use of bind().
// route = /dbread
import { NextResponse } from 'next/server';
const sqlite3 = require("sqlite3").verbose();
const util = require('util');
export async function GET(req) {
const db = new sqlite3.Database("./database.db");
const dbAll = util.promisify(db.all).bind(db);
let response = await dbAll('SELECT * FROM "text"')
.then(db.close())
.catch((err => {
console.log(err);
return ['error'];
}));
return NextResponse.json(JSON.stringify(response));
}
However, if I take bind out, it breaks, so I keep that in, even though I don't fully understand what it's doing. Is there a better way to do this? The solution seems simple enough, but I just want to make sure I'm stepping on solid ground.