You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Raw SQL query vs Query builders (Knex.js/Objection.js)
Raw SQL
staticasynclistPostsPublicRaw({ offset, limit, filter ={}}={}){assert.integer(limit,{required: true,min: 10})assert.integer(offset,{required: true,min: 0})assert.validate(filter.blogId,BlogModel.schema.id,{required: true})assert.validate(filter.stackId,StackModel.schema.id)constcountWithStackIdQuery=` SELECT COUNT("posts"."id") FROM "posts" FULL JOIN "stacks" ON "posts"."stackId" = "stacks"."id" WHERE "posts"."blogId" = ? AND "posts"."private" = false AND "stacks"."private" = false AND "posts"."isDraft" = false AND "posts"."stackId" = ?; `constcountNoStackIdQuery=` SELECT COUNT("posts"."id") FROM "posts" FULL JOIN "stacks" ON "posts"."stackId" = "stacks"."id" WHERE "posts"."blogId" = ? AND "posts"."private" = false AND "stacks"."private" = false AND "posts"."isDraft" = false OR "posts"."blogId" = ? AND "posts"."private" = false AND "posts"."stackId" IS NULL AND "posts"."isDraft" = false; `constcountQuery=filter.stackId ? [countWithStackIdQuery,[filter.blogId,filter.stackId]] : [countNoStackIdQuery,[filter.blogId,filter.blogId]]constpostsWithStackIdQuery=` SELECT "posts".* FROM "posts" FULL JOIN "stacks" ON "posts"."stackId" = "stacks"."id" WHERE "posts"."blogId" = ? AND "posts"."private" = false AND "stacks"."private" = false AND "posts"."isDraft" = false AND "posts"."stackId" = ? ORDER BY "posts"."createdAt" DESC LIMIT ? OFFSET ?;`constpostsNoStackIdQuery=` SELECT "posts".* FROM "posts" FULL JOIN "stacks" ON "posts"."stackId" = "stacks"."id" WHERE "posts"."blogId" = ? AND "posts"."private" = false AND "stacks"."private" = false AND "posts"."isDraft" = false OR "posts"."blogId" = ? AND "posts"."private" = false AND "posts"."stackId" IS NULL AND "posts"."isDraft" = false ORDER BY "posts"."createdAt" DESC LIMIT ? OFFSET ?;`constpostsQuery=filter.stackId ? [postsWithStackIdQuery,[filter.blogId,filter.stackId,limit,offset]] : [postsNoStackIdQuery,[filter.blogId,filter.blogId,limit,offset]]constcount=awaitthis.knex().raw(...countQuery)consttotal=Number(count.rows[0].count)if(total){const{ rows }=awaitthis.knex().raw(...postsQuery)return{results: rows, total }}returnthis.emptyPageResponse()}