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

27 lines
657 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 {
constructor(config, db) {
2019-12-15 02:11:21 +00:00
this.config = config;
this.db = db;
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 = ? AND _ROWID_ >= (abs(random()) % (SELECT max(_ROWID_) FROM tags)) LIMIT 1", tag, (err, row) => {
2019-12-15 02:11:21 +00:00
if (typeof row === 'undefined') {
return callback();
}
callback(new Track(row['tag'], row['uuid'], row['label']));
2019-12-15 02:11:21 +00:00
});
}
}
module.exports = function(config, db) {
return new SqliteBackend(config, db);
2019-12-15 02:11:21 +00:00
};
// vim:ts=2 sw=2 et: