You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

336 lines
14 KiB

import * as api from '../../client/src/api';
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types';
import Knex from 'knex';
enum ObjectType {
Song = 0,
Artist,
Tag,
Album,
}
// To keep track of which database objects are needed to filter on
// certain properties.
const propertyObjects: Record<api.QueryElemProperty, ObjectType> = {
[api.QueryElemProperty.albumName]: ObjectType.Album,
[api.QueryElemProperty.artistId]: ObjectType.Artist,
[api.QueryElemProperty.artistName]: ObjectType.Artist,
[api.QueryElemProperty.songId]: ObjectType.Song,
[api.QueryElemProperty.songTitle]: ObjectType.Song,
}
// To keep track of the tables in which objects are stored.
const objectTables: Record<ObjectType, string> = {
[ObjectType.Album]: 'albums',
[ObjectType.Artist]: 'artists',
[ObjectType.Song]: 'songs',
[ObjectType.Tag]: 'tags',
}
// To keep track of linking tables between objects.
const linkingTables: any = [
[[ObjectType.Song, ObjectType.Album], 'songs_albums'],
[[ObjectType.Song, ObjectType.Artist], 'songs_artists'],
[[ObjectType.Song, ObjectType.Tag], 'songs_tags'],
[[ObjectType.Artist, ObjectType.Album], 'artists_albums'],
[[ObjectType.Artist, ObjectType.Tag], 'artists_tags'],
[[ObjectType.Album, ObjectType.Tag], 'albums_tags'],
]
function getLinkingTable(a: ObjectType, b: ObjectType): string {
var res: string | undefined = undefined;
linkingTables.forEach((row: any) => {
if (row[0].includes(a) && row[0].includes(b)) {
res = row[1];
}
})
if (res) return res;
throw "Could not find linking table for objects: " + JSON.stringify(a) + ", " + JSON.stringify(b);
}
// To keep track of ID fields used in linking tables.
const linkingTableIdNames: Record<ObjectType, string> = {
[ObjectType.Album]: 'albumId',
[ObjectType.Artist]: 'artistId',
[ObjectType.Song]: 'songId',
[ObjectType.Tag]: 'tagId',
}
function getRequiredDatabaseObjects(queryElem: api.QueryElem): Set<ObjectType> {
if (queryElem.prop) {
// Leaf node.
return new Set([propertyObjects[queryElem.prop]]);
} else if (queryElem.children) {
// Branch node.
var r = new Set<ObjectType>();
queryElem.children.forEach((child: api.QueryElem) => {
getRequiredDatabaseObjects(child).forEach(object => r.add(object));
});
return r;
}
return new Set([]);
}
function addJoin(knexQuery: any, base: ObjectType, other: ObjectType) {
const linkTable = getLinkingTable(base, other);
const baseTable = objectTables[base];
const otherTable = objectTables[other];
return knexQuery
.join(linkTable, { [baseTable + '.id']: linkTable + '.' + linkingTableIdNames[base] })
.join(otherTable, { [otherTable + '.id']: linkTable + '.' + linkingTableIdNames[other] });
}
function constructQuery(knex: Knex, queryFor: ObjectType, queryElem: api.QueryElem) {
// First, we create a base query for the type of object we need to yield.
var q = knex.select('*').distinct().from(objectTables[queryFor]);
// Now, we need to add join statements for other objects we want to filter on.
const allObjects = getRequiredDatabaseObjects(queryElem);
allObjects.delete(queryFor); // We are already querying this object in the base query.
allObjects.forEach((object: ObjectType) => {
q = addJoin(q, queryFor, object);
})
// TODO: apply filters.
return q;
}
// const sequelizeOps: any = {
// [api.QueryFilterOp.Eq]: Op.eq,
// [api.QueryFilterOp.Ne]: Op.ne,
// [api.QueryFilterOp.In]: Op.in,
// [api.QueryFilterOp.NotIn]: Op.notIn,
// [api.QueryFilterOp.Like]: Op.like,
// [api.QueryElemOp.And]: Op.and,
// [api.QueryElemOp.Or]: Op.or,
// };
// const sequelizeProps: any = {
// [QueryType.Song]: {
// [api.QueryElemProperty.songTitle]: "title",
// [api.QueryElemProperty.songId]: "id",
// [api.QueryElemProperty.artistName]: "$Artists.name$",
// [api.QueryElemProperty.artistId]: "$Artists.id$",
// [api.QueryElemProperty.albumName]: "$Albums.name$",
// },
// [QueryType.Artist]: {
// [api.QueryElemProperty.songTitle]: "$Songs.title$",
// [api.QueryElemProperty.songId]: "$Songs.id$",
// [api.QueryElemProperty.artistName]: "name",
// [api.QueryElemProperty.artistId]: "id",
// [api.QueryElemProperty.albumName]: "$Albums.name$",
// },
// [QueryType.Tag]: {
// [api.QueryElemProperty.songTitle]: "$Songs.title$",
// [api.QueryElemProperty.songId]: "$Songs.id$",
// [api.QueryElemProperty.artistName]: "$Artists.name$",
// [api.QueryElemProperty.artistId]: "$Artists.id$",
// [api.QueryElemProperty.albumName]: "$Albums.name$",
// }
// };
// const sequelizeOrderColumns: any = {
// [QueryType.Song]: {
// [api.OrderByType.Name]: 'title',
// [api.OrderByType.ArtistRanking]: '$Rankings.rank$',
// [api.OrderByType.TagRanking]: '$Rankings.rank$',
// },
// [QueryType.Artist]: {
// [api.OrderByType.Name]: 'name'
// },
// [QueryType.Tag]: {
// [api.OrderByType.Name]: 'name'
// },
// }
// // Returns the "where" clauses for Sequelize, per object type.
// const getSequelizeWhere = (queryElem: api.QueryElem, type: QueryType) => {
// var where: any = {
// [Op.and]: []
// };
// if (queryElem.prop && queryElem.propOperator && queryElem.propOperand) {
// // Visit a filter-like subquery leaf.
// where[Op.and].push({
// [sequelizeProps[type][queryElem.prop]]: {
// [sequelizeOps[queryElem.propOperator]]: queryElem.propOperand
// }
// });
// }
// if (queryElem.childrenOperator && queryElem.children) {
// // Recursively visit a nested subquery.
// const children = queryElem.children.map((child: api.QueryElem) => getSequelizeWhere(child, type));
// where[Op.and].push({
// [sequelizeOps[queryElem.childrenOperator]]: children
// });
// }
// return where;
// }
// function getSequelizeOrder(order: api.Ordering, type: QueryType) {
// const ascstring = order.ascending ? 'ASC' : 'DESC';
// return [
// [ sequelizeOrderColumns[type][order.orderBy.type], ascstring ]
// ];
// }
export const QueryEndpointHandler: EndpointHandler = async (req: any, res: any, knex: Knex) => {
if (!api.checkQueryRequest(req.body)) {
const e: EndpointError = {
internalMessage: 'Invalid Query request: ' + JSON.stringify(req.body),
httpStatus: 400
};
throw e;
}
const reqObject: api.QueryRequest = req.body;
try {
const songLimit = reqObject.offsetsLimits.songLimit;
const songOffset = reqObject.offsetsLimits.songOffset;
const tagLimit = reqObject.offsetsLimits.tagLimit;
const tagOffset = reqObject.offsetsLimits.tagOffset;
const artistLimit = reqObject.offsetsLimits.artistLimit;
const artistOffset = reqObject.offsetsLimits.artistOffset;
const artistsPromise: Promise<any> = (artistLimit && artistLimit > 0) ?
constructQuery(knex, ObjectType.Artist, reqObject.query)
.offset(artistOffset || 0).limit(artistLimit) :
(async () => [])();
const songsPromise: Promise<any> = (songLimit && songLimit > 0) ?
constructQuery(knex, ObjectType.Song, reqObject.query)
.offset(songOffset || 0).limit(songLimit) :
(async () => [])();
const tagsPromise: Promise<any> = (tagLimit && tagLimit > 0) ?
constructQuery(knex, ObjectType.Tag, reqObject.query)
.offset(tagOffset || 0).limit(tagLimit) :
(async () => [])();
const [songs, artists, tags] = await Promise.all([songsPromise, artistsPromise, tagsPromise]);
const response: api.QueryResponse = {
songs: songs.map((song: any) => {
return <api.SongDetails>{
songId: song['id'],
title: song['title'],
storeLinks: JSON.parse(song['storeLinks']),
artists: [], //FIXME
tags: [], //FIXME
albums: [], //FIXME
}
}),
artists: artists.map((artist: any) => {
return <api.ArtistDetails>{
artistId: artist['id'],
name: artist['name'],
storeLinks: JSON.parse(artist['storeLinks']),
}
}),
tags: tags.map((tag: any) => {
return <api.TagDetails>{
tagId: tag['id'],
name: tag['name'],
parentId: tag['parentId'],
}
}),
}
res.send(response);
} catch (e) {
catchUnhandledErrors(e);
}
// try {
// const songLimit = reqObject.offsetsLimits.songLimit;
// const songOffset = reqObject.offsetsLimits.songOffset;
// const tagLimit = reqObject.offsetsLimits.tagLimit;
// const tagOffset = reqObject.offsetsLimits.tagOffset;
// const artistLimit = reqObject.offsetsLimits.artistLimit;
// const artistOffset = reqObject.offsetsLimits.artistOffset;
// const songs = (songLimit && songLimit > 0) && await models.Song.findAll({
// // NOTE: have to disable limit and offset because of bug: https://github.com/sequelize/sequelize/issues/11938.
// // Custom pagination is implemented before responding.
// where: getSequelizeWhere(reqObject.query, QueryType.Song),
// order: getSequelizeOrder(reqObject.ordering, QueryType.Song),
// include: [ models.Artist, models.Album, models.Tag, models.Ranking ],
// //limit: reqObject.limit,
// //offset: reqObject.offset,
// })
// const artists = (artistLimit && artistLimit > 0) && await models.Artist.findAll({
// // NOTE: have to disable limit and offset because of bug: https://github.com/sequelize/sequelize/issues/11938.
// // Custom pagination is implemented before responding.
// where: getSequelizeWhere(reqObject.query, QueryType.Artist),
// order: getSequelizeOrder(reqObject.ordering, QueryType.Artist),
// include: [models.Song, models.Album, models.Tag],
// //limit: reqObject.limit,
// //offset: reqObject.offset,
// })
// const tags = (tagLimit && tagLimit > 0) && await models.Tag.findAll({
// // NOTE: have to disable limit and offset because of bug: https://github.com/sequelize/sequelize/issues/11938.
// // Custom pagination is implemented before responding.
// where: getSequelizeWhere(reqObject.query, QueryType.Tag),
// order: getSequelizeOrder(reqObject.ordering, QueryType.Tag),
// include: [models.Song, models.Album, models.Artist],
// //limit: reqObject.limit,
// //offset: reqObject.offset,
// })
// const response: api.QueryResponse = {
// songs: ((songLimit || -1) <= 0) ? [] : await Promise.all(songs.map(async (song: any) => {
// const artists = song.getArtists();
// const tags = song.getTags();
// const rankings = song.getRankings();
// return <api.SongDetails>{
// songId: song.id,
// title: song.title,
// storeLinks: song.storeLinks,
// artists: (await artists).map((artist: any) => {
// return <api.ArtistDetails>{
// artistId: artist.id,
// name: artist.name,
// }
// }),
// tags: (await tags).map((tag: any) => {
// return <api.TagDetails>{
// tagId: tag.id,
// name: tag.name,
// }
// }),
// rankings: await (await rankings).map(async (ranking: any) => {
// const maybeTagContext: api.TagDetails | undefined = await ranking.getTagContext();
// const maybeArtistContext: api.ArtistDetails | undefined = await ranking.getArtistContext();
// const maybeContext = maybeTagContext || maybeArtistContext;
// return <api.RankingDetails>{
// rankingId: ranking.id,
// type: api.ItemType.Song,
// rankedId: song.id,
// context: maybeContext,
// value: ranking.value,
// }
// })
// };
// }).slice(songOffset || 0, (songOffset || 0) + (songLimit || 10))),
// // TODO: custom pagination due to bug mentioned above
// artists: ((artistLimit || -1) <= 0) ? [] : await Promise.all(artists.map(async (artist: any) => {
// return <api.ArtistDetails>{
// artistId: artist.id,
// name: artist.name,
// };
// }).slice(artistOffset || 0, (artistOffset || 0) + (artistLimit || 10))),
// tags: ((tagLimit || -1) <= 0) ? [] : await Promise.all(tags.map(async (tag: any) => {
// return <api.TagDetails>{
// tagId: tag.id,
// name: tag.name,
// };
// }).slice(tagOffset || 0, (tagOffset || 0) + (tagLimit || 10))),
// };
// res.send(response);
// } catch (e) {
// catchUnhandledErrors(e);
// }
}