@app.posts("/posts")
@app.get("/posts/{id}")
@app.get("/posts")
@app.put("/posts/{id}")
@app.delete("/posts/{id}")
uvicorn app.main:app --reload
- Database is a collection of organized data that can be easaly accessed and managed
- We don't work or interact with databases directly
- Instead we make use of a software referred to as a Database Management System(DBMS)
Relational | NoSQL |
---|---|
MySQL | MongoDB |
PostgreSQL | DynamoDB |
Oracle | Oracle |
SQL Server | SQL Server |
- Structured Query Languege(SQL) - Language used to cummunicate with DBMS
- Each instance of postgres can be carved into multiple separete databases
- By Default every Postgres installation comes with one database already created called "postgres"
- This is important because Postgres requires you to specify the name of a database to make a connection. So there needs to always be one database
- A table represents a subject or event in an application
- A table is made up of columns and rows
- Each Column represents a different attribute
- Each row represents a different entry in the table
- Databases have datatypes just like any programming language | DataType | Postgres | Python | |----------|-------------------------|------------| | Numeric | int, decimal, precision | Int, float | | Text | Varchar, text | string | | Bool | boolean | boolean | | Sequence | array | list |
- Is a column or group of columns that uniquely identifies each row in a table
- Table can have one and only one primary key
- A UNIQUE constraint can be applied to any column to make sure every record has a unique value for that column
- By default, when adding a new entry to a database, any column can be left blank. When a column is left blank, it has a null value
- If you need column to be properly filled in to create a new record, a NOT NULL constraint can be added to yhe column to ensure that the column is never left blank
- Layer of obstraction that sits between the database and us
- We can perform all database operations through traditional python code. No more SQL!
- Instead of manually defining tables in postgres, we can define our tables as python models
- Queries can be made exclusively through python code. No SQL is necessary
class Post(Base):
__tablename__ = "posts"
id = Column(Integer, primary_key=True, index=True)
title = Column(String, index=True, nullable=False)
content = Column(String, nullable=False)
published = Column(Boolean)
db.query(models.Post).filter(models.Post.id == id).first()
- SQLALCHEMY is one of the most popular python ORMs
- It is stand-alone library and has no association with FastAPI, It can be used with any other python web frameworks or any python based application
- Schema/Pydantic Models define the structure of a request & response
- This ensure that when a user wants to create a post, the request will only go through if it has a "title" and "content" in the body
- Responsible for defining the columns of our "posts" table within postgres
- Is used to query, create, delete and update entries within the database