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

30 lines
801 B
JavaScript
Raw Normal View History

2019-12-15 02:11:21 +00:00
"use strict";
const Track = require('./track');
2019-12-15 02:11:21 +00:00
class SqliteBackend {
2019-12-17 18:39:18 +00:00
constructor(config, db, logger) {
2019-12-15 02:11:21 +00:00
this.config = config;
this.db = db;
2019-12-17 18:39:18 +00:00
this.logger = logger;
2019-12-15 02:11:21 +00:00
}
find(tag, callback) {
this.db.run("INSERT INTO tags (tag) VALUES (?)", tag);
this.db.get("SELECT uuid, tag, label FROM library WHERE tag = ? ORDER BY RANDOM() LIMIT 1", tag, (err, row) => {
2019-12-15 02:11:21 +00:00
if (typeof row === 'undefined') {
2019-12-17 18:39:18 +00:00
this.logger.debug("no matching tag in sqlite backend", { tag: tag });
2019-12-15 02:11:21 +00:00
return callback();
}
2019-12-17 18:39:18 +00:00
this.logger.debug("found a matching tag", { tag: tag });
callback(new Track(row['tag'], row['uuid'], row['label']));
2019-12-15 02:11:21 +00:00
});
}
}
2019-12-17 18:39:18 +00:00
module.exports = function(config, db, logger) {
return new SqliteBackend(config, db, logger);
2019-12-15 02:11:21 +00:00
};
// vim:ts=2 sw=2 et: