File size: 1,060 Bytes
68e9069
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

# Configuración de la base de datos
{% if database == "sqlite" %}
SQLALCHEMY_DATABASE_URL = "sqlite:///./{{ project_name.lower() }}.db"
{% elif database == "mysql" %}
SQLALCHEMY_DATABASE_URL = "mysql+mysqlconnector://username:password@localhost/{{ project_name.lower() }}"
{% elif database == "postgresql" %}
SQLALCHEMY_DATABASE_URL = "postgresql://username:password@localhost/{{ project_name.lower() }}"
{% else %}
SQLALCHEMY_DATABASE_URL = "sqlite:///./{{ project_name.lower() }}.db"
{% endif %}

engine = create_engine(
    SQLALCHEMY_DATABASE_URL, 
    {% if database == "sqlite" %}
    connect_args={"check_same_thread": False}
    {% else %}
    connect_args={}
    {% endif %}
)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

# Dependency para obtener sesión de BD
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()