This module enables secure execution of the basic queries. It prevents
deleting any entry that some user might be editing at the moment.
It also prevents editing the entry if someone else is already editing it.
It does so through token exchange system that gives editing rights to whoever
obtains it. The token only works for a particular entry in the database.
Additionally, the functions for critical queries deleteQuery, updateQuery and
requestEditing will never be executed synchronously. This is implemented with an
adapted Bakery algorithm.
- Version:
-
- 1.0
Methods
-
<async, inner> cancelEditing(entryTable, entryID, token)
-
Call this to delete the current token. This will allow other users to edit this entry. If the token is invalid, an error message will be returned.
Parameters:
Name Type Description entryTable
string Name of the table that we are editing. entryID
string Key of the entry that we are editing token
string Token that is used for verifying the edit permissions. Returns:
JSON object that contains response data or error message.- Type
- Promise.<JSON>
-
<async, inner> deleteAccessToken(accessToken)
-
Deletes the access token specified.
Parameters:
Name Type Description accessToken
string Access token we are editing. Returns:
JSON object that contains response data or error message.- Type
- Promise.<JSON>
-
<async, inner> deleteQuery(sql, entryTable, entryID)
-
Call this for DELETE queries.
Parameters:
Name Type Description sql
string The SQL query. entryTable
string Name of the table involved in the query. entryID
string Key of the entry that is being deleted. Returns:
JSON object that contains response data or error message, if the query was unsuccessful.- Type
- Promise.<JSON>
-
<async, inner> editTokensEntryExists(database, entryTable, entryID, token)
-
Check if the entry with the given parameters exists in the EditTokens table.
Parameters:
Name Type Description database
Database entryTable
string entryID
string token
string Returns:
True if such entry exists, false if not.- Type
- Promise.<boolean>
-
<inner> getErrResponse(cause)
-
Generate a JSON error response.
Parameters:
Name Type Description cause
string Returns:
Error response.- Type
- JSON
-
<async, inner> getResult(sql, database, treatResponse)
-
Helper function that executes the query.
Parameters:
Name Type Description sql
string Query to execute. database
Database Database object to execute the query on. treatResponse
function Decide how the response of a successful query is modified. Returns:
Result of the query or error response, if query unsuccessful.- Type
- JSON
-
<inner> getSQLErrorResponse(err)
-
Formats the error response to give some information about the query error (type, code, error number, SQL message).
Parameters:
Name Type Description err
Error Error thrown by the database query function. Returns:
Formatted error response.- Type
- JSON
-
<inner> getSuccessfulResponse(response)
-
Formats a successful response. Enables all responses to follow the same format.
Parameters:
Name Type Description response
JSON Preformatted query-specific response. Returns:
A formatted response.- Type
- JSON
-
<async, inner> insertQuery(sql)
-
Call this for INSERT queries.
Parameters:
Name Type Description sql
string The SQL query. Returns:
JSON object that contains response data or error message, if the query was unsuccessful.- Type
- Promise.<JSON>
-
<async, inner> isValidEntry(database, entryTable, entryID)
-
Checks if the table name and table ID pair actually represent some valid database entry.
Parameters:
Name Type Description database
Database entryTable
string entryID
string Returns:
True if entry exists, false if not.- Type
- Promise.<boolean>
-
<async, inner> nonCriticalQuery(sql, type, treatResponse)
-
A helper function for insertQuery and selectQuery.
Parameters:
Name Type Description sql
string The SQL query. type
string "select" or "insert" treatResponse
function This function is called to format the query response, if the query was successful. Returns:
JSON object that contains response data or error message, if the query was unsuccessful.- Type
- Promise.<JSON>
-
<async, inner> refreshToken(entryTable, entryID, token)
-
Call this to refresh the current token. This will generate a new token with a new expiration time. If the token is invalid, an error message will be returned.
Parameters:
Name Type Description entryTable
string Name of the table that we are editing. entryID
string Key of the entry that we are editing token
string Token that is used for verifying the edit permissions. Returns:
JSON object that contains response data or error message.- Type
- Promise.<JSON>
-
<async, inner> requestEditing(entryTable, entryID)
-
Request editing for the specific entry.
Parameters:
Name Type Description entryTable
string Table that we want to edit. entryID
string ID of the entry in that table that we want to edit. Returns:
Response containing the valid token, or error message.- Type
- Promise.<JSON>
-
<async, inner> selectQuery(sql)
-
Call this for SELECT queries.
Parameters:
Name Type Description sql
string The SQL query. Returns:
JSON object that contains response data or error message, if the query was unsuccessful.- Type
- Promise.<JSON>
-
<inner> sleep(ms)
-
Await for this function to pause execution for a certain time.
Parameters:
Name Type Description ms
number Time in milliseconds Returns:
- Type
- Promise
-
<inner> startsWith(toCheck, compareTo)
-
Shorthand function for comparing the start of a string. Leading spaces and capitalisation are ignored.
Parameters:
Name Type Description toCheck
string A string the start of which we want to check. compareTo
string A string that should appear at the start of the toCheck. Returns:
True if toCheck starts with compareTo, false otherwise.- Type
- boolean
-
<async, inner> updateAccessToken(accessToken, newExpiration)
-
Updates the expiration of an access token specified.
Parameters:
Name Type Description accessToken
string Access token we are editing. newExpiration
Date New expiration for this token. Returns:
JSON object that contains response data or error message.- Type
- Promise.<JSON>
-
<async, inner> updateQuery(sql, entryTable, entryID, token)
-
Call this for UPDATE queries.
Parameters:
Name Type Description sql
string The SQL query. entryTable
string Name of the table involved in the query. entryID
string Key of the entry that is being updated. token
string Token that is used for verifying the edit permissions. Returns:
JSON object that contains response data or error message, if the query was unsuccessful.- Type
- Promise.<JSON>