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.1.tar.gz (8.5 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: targa-1.0.1.tar.gz
  • Upload date:
  • Size: 8.5 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.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for targa-1.0.1.tar.gz
Algorithm Hash digest
SHA256 0436b9f69018d0af93bbe13079f5b5cc8cef7181c9c15f9235e012c030462664
MD5 fec569b857c94e022e54ea26acb614c1
BLAKE2b-256 f36a832ff501ec44d22b54d1929b13f6514ea89d1f3201686e00777c8c285806

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