The ShortUrl application is a NestJS-based system designed to create, manage, and track short URLs. It utilizes GraphQL for its API interface, Prisma as its ORM for interacting with a PostgreSQL database, and class-validator for input validation. The application supports operations such as creating a new short URL, retrieving all short URLs with pagination, finding specific short URLs, updating, and deleting them.
- NestJS: A progressive Node.js framework for building efficient and scalable server-side applications.
- GraphQL: A query language for APIs and a runtime for executing those queries with your existing data.
- Prisma: Next-generation ORM for Node.js and TypeScript, which supports PostgreSQL among other databases.
- PostgreSQL: A powerful, open source object-relational database system.
- Jest: Default test framework by NestJs
Postgreaql is used for database and is hosted on https://console.neon.tech
as a free hosting plans
We can use Docker as well but for simplycity(i.e. to run directly on local), I have used above.
run command git clone https://github.com/imsimransingh/short-url-app.git
run npm install
Run prisma migrations
run npx prisma migrate dev
run npx prisma studio
this will gives us the prisma UI for our database tables
run npm run start:dev
this will run our nest server, after success you will be able to access
http://localhost:3000/graphql
- Create Record in Database (i.e. create short url link)
- Inside of graphql playground
add below request in query windows
mutation createShortUrl($input: CreateShortUrlInput!) { createShortUrl(createShortUrlInput: $input){ id shortUrl fullUrl } }
{ "input": { "fullUrl": "https://nestjs.com"} }
{ "data": { "createShortUrl": { "id": 1, "shortUrl": "Zv2lbn5o", "fullUrl": "https://nestjs.com" } } }
{ { shortUrls(pageSize: 5, currentPage: 1) { id shortUrl fullUrl createdAt updatedAt } }
{ "data": { "shortUrls": [ { "id": 1, "shortUrl": "Zv2lbn5o", "fullUrl": "http://localhost:3000/redirect?url=Zv2lbn5o", "createdAt": "2024-04-25T21:38:11.511Z", "updatedAt": "2024-04-25T21:38:11.511Z" }, { "id": 2, "shortUrl": "X8odwsQq", "fullUrl": "http://localhost:3000/redirect?url=X8odwsQq", "createdAt": "2024-04-25T22:53:34.254Z", "updatedAt": "2024-04-25T22:53:34.254Z" } ] } }
query fetchById($input: Int!) { shortUrl(id:$input) { id shortUrl fullUrl } }
{"input":1}
{ "data": { "shortUrl": { "id": 1, "shortUrl": "Zv2lbn5o", "fullUrl": "https://nestjsssd22.com" } } }
mutation updateShortUrl($input: UpdateShortUrlInput!) { updateShortUrl(updateShortUrlInput: $input){ id shortUrl fullUrl } }
{ "input": {"id":1, "fullUrl": "https://www.facebook.com"} }
{ "data": { "updateShortUrl": { "id": 1, "shortUrl": "Zv2lbn5o", "fullUrl": "https://www.facebook.com" } } }
in above output fullUrl
is redirect url, when we paste this url on browser it will take us to original url
run npm run test
- ShortUrl
id
: Integer - Unique identifier for the short URL.shortUrl
: String - The generated short URL.fullUrl
: String - The original URL.createdAt
: DateTime - The creation timestamp.updatedAt
: DateTime - The last update timestamp.
-
CreateShortUrlInput
shortUrl
: String (optional) - The desired short URL.fullUrl
: String - The original URL that needs to be shortened.createdAt
: DateTime (optional) - The creation timestamp.updatedAt
: DateTime (optional) - The last update timestamp.
-
UpdateShortUrlInput
- Inherits all fields from
CreateShortUrlInput
and includes: id
: Integer - The unique identifier of the short URL to be updated.
- Inherits all fields from
-
shortUrls
- Retrieves a paginated list of all short URLs.
- Parameters:
pageSize
(Float),currentPage
(Float).
-
shortUrl
- Retrieves a single short URL by its ID.
- Parameters:
id
(Int).
-
createShortUrl
- Creates a new short URL entry.
- Parameters:
createShortUrlInput
(CreateShortUrlInput).
-
updateShortUrl
- Updates an existing short URL.
- Parameters:
updateShortUrlInput
(UpdateShortUrlInput).
-
removeShortUrl
- Deletes a short URL by its ID.
- Parameters:
id
(Int).
The ShortUrlResolver
class in NestJS handles the GraphQL operations. It uses the ShortUrlService
for business logic and data handling.