Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: added lean to a few queries #128

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/models/setting.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mongoose from 'mongoose';

const LeaderboardSchema = mongoose.Schema({
const LeaderboardSchema = new mongoose.Schema({
freezed: {
type: Boolean,
default: false
Expand All @@ -15,7 +15,7 @@ const LeaderboardSchema = mongoose.Schema({
}
});

const SettingSchema = mongoose.Schema(
const SettingSchema = new mongoose.Schema(
{
submission_deadline: {
type: Date,
Expand Down
2 changes: 1 addition & 1 deletion src/models/submission.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';
import mongoosePaginate from 'mongoose-paginate-v2';

const SubmissionSchema = mongoose.Schema(
const SubmissionSchema = new mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
Expand Down
2 changes: 1 addition & 1 deletion src/repository/question/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const getQuestionById = (id, user) => {
};

export const findAndUpdateQuestion = (filters, data) => {
return Question.findOneAndUpdate(filters, data, { new: true });
return Question.findOneAndUpdate(filters, data, { new: true }).lean();
};

export const deleteAQuestion = (filters) => {
Expand Down
2 changes: 1 addition & 1 deletion src/repository/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const getSettingsDoc = () => {
};

export const updateSettingsDoc = async (settings) => {
await Setting.findOneAndUpdate({}, settings, { new: true, upsert: true });
await Setting.findOneAndUpdate({}, settings, { new: true, upsert: true }).lean();
};

export const getRegistrationDeadline = async () => {
Expand Down
16 changes: 5 additions & 11 deletions src/repository/submission.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { moduleLogger } from '@sliit-foss/module-logger';
import { ROLE } from '@/constants';
import { Submission } from '@/models';
import { prefixObjectKeys } from '@/utils';

const logger = moduleLogger('Submission-repository');

export const insertSubmission = (userId, question, link) => {
return Submission.create({
user: userId,
Expand All @@ -30,14 +27,11 @@ export const getSubmissions = ({ sort = {}, filter = {}, page, limit = 10 }) =>
collation: {
locale: 'en'
},
populate
populate,
lean: true
};
return page
? Submission.paginate(filter, options).catch((err) => {
logger.error(`An error occurred when retrieving submissions - err: ${err.message}`);
throw err;
})
: Submission.find(filter).sort(sort).populate(populate).lean();

return page ? Submission.paginate(filter, options) : Submission.find(filter).sort(sort).populate(populate).lean();
};

export const getSubmissionById = (id) => {
Expand All @@ -53,7 +47,7 @@ export const insertGrade = async (submission, score, automated, userId) => {
{ _id: submission },
{ score, graded_by: userId, automatically_graded: automated },
{ upsert: true }
);
).lean();
};

export const getDistinctSubmissions = (questionId) => {
Expand Down
8 changes: 1 addition & 7 deletions src/repository/user.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { moduleLogger } from '@sliit-foss/module-logger';
import { ROLE } from '@/constants';
import { User } from '@/models';

const logger = moduleLogger('User-repository');

export const createUser = async (user) => {
const newUser = (await new User(user).save()).toObject();
delete newUser.password;
Expand Down Expand Up @@ -59,10 +56,7 @@ export const getAllUsers = ({ sort = {}, filter = {}, page, limit = 10 }) => {
{ $unset: ['password', 'verification_code', 'submissions'] }
]);

return (page ? User.aggregatePaginate(pipeline, options) : pipeline).catch((err) => {
logger.error(`An error occurred when retrieving users - err: ${err.message}`);
throw err;
});
return page ? User.aggregatePaginate(pipeline, options) : pipeline;
};

export const getOneUser = async (filters, returnPassword = false) => {
Expand Down
Loading