Run Prisma
This app is derived from Prismaβs REST API Example and shows how to implement a REST API using Express and Prisma Client and deploy it onto Unikraft Cloud.
It uses a SQLite database file with some initial dummy data which you can find at ./prisma/store.db
.
To run it, follow these steps:
-
Install the
kraft
CLI tool and a container runtime engine, e.g. Docker. -
Clone the
examples
repository andcd
into theexamples/node18-prisma-rest-express/
directory:
Make sure to log into Unikraft Cloud by setting your token and a metro close to you.
We use fra0
(Frankfurt, π©πͺ) in this guide:
When done, invoke the following command to deploy this application on Unikraft Cloud:
The output shows the instance URL and other details:
In this case, the instance name is node18-prisma-hdof1
and the URL is https://funky-sun-4bf8v7g9.fra0.kraft.host
.
They are different for each run.
Use curl
to test the REST API, such as the /users
endpoint:
At any point in time, you can list information about the instance:
When done, you can remove the instance:
If you would like to test the server locally before deploying to Kraftcloud, follow the instructions here.
Using the REST API
You can access the REST API of the server using the following endpoints:
GET
/post/:id
: Fetch a single post by itsid
/feed?searchString={searchString}&take={take}&skip={skip}&orderBy={orderBy}
: Fetch all published posts- Query Parameters
searchString
(optional): This filters posts bytitle
orcontent
take
(optional): This specifies how many objects should be returned in the listskip
(optional): This specifies how many of the returned objects in the list should be skippedorderBy
(optional): The sort order for posts in either ascending or descending order. The value can eitherasc
ordesc
- Query Parameters
/user/:id/drafts
: Fetch userβs drafts by theirid
/users
: Fetch all users
POST
/post
: Create a new post- Body:
title: String
(required): The title of the postcontent: String
(optional): The content of the postauthorEmail: String
(required): The email of the user that creates the post
- Body:
/signup
: Create a new user- Body:
email: String
(required): The email address of the username: String
(optional): The name of the userpostData: PostCreateInput[]
(optional): The posts of the user
- Body:
PUT
/publish/:id
: Toggle the publish value of a post by itsid
/post/:id/views
: Increases theviewCount
of aPost
by oneid
DELETE
/post/:id
: Delete a post by itsid
Evolving the App
Evolving the application typically requires two steps:
- Migrate your database using Prisma Migrate
- Update your application code
For the following example scenario, assume you want to add a βprofileβ feature to the app where users can create a profile and write a short bio about themselves.
1. Migrate your database using Prisma Migrate
The first step is to add a new table, e.g. called Profile
, to the database. You can do this by adding a new model to your Prisma schema file file and then running a migration afterwards:
Once youβve updated your data model, you can execute the changes against your database with the following command:
This adds another migration to the prisma/migrations
directory and creates the new Profile
table in the database.
2. Update your application code
You can now use your PrismaClient
instance to perform operations against the new Profile
table. Those operations can be used to implement API endpoints in the REST API.
2.1 Add the API endpoint to your app
Update your src/index.js
file by adding a new endpoint to your API:
2.2 Testing out your new endpoint
Restart your application server and test out your new endpoint.
POST
/user/:id/profile
: Create a new profile based on the user id- Body:
bio: String
: The bio of the user
- Body:
Here are some more sample Prisma Client queries on the new Profile
model:
Create a new profile for an existing user
Create a new user with a new profile
Update the profile of an existing user
Switch to Another Database
If you want to try this example with another database than SQLite, you can adjust the database connection in prisma/schema.prisma
by reconfiguring the datasource
block.
Learn more about the different connection configurations in the docs.
PostgreSQL
For PostgreSQL, the connection URL has the following structure:
Here is an example connection string with a local PostgreSQL database:
MySQL
For MySQL, the connection URL has the following structure:
Here is an example connection string with a local MySQL database:
Microsoft SQL Server
Here is an example connection string with a local Microsoft SQL Server database:
MongoDB
Here is an example connection string with a local MongoDB database:
Learn More
Use the --help
option for detailed information on using Unikraft Cloud:
Or visit the CLI Reference.