Moved contents of seasoned_api up to root folder
This commit is contained in:
13
src/database/database.js
Normal file
13
src/database/database.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const configuration = require("../config/configuration").getInstance();
|
||||
const SqliteDatabase = require("./sqliteDatabase");
|
||||
|
||||
const database = new SqliteDatabase(configuration.get("database", "host"));
|
||||
/**
|
||||
* This module establishes a connection to the database
|
||||
* specified in the confgiuration file. It tries to setup
|
||||
* the required tables after successfully connecting.
|
||||
* If the tables already exists, it simply proceeds.
|
||||
*/
|
||||
Promise.resolve().then(() => database.setUp());
|
||||
|
||||
module.exports = database;
|
||||
95
src/database/schemas/setup.sql
Normal file
95
src/database/schemas/setup.sql
Normal file
@@ -0,0 +1,95 @@
|
||||
CREATE TABLE IF NOT EXISTS user (
|
||||
user_name varchar(127) UNIQUE,
|
||||
password varchar(127),
|
||||
admin boolean DEFAULT 0,
|
||||
email varchar(127) UNIQUE,
|
||||
primary key (user_name)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
user_name varchar(127) UNIQUE,
|
||||
dark_mode boolean DEFAULT 0,
|
||||
plex_userid varchar(127) DEFAULT NULL,
|
||||
emoji varchar(16) DEFAULT NULL,
|
||||
foreign key(user_name) REFERENCES user(user_name) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS cache (
|
||||
key varchar(255),
|
||||
value blob,
|
||||
time_to_live INTEGER DEFAULT 60,
|
||||
created_at DATE DEFAULT (datetime('now','localtime')),
|
||||
primary key(key)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS search_history (
|
||||
id integer,
|
||||
user_name varchar(127),
|
||||
search_query varchar(255),
|
||||
primary key (id),
|
||||
foreign key(user_name) REFERENCES user(user_name) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS requests (
|
||||
id NUMBER,
|
||||
title TEXT,
|
||||
year NUMBER,
|
||||
poster_path TEXT DEFAULT NULL,
|
||||
background_path TEXT DEFAULT NULL,
|
||||
requested_by varchar(127) DEFAULT NULL,
|
||||
ip TEXT,
|
||||
date DATE DEFAULT CURRENT_TIMESTAMP,
|
||||
status CHAR(25) DEFAULT 'requested' NOT NULL,
|
||||
user_agent CHAR(255) DEFAULT NULL,
|
||||
type CHAR(50) DEFAULT 'movie'
|
||||
-- foreign key(requested_by) REFERENCES user(user_name) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS request(
|
||||
id int not null,
|
||||
title text not null,
|
||||
year int not null,
|
||||
type char(10) not null,
|
||||
date timestamp default (strftime('%s', 'now'))
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS stray_eps(
|
||||
id TEXT UNIQUE,
|
||||
parent TEXT,
|
||||
path TEXT,
|
||||
name TEXT,
|
||||
season NUMBER,
|
||||
episode NUMBER,
|
||||
video_files TEXT,
|
||||
subtitles TEXT,
|
||||
trash TEXT,
|
||||
verified BOOLEAN DEFAULT 0,
|
||||
primary key(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS shows(
|
||||
show_names TEXT,
|
||||
date_added DATE,
|
||||
date_modified DATE DEFUALT CURRENT_DATE NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS requested_torrent (
|
||||
magnet TEXT UNIQUE,
|
||||
torrent_name TEXT,
|
||||
tmdb_id TEXT
|
||||
date_added DATE DEFAULT (datetime('now','localtime'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS deluge_torrent (
|
||||
key TEXT UNIQUE,
|
||||
name TEXT,
|
||||
progress TEXT,
|
||||
eta NUMBER,
|
||||
save_path TEXT,
|
||||
state TEXT,
|
||||
paused BOOLEAN,
|
||||
finished BOOLEAN,
|
||||
files TEXT,
|
||||
is_folder BOOLEAN
|
||||
)
|
||||
5
src/database/schemas/teardown.sql
Normal file
5
src/database/schemas/teardown.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
DROP TABLE IF EXISTS user;
|
||||
DROP TABLE IF EXISTS settings;
|
||||
DROP TABLE IF EXISTS search_history;
|
||||
DROP TABLE IF EXISTS requests;
|
||||
DROP TABLE IF EXISTS request;
|
||||
119
src/database/sqliteDatabase.js
Normal file
119
src/database/sqliteDatabase.js
Normal file
@@ -0,0 +1,119 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const sqlite3 = require("sqlite3").verbose();
|
||||
|
||||
class SqliteDatabase {
|
||||
constructor(host) {
|
||||
this.host = host;
|
||||
this.connection = new sqlite3.Database(this.host);
|
||||
this.execute("pragma foreign_keys = on;");
|
||||
this.schemaDirectory = path.join(__dirname, "schemas");
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to the database.
|
||||
* @returns {Promise} succeeds if connection was established
|
||||
*/
|
||||
// connect() {
|
||||
// let database = ;
|
||||
// this.connection = database;
|
||||
// return database;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Run a SQL query against the database.
|
||||
* @param {String} sql SQL query
|
||||
* @param {Array} parameters in the SQL query
|
||||
* @returns {Promise}
|
||||
*/
|
||||
run(sql, parameters) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.connection.run(sql, parameters, (error, result) => {
|
||||
if (error) reject(error);
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a SQL query against the database and retrieve all the rows.
|
||||
* @param {String} sql SQL query
|
||||
* @param {Array} parameters in the SQL query
|
||||
* @returns {Promise}
|
||||
*/
|
||||
all(sql, parameters) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.connection.all(sql, parameters, (err, rows) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
resolve(rows);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a SQL query against the database and retrieve one row.
|
||||
* @param {String} sql SQL query
|
||||
* @param {Array} parameters in the SQL query
|
||||
* @returns {Promise}
|
||||
*/
|
||||
get(sql, parameters) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.connection.get(sql, parameters, (err, rows) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
resolve(rows);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a SQL query against the database and retrieve the status.
|
||||
* @param {String} sql SQL query
|
||||
* @param {Array} parameters in the SQL query
|
||||
* @returns {Promise}
|
||||
*/
|
||||
execute(sql) {
|
||||
return new Promise(resolve => {
|
||||
this.connection.exec(sql, (err, database) => {
|
||||
if (err) {
|
||||
console.log("ERROR: ", err);
|
||||
reject(err);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the database by running setup.sql file in schemas/.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
setUp() {
|
||||
const setupSchema = this.readSqlFile("setup.sql");
|
||||
return Promise.resolve(this.execute(setupSchema));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tears down the database by running tearDown.sql file in schemas/.
|
||||
* @returns {Promise}
|
||||
*/
|
||||
tearDown() {
|
||||
const tearDownSchema = this.readSqlFile("teardown.sql");
|
||||
return Promise.resolve(this.execute(tearDownSchema));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file contents of a SQL file in schemas/.
|
||||
* @returns {String}
|
||||
*/
|
||||
readSqlFile(filename) {
|
||||
const schemaPath = path.join(this.schemaDirectory, filename);
|
||||
const schema = fs.readFileSync(schemaPath).toString("utf-8");
|
||||
return schema;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SqliteDatabase;
|
||||
Reference in New Issue
Block a user