Skip to content

Commit

Permalink
Use database singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
AndresChapo committed Jul 23, 2024
1 parent bbda346 commit 9617ca7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 31 deletions.
23 changes: 4 additions & 19 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
from flask import Flask
#from database import db_session

DATABASE_URI='sqlite:///users.db'


def register_blueprints(app):
"""Register blueprint endpoints"""

from handlers.routes import example_endpoints_blueprint
from base import create_app, register_env_vars
from database import Database

app.register_blueprint(example_endpoints_blueprint)

def register_env_vars(app):
app.config['DATABASE_URI'] = DATABASE_URI
# app.config['DB_SESSION'] = db_session

def create_app():
app = Flask(__name__)
register_blueprints(app)
return app
DATABASE_URI='sqlite:///users.db'

db = Database(DATABASE_URI)
app = create_app()

register_env_vars(app)
Expand Down
18 changes: 18 additions & 0 deletions base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from flask import Flask

DATABASE_URI='sqlite:///users.db'

def register_blueprints(app):
"""Register blueprint endpoints"""

from handlers.routes import example_endpoints_blueprint

app.register_blueprint(example_endpoints_blueprint)

def register_env_vars(app):
app.config['DATABASE_URI'] = DATABASE_URI

def create_app():
app = Flask(__name__)
register_blueprints(app)
return app
47 changes: 43 additions & 4 deletions database.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session, declarative_base
#from app import DATABASE_URI
from models.singleton import Singleton

DATABASE_URI ='sqlite:///users.db' # Valor por defecto
DATABASE_URI='sqlite:///users.db'
Base = declarative_base()

class Database(metaclass=Singleton):
session = None
connection = None
trans = None

def __init__(self, uri=None) -> None:
print("BD: ", uri)
engine = create_engine(uri, echo=False)
self.session = scoped_session(sessionmaker(bind=engine, autocommit=False, autoflush=True))

self.connection = engine.connect()
self.trans = self.connection.begin()

Base.metadata.create_all(bind=engine)


"""
def init_dbx(uri=None):
# global engine
# global db_session
if uri:
DATABASE_URI = uri
engine = create_engine(DATABASE_URI, echo=False)
db_session = scoped_session(sessionmaker(bind=engine, autocommit=False, autoflush=True))
connection = engine.connect()
trans = connection.begin()
import models.users_model # Importar todos los modelos aquí
Base.metadata.create_all(bind=engine)
return connection, trans
def create_db_session(database_uri):
# Crear un motor que almacena los datos en la base de datos SQLite local llamada "users.db"
engine = create_engine(database_uri)
Expand All @@ -19,4 +56,6 @@ def create_db_session(database_uri):
session = Session()
return session
session = create_db_session(DATABASE_URI)
#db_session = create_db_session(DATABASE_URI)
db_session = init_db(DATABASE_URI)
"""
3 changes: 1 addition & 2 deletions handlers/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from flask import Blueprint, current_app, request
from flask import Blueprint, request
import json
from database import db_session
from models.users_model import User

example_endpoints_blueprint = Blueprint("example_endpoints", __name__)
Expand Down
13 changes: 7 additions & 6 deletions models/users_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask import app

from sqlalchemy import Column, Integer, String
from database import Base, db_session
from database import Base, Database


class User(Base):
Expand All @@ -17,11 +17,12 @@ def create(name, email, password):
u.name = name
u.email = email
u.password = password

db_session.add(u)
db_session.commit()
db = Database()
db.session.add(u)
db.session.commit()
return u

@staticmethod
def get_users_list():
return db_session.query(User).all()
db = Database()
return db.session.query(User).all()

0 comments on commit 9617ca7

Please sign in to comment.