Officially out now: The TypeDB 3.0 Roadmap >>

Lesson 6.2: Managing databases

Connecting to the server

With TypeDB Studio, we were able to connect to the server automatically by providing the address, username, and password. Afterward, Studio managed the connection, session, and transactions for us. When working with the native language drivers, we must manage these ourselves.

To connect to the server, we must instantiate a driver object, as illustrated in the following code snippet.

ADDRESS = "localhost:1730"

with TypeDB.core_driver(ADDRESS) as driver:
    # code goes here

The driver object driver takes one argument, the server address, and should be instantiated using a context manager. This is essential to ensure that the driver’s destructor is called regardless of how the program terminates, which will free the resources on the server. We then conduct all application operations within the scope of the context manager.

If destructors are not properly called when using the TypeDB driver APIs, it is possible to temporarily deadlock transactions in some niche cases. To avoid this, ensure that you always take appropriate measures to ensure that destructors are called regardless of application termination state.

There are three key ways we can use a driver object:

  • Manage users.

  • Manage databases.

  • Open sessions.

We’ll cover managing databases in this lesson, and then see how to open sessions in Lesson 6.3.

User management features are only available in TypeDB Cloud and Enterprise, and will not be covered in this course. Instructions can be found on the user management page of the TypeDB manual.

Managing databases

Database management controls can be accessed through the databases property of a driver object. The following code snippet shows some of its methods.

with TypeDB.core_driver(ADDRESS) as driver:
    # Creates a new database with the specified name.
    driver.databases.create("database-name")

    # Checks if a database with the specified name exists.
    database_exists: bool = driver.databases.contains("database-name")

    # Retrieves a database object by specified name.
    specific_database: Database = driver.databases.get("database-name")

    # Retrieves a list of database objects for every database.
    all_databases: list[Database] = driver.databases.all()

Some of the methods of database objects are shown in the following code snippet.

with TypeDB.core_driver(ADDRESS) as driver:
    database: Database

    # Retrieves the name of a given database.
    name: str = database.name

    # Retrieves the schema of a given database.
    schema: str = database.schema()

    # Deletes a given database.
    database.delete()
Exercise

Write a function to create a new database, replacing any existing database with the same name. The function should have the following signature.

force_create_database(driver: TypeDBDriver, database_name: str) -> None
Sample solution
def force_create_database(driver: TypeDBDriver, database_name: str) -> None:
    if driver.databases.contains(database_name):
        driver.databases.get(database_name).delete()

    driver.databases.create(database_name)

Write a function to print every database’s name and schema. The function should have the following signature.

print_database_details(driver: TypeDBDriver) -> None
Sample solution
def print_database_details(driver: TypeDBDriver) -> None:
    for database in driver.databases.all():
        print(database.name)
        print(database.schema())