|
|
@ -80,68 +80,92 @@ function addJoin(knexQuery: any, base: ObjectType, other: ObjectType) { |
|
|
|
.join(otherTable, { [otherTable + '.id']: linkTable + '.' + linkingTableIdNames[other] }); |
|
|
|
.join(otherTable, { [otherTable + '.id']: linkTable + '.' + linkingTableIdNames[other] }); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function addLeafWhere(knexQuery: any, queryElem: api.QueryElem) { |
|
|
|
|
|
|
|
const simpleLeafOps = { |
|
|
|
|
|
|
|
[api.QueryFilterOp.Eq]: "=", |
|
|
|
|
|
|
|
[api.QueryFilterOp.Ne]: "!=", |
|
|
|
|
|
|
|
[api.QueryFilterOp.Like]: "like", |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const propertyKeys = { |
|
|
|
|
|
|
|
[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', |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!queryElem.propOperator) throw "Cannot create where clause without an operator."; |
|
|
|
|
|
|
|
const operator = queryElem.propOperator || api.QueryFilterOp.Eq; |
|
|
|
|
|
|
|
const a = queryElem.prop && propertyKeys[queryElem.prop]; |
|
|
|
|
|
|
|
const b = queryElem.propOperand || ""; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Object.keys(simpleLeafOps).includes(operator)) { |
|
|
|
|
|
|
|
return knexQuery.where(a, operator, b); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw "Query filter not implemented"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function addWhere(knexQuery: any, queryElem: api.QueryElem) { |
|
|
|
|
|
|
|
if (queryElem.prop) { |
|
|
|
|
|
|
|
// Leaf node.
|
|
|
|
|
|
|
|
return addLeafWhere(knexQuery, queryElem); |
|
|
|
|
|
|
|
} else if (queryElem.children) { |
|
|
|
|
|
|
|
// Branch node.
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw "Query filter not implemented."; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const objectColumns = { |
|
|
|
|
|
|
|
[ObjectType.Song]: ['songs.id as songs.id', 'songs.title as songs.title', 'songs.storeLinks as songs.storeLinks'], |
|
|
|
|
|
|
|
[ObjectType.Artist]: ['artists.id as artists.id', 'artists.name as artists.name', 'artists.storeLinks as artists.storeLinks'], |
|
|
|
|
|
|
|
[ObjectType.Album]: ['albums.id as albums.id', 'albums.name as albums.name', 'albums.storeLinks as albums.storeLinks'], |
|
|
|
|
|
|
|
[ObjectType.Tag]: ['tags.id as tags.id', 'tags.name as tags.name', 'tags.parentId as tags.parentId'] |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
function constructQuery(knex: Knex, queryFor: ObjectType, queryElem: api.QueryElem) { |
|
|
|
function constructQuery(knex: Knex, queryFor: ObjectType, queryElem: api.QueryElem) { |
|
|
|
|
|
|
|
const joinObjects = getRequiredDatabaseObjects(queryElem); |
|
|
|
|
|
|
|
joinObjects.delete(queryFor); // We are already querying this object in the base query.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Figure out what data we want to select from the results.
|
|
|
|
|
|
|
|
var columns: string[] = []; |
|
|
|
|
|
|
|
joinObjects.forEach((obj: ObjectType) => columns.push(...objectColumns[obj])); |
|
|
|
|
|
|
|
columns.push(...objectColumns[queryFor]); |
|
|
|
|
|
|
|
|
|
|
|
// First, we create a base query for the type of object we need to yield.
|
|
|
|
// First, we create a base query for the type of object we need to yield.
|
|
|
|
var q = knex.select('*').distinct().from(objectTables[queryFor]); |
|
|
|
var q = knex.select(columns) |
|
|
|
|
|
|
|
.distinct(objectTables[queryFor] + '.' + 'id') |
|
|
|
|
|
|
|
.from(objectTables[queryFor]); |
|
|
|
|
|
|
|
|
|
|
|
// Now, we need to add join statements for other objects we want to filter on.
|
|
|
|
// Now, we need to add join statements for other objects we want to filter on.
|
|
|
|
const allObjects = getRequiredDatabaseObjects(queryElem); |
|
|
|
joinObjects.forEach((object: ObjectType) => { |
|
|
|
allObjects.delete(queryFor); // We are already querying this object in the base query.
|
|
|
|
|
|
|
|
allObjects.forEach((object: ObjectType) => { |
|
|
|
|
|
|
|
q = addJoin(q, queryFor, object); |
|
|
|
q = addJoin(q, queryFor, object); |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
// TODO: apply filters.
|
|
|
|
// Apply filtering.
|
|
|
|
|
|
|
|
q = addWhere(q, queryElem); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: add ordering, limiting
|
|
|
|
|
|
|
|
|
|
|
|
return q; |
|
|
|
return q; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// const sequelizeOps: any = {
|
|
|
|
async function getLinkedObjects(knex: Knex, base: ObjectType, linked: ObjectType, baseIds: number[]) { |
|
|
|
// [api.QueryFilterOp.Eq]: Op.eq,
|
|
|
|
var result: Record<number, any[]> = {}; |
|
|
|
// [api.QueryFilterOp.Ne]: Op.ne,
|
|
|
|
const otherTable = objectTables[linked]; |
|
|
|
// [api.QueryFilterOp.In]: Op.in,
|
|
|
|
const linkingTable = getLinkingTable(base, linked); |
|
|
|
// [api.QueryFilterOp.NotIn]: Op.notIn,
|
|
|
|
const columns = objectColumns[linked]; |
|
|
|
// [api.QueryFilterOp.Like]: Op.like,
|
|
|
|
|
|
|
|
// [api.QueryElemOp.And]: Op.and,
|
|
|
|
await Promise.all(baseIds.map((baseId: number) => { |
|
|
|
// [api.QueryElemOp.Or]: Op.or,
|
|
|
|
return knex.select(columns).distinct(otherTable + '.id').from(otherTable) |
|
|
|
// };
|
|
|
|
.join(linkingTable, { [linkingTable + '.' + linkingTableIdNames[linked]]: otherTable + '.id' }) |
|
|
|
|
|
|
|
.where({ [linkingTable + '.' + linkingTableIdNames[base]]: baseId }) |
|
|
|
// const sequelizeProps: any = {
|
|
|
|
.then((others: any) => { result[baseId] = others; }) |
|
|
|
// [QueryType.Song]: {
|
|
|
|
})) |
|
|
|
// [api.QueryElemProperty.songTitle]: "title",
|
|
|
|
return result; |
|
|
|
// [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.
|
|
|
|
// // Returns the "where" clauses for Sequelize, per object type.
|
|
|
|
// const getSequelizeWhere = (queryElem: api.QueryElem, type: QueryType) => {
|
|
|
|
// const getSequelizeWhere = (queryElem: api.QueryElem, type: QueryType) => {
|
|
|
@ -210,31 +234,74 @@ export const QueryEndpointHandler: EndpointHandler = async (req: any, res: any, |
|
|
|
.offset(tagOffset || 0).limit(tagLimit) : |
|
|
|
.offset(tagOffset || 0).limit(tagLimit) : |
|
|
|
(async () => [])(); |
|
|
|
(async () => [])(); |
|
|
|
|
|
|
|
|
|
|
|
const [songs, artists, tags] = await Promise.all([songsPromise, artistsPromise, tagsPromise]); |
|
|
|
// For some objects, we want to return linked information as well.
|
|
|
|
|
|
|
|
// For that we need to do further queries.
|
|
|
|
|
|
|
|
const songIdsPromise = (async () => { |
|
|
|
|
|
|
|
const songs = await songsPromise; |
|
|
|
|
|
|
|
console.log("Found songs:", songs); |
|
|
|
|
|
|
|
const ids = songs.map((song: any) => song['songs.id']); |
|
|
|
|
|
|
|
return ids; |
|
|
|
|
|
|
|
})(); |
|
|
|
|
|
|
|
const songsArtistsPromise: Promise<Record<number, any[]>> = (songLimit && songLimit > 0) ? |
|
|
|
|
|
|
|
(async () => { |
|
|
|
|
|
|
|
return await getLinkedObjects(knex, ObjectType.Song, ObjectType.Artist, await songIdsPromise); |
|
|
|
|
|
|
|
})() : |
|
|
|
|
|
|
|
(async () => { return {}; })(); |
|
|
|
|
|
|
|
const songsTagsPromise: Promise<Record<number, any[]>> = (songLimit && songLimit > 0) ? |
|
|
|
|
|
|
|
(async () => { |
|
|
|
|
|
|
|
return await getLinkedObjects(knex, ObjectType.Song, ObjectType.Tag, await songIdsPromise); |
|
|
|
|
|
|
|
})() : |
|
|
|
|
|
|
|
(async () => { return {}; })(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const [ |
|
|
|
|
|
|
|
songs, |
|
|
|
|
|
|
|
artists, |
|
|
|
|
|
|
|
tags, |
|
|
|
|
|
|
|
songsArtists, |
|
|
|
|
|
|
|
songsTags |
|
|
|
|
|
|
|
] = |
|
|
|
|
|
|
|
await Promise.all([ |
|
|
|
|
|
|
|
songsPromise, |
|
|
|
|
|
|
|
artistsPromise, |
|
|
|
|
|
|
|
tagsPromise, |
|
|
|
|
|
|
|
songsArtistsPromise, |
|
|
|
|
|
|
|
songsTagsPromise, |
|
|
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
const response: api.QueryResponse = { |
|
|
|
const response: api.QueryResponse = { |
|
|
|
songs: songs.map((song: any) => { |
|
|
|
songs: songs.map((song: any) => { |
|
|
|
return <api.SongDetails>{ |
|
|
|
return <api.SongDetails>{ |
|
|
|
songId: song['id'], |
|
|
|
songId: song['songs.id'], |
|
|
|
title: song['title'], |
|
|
|
title: song['songs.title'], |
|
|
|
storeLinks: JSON.parse(song['storeLinks']), |
|
|
|
storeLinks: JSON.parse(song['songs.storeLinks']), |
|
|
|
artists: [], //FIXME
|
|
|
|
artists: songsArtists[song['songs.id']].map((artist: any) => { |
|
|
|
tags: [], //FIXME
|
|
|
|
return <api.ArtistDetails>{ |
|
|
|
|
|
|
|
artistId: artist['artists.id'], |
|
|
|
|
|
|
|
name: artist['artists.name'], |
|
|
|
|
|
|
|
storeLinks: JSON.parse(artist['artists.storeLinks']), |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
tags: songsTags[song['songs.id']].map((tag: any) => { |
|
|
|
|
|
|
|
return <api.TagDetails>{ |
|
|
|
|
|
|
|
tagId: tag['tags.id'], |
|
|
|
|
|
|
|
name: tag['tags.name'], |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
}), |
|
|
|
albums: [], //FIXME
|
|
|
|
albums: [], //FIXME
|
|
|
|
} |
|
|
|
} |
|
|
|
}), |
|
|
|
}), |
|
|
|
artists: artists.map((artist: any) => { |
|
|
|
artists: artists.map((artist: any) => { |
|
|
|
return <api.ArtistDetails>{ |
|
|
|
return <api.ArtistDetails>{ |
|
|
|
artistId: artist['id'], |
|
|
|
artistId: artist['artists.id'], |
|
|
|
name: artist['name'], |
|
|
|
name: artist['artists.name'], |
|
|
|
storeLinks: JSON.parse(artist['storeLinks']), |
|
|
|
storeLinks: JSON.parse(artist['artists.storeLinks']), |
|
|
|
} |
|
|
|
} |
|
|
|
}), |
|
|
|
}), |
|
|
|
tags: tags.map((tag: any) => { |
|
|
|
tags: tags.map((tag: any) => { |
|
|
|
return <api.TagDetails>{ |
|
|
|
return <api.TagDetails>{ |
|
|
|
tagId: tag['id'], |
|
|
|
tagId: tag['tags.id'], |
|
|
|
name: tag['name'], |
|
|
|
name: tag['tags.name'], |
|
|
|
parentId: tag['parentId'], |
|
|
|
parentId: tag['tags.parentId'], |
|
|
|
} |
|
|
|
} |
|
|
|
}), |
|
|
|
}), |
|
|
|
} |
|
|
|
} |
|
|
|