async def db_session_middleware(request: Request, call_next): response = Response( "Cannot establish connection with persistence provider", status_code=500) try: request.state.db = SessionLocal() response = await call_next(request) finally: request.state.db.close() return response
def get_db() -> Generator: """ ## Dependency Generator for yielding db instances """ try: db = SessionLocal() yield db finally: db.close()
def main(): parser = argparse.ArgumentParser(description='Load data into DB.') parser.add_argument('--file-name', type=str) #parser.add_argument('save-frequency', type=int, default=100) args = parser.parse_args() db = SessionLocal() with open(args.file_name) as f: json_data = json.load(f) objects = [ TextSample(text=str(item), id=str(uuid.uuid4().hex)) for item in json_data['data'] ] db.bulk_save_objects(objects) db.commit()
def get_db(): db = SessionLocal() try: yield db finally: db.close()
#!/usr/bin/python3 from app import models from app.db import SessionLocal, engine db = SessionLocal() models.Base.metadata.create_all(bind=engine) account_record = models.Account(accountname='Transaktionskonto Bank', accountnumber=1910) db.add(account_record) account_record = models.Account(accountname='Utgående MOMS 25%', accountnumber=2611) db.add(account_record) account_record = models.Account(accountname='Debiterad ingående MOMS', accountnumber=2641) db.add(account_record) account_record = models.Account(accountname='Försäljning datorer', accountnumber=3010) db.add(account_record) account_record = models.Account(accountname='inköp komponenter', accountnumber=4010) db.add(account_record) db.commit() account_record = db.query(models.Account).filter_by(accountnumber=1910).all() db.close()