# DM-aiomysql
## Urls
* [PyPI](https://pypi.org/project/dm-aiomysql)
* [GitHub](https://github.com/MykhLibs/dm-aiomysql)
### * Package contains both `asynchronous` and `synchronous` clients
## Usage
### Run in Windows
_If you run async code in **Windows**, set correct selector_
```python
import asyncio
import sys
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
```
### Example of using DMAioMysqlClient
Analogue to `DMAioMysqlClient` is the synchronous client `DMMysqlClient`.
```python
from dm_aiomysql import DMAioMysqlClient
import asyncio
async def main():
# create client
mysql_client = DMAioMysqlClient("localhost", 3306, "username", "password", "database")
# execute query - results is list[list[Any]]
list_results = await mysql_client.query("SELECT * FROM users")
# execute query - results is list[dict[str, Any]
dict_results = await mysql_client.query("SELECT * FROM users", dict_results=True)
# execute query with params placeholders
results = await mysql_client.query("SELECT * FROM users WHEN name = %s", params=["John"])
# commit data
await mysql_client.query("UPDATE users SET age = %s WHERE id = %s", params=[25, 2], commit=True)
# insert data
data = {"id": 1, "name": "John_1", "age": 21}
await mysql_client.insert_one("my_table", data)
# insert many data
data_list = [{"id": 2, "name": "John_2", "age": 22}, {"id": 3, "name": "John_3", "age": 23}]
await mysql_client.insert_many("users", data_list)
if __name__ == "__main__":
asyncio.run(main())
```
### Example of using DMAioEnvMysqlClient
`DMAioEnvMysqlClient` fully inherits the `DMAioMysqlClient` class.
But the connection parameters are loaded from the **ENV** variables.
**_The client will not be created until all ENV variables are set._**
Analogue to `DMAioEnvMysqlClient` is the synchronous client `DMEnvMysqlClient`.
```python
from dm_aiomysql import DMAioEnvMysqlClient
from dotenv import load_dotenv, find_dotenv
# load ENV variables
load_dotenv(find_dotenv())
# create default client
mysql_client = DMAioEnvMysqlClient()
# needed ENV variables MYSQL_HOST, MYSQL_PORT, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE
# create custom client
custom_mysql_client = DMAioEnvMysqlClient(env_prefix="PROD_MYSQL") # by default: env_prefix="MYSQL"
# needed ENV variables PROD_MYSQL_HOST, PROD_MYSQL_PORT, ...
```
### Set custom logger
_If you want set up custom logger_
```python
from dm_aiomysql import DMAioMysqlClient
# create custom logger
class MyLogger:
def debug(self, message):
pass
def info(self, message):
pass
def warning(self, message):
print(message)
def error(self, message):
print(message)
# set up custom logger for all clients
DMAioMysqlClient.set_logger(MyLogger())
```
Raw data
{
"_id": null,
"home_page": "https://pypi.org/project/dm-aiomysql",
"name": "dm-aiomysql",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "dm aiomysql",
"author": "dimka4621",
"author_email": "mismartconfig@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/46/85/8fde7f2000552c18bbd4ffadc0f2a2f761a4ce35f68bc5d3cd4fd633366e/dm_aiomysql-0.1.4.tar.gz",
"platform": null,
"description": "# DM-aiomysql\n\n## Urls\n\n* [PyPI](https://pypi.org/project/dm-aiomysql)\n* [GitHub](https://github.com/MykhLibs/dm-aiomysql)\n\n### * Package contains both `asynchronous` and `synchronous` clients\n\n## Usage\n\n### Run in Windows\n\n_If you run async code in **Windows**, set correct selector_\n\n```python\nimport asyncio\nimport sys\n\nif sys.platform == \"win32\":\n asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())\n```\n\n### Example of using DMAioMysqlClient\n\nAnalogue to `DMAioMysqlClient` is the synchronous client `DMMysqlClient`.\n\n```python\nfrom dm_aiomysql import DMAioMysqlClient\nimport asyncio\n\n\nasync def main():\n # create client\n mysql_client = DMAioMysqlClient(\"localhost\", 3306, \"username\", \"password\", \"database\")\n\n # execute query - results is list[list[Any]]\n list_results = await mysql_client.query(\"SELECT * FROM users\")\n\n # execute query - results is list[dict[str, Any]\n dict_results = await mysql_client.query(\"SELECT * FROM users\", dict_results=True)\n\n # execute query with params placeholders\n results = await mysql_client.query(\"SELECT * FROM users WHEN name = %s\", params=[\"John\"])\n\n # commit data\n await mysql_client.query(\"UPDATE users SET age = %s WHERE id = %s\", params=[25, 2], commit=True)\n\n # insert data\n data = {\"id\": 1, \"name\": \"John_1\", \"age\": 21}\n await mysql_client.insert_one(\"my_table\", data)\n\n # insert many data\n data_list = [{\"id\": 2, \"name\": \"John_2\", \"age\": 22}, {\"id\": 3, \"name\": \"John_3\", \"age\": 23}]\n await mysql_client.insert_many(\"users\", data_list)\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n\n### Example of using DMAioEnvMysqlClient\n\n`DMAioEnvMysqlClient` fully inherits the `DMAioMysqlClient` class.\nBut the connection parameters are loaded from the **ENV** variables.\n\n**_The client will not be created until all ENV variables are set._**\n\nAnalogue to `DMAioEnvMysqlClient` is the synchronous client `DMEnvMysqlClient`.\n\n```python\nfrom dm_aiomysql import DMAioEnvMysqlClient\nfrom dotenv import load_dotenv, find_dotenv\n\n# load ENV variables\nload_dotenv(find_dotenv())\n\n# create default client\nmysql_client = DMAioEnvMysqlClient()\n# needed ENV variables MYSQL_HOST, MYSQL_PORT, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE\n\n# create custom client\ncustom_mysql_client = DMAioEnvMysqlClient(env_prefix=\"PROD_MYSQL\") # by default: env_prefix=\"MYSQL\"\n# needed ENV variables PROD_MYSQL_HOST, PROD_MYSQL_PORT, ...\n```\n\n### Set custom logger\n\n_If you want set up custom logger_\n\n```python\nfrom dm_aiomysql import DMAioMysqlClient\n\n\n# create custom logger\nclass MyLogger:\n def debug(self, message):\n pass\n\n def info(self, message):\n pass\n\n def warning(self, message):\n print(message)\n\n def error(self, message):\n print(message)\n\n\n# set up custom logger for all clients\nDMAioMysqlClient.set_logger(MyLogger())\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "This is my custom aiomysql client",
"version": "0.1.4",
"project_urls": {
"GitHub": "https://github.com/MykhLibs/dm-aiomysql",
"Homepage": "https://pypi.org/project/dm-aiomysql"
},
"split_keywords": [
"dm",
"aiomysql"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "15ee7002b102bdb3ad99054d3da5cf5c320a764691c8f96817154b17a9bf62e4",
"md5": "7bdeaa229b4c8cedf0483d0b8ebcbe4b",
"sha256": "372aad56da48942e0a145927359d17b3dfbf317d4481829f30bf9f0ae66dbb27"
},
"downloads": -1,
"filename": "dm_aiomysql-0.1.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7bdeaa229b4c8cedf0483d0b8ebcbe4b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 5836,
"upload_time": "2024-11-13T09:54:38",
"upload_time_iso_8601": "2024-11-13T09:54:38.271505Z",
"url": "https://files.pythonhosted.org/packages/15/ee/7002b102bdb3ad99054d3da5cf5c320a764691c8f96817154b17a9bf62e4/dm_aiomysql-0.1.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "46858fde7f2000552c18bbd4ffadc0f2a2f761a4ce35f68bc5d3cd4fd633366e",
"md5": "a9e1a865ee27cc6f88fc36fca80694df",
"sha256": "b3d985169775b7de0b2d3718e8d6216acdce216281b081b29ad1917dbdd5d849"
},
"downloads": -1,
"filename": "dm_aiomysql-0.1.4.tar.gz",
"has_sig": false,
"md5_digest": "a9e1a865ee27cc6f88fc36fca80694df",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 4114,
"upload_time": "2024-11-13T09:54:39",
"upload_time_iso_8601": "2024-11-13T09:54:39.814076Z",
"url": "https://files.pythonhosted.org/packages/46/85/8fde7f2000552c18bbd4ffadc0f2a2f761a4ce35f68bc5d3cd4fd633366e/dm_aiomysql-0.1.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-13 09:54:39",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "MykhLibs",
"github_project": "dm-aiomysql",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "dm-aiomysql"
}