Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const main = async () => {
try {
const hrProcStart = process.hrtime()
let hrQueryStart = 0
await db.open(':memory:')
await csvimport.importSqlite(testPath, ',', {noHeaderRow: false})
// await db.open('/Users/antony/data/testdb.sqlite')
const [es, ens] = process.hrtime(hrProcStart)
console.info('runQuery: import completed in %ds %dms', es, ens / 1e6)
// console.log('table import complete: ', md.tableName)
console.log('running query:\n', tq)
hrQueryStart = process.hrtime()
const rows = await db.all(tq)
const [qes, qens] = process.hrtime(hrQueryStart)
console.log('read rows from sqlite table.')
console.table(rows)
console.info('runQuery: evaluated query in %ds %dms', qes, qens / 1e6)
await db.close()
} catch (err) {
console.error('caught exception running query: ', err, err.stack)
}
}
static async getStarCounts () {
const artists = {}
const songs = {}
// get artist star counts
{
const query = sql`
SELECT artistId, COUNT(userId) AS count
FROM artistStars
GROUP BY artistId
`
const rows = await db.all(String(query), query.parameters)
rows.forEach(row => { artists[row.artistId] = row.count })
}
// get song star counts
{
const query = sql`
SELECT songId, COUNT(userId) AS count
FROM songStars
GROUP BY songId
`
const rows = await db.all(String(query), query.parameters)
rows.forEach(row => { songs[row.songId] = row.count })
}
router.get('/api/library/scan', async (ctx, next) => {
const log = debug('app:library:scan')
if (isScanning) {
log('ignoring request: scan already in progress')
return
}
isScanning = true
// get provider configs
let cfg = {}
let rows = await db.all('SELECT * FROM config WHERE domain LIKE "%.provider"')
rows.forEach(function(row){
const provider = row.domain.substr(0, row.domain.lastIndexOf('.provider'))
cfg[provider] = JSON.parse(row.data)
})
for (let provider in cfg) {
if (!cfg[provider].enabled) {
log('Provider "%s" not enabled; skipping', provider)
continue
}
if (!Providers[provider]) {
log('Provider "%s" is enabled but not loaded', provider)
continue
}
.then(() => {
db.all("SELECT * FROM dataProviderSchemas")
.then(rows => {
console.log(rows);
rows.map(r=> schemaCache.set(r.name, r.schema));
});
resolve();
})
.catch(reject);
const getArtistWatches = ids => db
.all(`SELECT * FROM ArtistWatch WHERE artistWatchId IN (${parameterize(ids)})`, ids);
static async guildLeaderboard (guildID, page, client, locale) {
let entries = await sql.all(`SELECT userID, level, total FROM xp WHERE guildID = '${guildID}'`);
if (!entries) return {
max: 1,
array: [ i18n.getString('struct.xp.no_one_talked_yet', locale) ]
};
entries = entries.sort((a, b) => {
return a.total < b.total ? 1 : -1;
});
let maxPage = Math.ceil(entries.length / 5);
if (page > maxPage) return false;
let pageArray = [];
entries = entries.slice(page * 5 - 5, page * 5);
let startNum = page * 5 - 4;
static async addXP (message, guildRow) {
if (message.channel.type !== 'text') return;
if (message.channel.id === '304429222477299712') return;
if (!guildRow || guildRow.levels === 'false') return;
let rows = await sql.all(`SELECT * FROM xp WHERE userID = '${message.author.id}'`);
let global = Math.max.apply(Math, rows.map(a => a.global));
let row = await sql.get(`SELECT * FROM xp WHERE userID = '${message.author.id}' AND guildID = '${message.guild.id}'`);
if (!row) return sql.run(`INSERT INTO xp (userID, guildID, current, total, level, mult, global, lastXP, lastMessage) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, [message.author.id, message.guild.id, 0, 0, 0, 1, 0, 0, Date.now()]);
if (Date.now() - row.lastXP < config.xp.xpAdd) return sql.run(`UPDATE xp SET lastMessage = ${Date.now()} WHERE userID = '${message.author.id}' AND guildID = '${message.guild.id}'`);
let mult;
if (Date.now() - row.lastMessage < 10000) mult = row.mult * 1.02;
else if (Date.now() - row.lastMessage < 25000) mult = row.mult * 1.01;
else if (Date.now() - row.lastMessage < 50000) mult = row.mult * 1.005;
else mult = 1;
if (mult > config.xp.maxMult) mult = config.xp.maxMult;
sql.run(`UPDATE xp SET mult = ${mult} WHERE userID = '${message.author.id}' AND guildID = '${message.guild.id}'`);
let base = Math.round(mult * config.xp.base);
const getAlbumIdsByLabel = (ids, first) => db
.all(`SELECT albumId FROM Album WHERE labelId IN (${parameterize(ids)}) LIMIT ?`, asArray(ids).concat(first))
.then(rows => rows.map(({albumId}) => albumId));
return joinMonster(resolveInfo, ctx, sql => {
return db.all(sql)
}, { dialect: 'sqlite3' })
}
}
const query = sql`
SELECT media.*, paths.path, songs.artistId, songs.songId, songs.title
FROM media
INNER JOIN (
SELECT *
FROM paths
GROUP BY pathId
) paths ON (paths.pathId = media.pathId)
INNER JOIN songs USING (songId)
INNER JOIN artists USING (artistId)
WHERE songId = ${songId}
ORDER BY paths.priority ASC
`
const rows = await db.all(String(query), query.parameters)
rows.forEach(row => {
media.result.push(row.mediaId)
row.file = row.path + path.sep + row.relPath
media.entities[row.mediaId] = row
})
return media
}