Skip to main content

A lightweight async Python library for MySQL queries and modeling.

Project description

banner

A lightweight async Python library for MySQL queries and modeling.

Installation

The latest version of Targa can be downloaded and installed using pip.

pip install targa

Any requirements (including aiomysql) should be automatically installed for you.

Usage

Connecting to a database

The targa.Database.connect method may be used to connect to an existing MySQL database. This method returns a targa.Database instance that can be used to issue queries.

import asyncio
import targa

async def main():
    database = targa.Database.connect(
        host          = '', # hostname or IP of the database server
        username      = '', # username to connect with
        password      = '', # password to connect with
        database_name = ''  # name of the database instance to connect to
    )

if __name__ == '__main__':
    asyncio.run(main())

Issuing a query

Once a targa.Database is initialized, the query method may be used to execute SQL queries. For example, consider a scenario where the rows in the following persons table need to be read:

id first_name occupation
1 Will Developer
2 John Accountant
3 Sue Engineer

Assuming a database connection has already been established, these rows could be accessed as follows:

import asyncio
import targa

async def main():
    # ... database connection already established

    persons = await database.query('SELECT * FROM persons')

    for person_dict in persons:
        print(person_dict)

if __name__ == '__main__':
    asyncio.run(main())

The query method returns each result row as a dict mapping the column names as keys to the row values. As a result, this program would output:

{'id': 1, 'first_name': 'Will', 'occupation': 'Developer'}
{'id': 2, 'first_name': 'John', 'occupation': 'Accountant'}
{'id': 3, 'first_name': 'Sue', 'occupation': 'Engineer'}

Defining models

Object models in Targa are represented as annotated Python classes that inherit the targa.Model base class. For example, a Person model for the table previously discussed would look like this:

import targa

class Person(targa.Model):
    id: int
    first_name: str
    occupation: str

Once this model is defined, an individual dict returned from querying the persons table could be wrapped as follows:

import asyncio
import targa

async def main():
    # ... database connection already established

    persons = await database.query('SELECT * FROM persons')

    for person_dict in persons:
        print(Person(**person_dict))

if __name__ == '__main__':
    asyncio.run(main())

This program would output the following:

Person(id=1, name='Will', occupation='Developer')
Person(id=2, name='John', occupation='Accountant')
Person(id=3, name='Sue', occupation='Engineer')

The fields of each Targa model are type checked as they are instantiated and may be accessed just like the fields of any other Python class.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

targa-1.0.5.tar.gz (9.2 kB view details)

Uploaded Source

File details

Details for the file targa-1.0.5.tar.gz.

File metadata

  • Download URL: targa-1.0.5.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.4

File hashes

Hashes for targa-1.0.5.tar.gz
Algorithm Hash digest
SHA256 76695a0114f71f37ef10f0b851e3b79439cf210b7bf8d7c6a90fea0027a5d232
MD5 0b3f7d817f259ddd4c35dacdad3db7ff
BLAKE2b-256 665e8d8b66c898948759658398b684f22dac783f04fd79ca580f7294bc1e9fa3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page