// JavaScript Document

const sqlite3 = require('sqlite3').verbose();

async function insertPdfBytesIntoDb(pdfBytes) {
    // Create or open the database
    const db = new sqlite3.Database('example.db');

    // Ensure the table exists
    const createTableQuery = `
        CREATE TABLE IF NOT EXISTS PDFdata (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            pdfByteData BLOB
        )
    `;

    // Execute the table creation query
    await new Promise((resolve, reject) => {
        db.run(createTableQuery, (err) => {
            if (err) return reject(err);
            resolve();
        });
    });

    // Insert the PDF bytes into the table
    const insertQuery = `
        INSERT INTO PDFdata (pdfByteData) VALUES (?)
    `;

    await new Promise((resolve, reject) => {
        db.run(insertQuery, pdfBytes, function (err) {
            if (err) return reject(err);
            console.log(`Inserted PDF bytes with ID: ${this.lastID}`);
            resolve(this.lastID); // Return the last inserted ID
        });
    });

    // Close the database
    db.close();
}
