Skip to content

Commit c077d3f

Browse files
committed
Add basic structure
1 parent d6824ae commit c077d3f

18 files changed

Lines changed: 1699 additions & 14 deletions

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
# Database API
1+
# Database API
2+
3+
To run the application:
4+
`uvicorn main:app --reload`
File renamed without changes.

app/controllers.py

Lines changed: 0 additions & 8 deletions
This file was deleted.
File renamed without changes.

app/core/config.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from app.core.database import SessionLocal
2+
from fastapi import Depends
3+
from sqlalchemy.orm import Session
4+
from typing import Annotated
5+
6+
def get_db():
7+
db = SessionLocal()
8+
try:
9+
yield db
10+
finally:
11+
db.close
12+
13+
db_dependency = Annotated[Session, Depends(get_db)]

app/core/database.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from sqlalchemy import create_engine
2+
from fastapi import Depends, HTTPException
3+
from sqlalchemy.orm import sessionmaker, Session
4+
from typing import List, Annotated
5+
from sqlalchemy.ext.declarative import declarative_base
6+
from sqlalchemy_utils import create_database, database_exists
7+
from app.models import Base, Database
8+
9+
DATABASE_URL = 'postgresql://neelxie:password@localhost:5433/ccdatabase'
10+
11+
if not database_exists(DATABASE_URL):
12+
create_database(DATABASE_URL)
13+
14+
engine = create_engine(DATABASE_URL, pool_pre_ping=True)
15+
16+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
17+
18+
Base.metadata.create_all(bind=engine)
19+

app/core/database_flavor.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
from types import SimpleNamespace
3+
from app.core.database_service import MysqlDbService, PostgresqlDbService
4+
5+
db_flavors = {
6+
'postgres': {
7+
'name': 'postgres',
8+
'image': 'postgres:10.8-alpine',
9+
'port': 5432
10+
},
11+
'mysql': {
12+
'name': 'mysql',
13+
'image': 'mysql:8.0',
14+
'port': 3306
15+
},
16+
'mariadb': {
17+
'name': 'mariadb',
18+
'image': 'mariadb:10.5',
19+
'port': 3306
20+
}
21+
}
22+
23+
# Database flavours
24+
database_flavours = [
25+
{
26+
'name': 'mysql',
27+
'host': os.getenv('ADMIN_MYSQL_HOST'),
28+
'port': os.getenv('ADMIN_MYSQL_PORT'),
29+
'class': MysqlDbService()
30+
},
31+
{
32+
'name': 'postgres',
33+
'host': os.getenv('ADMIN_PSQL_HOST'),
34+
'port': os.getenv('ADMIN_PSQL_PORT'),
35+
'class': PostgresqlDbService()
36+
}
37+
]
38+
39+
40+
def get_db_flavour(flavour_name=None):
41+
if flavour_name == 'mysql':
42+
return database_flavours[0]
43+
elif flavour_name == 'postgres':
44+
return database_flavours[1]
45+
else:
46+
return False
47+
48+
49+
def get_all_db_flavours():
50+
return database_flavours

0 commit comments

Comments
 (0)