pi-rfid-jukebox/jukebox/library/sqlite-backend.js

28 lines
642 B
JavaScript

"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: