Эх сурвалжийг харах

Fix SQL error, when running mostViewedVideosConnection query (#128)

* fix mostViewedVideosConnection query

* cr fixes, use inner join, fix count order

* bring back prev ordering approach

* remove redundant template string

* fix ordering
Bartosz Dryl 1 жил өмнө
parent
commit
776338a4fd

+ 19 - 17
src/server-extension/resolvers/VideosResolver/index.ts

@@ -107,7 +107,7 @@ export class VideosResolver {
     idsQuerySql = extendClause(
       idsQuerySql,
       'FROM',
-      `LEFT JOIN "processor"."video_view_event" ` +
+      `INNER JOIN "processor"."video_view_event" ` +
         `ON "video_view_event"."video_id" = "video"."id"` +
         (args.periodDays
           ? ` AND "video_view_event"."timestamp" > '${new Date(
@@ -116,6 +116,7 @@ export class VideosResolver {
           : ''),
       ''
     )
+
     idsQuerySql = overrideClause(idsQuerySql, 'GROUP BY', '"video"."id"')
     idsQuerySql = overrideClause(idsQuerySql, 'ORDER BY', 'COUNT("video_view_event"."id") DESC')
     idsQuerySql = overrideClause(idsQuerySql, 'SELECT', '"video"."id"')
@@ -134,29 +135,30 @@ export class VideosResolver {
 
     let connectionQuerySql: string
 
+    connectionQuerySql = extendClause(
+      connectionQuery.sql,
+      'WHERE',
+      `"video"."id" IN (${ids.map((id) => `'${id}'`).join(', ')})`,
+      'AND'
+    )
+
     const hasPeriodDaysArgAndIsOrderedByViews =
       args.periodDays &&
       (args.orderBy.find((orderByArg) => orderByArg === 'viewsNum_DESC') ||
         args.orderBy.find((orderByArg) => orderByArg === 'viewsNum_ASC'))
 
     if (hasPeriodDaysArgAndIsOrderedByViews) {
-      connectionQuerySql = extendClause(
-        connectionQuery.sql,
-        'FROM',
-        `JOIN unnest('{${ids
-          .map((id) => `"${id}"`)
-          .join(', ')}}'::text[]) WITH ORDINALITY t(id, ord) USING ("id")`,
-        ''
+      const arrayPosition = `array_position(
+        array[${ids.map((id) => `'${id}'`).join(', ')}],
+        video.id  
+      )`
+      connectionQuerySql = connectionQuerySql.replace(
+        '"video"."views_num" DESC',
+        `${arrayPosition} ASC`
       )
-
-      connectionQuerySql = connectionQuerySql.replace('"video"."views_num" DESC', 't.ord')
-      connectionQuerySql = connectionQuerySql.replace('"video"."views_num" ASC', 't.ord DESC')
-    } else {
-      connectionQuerySql = extendClause(
-        connectionQuery.sql,
-        'WHERE',
-        `"video"."id" IN (${ids.map((id) => `'${id}'`).join(', ')})`,
-        'AND'
+      connectionQuerySql = connectionQuerySql.replace(
+        '"video"."views_num" ASC',
+        `${arrayPosition} DESC`
       )
     }