simplify item queries with subs
This commit is contained in:
parent
b406f0c340
commit
3d4d86a40a
|
@ -146,8 +146,8 @@ function recentClause (type) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const subClause = (sub, num) => {
|
const subClause = (sub, num, table) => {
|
||||||
return sub ? ` AND "subName" = $${num} ` : ` AND ("subName" IS NOT NULL OR "subName" = $${num}) `
|
return sub ? ` AND ${table ? `${table}.` : ''}"subName" = $${num} ` : ''
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
@ -202,6 +202,10 @@ export default {
|
||||||
return me ? ` AND (status <> 'STOPPED' OR "userId" = ${me.id}) ` : ' AND status <> \'STOPPED\' '
|
return me ? ` AND (status <> 'STOPPED' OR "userId" = ${me.id}) ` : ' AND status <> \'STOPPED\' '
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HACK we want to optionally include the subName in the query
|
||||||
|
// but the query planner doesn't like unused parameters
|
||||||
|
const subArr = sub ? [sub] : []
|
||||||
|
|
||||||
switch (sort) {
|
switch (sort) {
|
||||||
case 'user':
|
case 'user':
|
||||||
if (!name) {
|
if (!name) {
|
||||||
|
@ -235,7 +239,7 @@ export default {
|
||||||
${recentClause(type)}
|
${recentClause(type)}
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
OFFSET $2
|
OFFSET $2
|
||||||
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, sub)
|
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, ...subArr)
|
||||||
break
|
break
|
||||||
case 'top':
|
case 'top':
|
||||||
items = await models.$queryRaw(`
|
items = await models.$queryRaw(`
|
||||||
|
@ -277,7 +281,7 @@ export default {
|
||||||
ORDER BY created_at DESC)
|
ORDER BY created_at DESC)
|
||||||
) a
|
) a
|
||||||
OFFSET $2
|
OFFSET $2
|
||||||
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, sub)
|
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, ...subArr)
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
// HACK we can speed hack the first hot page, by limiting our query to only
|
// HACK we can speed hack the first hot page, by limiting our query to only
|
||||||
|
@ -296,7 +300,7 @@ export default {
|
||||||
${await filterClause(me, models)}
|
${await filterClause(me, models)}
|
||||||
${await newTimedOrderByWeightedSats(me, models, 1)}
|
${await newTimedOrderByWeightedSats(me, models, 1)}
|
||||||
OFFSET $2
|
OFFSET $2
|
||||||
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, new Date(new Date().setDate(new Date().getDate() - 5)), sub || 'NULL')
|
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, new Date(new Date().setDate(new Date().getDate() - 5)), ...subArr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (decodedCursor.offset !== 0 || items?.length < LIMIT) {
|
if (decodedCursor.offset !== 0 || items?.length < LIMIT) {
|
||||||
|
@ -309,7 +313,7 @@ export default {
|
||||||
${await filterClause(me, models)}
|
${await filterClause(me, models)}
|
||||||
${await newTimedOrderByWeightedSats(me, models, 1)}
|
${await newTimedOrderByWeightedSats(me, models, 1)}
|
||||||
OFFSET $2
|
OFFSET $2
|
||||||
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, sub || 'NULL')
|
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, ...subArr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (decodedCursor.offset === 0) {
|
if (decodedCursor.offset === 0) {
|
||||||
|
@ -324,7 +328,7 @@ export default {
|
||||||
FROM "Item"
|
FROM "Item"
|
||||||
WHERE "pinId" IS NOT NULL
|
WHERE "pinId" IS NOT NULL
|
||||||
${subClause(sub, 1)}
|
${subClause(sub, 1)}
|
||||||
) rank_filter WHERE RANK = 1`, sub)
|
) rank_filter WHERE RANK = 1`, ...subArr)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -431,6 +435,9 @@ export default {
|
||||||
},
|
},
|
||||||
moreFlatComments: async (parent, { sub, cursor, name, sort, within }, { me, models }) => {
|
moreFlatComments: async (parent, { sub, cursor, name, sort, within }, { me, models }) => {
|
||||||
const decodedCursor = decodeCursor(cursor)
|
const decodedCursor = decodeCursor(cursor)
|
||||||
|
// HACK we want to optionally include the subName in the query
|
||||||
|
// but the query planner doesn't like unused parameters
|
||||||
|
const subArr = sub ? [sub] : []
|
||||||
|
|
||||||
let comments, user
|
let comments, user
|
||||||
switch (sort) {
|
switch (sort) {
|
||||||
|
@ -440,11 +447,11 @@ export default {
|
||||||
FROM "Item"
|
FROM "Item"
|
||||||
JOIN "Item" root ON "Item"."rootId" = root.id
|
JOIN "Item" root ON "Item"."rootId" = root.id
|
||||||
WHERE "Item"."parentId" IS NOT NULL AND "Item".created_at <= $1
|
WHERE "Item"."parentId" IS NOT NULL AND "Item".created_at <= $1
|
||||||
AND (root."subName" = $3 OR (root."subName" IS NOT NULL AND $3 IS NULL))
|
${subClause(sub, 3, 'root')}
|
||||||
${await filterClause(me, models)}
|
${await filterClause(me, models)}
|
||||||
ORDER BY "Item".created_at DESC
|
ORDER BY "Item".created_at DESC
|
||||||
OFFSET $2
|
OFFSET $2
|
||||||
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, sub)
|
LIMIT ${LIMIT}`, decodedCursor.time, decodedCursor.offset, ...subArr)
|
||||||
break
|
break
|
||||||
case 'user':
|
case 'user':
|
||||||
if (!name) {
|
if (!name) {
|
||||||
|
|
Loading…
Reference in New Issue