Last active
March 25, 2021 16:18
-
-
Save lvngd/b054546c3e7fc3e502b9520f95abc8c6 to your computer and use it in GitHub Desktop.
Node + Knex code to create postgres tables for darklyrics data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
1st create database: | |
-go into the interactive interpreter | |
psql postgres | |
Here we're creating: | |
1. a databased called 'darklyrics' | |
2. a user 'darklyricsuser' | |
3. with password 'darklyricspassword' | |
Then giving the newly created user privileges on the database. | |
You can copy and paste these three lines directly into the psql interpreter: | |
CREATE DATABASE darklyrics; | |
CREATE USER darklyricsuser WITH ENCRYPTED PASSWORD 'darklyricspassword'; | |
GRANT ALL PRIVILEGES ON DATABASE darklyrics TO darklyricsuser; | |
******DATABASE CREDENTIALS SHOULD GO IN A SEPARATE CONFIG FILE LIKE WITH THE | |
GOOGLE API KEY, BUT THIS CODE IS JUST FOR TESTING FOR NOW****** | |
Now we can connect to the database with knex. | |
Install it globally and also into your project: | |
npm install -g knex | |
npm install knex --save | |
Then run this command: | |
knex init | |
*/ | |
/* | |
//two ways to connect...this commented one, or the one right below | |
const knex = require('knex')({ | |
client: 'pg', | |
connection: { | |
host : '127.0.0.1', | |
user : 'darklyricsuser', | |
password : 'darklyricspassword', | |
database : 'darklyrics' | |
} | |
}); | |
*/ | |
//second way to connect | |
const knex = require('knex')({ | |
client: 'pg', | |
connection: 'postgresql://darklyricsuser:darklyricspassword@localhost/darklyrics', | |
}); | |
async function createBandsTable(){ | |
return knex.schema.hasTable('bands') | |
.then(function(exists) { | |
if (!exists) { | |
return knex.schema.createTable('bands', function(table) { | |
table.increments('id').primary(); | |
table.string('name'); | |
table.string('url'); | |
}); | |
} | |
}); | |
} | |
async function createAlbumsTable(){ | |
return knex.schema.hasTable('albums') | |
.then(function(exists) { | |
if (!exists) { | |
return knex.schema.createTable('albums', function(table) { | |
table.increments('id').primary(); | |
table.string('name'); | |
table.string('type'); | |
table.string('year') | |
table.integer('band_id').unsigned().notNullable() | |
table.foreign('band_id').references('id').inTable('bands').onDelete("CASCADE"); | |
}); | |
} | |
}); | |
} | |
async function createSongsTable(){ | |
return knex.schema.hasTable('songs') | |
.then(function(exists) { | |
if (!exists) { | |
return knex.schema.createTable('songs', function(table) { | |
table.increments('id').primary(); | |
table.string('name'); | |
table.string('url'); | |
//table.string('translation') | |
//table.string('language')//original language | |
//any other data related to the translation | |
table.integer('album_id').unsigned().notNullable() | |
table.foreign('album_id').references('id').inTable('albums').onDelete("CASCADE"); | |
}); | |
} | |
}) | |
} | |
async function createTables(){ | |
await createBandsTable(); | |
await createAlbumsTable(); | |
await createSongsTable(); | |
knex.destroy(); | |
} | |
createTables(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment