@replyke/express
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
101 lines (100 loc) • 3.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.entityParams = exports.commentParams = void 0;
const sequelize_1 = require("sequelize");
const models_1 = require("../models");
exports.commentParams = {
attributes: {
include: [
[
// Subquery to count replies for each comment
sequelize_1.Sequelize.literal(`(
SELECT COUNT(*)::int
FROM "Comments" AS "reply"
WHERE "reply"."parentId" = "Comment"."id"
AND "reply"."deletedAt" IS NULL
AND "reply"."parentDeletedAt" IS NULL
)`),
"repliesCount",
],
],
},
include: [
{
model: models_1.User,
as: "user", // Assuming you used `as: "user"` in the Comment-User association
attributes: {
exclude: [
"hash",
"salt",
"email",
"isVerified",
"isActive",
"lastActive",
"secureMetadata",
],
}, // Customize fields as needed
},
],
};
exports.entityParams = {
attributes: {
include: [
[
// Subquery to count comments for each entity
sequelize_1.Sequelize.literal(`(
SELECT COUNT(*)::int
FROM "Comments" AS "comment"
WHERE "comment"."entityId" = "Entity"."id"
AND "comment"."deletedAt" IS NULL
AND "comment"."parentDeletedAt" IS NULL
)`),
"repliesCount",
],
],
},
include: [
{
model: models_1.User,
as: "user", // Assuming you used `as: "user"` in the Comment-User association
attributes: {
exclude: [
"hash",
"salt",
"email",
"isVerified",
"isActive",
"lastActive",
"secureMetadata",
],
}, // Customize fields as needed
},
],
};
// ** This was part of the entityParams to also attach top comment to each entity but it slows things down
// [
// // Subquery to get the topComment with user details
// Sequelize.literal(`(
// SELECT row_to_json(c)
// FROM (
// SELECT
// "comment"."id",
// "comment"."content",
// array_length("comment"."upvotes", 1) AS "upvotesCount",
// json_build_object(
// 'id', "user"."id",
// 'name', "user"."name",
// 'username', "user"."username",
// 'avatar', "user"."avatar"
// ) AS "user"
// FROM "Comments" AS "comment"
// LEFT JOIN "Users" AS "user" ON "user"."id" = "comment"."userId"
// WHERE "comment"."entityId" = "Entity"."id"
// AND "comment"."deletedAt" IS NULL
// AND "comment"."parentDeletedAt" IS NULL
// ORDER BY array_length("comment"."upvotes", 1) DESC
// LIMIT 1
// ) c
// )`),
// "topComment",
// ] as [Literal, string],