In general we prefer simplicity. We standardize on JavaScript/TypeScript (Node.js) and SQL (PostgreSQL) as the languages of implementation and try to minimize the number of complex libraries or frameworks being used. The website is server-side generated pages with minimal client-side JavaScript.
-
The questions and assessments for a course are stored in a git repository. This is synced into the database by the course instructor and DB data is updated or added to represent the course. Students then interact with the course website by doing questions, with the results being stored in the DB. The instructor can view the student results on the website and download CSV files with the data.
-
The majority of course content and configuration is done via plain text files in the git repository, which is the master source for this data.
-
All student data is all stored in the DB and is not pushed back into the git repository or disk at any point.
-
Integration tests are stored in the
apps/prairielearn/src/tests/directory. -
Unit tests are typically located next to the file under test, with the filename ending in
.test.ts. For instance, tests forfoo.tswould be infoo.test.tsin the same directory. -
To run the tests during development, see Running the test suite.
-
The tests are run by GitHub Actions on every push to GitHub.
-
The tests are mainly integration tests that start with a blank database, run the server to initialize the database, load the
testCourse, and then emulate a client web browser that answers questions on assessments. If a test fails then it is often easiest to debug by recreating the error by doing questions yourself against a locally-running server. -
If the
PL_KEEP_TEST_DBenvironment is set, the test database (normallypltest_1,pltest_2, etc.) won't be removed when testing ends. This allows you inspect the state of the database whenever your testing ends. The database will get overwritten when you start a new test run.
-
Use the debug package to help trace execution flow in JavaScript. To run the server with debugging output enabled:
DEBUG=* make dev -
To just see debugging logs from PrairieLearn you can use:
DEBUG=prairielearn:* make dev -
To insert more debugging output, import
debugand use it like this:import debugfn from 'debug'; const debug = debugfn('prairielearn:my-file'); // in some function later debug('func()', 'param:', param);
-
UnhandledPromiseRejectionWarningerrors are frequently due to improper async/await handling. Make sure you are calling async functions withawait/.then()/.catch()as appropriate, and that async functions are not being called from callback-style code without acallbackify(). To get more information, Node can be run with the--trace-warningsflag. For example,node_modules/.bin/mocha --trace-warnings tests/index.js.
- Make sure you have the JavaScript Console open in your browser and reload the page.
-
Use the
psqlcommandline interface to test SQL separately. A default development PrairieLearn install uses thepostgresdatabase, so you should run:psql postgres
-
To debug syntax errors in a stored procedure, import it manually with
\i filename.sqlinpsql. -
To follow execution flow in PL/pgSQL use
RAISE NOTICE. This will log to the console when run frompsqland to the server log file when run from within PrairieLearn. The syntax is:RAISE NOTICE 'This is logging: % and %', var1, var2; -
To manually run a function:
SELECT the_sql_function (arg1, arg2);
-
Express is used as the web framework.
-
All pages are server-side rendered and we try and minimize the amount of client-side JavaScript. Client-side JS should use vanilla JavaScript/TypeScript where possible, but third-party libraries may be used when appropriate.
-
Each web page typically has all its files in a single directory, with the directory, the files, and the URL all named the same. Not all pages need all files. For a real-world example, consider the page where users can accept the PrairieLearn terms and conditions, located at
apps/prairielearn/src/ee/pages/terms. That directory contains the following files:terms.ts: The main entry point for the page. It runs SQL queries and renders a template.terms.sql: All SQL queries for the page.terms.html.ts: The template for the page. Exports a function that returns an HTML document.
-
When possible, prefer explicitly passing individual typed properties to templates instead of adding properties to
res.locals. However,res.localsmay be used for data coming from middlewares that will be used on many pages. -
Use
@prairielearn/htmlto generate HTML pages. This uses HTML tagged-template literals to generate HTML, which in turn makes it easy to get full type-checking. -
Reused templates are stored in the
apps/prairielearn/src/components/directory. These should generally accept an object with properties instead of being passed the fullres.localsobject.
-
Use Bootstrap as the style. As of 2024-06-05 we are using v4.
-
Local CSS rules go in
public/stylesheets/local.css. Try to minimize use of this and use plain Bootstrap styling wherever possible. -
Buttons should use the
<button>element when they take actions and the<a>element when they are simply links to other pages. We should not use<a role="button">to fake a button element. Buttons that do not submit a form should always start with<button type="button" class="btn ...">, wheretype="button"specifies that they don't submit.
-
PostgreSQL v15 is used as the database.
-
The PostgreSQL manual is an excellent reference.
-
Write raw SQL rather than using a ORM library. This reduces the number of frameworks/languages needed.
-
Prefer implementing complex logic in TypeScript instead of inside queries.
-
Use the SQL convention of
snake_casefor names. Also use the same convention in JavaScript for names that are the same as in SQL, so thequestion_idvariable in SQL is also calledquestion_idin JavaScript code. -
Use uppercase for SQL reserved words like
SELECT,FROM,AS, etc. -
SQL code should not be inline in JavaScript files. Instead it should be in a separate
.sqlfile, following the Yesql concept. Eachfilename.jsfile will normally have a correspondingfilename.sqlfile in the same directory. The.sqlfile should look like:-- BLOCK select_question SELECT * FROM questions WHERE id = $question_id; -- BLOCK insert_submission INSERT INTO submissions (submitted_answer) VALUES ($submitted_answer) RETURNING *;
From JavaScript you can then do:
import { loadSqlEquiv, queryRow } from '@prairielearn/postgres';
import { QuestionSchema } from './lib/db-types.js';
const sql = loadSqlEquiv(import.meta.url);
const question = await queryRow(sql.select_question, { question_id: 45 }, QuestionSchema);-
To keep SQL code organized it is a good idea to use CTEs (
WITHqueries). These are formatted like:WITH first_preliminary_table AS ( SELECT -- first preliminary query ), second_preliminary_table AS ( SELECT -- second preliminary query ) SELECT -- main query here FROM first_preliminary_table AS fpt, second_preliminary_table AS spt;
-
Stored procedures are created by the files in
sprocs/. To call a stored procedure from JavaScript, use code like:const workspace_id = 1342; const message = 'Startup successful'; await sqldb.callAsync('workspaces_message_update', [workspace_id, message]);
-
The stored procedures are all contained in a separate database schema with a name like
server_2021-07-07T20:25:04.779Z_T75V6Y. To see a list of the schemas use the\dncommand inpsql. -
To be able to use the stored procedures from the
psqlcommand line it is necessary to get the most recent schema name using\dnand set thesearch_pathto use this quoted schema name and thepublicschema:set search_path to "server_2021-07-07T20:25:04.779Z_T75V6Y",public; -
During startup we initially have no non-public schema in use. We first run the migrations to update all tables in the
publicschema, then we callsqldb.setRandomSearchSchemaAsync()to activate a random per-execution schema, and we run the sproc creation code to generate all the stored procedures in this schema. This means that every invocation of PrairieLearn will have its own locally-scoped copy of the stored procedures which are the correct versions for its code. This lets us upgrade PrairieLearn servers one at a time, while old servers are still running with their own copies of their sprocs. When PrairieLearn first starts up it hassearch_path = public, but later it will havesearch_path = "server_2021-07-07T20:25:04.779Z_T75V6Y",publicso that it will first search the random schema and then fall back topublic. The naming convention for the random schema uses the local instance name, the date, and a random string. Note that schema names need to be quoted using double-quotations inpsqlbecause they contain characters such as hyphens. -
For more details see
sprocs/array_and_number.sqland comments inserver.jsnear the call tosqldb.setRandomSearchSchemaAsync().
- The most important tables in the database are shown in the diagram below (also as a PDF image).
-
Detailed descriptions of the format of each table are in the list of DB tables.
-
Each table has an
idnumber that is used for cross-referencing. For example, each row in thequestionstable has anidand other tables will refer to this as aquestion_id. For legacy reasons, there are two exceptions to this rule:- Tables that reference the
pl_coursestable usecourse_idinstead ofpl_course_id. - The
userstable has auser_idprimary key instead ofid.
- Tables that reference the
-
Each user is stored as a single row in the
userstable. -
The
pl_coursestable has one row for each course, likeTAM 212. -
The
course_instancestable has one row for each semester ("instance") of each course, with thecourse_idindicating which course it belongs to. -
Every question is a row in the
questionstable, and thecourse_idshows which course it belongs to. All the questions for a course can be thought of as the "question pool" for that course. This same pool is used for all semesters (all course instances). -
Assessments are stored in the
assessmentstable and each assessment row has acourse_instance_idto indicate which course instance (and hence which course) it belongs to. An assessment is something like "Homework 1" or "Exam 3". To determine this we can use theassessment_set_idandnumberof each assessment row. -
Each assessment has a list of questions associated with it. This list is stored in the
assessment_questionstable, where each row has aassessment_idandquestion_idto indicate which questions belong to which assessment. For example, there might be 20 different questions that are on "Exam 1", and it might be the case that each student gets 5 of these questions randomly selected. -
Each student will have their own copy of an assessment, stored in the
assessment_instancestable with each row having auser_idandassessment_id. This is where the student's score for that assessment is stored. -
The selection of questions that each student is given on each assessment is in the
instance_questionstable. Here each row has anassessment_question_idand anassessment_instance_idto indicate that the corresponding question is on that assessment instance. This row will also store the student's score on this particular question. -
Questions can randomize their parameters, so there are many possible variants of each question. These are stored in the
variantstable with aninstance_question_idindicating which instance question the variant belongs to. -
For each variant of a question that a student sees they will have submitted zero or more
submissionswith avariant_idto show what it belongs to. The submissions row also contains information the submitted answer and whether it was correct.
- See the list of DB tables, with the ER (entity relationship) diagram below (PDF ER diagram).
-
Tables have plural names (e.g.
assessments) and always have a primary key calledid. The foreign keys pointing to this table are non-plural, likeassessment_id. When referring to this use an abbreviation of the first letters of each word, likeaiin this case. The only exceptions areasetforassessment_sets(to avoid conflicting with the SQLASkeyword),topfortopics, andtagfortags(to avoid conflicts). This gives code like:-- select all active assessment_instances for a given assessment SELECT ai.* FROM assessments AS a JOIN assessment_instances AS ai ON (ai.assessment_id = a.id) WHERE a.id = 45 AND ai.deleted_at IS NULL;
-
We (almost) never delete student data from the DB. To avoid having rows with broken or missing foreign keys, course configuration tables (e.g.
assessments) can't be actually deleted. Instead they are "soft-deleted" by setting thedeleted_atcolumn to non-NULL. This means that when using any soft-deletable table we need to have aWHERE deleted_at IS NULLto get only the active rows.
-
DB access is via the
@prairielearn/postgrespackage. This wraps the node-postgres library. -
For single queries we normally use the following pattern, which automatically uses connection pooling from node-postgres and safe variable interpolation with named parameters and prepared statements:
const questions = await queryRows( sql.select_questions_by_course, { course_id: 45 }, QuestionSchema, );
Where the corresponding filename.sql file contains:
-- BLOCK select_questions_by_course
SELECT
*
FROM
questions
WHERE
course_id = $course_id;-
For queries where it would be an error to not return exactly one result row:
const question = await queryRow(sql.block_name, QuestionSchema);
-
Use explicit row locking whenever modifying student data related to an assessment. This must be done within a transaction. The rule is that we lock either the variant (if there is no corresponding assessment instance) or the assessment instance (if we have one). It is fine to repeatedly lock the same row within a single transaction, so all functions involved in modifying elements of an assessment (e.g., adding a submission, grading, etc) should call a locking function when they start. All locking functions are equivalent in their action, so the most convenient one should be used in any given situation:
Locking function Argument assessment_instances_lockassessment_instance_idinstance_questions_lockinstance_question_idvariants_lockvariant_idsubmission_locksubmission_id -
To pass an array of parameters to SQL code, use the following pattern, which allows zero or more elements in the array. This replaces
$points_listwithARRAY[10, 5, 1]in the SQL. It's required to specify the type of array in case it is empty:await sqldb.queryAsync(sql.insert_assessment_question, { points_list: [10, 5, 1], });
-- BLOCK insert_assessment_question INSERT INTO assessment_questions (points_list) VALUES ($points_list::INTEGER[]);
-
To use a JavaScript array for membership testing in SQL use
unnest()like:const questions = await sqldb.queryRows( sql.select_questions, { id_list: [7, 12, 45] }, QuestionSchema, );
-- BLOCK select_questions SELECT * FROM questions WHERE id IN ( SELECT unnest($id_list::INTEGER[]) );
-
To pass a lot of data to SQL a useful pattern is to send a JSON object array and unpack it in SQL to the equivalent of a table. This is the pattern used by the "sync" code, such as sprocs/sync_news_items.sql. For example:
let data = [ { a: 5, b: 'foo' }, { a: 9, b: 'bar' }, ]; await sqldb.queryAsync(sql.insert_data, { data: JSON.stringify(data), });
-- BLOCK insert_data INSERT INTO my_table (a, b) SELECT * FROM jsonb_to_recordset($data) AS (a INTEGER, b TEXT);
-
To use a JSON object array in the above fashion, but where the order of rows is important, use
ROWS FROM () WITH ORDINALITYto generate a row index like this:-- BLOCK insert_data INSERT INTO my_table (a, b, order_by) SELECT * FROM ROWS FROM (jsonb_to_recordset($data) AS (a INTEGER, b TEXT)) WITH ORDINALITY AS data (a, b, order_by);
-
New code in PrairieLearn should use
async/awaitwhenever possible. -
Use the async library for complex control flow or when mixing Promise-based and callback-based code.
-
Express can't directly use async route handlers. Instead we use express-async-handler like this:
import asyncHandler from 'express-async-handler'; router.get( '/', asyncHandler(async (req, res, next) => { // can use "await" here }), );
-
We distinguish between authentication and authorization. Authentication occurs as the first stage in server response and the authenticated user data is stored as
res.locals.authn_user. -
The authentication flow is:
-
We first redirect to a remote authentication service (e.g. SAML SSO, Google, Microsoft).
-
The remote authentication service redirects back to a callback URL, e.g.
/pl/oauth2callbackfor Google. These endpoints confirm authentication, create the user in theuserstable if necessary, set a signedpl_authncookie in the browser with the authenticateduser_id, and then redirect to the main PL homepage. This cookie is set with theHttpOnlyattribute, which prevents client-side JavaScript from reading the cookie. -
Every other page authenticates using the signed browser
pl_authncookie. This is read bymiddlewares/authn.jswhich checks the signature and then loads the user data from the DB using theuser_id, storing it asres.locals.authn_user.
-
-
Similar to unix, we distinguish between the real and effective user. The real user is stored as
res.locals.authn_userand is the user that authenticated. The effective user is stored asres.locals.user. Only users withrole = TAor higher can set an effective user that is different from their real user. Moreover, users withrole = TAor higher can also set an effectiveroleandmodethat is different to the real values. -
Authorization occurs at multiple levels:
-
The
course_instancechecks authorization based on theauthn_user. -
The
course_instanceauthorization is checked against the effectiveuser. -
The
assessmentchecks authorization based on the effectiveuser,role,mode, anddate.
-
-
All state-modifying requests must (normally) be POST and all associated data must be in the body. GET requests may use query parameters for viewing options only.
-
Use the Post/Redirect/Get pattern for all state modification. This means that the initial GET should render the page with a
<form>that has noactionset, so it will submit back to the current page. This should be handled by a POST handler that performs the state modification and then issues a redirect back to the same page as a GET:router.post( '/', asyncHandler(async (req, res) => { if (req.body.__action == 'enroll') { await queryAsync(sql.enroll, { course_instance_id: req.body.course_instance_id, user_id: res.locals.authn_user.user_id, }); res.redirect(req.originalUrl); } else { throw new error.HttpStatusError(400, `unknown __action: ${req.body.__action}`); } }), );
-
To defeat CSRF (Cross-Site Request Forgery) we use the Encrypted Token Pattern. This stores an HMAC-authenticated token inside the POST data.
-
All data modifying requests should come from
formelements like:<form name="enroll-form" method="POST"> <input type="hidden" name="__action" value="enroll" /> <input type="hidden" name="__csrf_token" value="${__csrf_token}" /> <input type="hidden" name="course_instance_id" value="56" /> <button type="submit" class="btn btn-info">Enroll in course instance 56</button> </form>
-
The
res.locals.__csrf_tokenvariable is set and checked by early-stage middleware, so no explicit action is needed on each page.
-
We use Winston for logging to the console and to files:
import { logger } from '@prairielearn/logger'; logger.info('This is an info message'); logger.error('This is an error message'); // This will be logged to the log file, but not to the console: logger.verbose('This is a verbose message');
-
All
loggerfunctions have a mandatory first argument that is a string, and an optional second argument that is an object containing useful information. It is important to always provide a string as the first argument.
ESLint and Prettier are used to enforce consistent code conventions and formatting throughout the codebase. See .eslintrc.js and .prettierrc.json in the root of the PrairieLearn repository to view our specific configuration. The repo includes an .editorconfig file that most editors will detect and use to automatically configure things like indentation. If your editor doesn't natively support an EditorConfig file, there are plugins available for most other editors.
For Python files, ruff is used for autoformatting and enforcing code conventions, and Pyright is used for static typechecking. See pyproject.toml in the root of the PrairieLearn repository to view our specific configuration. We encourage all new Python code to include type hints for use with the static typechecker, as this makes it easier to read, review, and verify contributions.
To lint the code, use make lint. This is also run by the CI tests.
To automatically fix lint and formatting errors, run make format.
-
The core files involved in question rendering are lib/question-render.js, lib/question-render.sql, and components/QuestionContainer.html.ts.
-
The above files are all called/included by each of the top-level pages that needs to render a question (e.g.,
pages/instructorQuestionPreview,pages/studentInstanceQuestion, etc). Unfortunately the control-flow is complicated because we need to calllib/question-render.jsduring page data load, store the data it generates, and then later include thecomponents/QuestionContainer.html.tstemplate to actually render this data. -
For example, the exact control-flow for
pages/instructorQuestionis:-
The top-level page
pages/instructorQuestion/instructorQuestion.jscode callslib/question-render.getAndRenderVariant(). -
getAndRenderVariant()inserts data intores.localsfor later use bycomponents/QuestionContainer.html.ts. -
The top-level page code renders the top-level template
pages/instructorQuestion/instructorQuestion.html.ts, which then includescomponents/QuestionContainer.html.ts. -
components/QuestionContainer.html.tsrenders the data that was earlier generated bylib/question-render.js.
-
-
There are three levels at which “open” status is tracked, as follows. If
open = falsefor any object then it will block the creation of new objects below it. For example, to create a new submission the corresponding variant, instance_question, and assessment_instance must all be open.Variable Allow new instance_questionsAllow new variantsAllow new submissionsassessment_instance.open✓ ✓ ✓ instance_question.open✓ ✓ variant.open✓
-
We distinguish between two different types of student errors:
-
The answer might be not be gradable (
submission.gradable = false). This could be due to a missing answer, an invalid format (e.g., entering a string in a numeric input), or a answer that doesn't pass some basic check (e.g., a code submission that didn't compile). This can be discovered during either the parsing or grading phases. In such a case thesubmission.format_errorsobject should store information on what was wrong to allow the student to correct their answer. A submission withgradable = falsewill not cause any updating of points for the question. That is, it acts like a saved-but-not-graded submission, in that it is recorded but has no impact on the question. Ifgradable = falsethen thescoreandfeedbackwill not be displayed to the student. -
The answer might be gradable but incorrect. In this case
submission.gradable = truebutsubmission.score = 0(or less than 1 for a partial score). If desired, thesubmission.feedbackobject can be set to give information to the student on what was wrong with their answer. This is not necessary, however. Ifsubmission.feedbackis set then it will be shown to the student along with theirsubmission.scoreas soon as the question is graded.
-
-
There are three levels of errors that can occur during the creation, answering, and grading of a question:
Error level Caused Stored Reported Effect System errors Internal PrairieLearn errors On-disk logs Error page Operation is blocked. Data is not saved to the database. Question errors Errors in question code issuestableIssue panels on the question page variant.broken_at != nullorsubmission.broken == true. Operation completes, but future operations are blocked.Student errors Invalid data submitted by the student (unparsable or ungradable) submission.gradableset tofalseand details are stored insubmission.format_errorsInside the rendered submission panel The submission is not assigned a score and no further action is taken (e.g., points are changed for the instance question). The student can resubmit to correct the error. -
The important variables involved in tracking question errors are:
Variable Error level Description variant.broken_atQuestion error Set to NOW()if there were question code errors in generating the variant. Such a variant will be not haverender()functions called, but will instead be displayed asThis question is broken.submission.brokenQuestion error Set to trueif there question code errors in parsing or grading the variant. Aftersubmission.brokenistrue, no further actions will be taken with the submission.issuestableQuestion error Rows are inserted to record the details of the errors that caused variant.broken != nullorsubmission.broken == trueto be set totrue.submission.gradableStudent error Whether this submission can be given a score. Set to falseif format errors in thesubmitted_answerwere encountered during either parsing or grading.submission.format_errorsStudent error Details on any errors during parsing or grading. Should be set to something meaningful if gradable = falseto explain what was wrong with the submitted answer.submission.graded_atNone NULL if grading has not yet occurred, otherwise a timestamp. submission.scoreNone Final score for the submission. Only used if gradable = trueandgraded_atis not NULL.submission.feedbackNone Feedback generated during grading. Only used if gradable = trueandgraded_atis not NULL. -
Note that
submission.format_errorsstores information about student errors, while theissuestable stores information about question code errors. -
The question flow is shown in the diagram below (also as a PDF image).
You should almost always use the === operator for comparisons; this is enforced with an ESLint rule.
The only case where the == operator is frequently useful is for comparing entity IDs that may be coming from the client/database/etc. These may be either strings or numbers depending on where they're coming from or how they're fetched. To make it abundantly clear that ids are being compared, you should use the idsEqual utility:
import { idsEqual } from './lib/id';
console.log(idsEqual(12345, '12345'));
// > true"Modern" queries that use Zod validation will automatically coerce all IDs to strings. If you're confident that data on both sides of the comparison is coming from a Zod-validated query, you can use the === operator directly.



