I recently refactored code for getting posts from a Postgres database from this:
const getPostByPk = async (token, post_id) => {
const requestor_id = await verifyToken(token)
const post = await Posts.findByPk(post_id, { raw: true });
const isLiked = await Likes.findOne({
where: {
[Op.and]: [{ user_id: requestor_id }, { post_id: post_id }],
},
});
return {
...post,
isLiked: isLiked != null,
isAuthor: (user_id == requestor_id),
};
};
const getPostByLoc = async (token, college) => {
const post_ids = await Posts.findAll({
where: {
college: college,
public: true
},
attributes: ["post_id"],
})
const posts = await Promise.all(
post_ids.map((post) => getPostByPk(token, post.post_id))
);
return posts;
};
This became painfully slow after more than a few hundred posts, so I switched to eager loading w/ sequelize associations:
async function getCleanedPosts(requester_id, queryObject) {
const posts = await Posts.findAll({
where: queryObject,
include: [
{
model: User,
},
{
model: Loops,
required: false
},
{
model: Likes,
where: {
user_id: requester_id
},
required: false
}
]
});
return posts.map(post => {
const {torus_user, like_relationships, loop, ...cleanedPost} = post.toJSON()
const isLiked = like_relationships.length > 0;
return {
...cleanedPost,
isLiked,
loop_name: loop?.name,
loop_id: loop?.loop_id,
isAuthor: torus_user.user_id === requester_id,
};
});
}
Though this is much faster, I’m now often encountering some pretty unsettling errors with just a few active users at a time.
ConnectionAcquireTimeoutError [SequelizeConnectionAcquireTimeoutError]: Operation timeout
Causes all requests to fail often but then goes away after a few minutes or a restart
I tried updating the Sequelize pool configuration
const sequelize = new Sequelize(
process.env.DB_DATABASE,
process.env.DB_USERNAME,
process.env.DB_PASSWORD,
{
host: process.env.DB_HOST,
dialect: "postgres",
pool: {
max: 30,
min: 0,
acquire: 60000,
idle: 10000
}
},
);
But this just caused numerous ConnectionError [SequelizeConnectionError]: remaining connection slots are reserved for non-replication superuser connections
errors
Any help is much appreciated!