1
0
mirror of https://github.com/matt-fidd/stratos.git synced 2026-01-01 20:19:30 +00:00

Reduce overhead on calls to getStudents and getTeacher to not fetch objects when they're not required

This commit is contained in:
2022-04-01 12:46:07 +00:00
parent 73b566f6ad
commit 95b1636288

View File

@@ -95,8 +95,12 @@ class Class {
this.getSubject() this.getSubject()
]); ]);
this.teachers = teachers; this.teachers = teachers.objs;
this.students = students; this.teacherIds = teachers.ids;
this.students = students.objs;
this.studentIds = students.ids;
this.subject = subject; this.subject = subject;
return this; return this;
@@ -107,7 +111,7 @@ class Class {
return new (require('./Subject'))(this.#conn, this.subjectId); return new (require('./Subject'))(this.#conn, this.subjectId);
} }
async getTeachers() { async getTeachers(fetchObjects = true) {
const sql = ` const sql = `
select select
a.accountId as id a.accountId as id
@@ -121,14 +125,19 @@ class Class {
const res = await this.#conn.runQuery(sql, [ this.id ]); const res = await this.#conn.runQuery(sql, [ this.id ]);
this.teacherIds = res.map(record => record.id); const ids = res.map(record => record.id);
this.teachers = await this.getUsers(this.teacherIds, 'account'); if (!fetchObjects)
return { ids: ids };
return this.teachers; const teachers = await this.getUsers(
ids,
'account');
return { ids: ids, objs: teachers };
} }
async getStudents() { async getStudents(fetchObjects = true) {
const sql = ` const sql = `
select select
s.studentId as id s.studentId as id
@@ -141,11 +150,16 @@ class Class {
const res = await this.#conn.runQuery(sql, [ this.id ]); const res = await this.#conn.runQuery(sql, [ this.id ]);
this.studentIds = res.map(record => record.id); const ids = res.map(record => record.id);
this.students = await this.getUsers(this.studentIds, 'student'); if (!fetchObjects)
return { ids: ids };
return this.students; const students = await this.getUsers(
ids,
'student');
return { ids: ids, objs: students };
} }
getUsers(ids, type) { getUsers(ids, type) {
@@ -244,9 +258,9 @@ class Class {
if (!validTypes.includes(u.type)) if (!validTypes.includes(u.type))
throw new Error('Invalid user type'); throw new Error('Invalid user type');
this.teachers = await this.getTeachers(); const teachers = (await this.getTeachers(false)).ids;
if (u.type === 'account' && this.teachers.length < 2) if (u.type === 'account' && teachers.length < 2)
throw new Error('Can\'t remove last teacher'); throw new Error('Can\'t remove last teacher');
const sql = ` const sql = `