nodejs sqlite create db if not exists

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

const dbName = 'your_database.db';

// Step 1: Check if the database file exists
if (!fs.existsSync(dbName)) {

  // Step 2: If the database file doesn't exist, create a new SQLite database
  const db = new sqlite3.Database(dbName);

  // Step 3: Define the SQL statement to create a table or perform other operations if needed
  const createTableQuery = `
    CREATE TABLE IF NOT EXISTS your_table (
      id INTEGER PRIMARY KEY,
      name TEXT,
      age INTEGER
    );
  `;

  // Step 4: Execute the SQL statement to create the table
  db.run(createTableQuery, (err) => {
    if (err) {
      console.error('Error creating table:', err.message);
    } else {
      console.log('Table created successfully.');
    }

    // Step 5: Close the database connection
    db.close();
  });

} else {
  console.log('Database file already exists.');
}