Name | matter-persistence JSON |
Version |
0.8.0
JSON |
| download |
home_page | |
Summary | Matter persistance library. |
upload_time | 2024-01-22 14:11:05 |
maintainer | |
docs_url | None |
author | |
requires_python | >=3.10 |
license | |
keywords |
database
migrations
orm
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# matter-persistence
[![PyPI - Version](https://img.shields.io/pypi/v/matter-persistence.svg)](https://pypi.org/project/matter-persistence)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/matter-persistence.svg)](https://pypi.org/project/matter-persistence)
**Table of Contents**
- [Installation](#installation)
- [License](#license)
## Installation
```console
pip install matter-persistence
```
### With migration support
```console
pip install matter-persistence[database-migration]
```
### With postgres support
```console
pip install matter-persistence[database-postgres]
```
### With cache support
```console
pip install matter-persistence[cache]
```
### With memcached support
```console
pip install matter-persistence[cache-memcached]
```
## Usage
First you need to configure your database.
```python
from matter_persistence.database import DatabaseConfig
db_config = DatabaseConfig(connection_uri="sqlite:///test.db")
```
Then you need start the DatabaseClient
```python
from matter_persistence.database import DatabaseConfig, DatabaseClient
db_config = DatabaseConfig(connection_uri="sqlite:///test.db")
DatabaseClient.start(db_config)
```
One now have two options:
* Create ORM models
* Use the sqlalchemy connection directly
### ORM models
```python
from matter_persistence.database import DatabaseBaseModel
from sqlalchemy import Column, Integer, String
class ExampleModel(DatabaseBaseModel):
__tablename__ = "example"
id = Column(Integer, primary_key=True)
name = Column(String)
async def an_async_function():
example = ExampleModel(name="test")
await example.save()
```
If you need to define a different schema or namespace for your class you can do:
```python
from matter_persistence.database import DatabaseBaseModel
from sqlalchemy import Column, Integer, String
class ExampleModel(DatabaseBaseModel):
__tablename__ = "example"
__table_args__ = {"schema": "some_schema"}
id = Column(Integer, primary_key=True)
name = Column(String)
```
### sqlalchemy connection directly
```python
from matter_persistence.database import get_or_reuse_connection
import sqlalchemy as sa
async def another_sync_function():
async with get_or_reuse_connection() as conn:
await conn.execute(sa.text("SELECT 1"))
```
## Migrations
One may use the command `migrations` to create and apply migrations.
First you need to configure you database client:
```python
from matter_persistence.database import DatabaseConfig
db_config = DatabaseConfig(connection_uri="sqlite:///test.db",
migration={"path": <a path to your migrations folder>,
"models": [<a list of full qualified class path of your ORM models>]})
```
If models is an empty array, or you don't have changed the models, the command will create an empty migration
and you can customize it.
Then you can use the command `migrations` to create and apply migrations. You must provide the full qualified
python path to your configuration instance
```console
migrations create --config python.path.to.your.db_config.instance --message <migration name>
```
Then apply it, You must provide the full qualified python path to your configuration instance:
```console
migrations apply --config python.path.to.your.db_config.instance
```
### Schema support
If you need deal with schemas in your database, you can define one for you versions:
```python
from matter_persistence.database import DatabaseConfig
db_config = DatabaseConfig(connection_uri="sqlite:///test.db",
migration={"path": <a path to your migrations folder>,
"models": [<a list of full qualified class path of your ORM models>]},
"version_schema": "another_schema")
```
However, the autogenerated migrations **must** be manually edited.
### Contributing
for contributions, check the [CONTRIBUTING.md](CONTRIBUTING.md) file
Raw data
{
"_id": null,
"home_page": "",
"name": "matter-persistence",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": "",
"keywords": "database,migrations,orm",
"author": "",
"author_email": "R\u00f4mulo Jales <romulo@thisismatter.com>",
"download_url": "",
"platform": null,
"description": "# matter-persistence\n\n[![PyPI - Version](https://img.shields.io/pypi/v/matter-persistence.svg)](https://pypi.org/project/matter-persistence)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/matter-persistence.svg)](https://pypi.org/project/matter-persistence)\n\n**Table of Contents**\n\n- [Installation](#installation)\n- [License](#license)\n\n## Installation\n\n```console\npip install matter-persistence\n```\n\n### With migration support\n\n```console\npip install matter-persistence[database-migration]\n```\n\n### With postgres support\n\n```console\npip install matter-persistence[database-postgres]\n```\n\n### With cache support \n\n```console\npip install matter-persistence[cache]\n```\n\n### With memcached support \n\n```console\npip install matter-persistence[cache-memcached]\n```\n## Usage\n\nFirst you need to configure your database.\n\n```python\nfrom matter_persistence.database import DatabaseConfig\n\ndb_config = DatabaseConfig(connection_uri=\"sqlite:///test.db\")\n```\n\nThen you need start the DatabaseClient\n\n\n```python\nfrom matter_persistence.database import DatabaseConfig, DatabaseClient\n\ndb_config = DatabaseConfig(connection_uri=\"sqlite:///test.db\")\nDatabaseClient.start(db_config)\n```\n\nOne now have two options:\n\n* Create ORM models\n* Use the sqlalchemy connection directly\n\n### ORM models\n\n```python\nfrom matter_persistence.database import DatabaseBaseModel\nfrom sqlalchemy import Column, Integer, String\n\nclass ExampleModel(DatabaseBaseModel):\n __tablename__ = \"example\"\n id = Column(Integer, primary_key=True)\n name = Column(String)\n\nasync def an_async_function():\n example = ExampleModel(name=\"test\")\n await example.save()\n```\n\nIf you need to define a different schema or namespace for your class you can do:\n\n```python\nfrom matter_persistence.database import DatabaseBaseModel\nfrom sqlalchemy import Column, Integer, String\n\nclass ExampleModel(DatabaseBaseModel):\n __tablename__ = \"example\"\n __table_args__ = {\"schema\": \"some_schema\"}\n \n id = Column(Integer, primary_key=True)\n name = Column(String)\n```\n### sqlalchemy connection directly\n\n```python\nfrom matter_persistence.database import get_or_reuse_connection\nimport sqlalchemy as sa\n\nasync def another_sync_function():\n async with get_or_reuse_connection() as conn:\n await conn.execute(sa.text(\"SELECT 1\"))\n```\n\n## Migrations\n\nOne may use the command `migrations` to create and apply migrations.\n\nFirst you need to configure you database client:\n```python\nfrom matter_persistence.database import DatabaseConfig\n\ndb_config = DatabaseConfig(connection_uri=\"sqlite:///test.db\",\n migration={\"path\": <a path to your migrations folder>,\n \"models\": [<a list of full qualified class path of your ORM models>]})\n```\nIf models is an empty array, or you don't have changed the models, the command will create an empty migration\nand you can customize it.\n\nThen you can use the command `migrations` to create and apply migrations. You must provide the full qualified\npython path to your configuration instance\n\n```console\nmigrations create --config python.path.to.your.db_config.instance --message <migration name>\n```\n\nThen apply it, You must provide the full qualified python path to your configuration instance:\n```console\nmigrations apply --config python.path.to.your.db_config.instance \n```\n### Schema support\n\nIf you need deal with schemas in your database, you can define one for you versions:\n\n```python\nfrom matter_persistence.database import DatabaseConfig\n\ndb_config = DatabaseConfig(connection_uri=\"sqlite:///test.db\",\n migration={\"path\": <a path to your migrations folder>,\n \"models\": [<a list of full qualified class path of your ORM models>]},\n \"version_schema\": \"another_schema\")\n``` \n\nHowever, the autogenerated migrations **must** be manually edited. \n### Contributing\n\nfor contributions, check the [CONTRIBUTING.md](CONTRIBUTING.md) file\n",
"bugtrack_url": null,
"license": "",
"summary": "Matter persistance library.",
"version": "0.8.0",
"project_urls": {
"Documentation": "https://github.com/Matter-Tech/matter-persistence#readme",
"Issues": "https://github.com/Matter-Tech/matter-persistence/issues",
"Source": "https://github.com/Matter-Tech/matter-persistence"
},
"split_keywords": [
"database",
"migrations",
"orm"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "48b4dc8ecc052f56d67151f472a869bce5b7cc8e79149d0cc1bab33abc99ca40",
"md5": "46ed68e5d64f18a520328bf393792e98",
"sha256": "a58d1945f50a2b6048708248ff51f3553334be1ad405b98a70260d103c816fbe"
},
"downloads": -1,
"filename": "matter_persistence-0.8.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "46ed68e5d64f18a520328bf393792e98",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 15248,
"upload_time": "2024-01-22T14:11:05",
"upload_time_iso_8601": "2024-01-22T14:11:05.754264Z",
"url": "https://files.pythonhosted.org/packages/48/b4/dc8ecc052f56d67151f472a869bce5b7cc8e79149d0cc1bab33abc99ca40/matter_persistence-0.8.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-01-22 14:11:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Matter-Tech",
"github_project": "matter-persistence#readme",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "matter-persistence"
}