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 entryTablestring Name of the table that we are editing. entryIDstring Key of the entry that we are editing tokenstring 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 accessTokenstring 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 sqlstring The SQL query. entryTablestring Name of the table involved in the query. entryIDstring 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 databaseDatabase entryTablestring entryIDstring tokenstring Returns:
True if such entry exists, false if not.- Type
- Promise.<boolean>
-
<inner> getErrResponse(cause)
-
Generate a JSON error response.
Parameters:
Name Type Description causestring Returns:
Error response.- Type
- JSON
-
<async, inner> getResult(sql, database, treatResponse)
-
Helper function that executes the query.
Parameters:
Name Type Description sqlstring Query to execute. databaseDatabase Database object to execute the query on. treatResponsefunction 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 errError 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 responseJSON Preformatted query-specific response. Returns:
A formatted response.- Type
- JSON
-
<async, inner> insertQuery(sql)
-
Call this for INSERT queries.
Parameters:
Name Type Description sqlstring 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 databaseDatabase entryTablestring entryIDstring 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 sqlstring The SQL query. typestring "select" or "insert" treatResponsefunction 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 entryTablestring Name of the table that we are editing. entryIDstring Key of the entry that we are editing tokenstring 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 entryTablestring Table that we want to edit. entryIDstring 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 sqlstring 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 msnumber 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 toCheckstring A string the start of which we want to check. compareTostring 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 accessTokenstring Access token we are editing. newExpirationDate 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 sqlstring The SQL query. entryTablestring Name of the table involved in the query. entryIDstring Key of the entry that is being updated. tokenstring 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>