From 5782510af18a24b8d98d95919aa8412b0325243a Mon Sep 17 00:00:00 2001 From: Annika Backstrom Date: Sat, 14 Dec 2019 21:11:21 -0500 Subject: [PATCH] Add sqlite3 library backend --- Vagrantfile | 2 +- create.sql | 1 + jukebox/library/sqlite-backend.js | 27 +++++++++++++++++++++++++++ package.json | 2 ++ player.js | 4 ++-- 5 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 create.sql create mode 100644 jukebox/library/sqlite-backend.js diff --git a/Vagrantfile b/Vagrantfile index fe963a2..b84c186 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -17,7 +17,7 @@ Vagrant.configure("2") do |config| config.vm.provision "shell", inline: "sudo apt-get update -y" config.vm.provision "shell", inline: "sudo apt-get upgrade -y" - config.vm.provision "shell", inline: "sudo apt-get install -y make nodejs npm" + config.vm.provision "shell", inline: "sudo apt-get install -y make nodejs npm sqlite3" end # vim:set ts=2 sw=2 et: diff --git a/create.sql b/create.sql new file mode 100644 index 0000000..46415fe --- /dev/null +++ b/create.sql @@ -0,0 +1 @@ +CREATE TABLE tags(uuid TEXT PRIMARY KEY, tag TEXT); diff --git a/jukebox/library/sqlite-backend.js b/jukebox/library/sqlite-backend.js new file mode 100644 index 0000000..c65b27c --- /dev/null +++ b/jukebox/library/sqlite-backend.js @@ -0,0 +1,27 @@ +"use strict"; + +const sqlite3 = require('sqlite3'); + +class SqliteBackend { + constructor(config) { + this.config = config; + sqlite3.verbose(); + this.db = new sqlite3.Database(this.config.db, sqlite3.OPEN_READONLY); + console.log(this.db); + } + + find(tag, callback) { + this.db.get("SELECT uuid FROM tags WHERE tag = ? AND _ROWID_ >= (abs(random()) % (SELECT max(_ROWID_) FROM tags)) LIMIT 1", tag, (err, row) => { + if (typeof row === 'undefined') { + return callback(); + } + callback(row['uuid']); + }); + } +} + +module.exports = function(config) { + return new SqliteBackend(config); +}; + +// vim:ts=2 sw=2 et: diff --git a/package.json b/package.json index 4be3cf9..7168aaf 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,10 @@ "glob": "^7.1.6", "morgan": "^1.7.0", "mustache": "^3.1.0", + "sqlite": "^3.0.3", "throttle-debounce": "^2.1.0", "utf-8-validate": "^5.0.2", + "uuid": "^3.3.3", "winston": "^3.2.1", "ws": "^7.2.1" }, diff --git a/player.js b/player.js index 6299fde..e3ef31e 100644 --- a/player.js +++ b/player.js @@ -5,8 +5,8 @@ const config = require('./config.json') const views = require('./jukebox/views')(__dirname + '/templates/'); const library = require('./jukebox/library'); -const MediaLibraryFileBackend = require('./jukebox/library/file-backend')(config); -const MediaLibrary = new library.Library(config, MediaLibraryFileBackend); +const MediaLibrarySqliteBackend = require('./jukebox/library/sqlite-backend')(config); +const MediaLibrary = new library.Library(config, MediaLibrarySqliteBackend); const ScriptRunner = require('./jukebox/scripts')(config);