Database

Database Facade

Database Facade.

class db.facade.DBFacade(db)

A database facade that gives an overall API for any databases.

Currently, we plan on having DynamoDB, but other databases, such as MongoDB or Postgres are also being considered. Please use this class instead of db/dynamodb.py, because we might change the databases, but the facade would stay the same.

__init__(db)

Initialize facade using a given class.

Currently, we can only initialize with db.dynamodb.DynamoDB.

Parameters:db (DynamoDB) – Database class for API calls
bulk_retrieve(Model, ks)

Retrieve a list of models from the database.

Keys not found in the database will be skipped.

Parameters:
  • Model (Type[~T]) – the actual class you want to retrieve
  • ks (List[str]) – retrieve based on this key (or ID)
Return type:

List[~T]

Returns:

a list of models Model

delete(Model, k)

Remove an object from a table.

Parameters:
  • Model (Type[~T]) – table type to remove the object from
  • k (str) – ID or key of the object to remove (must be primary key)
query(Model, params=[])

Query a table using a list of parameters.

Returns a list of Model that have all of the attributes specified in the parameters. Every item in parameters is a tuple, where the first element is the user attribute, and the second is the value.

Example:

ddb = DynamoDb(config)
users = ddb.query(User, [('platform', 'slack')])

If you try to query a table without any parameters, the function will return all objects of that table.:

projects = ddb.query(Project)

Attributes that are sets (e.g. team.member, project.github_urls) would be treated differently. This function would check to see if the entry contains a certain element. You can specify multiple elements, but they must be in different parameters (one element per tuple).:

teams = ddb.query(Team, [('members', 'abc123'),
                         ('members', '231abc')])
Parameters:
  • Model (Type[~T]) – type of list elements you’d want
  • params (List[Tuple[str, str]]) – list of tuples to match
Return type:

List[~T]

Returns:

a list of Model that fit the query parameters

query_or(Model, params=[])

Query a table using a list of parameters.

Returns a list of Model that have one of the attributes specified in the parameters. Some might say that this is a union of the parameters. Every item in parameters is a tuple, where the first element is the user attribute, and the second is the value.

Example:

ddb = DynamoDb(config)
users = ddb.query_or(User, [('platform', 'slack')])

If you try to query a table without any parameters, the function will return all objects of that table.:

projects = ddb.query_or(Project)

Attributes that are sets (e.g. team.member, project.github_urls) would be treated differently. This function would check to see if the entry contains a certain element. You can specify multiple elements, but they must be in different parameters (one element per tuple).:

teams = ddb.query_or(Team, [('members', 'abc123'),
                            ('members', '231abc')])

The above would get you the teams that contain either member abc123 or 231abc.

Parameters:
  • Model (Type[~T]) – type of list elements you’d want
  • params (List[Tuple[str, str]]) – list of tuples to match
Return type:

List[~T]

Returns:

a list of Model that fit the query parameters

retrieve(Model, k)

Retrieve a model from the database.

Parameters:
  • Model (Type[~T]) – the actual class you want to retrieve
  • k (str) – retrieve based on this key (or ID)
Raise:

LookupError if key is not found

Return type:

~T

Returns:

a model Model if key is found

store(obj)

Store object into the correct table.

Object can be of type model.user.User, model.team.Team, or model.project.Project.

Parameters:obj (~T) – Object to store in database
Return type:bool
Returns:True if object was stored, and false otherwise

DynamoDB

DynamoDB.

class db.dynamodb.DynamoDB(config)

Handles calls to database through API.

Please do not use this class, and instead use db.facade.DBFacade. This class only works on DynamoDB, and should not be used outside of the facade class.

class Const(config)

A bunch of static constants and functions.

__init__(config)

Initialize the constants.

get_key(table_name)

Get primary key of the table name.

Parameters:cls – the name of the table
Raise:TypeError if table does not exist
Return type:str
Returns:primary key of the table
get_set_attrs(table_name)

Get class attributes that are sets.

Parameters:cls – the table name
Raise:TypeError if table does not exist
Return type:List[str]
Returns:list of strings of set attributes
get_table_name(cls)

Convert class into corresponding table name.

Parameters:cls (Type[~T]) – Either User, Team, or Project
Raise:TypeError if it is not either User, Team, or Project
Return type:str
Returns:table name string
__init__(config)

Initialize facade using DynamoDB settings.

To avoid local tests failure when the DynamoDb server is used, a testing environment variable is set. When testing environmental variable is true, the local dynamodb is run. When testing environmental variable is true, the server dynamodb is run.

boto3.resource() takes in a service_name, region_name, and endpoint_url (only for local dynamodb). service_name: The name of a service, “dynamodb” in this case. region_name: The name of the region associated with the client. A list of different regions can be obtained online. endpoint_url: The complete URL to use for the constructed client.

bulk_retrieve(Model, ks)

Retrieve a list of models from the database.

Keys not found in the database will be skipped.

Parameters:
  • Model (Type[~T]) – the actual class you want to retrieve
  • ks (List[str]) – retrieve based on this key (or ID)
Return type:

List[~T]

Returns:

a list of models Model

check_valid_table(table_name)

Check if table with table_name exists.

Parameters:table_name (str) – table identifier
Return type:bool
Returns:boolean value, true if table exists, false otherwise
delete(Model, k)

Remove an object from a table.

Parameters:
  • Model (Type[~T]) – table type to remove the object from
  • k (str) – ID or key of the object to remove (must be primary key)
query(Model, params=[])

Query a table using a list of parameters.

Returns a list of Model that have all of the attributes specified in the parameters. Every item in parameters is a tuple, where the first element is the user attribute, and the second is the value.

Example:

ddb = DynamoDb(config)
users = ddb.query(User, [('platform', 'slack')])

If you try to query a table without any parameters, the function will return all objects of that table.:

projects = ddb.query(Project)

Attributes that are sets (e.g. team.member, project.github_urls) would be treated differently. This function would check to see if the entry contains a certain element. You can specify multiple elements, but they must be in different parameters (one element per tuple).:

teams = ddb.query(Team, [('members', 'abc123'),
                         ('members', '231abc')])
Parameters:
  • Model (Type[~T]) – type of list elements you’d want
  • params (List[Tuple[str, str]]) – list of tuples to match
Return type:

List[~T]

Returns:

a list of Model that fit the query parameters

query_or(Model, params=[])

Query a table using a list of parameters.

Returns a list of Model that have one of the attributes specified in the parameters. Some might say that this is a union of the parameters. Every item in parameters is a tuple, where the first element is the user attribute, and the second is the value.

Example:

ddb = DynamoDb(config)
users = ddb.query_or(User, [('platform', 'slack')])

If you try to query a table without any parameters, the function will return all objects of that table.:

projects = ddb.query_or(Project)

Attributes that are sets (e.g. team.member, project.github_urls) would be treated differently. This function would check to see if the entry contains a certain element. You can specify multiple elements, but they must be in different parameters (one element per tuple).:

teams = ddb.query_or(Team, [('members', 'abc123'),
                            ('members', '231abc')])

The above would get you the teams that contain either member abc123 or 231abc.

Parameters:
  • Model (Type[~T]) – type of list elements you’d want
  • params (List[Tuple[str, str]]) – list of tuples to match
Return type:

List[~T]

Returns:

a list of Model that fit the query parameters

retrieve(Model, k)

Retrieve a model from the database.

Parameters:
  • Model (Type[~T]) – the actual class you want to retrieve
  • k (str) – retrieve based on this key (or ID)
Raise:

LookupError if key is not found

Return type:

~T

Returns:

a model Model if key is found

store(obj)

Store object into the correct table.

Object can be of type model.user.User, model.team.Team, or model.project.Project.

Parameters:obj (~T) – Object to store in database
Return type:bool
Returns:True if object was stored, and false otherwise