sqlite3 multithreading nodejs

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

// Create a new database object and open a connection to the SQLite database file
const db = new sqlite3.Database(':memory:');

// Define a SQL statement to create a table named 'users' with columns id, name, and age
const createTableQuery = `
    CREATE TABLE users (
        id INTEGER PRIMARY KEY,
        name TEXT,
        age INTEGER
    )
`;

// Execute the SQL statement to create the 'users' table in the database
db.serialize(() => {
    db.run(createTableQuery, (err) => {
        if (err) {
            console.error('Error creating table:', err.message);
        } else {
            console.log('Users table created');
        }
    });
});

// Define a SQL statement to insert data into the 'users' table
const insertQuery = `
    INSERT INTO users (name, age) VALUES (?, ?)
`;

// Run a loop to insert multiple rows of data into the 'users' table using prepared statements
db.serialize(() => {
    db.run('BEGIN TRANSACTION'); // Begin a transaction for bulk insert
    const stmt = db.prepare(insertQuery); // Prepare the insert statement

    for (let i = 0; i < 10; i++) {
        stmt.run(`User ${i}`, 20 + i); // Bind parameters and execute the prepared statement
    }

    stmt.finalize(); // Finalize the prepared statement
    db.run('COMMIT'); // Commit the transaction to save changes
});

// Define a SQL statement to fetch all rows from the 'users' table
const selectQuery = `
    SELECT * FROM users
`;

// Execute the select query to fetch all rows from the 'users' table
db.all(selectQuery, (err, rows) => {
    if (err) {
        console.error('Error fetching data:', err.message);
    } else {
        console.log('Fetched data:', rows);
    }
});

// Close the database connection
db.close((err) => {
    if (err) {
        console.error('Error closing database:', err.message);
    } else {
        console.log('Database connection closed');
    }
});