|
|
@ -1,6 +1,7 @@ |
|
|
|
import * as api from '../../client/src/api'; |
|
|
|
import * as api from '../../client/src/api'; |
|
|
|
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; |
|
|
|
import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; |
|
|
|
import Knex from 'knex'; |
|
|
|
import Knex from 'knex'; |
|
|
|
|
|
|
|
import asJson from '../lib/asJson'; |
|
|
|
|
|
|
|
|
|
|
|
enum ObjectType { |
|
|
|
enum ObjectType { |
|
|
|
Song = 0, |
|
|
|
Song = 0, |
|
|
@ -176,13 +177,13 @@ function constructQuery(knex: Knex, queryFor: ObjectType, queryElem: api.QueryEl |
|
|
|
joinObjects.delete(queryFor); // We are already querying this object in the base query.
|
|
|
|
joinObjects.delete(queryFor); // We are already querying this object in the base query.
|
|
|
|
|
|
|
|
|
|
|
|
// Figure out what data we want to select from the results.
|
|
|
|
// Figure out what data we want to select from the results.
|
|
|
|
var columns: string[] = []; |
|
|
|
var columns: any[] = []; |
|
|
|
joinObjects.forEach((obj: ObjectType) => columns.push(...objectColumns[obj])); |
|
|
|
joinObjects.forEach((obj: ObjectType) => columns.push(...objectColumns[obj])); |
|
|
|
columns.push(...objectColumns[queryFor]); |
|
|
|
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(columns) |
|
|
|
var q = knex.select(columns) |
|
|
|
.distinct(objectTables[queryFor] + '.' + 'id') |
|
|
|
.groupBy(objectTables[queryFor] + '.' + 'id') |
|
|
|
.from(objectTables[queryFor]); |
|
|
|
.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.
|
|
|
@ -213,11 +214,13 @@ async function getLinkedObjects(knex: Knex, base: ObjectType, linked: ObjectType |
|
|
|
const columns = objectColumns[linked]; |
|
|
|
const columns = objectColumns[linked]; |
|
|
|
|
|
|
|
|
|
|
|
await Promise.all(baseIds.map((baseId: number) => { |
|
|
|
await Promise.all(baseIds.map((baseId: number) => { |
|
|
|
return knex.select(columns).distinct(otherTable + '.id').from(otherTable) |
|
|
|
return knex.select(columns).groupBy(otherTable + '.id').from(otherTable) |
|
|
|
.join(linkingTable, { [linkingTable + '.' + linkingTableIdNames[linked]]: otherTable + '.id' }) |
|
|
|
.join(linkingTable, { [linkingTable + '.' + linkingTableIdNames[linked]]: otherTable + '.id' }) |
|
|
|
.where({ [linkingTable + '.' + linkingTableIdNames[base]]: baseId }) |
|
|
|
.where({ [linkingTable + '.' + linkingTableIdNames[base]]: baseId }) |
|
|
|
.then((others: any) => { result[baseId] = others; }) |
|
|
|
.then((others: any) => { result[baseId] = others; }) |
|
|
|
})) |
|
|
|
})) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log("Query results for", baseIds, ":", result); |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -309,12 +312,12 @@ export const QueryEndpointHandler: EndpointHandler = async (req: any, res: any, |
|
|
|
return <api.SongDetails>{ |
|
|
|
return <api.SongDetails>{ |
|
|
|
songId: song['songs.id'], |
|
|
|
songId: song['songs.id'], |
|
|
|
title: song['songs.title'], |
|
|
|
title: song['songs.title'], |
|
|
|
storeLinks: JSON.parse(song['songs.storeLinks']), |
|
|
|
storeLinks: asJson(song['songs.storeLinks']), |
|
|
|
artists: songsArtists[song['songs.id']].map((artist: any) => { |
|
|
|
artists: songsArtists[song['songs.id']].map((artist: any) => { |
|
|
|
return <api.ArtistDetails>{ |
|
|
|
return <api.ArtistDetails>{ |
|
|
|
artistId: artist['artists.id'], |
|
|
|
artistId: artist['artists.id'], |
|
|
|
name: artist['artists.name'], |
|
|
|
name: artist['artists.name'], |
|
|
|
storeLinks: JSON.parse(artist['artists.storeLinks']), |
|
|
|
storeLinks: asJson(artist['artists.storeLinks']), |
|
|
|
}; |
|
|
|
}; |
|
|
|
}), |
|
|
|
}), |
|
|
|
tags: songsTags[song['songs.id']].map((tag: any) => { |
|
|
|
tags: songsTags[song['songs.id']].map((tag: any) => { |
|
|
@ -330,7 +333,7 @@ export const QueryEndpointHandler: EndpointHandler = async (req: any, res: any, |
|
|
|
return <api.ArtistDetails>{ |
|
|
|
return <api.ArtistDetails>{ |
|
|
|
artistId: artist['artists.id'], |
|
|
|
artistId: artist['artists.id'], |
|
|
|
name: artist['artists.name'], |
|
|
|
name: artist['artists.name'], |
|
|
|
storeLinks: JSON.parse(artist['artists.storeLinks']), |
|
|
|
storeLinks: asJson(artist['artists.storeLinks']), |
|
|
|
} |
|
|
|
} |
|
|
|
}), |
|
|
|
}), |
|
|
|
tags: tags.map((tag: any) => { |
|
|
|
tags: tags.map((tag: any) => { |
|
|
|