'use strict'; const knex = require('./knex'); async function matchTags(rawTags) { const tagQuery = knex('tags') .select(knex.raw('ifnull(original.tag, tags.tag) as tag'), knex.raw('ifnull(original.tag, tags.tag) as tag')) .whereIn('tags.tag', rawTags) .leftJoin('tags as original', 'tags.alias_for', 'original.tag') .toString() .replace('where `tags`.`tag` in', 'where `tags`.`tag` collate NOCASE in'); const tagEntries = await knex.raw(tagQuery); return Array.from(new Set(tagEntries.map(({ tag }) => tag))).sort(); // reduce to tag name and filter duplicates } module.exports = { matchTags };