Connecting to a Database¶
Before using ChemRecon, the library needs to be connected to a database. For this, there are several options.
A public ChemRecon database is available, hosted by the University of Southern Denmark. By default, ChemRecon will connect to this databae.
A local Docker container can be hosted based on a ChemRecon database image (see docs).
By default, ChemRecon assumes that container port
5432is bound to port54320on the host machine.
For other options, ChemRecon can connect to an arbitrary PostgreSQL endpoint.
Connecting will create a global QueryHandler object, used by default for all queries.
The QueryHandler object can be accessed using the get_query_handler() function.
- chemrecon.connection.get_query_handler() QueryHandler¶
Returns the current database query handler.
Connecting to the Public Database¶
If nothing else is specified, ChemRecon will connect to the public database.
- class chemrecon.connect_public¶
Bases:
Sets the ChemRecon library to use a database connection to the public ChemRecon database maintained by the developer.
Hosting a Local database¶
Hosting the database locally is as easy as deploying a Docker container (see the Docker documentation). See the documentation page for details.
Hosting the database locally has several advantages:
Queries will be faster, as there is a lower response time.
Results of procedural queries can be cached in the local database, making subsequent queries of these relations much faster.
After deploying the Docker image, the library can connect (with or without write access) using the following functions.
Note that these functions assume the database is hosted on port 54320 on the host machine, as specified in the Docker image.
If running on a different port, custom connection parameters must be specified (see: Params).
- class chemrecon.connect_local_docker¶
Bases:
Sets the ChemRecon library to use a database in a local Docker container. Refer to the documentation for details on how to run and administrate this database.
- class chemrecon.connect_local_docker_dev¶
Bases:
Sets the ChemRecon library to use a database in a local Docker container. Refer to the documentation for details on how to run and administrate this database.
Connecting to an arbitrary PostgreSQL endpoint¶
Finally, ChemRecon can connect to any PostgreSQL endpoint.
- chemrecon.connection.connect(params: Params, can_write: bool)¶
Sets the database handler for the ChemRecon library to a custom database connection. Also defines whether the library is allowed to write/cache results in the database.
This takes a Params object as input, which contains the connection parameters.
This can be used to specify custom connection parameters, such as a custom port.
- class chemrecon.database.params.Params(connection_title: str, db_name: str, db_host: str, db_port: str, username: str, password: str)¶
Bases:
objectRepresents database connection parameters and provides a method to generate a formatted connection string compatible with PostgreSQL.
- connection_string() str¶
Returns a connection string compatible with PostgreSQL.
Extending the database¶
Another advantage of hosting a local database is that you can extend the database to include custom data sources. See the documentation page for instructions.