1 of 31

ORMs and APIs

Ketan Mayer-Patel

UNC Department of Computer Science

COMP 426: Modern Web Programming (Fall 2026)

Module 4: Backend

Lecture 15

2 of 31

​

​

​

​

​

​

Last lecture, we introduced relational databases.

Recap

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

Client

Router

React, etc

Next App

DB Server

Database

Our applications can perform CRUD operations on the database.

C: Create�R: Read

U: Update�D: Delete

3 of 31

We can interact with databases using SQL (structured query language)

  • SQL is a “little language” built for querying and working with data in a relational database!

​

SQL (Structured Query Language)

COMP 426: Modern Web Programming (Fall '26)

ORMs and APIs

M4: Backend

L15

script.sql

​

SELECT singer_id, title, duration

FROM song

WHERE duration > 20;

4 of 31

SQL is a language dedicated to interacting with databases.

  • We can run SQL commands on a database from within our TypeScript projects, but we would have to define this SQL commands in raw strings.
  • Also, the result we will get from SQL is also in a string format - not easily parsable by TypeScript!

The Problem

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

5 of 31

With SQL...

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

Client

Router

React, etc

Next App

DB Server

Database

Raw SQL

SELECT * FROM users;

Raw string

“...”

6 of 31

SQL is a language dedicated to interacting with databases.

  • We can run SQL commands on a database from within our TypeScript projects, but we would have to define this SQL commands in raw strings.
  • Also, the result we will get from SQL is also in a string format - not easily parsable by TypeScript!�

We need a tool to be able to interact with SQL databases from within our TypeScript code in a manner that is native to our programming language...

The Problem

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

7 of 31

  • ORM: Tools that let developers interact with databases using objects and functions in their programming language of choice instead of writing raw SQL queries.
    • ORMs essentially serve as the bridge between applications and their databases!
    • ORMs convert back and forth from programming language-friendly objects and functions to SQL strings.

Object Relational Mappers (ORMs)

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

8 of 31

With SQL...

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

Client

Router

React, etc

Next App

DB Server

Database

Raw SQL

SELECT * FROM users;

Raw string

“...”

9 of 31

With an ORM

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

Client

Router

React, etc

Next App

DB Server

Database

Raw SQL

Raw string

ORM

TS func

TS object

10 of 31

  • Drizzle ( ): Popular ORM for the TypeScript ecosystem that allows us to interact with our PostgreSQL databases using TypeScript.
    • Over 30k+ stars on GitHub.
    • Drizzle has an easy-to-use API and is extremely performant.
    • Website�

Drizzle

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

11 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

Client

Router

React, etc

Next App

DB Server

Database

C: Create�R: Read

U: Update�D: Delete

Just like with regular servers, databases are accessible on the network.

We pass our database URL into Drizzle to access our database.

const db = drizzle(“postgresql://user:password@host:port/dbname”);

Our applications can perform CRUD operations on the database using Drizzle.

Drizzle

12 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

Next App

Just like with regular servers, databases are accessible on the network.

We pass our database URL into Drizzle to access our database.

const db = drizzle(“postgresql://user:password@host:port/dbname”);

There is a SEVERE problem here with our approach.

Discuss with the people around you - can you find it?�(Hint in 2 minutes)

Client

Router

React, etc

Next App

DB Server

Database

C: Create�R: Read

U: Update�D: Delete

Our applications can perform CRUD operations on the database using Drizzle.

Drizzle

13 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

Next App

Just like with regular servers, databases are accessible on the network.

We pass our database URL into Drizzle to access our database.

const db = drizzle(“postgresql://user:password@host:port/dbname”);

There is a SEVERE problem here with our approach.

Discuss with the people around you - can you find it?�(Hint in 2 minutes)

Think security!

Client

Router

React, etc

Next App

DB Server

Database

C: Create�R: Read

U: Update�D: Delete

Our applications can perform CRUD operations on the database using Drizzle.

Drizzle

!

Attendance

Check!

14 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

Router

React, etc

Next App

Drizzle

M4: Backend

L15

Vercel (or Dev) Server

Next Server Functionality

Review: Loading a Website

​

Data Server

Client

The client requests a page on our website.

The server responds with HTML, CSS, JS

The JS runs, loading all functionality (including React, our Router, and Drizzle).

Database

Drizzle connects to the database with the provided database URL.

… where is that database URL stored?

In the JS files received by the client.

15 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

Router

React, etc

Next App

Drizzle

M4: Backend

L15

Vercel (or Dev) Server

Next Server Functionality

Review: Loading a Website

​

Data Server

Client

The client requests a page on our website.

The server responds with HTML, CSS, JS

The JS runs, loading all functionality (including React, our Router, and Drizzle).

Database

Drizzle connects to the database with the provided database URL.

… where is that database URL stored?

In the JS files received by the client.

The direct URL to access our database is part of the JS bundle the client receives.

Any malicious user can look through the JS received from our site and gain unauthorized access to our database!

We need to modify our setup so that we protect direct access to our database.

16 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

Router

React, etc

Next App

Drizzle

M4: Backend

L15

Vercel (or Dev) Server

Next Server Functionality

Data Server

Client

Database

Remember that Next.js is capable of running functionality on the server.

This powers server-side rendering (SSR).

What if our server can handle database operations?

17 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

Router

React, etc

Next App

Drizzle

M4: Backend

L15

Vercel (or Dev) Server

Next Server Functionality

Data Server

Client

Database

What if our server can handle database operations?

Drizzle

18 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

Router

React, etc

Next App

M4: Backend

L15

Vercel (or Dev) Server

Next Server Functionality

Data Server

Client

Database

Drizzle

The server is responsible for interacting with the database and is given access to the database URL.

This database URL is then not given as part of the client JS bundle, ensuring it never gets leaked to malicious users.

19 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

Router

React, etc

Next App

M4: Backend

L15

Vercel (or Dev) Server

Next Server Functionality

Data Server

Client

Database

Drizzle

Now, we just need to add a way for the client to request database operations…

APIs!

Our client code can make HTTP requests to the server and the server will respond to these requests by running some TS code.

The server performs CRUD operations on the database.

20 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

Router

React, etc

Next App

M4: Backend

L15

Vercel (or Dev) Server

Next Server Functionality

Data Server

Client

Database

Drizzle

Our client code can make HTTP requests to the server and the server will respond to these requests by running some TS code.

The server performs CRUD operations on the database.

We can supply a method with our HTTP request to specify information about the type of operation we want to perform.��GET, POST, PUT, DELETE

C: Create�R: Read

U: Update�D: Delete

(READ)

(CREATE)

(UPDATE)

(DELETE)

GET /api/pokemon

GET /api/wordle/random-word

We have been doing this already - just, we have interacted with external servers using fetch.

+ more

We can now define what the server does and what it responds with ourselves.

21 of 31

22 of 31

Music API Example

23 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

Router

React, etc

Next App

M4: Backend

L15

Vercel (or Dev) Server

Next Server Functionality

Data Server

Client

Database

Drizzle

24 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

Vercel (or Dev) Server

Next Server Functionality

Data Server

Client

Database

Drizzle

Code running on the client side is typically called the “frontend”

25 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

Vercel (or Dev) Server

Data Server

Client

Database

Code running on the client side is typically called the “frontend”

Code running on the server side is typically called the “backend”

26 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

M4: Backend

L15

Vercel (or Dev) Server

Data Server

Client

Code running on the client side is typically called the “frontend”

Code running on the server side is typically called the “backend”

Databases + other svcs the backend relies on is typically called the “data layer”

The Three-Tier Architecture

You will be writing server-side code in the next assignment.

27 of 31

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

Router

React, etc

Next App

M4: Backend

L15

Vercel (or Dev) Server

Next Server Functionality

Data Server

Client

Database

Drizzle

How do we actually set up this data server to hold our database?

28 of 31

BaaS Providers

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

  • Backend-as-a-Service (BaaS): Providers that manage the data server for you.
    • Many large projects often outsource backend management to a BaaS provider.
    • This leads to faster development and often a more reliable application.
      • You as a developer do not need to also manage the infrastructure!
  • BaaS providers also often include many additional services that are a pain to implement and upkeep manually, including user authentication, file storage, realtime updates, and more!

M4: Backend

L15

29 of 31

  • Firebase ( ): BaaS provider managed by Google.
    • Benefits: Very reliable with many features. Integrates well across tech stacks and programming languages (web: TypeScript, mobile: Swift and Kotlin, etc).
    • Drawbacks: The database Firebase provides is document-based. It is not a relational SQL database. Rate limits are also more restrictive.
  • AWS Amplify ( ): BaaS provider managed by Amazon.
    • Similar benefits and drawbacks to Firebase. Uses DynamoDB, a document or key-value based data store – not a relational database.

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

Popular BaaS Providers

M4: Backend

L15

30 of 31

Intro to Supabase

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

  • Supabase ( ): Open-source BaaS provider.
    • Benefits: Very reliable with many of the same features as Firebase. Integrates well across tech stacks and programming languages (web: TypeScript, mobile: Swift and Kotlin, etc). Uses Postgres as its data storage medium!
      • Also - generous free tier… unlimited reads and writes unlike Firebase.
      • Limits only on storage taken up.
    • Because Supabase is open source, we could use Supabase to host our data server, or we can host it ourselves…

M4: Backend

L15

31 of 31

Let’s See Supabase in Action

ORMs and APIs

COMP 426: Modern Web Programming (Fall '26)

  • https://supabase.com

​

M4: Backend

L15