movie database | Cell 1 | use elastic search from node | Search

The code initializes a SQLite database and defines a function dropTitles to drop indices and a table from the database. It also prepares a query to count the number of rows in the name table, but the result is not utilized.

Cell 2

var sqlite3 = require('better-sqlite3');
var movies = './movies.sqlite.db';
var db = new sqlite3(movies, {});

function dropTitles() {
    db.prepare(`DROP INDEX titles_titles`).run();
    db.prepare(`DROP INDEX titles_types`).run();
    db.prepare(`DROP TABLE names`).run();
}

db.prepare(`SELECT COUNT(*) FROM name`).get();

What the code could have been:

const sqlite3 = require('better-sqlite3');
const path = require('path');
const moviesDbPath = path.join(__dirname,'movies.sqlite.db');
const db = new sqlite3(moviesDbPath, {});

/**
 * Drops titles index and table.
 */
function dropTitles() {
    // Drop titles index
    db.prepare(`DROP INDEX IF EXISTS titles_titles`).run();
    db.prepare(`DROP INDEX IF EXISTS titles_types`).run();
    
    // Drop names table
    db.prepare(`
        DROP TABLE IF EXISTS names
    `).run();
}

/**
 * Retrieves the count of names in the database.
 * @returns {Promise} The count of names.
 */
function getCountOfNames() {
    return db.prepare('SELECT COUNT(*) FROM names').get().count;
}

// Call dropTitles function
dropTitles();

// Call getCountOfNames function
getCountOfNames().then(count => {
    console.log(`Count of names: ${count}`);
});

Code Breakdown

Dependencies and Initialization

var sqlite3 = require('better-sqlite3');
var movies = './movies.sqlite.db';
var db = new sqlite3(movies, {});

dropTitles Function

function dropTitles() {
    db.prepare(`DROP INDEX titles_titles`).run();
    db.prepare(`DROP INDEX titles_types`).run();
    db.prepare(`DROP TABLE names`).run();
}

Database Query

db.prepare(`SELECT COUNT(*) FROM name`).get();