Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/JoseVL92/sqlalchemy_multiconnector
Easy connector for SQL Databases, support for sqlite, postgresql and mysql
https://github.com/JoseVL92/sqlalchemy_multiconnector
Last synced: about 2 months ago
JSON representation
Easy connector for SQL Databases, support for sqlite, postgresql and mysql
- Host: GitHub
- URL: https://github.com/JoseVL92/sqlalchemy_multiconnector
- Owner: JoseVL92
- License: mit
- Created: 2019-09-03T14:01:47.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-12-11T18:27:41.000Z (about 1 year ago)
- Last Synced: 2024-05-20T03:20:26.873Z (7 months ago)
- Language: Python
- Size: 70.3 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- cuban-opensource - sqlalchemy-connector
README
# sqlalchemy-multiconnector
Python SQLAlchemy connector for SQL databases (sqlite, postgresql and mysql).
Easy resources CRUD management.
Supports multi-tenancy with multiple databases and/or multiple schemas.## Example of use
```python
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, Text
from sqlalchemy.orm import relationship
from sqlalchemy_multiconnector import BASE, SQLConnector, manage_session
DB_TYPE = 'postgresql' # it also accepts 'mysql' and 'sqlite'
DB_HOST = 'hostname'
DB_PORT = 5432
DB_NAME = 'test'
DB_USER = 'username'
DB_PASSWD = 'password'
class User(BASE):
__tablename__ = 'users'
id = Column(Integer, autoincrement=True, primary_key=True)
username = Column(String)
active = Column(Boolean, default=True)
articles = relationship('Article', back_populates='user')
class Article(BASE):
__tablename__ = 'articles'
id = Column(Integer, autoincrement=True, primary_key=True)
title = Column(String)
description = Column(Text, nullable=True)
user_id = Column(Integer, ForeignKey(User.id))
user = relationship('User', back_populates='articles')# If some schema should be used, you can specify it on object creation
db_connection = SQLConnector(DB_TYPE, DB_HOST, DB_NAME, DB_PORT,
db_schemas=['schema1', 'schema2'],
db_user=DB_USER,
db_passwd=DB_PASSWD)# or with 'schemas' property
db_connection.schemas = ['schema1', 'schema2']# or in 'create_tables' method
db_connection.create_tables(schemas=['schema1', 'schema2'])# add a user in schema1
user_id = db_connection.create_resource(User, {'username': 'Usuario', 'is_active': False}, return_id=True, schema_name='schema1')# add an article related to this user
with db_connection.session_scope(schema_name = 'schema1') as session:
user_owner = session.query(User).get(user_id)
article_id = db_connection.create_resource(Article, {'title': 'Hi', 'user': user_owner}, session=session)# update article title
db_connection.update_resource(Article, pk=article_id, updated_fields: {'title': 'Hi again!'})# get and delete a specific resource and list all resources are also available
```