Skip to content
Extraits de code Groupes Projets

Playlist system creation

Ouvert Deurstann a demandé de fusionner dev-deurstann-3 vers master
Comparer et
15 fichiers
+ 628
109
Comparer les modifications
  • Côte à côte
  • En ligne
Fichiers
15
+ 45
42
const logger = require.main.require('./common/logger.js');
const config = require.main.require('./common/config.js');
const MiniSearch = require("minisearch")
var sqlite3 = require.main.require('sqlite3').verbose();
/* The kara table is defined as follows:
@@ -47,6 +48,28 @@ class KaraDatabase {
constructor() {
this.m_karaPath = config.content.database.path;
this.m_db = new sqlite3.Database(this.m_karaPath);
this.m_db.configure('busyTimeout', 50000);
this.m_search_engine = new MiniSearch({
fields: [
"id",
"cat",
"type",
"language",
"author",
"title",
"source"
], // fields to index for full-text search
storeFields: [
"id",
"cat",
"type",
"language",
"author",
"title",
"source",
"available"
] // fields to return with search results
})
logger.info('db', 'Create database from file ' + config.content.database.path);
}
@@ -56,55 +79,28 @@ class KaraDatabase {
logger.info('db', `Close database ${this.m_karaPath}`);
}
/* Search from the kara table, a string. Can be anything (query, name,
* language, source, etc).
/* Search for karas with the search engine
* - queryString: String
* - limit: Int | null -> maximal number of results
* - offset: Int | null -> offset in the search
* => a promize, callback with [(id: Int, string, cat, type, language, author, title, source)] */
search(queryString, offset = null, limit = null) {
var __ret = [];
var __sqlQuery = `SELECT id, string,
category AS cat,
(song_type || song_number) AS type,
language, author_name AS author,
song_name AS title, source_name AS source
FROM kara
WHERE string LIKE ? OR author_name COLLATE nocase = ?`;
if (offset != null && limit != null) __sqlQuery += ` LIMIT ${offset}, ${limit}`;
function __getRecords(db) {
return new Promise(resolv => {
db.all(__sqlQuery, [`%${queryString}%`, queryString], (err, rows) => {
if (err) {
logger.error('db', err);
throw err;
}
rows.forEach(row => {
__ret.push(row);
});
resolv(__ret);
});
});
}
return __getRecords(this.m_db);
* => A list of search results [(id: Int, string, cat, type, language, author, title, source)] */
search(queryString) {
let searchResult = this.m_search_engine.search(queryString, {
prefix: term => term.length,
fuzzy: term => term.length > 3 ? 0.2 : null,
combineWith: 'AND'
}).slice(0,200);
return searchResult;
}
/* List all the kara in the db.
* - limit: Int | null -> maximal number of results
* - offset: Int | null -> offset in the search
* => a promize, callback with [(id: Int, string, cat, type, language, author, title, source)] */
all(offset = null, limit = null) {
/* Initiate the search engine with the db
* => a promize */
setup_search_engine() {
var __ret = [];
var __sqlQuery = `SELECT id, string,
var __sqlQuery = `SELECT id,
category AS cat,
(song_type || song_number) AS type,
language, author_name AS author,
song_name AS title, source_name AS source
song_name AS title, source_name AS source, available
FROM kara`;
if (offset != null && limit != null) __sqlQuery += ` LIMIT ${offset}, ${limit}`;
function __getRecords(db) {
return new Promise(resolv => {
db.all(__sqlQuery, [], (err, rows) => {
@@ -120,7 +116,14 @@ class KaraDatabase {
});
}
return __getRecords(this.m_db);
function __SetupSE(karas, search_engine = db.m_search_engine){
search_engine.removeAll()
logger.info("db", karas.length)
search_engine.addAll(karas)
logger.info("db", search_engine.documentCount)
}
return __getRecords(this.m_db).then(__SetupSE);
}
/* List all the next karas in the queue.
Chargement en cours