This code sets up a SQLite database schema for storing IMDb movie data, including tables for movies, actors, titles, crew, and episodes, along with indexes to optimize data retrieval.
npm run import -- "create movie database tables"
function createTables(db) {
db.prepare(`
CREATE TABLE IF NOT EXISTS principals (
tconst TEXT NOT NULL,
ordering INT NOT NULL,
nconst TEXT NOT NULL,
category TEXT,
job TEXT,
characters TEXT,
UNIQUE(tconst, ordering),
PRIMARY KEY (tconst, ordering)
);`).run()
db.prepare(`
CREATE INDEX IF NOT EXISTS principles_titles
ON principals (tconst);`).run()
db.prepare(`
CREATE INDEX IF NOT EXISTS principles_names
ON principals (nconst);`).run()
db.prepare(`
CREATE TABLE IF NOT EXISTS titles (
tconst TEXT NOT NULL,
titleType TEXT NOT NULL,
primaryTitle TEXT,
originalTitle TEXT,
isAdult INTEGER,
startYear INTEGER,
endYear INTEGER,
runtimeMinutes INTEGER,
genres TEXT,
UNIQUE(tconst, titleType),
PRIMARY KEY (tconst, titleType)
);`).run()
db.prepare(`
CREATE INDEX IF NOT EXISTS titles_titles
ON titles (tconst);`).run()
db.prepare(`
CREATE INDEX IF NOT EXISTS titles_types
ON titles (tconst, titleType);`).run()
db.prepare(`
CREATE TABLE IF NOT EXISTS akas (
tconst TEXT NOT NULL,
ordering INTEGER NOT NULL,
title TEXT NOT NULL,
region TEXT NOT NULL,
language TEXT,
types TEXT,
attributes TEXT,
isOriginalTitle INTEGER,
UNIQUE(tconst, ordering),
PRIMARY KEY (tconst, ordering)
);`).run()
db.prepare(`
CREATE INDEX IF NOT EXISTS akas_titles
ON akas (tconst);`).run()
db.prepare(`
CREATE TABLE IF NOT EXISTS crew (
tconst TEXT NOT NULL,
directors TEXT,
writers TEXT,
UNIQUE(tconst),
PRIMARY KEY (tconst)
);`).run()
db.prepare(`
CREATE INDEX IF NOT EXISTS crew_titles
ON crew (tconst);`).run()
db.prepare(`
CREATE TABLE IF NOT EXISTS episode (
tconst TEXT NOT NULL,
parentTconst TEXT NOT NULL,
seasonNumber INTEGER,
episodeNumber INTEGER,
UNIQUE(tconst, parentTconst),
PRIMARY KEY (tconst, parentTconst)
);`).run()
db.prepare(`
CREATE INDEX IF NOT EXISTS episode_titles
ON episode (tconst);`).run()
db.prepare(`
CREATE INDEX IF NOT EXISTS episode_parents
ON episode (tconst, parentTconst);`).run()
db.prepare(`
CREATE TABLE IF NOT EXISTS ratings (
tconst TEXT NOT NULL,
averageRating INTEGER,
numVotes INTEGER,
UNIQUE(tconst),
PRIMARY KEY (tconst)
);`).run()
db.prepare(`
CREATE INDEX IF NOT EXISTS episode_titles
ON ratings (tconst);`).run()
db.prepare(`
CREATE TABLE IF NOT EXISTS name (
nconst TEXT NOT NULL,
primaryName TEXT,
birthYear INTEGER,
deathYear INTEGER,
primaryProfession TEXT,
knownForTitles TEXT,
UNIQUE(nconst),
PRIMARY KEY (nconst)
);`).run()
db.prepare(`
CREATE INDEX IF NOT EXISTS name_names
ON name (nconst);`).run()
return Promise.resolve();
}
module.exports = createTables;
```javascript
/**
* Creates tables in the database with indexes for efficient querying.
*
* @param {object} db - The database object.
* @returns {Promise} A promise that resolves when the tables are created.
*/
function createTables(db) {
// Create the principals table with an index on tconst and nconst for efficient lookup.
const principals = db.prepare(`
CREATE TABLE IF NOT EXISTS principals (
tconst TEXT NOT NULL,
ordering INT NOT NULL,
nconst TEXT NOT NULL,
category TEXT,
job TEXT,
characters TEXT,
UNIQUE(tconst, ordering),
PRIMARY KEY (tconst, ordering)
);
`).run();
// Create indexes on tconst and nconst for efficient lookup.
db.prepare(`
CREATE INDEX IF NOT EXISTS principles_titles ON principals (tconst);
`).run();
db.prepare(`
CREATE INDEX IF NOT EXISTS principles_names ON principals (nconst);
`).run();
// Create the titles table with an index on tconst and titleType for efficient lookup.
const titles = db.prepare(`
CREATE TABLE IF NOT EXISTS titles (
tconst TEXT NOT NULL,
titleType TEXT NOT NULL,
primaryTitle TEXT,
originalTitle TEXT,
isAdult INTEGER,
startYear INTEGER,
endYear INTEGER,
runtimeMinutes INTEGER,
genres TEXT,
UNIQUE(tconst, titleType),
PRIMARY KEY (tconst, titleType)
);
`).run();
// Create indexes on tconst and titleType for efficient lookup.
db.prepare(`
CREATE INDEX IF NOT EXISTS titles_titles ON titles (tconst);
`).run();
db.prepare(`
CREATE INDEX IF NOT EXISTS titles_types ON titles (tconst, titleType);
`).run();
// Create the akas table with an index on tconst for efficient lookup.
const akas = db.prepare(`
CREATE TABLE IF NOT EXISTS akas (
tconst TEXT NOT NULL,
ordering INTEGER NOT NULL,
title TEXT NOT NULL,
region TEXT NOT NULL,
language TEXT,
types TEXT,
attributes TEXT,
isOriginalTitle INTEGER,
UNIQUE(tconst, ordering),
PRIMARY KEY (tconst, ordering)
);
`).run();
// Create an index on tconst for efficient lookup.
db.prepare(`
CREATE INDEX IF NOT EXISTS akas_titles ON akas (tconst);
`).run();
// Create the crew table with an index on tconst for efficient lookup.
const crew = db.prepare(`
CREATE TABLE IF NOT EXISTS crew (
tconst TEXT NOT NULL,
directors TEXT,
writers TEXT,
UNIQUE(tconst),
PRIMARY KEY (tconst)
);
`).run();
// Create an index on tconst for efficient lookup.
db.prepare(`
CREATE INDEX IF NOT EXISTS crew_titles ON crew (tconst);
`).run();
// Create the episode table with an index on tconst and parentTconst for efficient lookup.
const episode = db.prepare(`
CREATE TABLE IF NOT EXISTS episode (
tconst TEXT NOT NULL,
parentTconst TEXT NOT NULL,
seasonNumber INTEGER,
episodeNumber INTEGER,
UNIQUE(tconst, parentTconst),
PRIMARY KEY (tconst, parentTconst)
);
`).run();
// Create indexes on tconst and parentTconst for efficient lookup.
db.prepare(`
CREATE INDEX IF NOT EXISTS episode_titles ON episode (tconst);
`).run();
db.prepare(`
CREATE INDEX IF NOT EXISTS episode_parents ON episode (tconst, parentTconst);
`).run();
// Create the ratings table with an index on tconst for efficient lookup.
const ratings = db.prepare(`
CREATE TABLE IF NOT EXISTS ratings (
tconst TEXT NOT NULL,
averageRating INTEGER,
numVotes INTEGER,
UNIQUE(tconst),
PRIMARY KEY (tconst)
);
`).run();
// Create an index on tconst for efficient lookup.
db.prepare(`
CREATE INDEX IF NOT EXISTS ratings_titles ON ratings (tconst);
`).run();
// Create the name table with an index on nconst for efficient lookup.
const name = db.prepare(`
CREATE TABLE IF NOT EXISTS name (
nconst TEXT NOT NULL,
primaryName TEXT,
birthYear INTEGER,
deathYear INTEGER,
primaryProfession TEXT,
knownForTitles TEXT,
UNIQUE(nconst),
PRIMARY KEY (nconst)
);
`).run();
// Create an index on nconst for efficient lookup.
db.prepare(`
CREATE INDEX IF NOT EXISTS name_names ON name (nconst);
`).run();
// Return a promise that resolves when all tables are created.
return Promise.all([
principals,
titles,
akas,
crew,
episode,
ratings,
name,
]).then(() => Promise.resolve());
}
module.exports = createTables;
```
This code defines SQL statements to create tables and indexes for storing IMDb movie data in a SQLite database.
Here's a breakdown:
Table Creation:
CREATE TABLE
statements for several tables:
principals
: Stores information about actors, directors, writers, etc. associated with movies.titles
: Stores basic information about movies (title, type, year, etc.).akas
: Stores alternative titles for movies in different languages and regions.crew
: Stores director and writer information for movies.episode
: Stores information about TV episodes, including their parent series.Index Creation:
CREATE INDEX
statements to create indexes on various columns within the tables.
Database Interaction:
db.prepare
to prepare the SQL statements and db.run
to execute them.CREATE TABLE IF NOT EXISTS
ensures that the tables are created only if they don't already exist.Let me know if you have any more questions!