Skip to content

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 KraftCloud. 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:

  1. Install the kraft CLI tool and a container runtime engine, e.g. Docker.

  2. Clone the examples repository and cd into the examples/node18-prisma-rest-express/ directory:

Terminal window
git clone https://github.com/kraftcloud/examples
cd examples/node18-prisma-rest-express/

Make sure to log into KraftCloud by setting your token and a metro close to you. We use fra0 (Frankfurt, πŸ‡©πŸ‡ͺ) in this guide:

Terminal window
# Set KraftCloud access token
export KRAFTCLOUD_TOKEN=token
# Set metro to Frankfurt, DE
export KRAFTCLOUD_METRO=fra0

When done, invoke the following command to deploy this application on KraftCloud:

Terminal window
kraft cloud deploy -M 512 -p 443:3000 .

The output shows the instance URL and other details:

Terminal window
[●] Deployed successfully!
β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ name: node18-prisma-hdof1
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ uuid: 066f55cb-bcbd-45e5-9f6b-b3866c3a3a4c
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€ state: running
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ url: https://funky-sun-4bf8v7g9.fra0.kraft.host
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€ image: node18-prisma@sha256:770d4af1d490daea11171c680eaf99e2a6017a262ba9fbf1ba8d708f5fc32bfe
β”œβ”€β”€β”€β”€β”€ boot time: 37.94 ms
β”œβ”€β”€β”€β”€β”€β”€β”€β”€ memory: 512 MiB
β”œβ”€ service group: funky-sun-4bf8v7g9
β”œβ”€β”€ private fqdn: node18-prisma-hdof1.internal
β”œβ”€β”€β”€β”€ private ip: 172.16.28.2
└────────── args: /usr/bin/node /usr/src/server.js

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:

Terminal window
curl https://funky-sun-4bf8v7g9.fra0.kraft.host/users
[{"id":1,"email":"[email protected]","name":"Alice"},
{"id":2,"email":"[email protected]","name":"Nilu"},
{"id":3,"email":"[email protected]","name":"Mahmoud"}]

At any point in time, you can list information about the instance:

Terminal window
kraft cloud instance list
NAME FQDN STATE CREATED AT IMAGE MEMORY ARGS BOOT TIME
node18-prisma-hdof1 funky-sun-4bf8v7g9.fra0.kraft.host running 1 minute ago node18-prisma@sha25... 512 MiB /usr/bin/node /usr/src/server.js 37935us

When done, you can remove the instance:

Terminal window
kraft cloud instance remove node18-prisma-hdof1

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 its id
  • /feed?searchString={searchString}&take={take}&skip={skip}&orderBy={orderBy}: Fetch all published posts
    • Query Parameters
      • searchString (optional): This filters posts by title or content
      • take (optional): This specifies how many objects should be returned in the list
      • skip (optional): This specifies how many of the returned objects in the list should be skipped
      • orderBy (optional): The sort order for posts in either ascending or descending order. The value can either asc or desc
  • /user/:id/drafts: Fetch user’s drafts by their id
  • /users: Fetch all users

POST

  • /post: Create a new post
    • Body:
      • title: String (required): The title of the post
      • content: String (optional): The content of the post
      • authorEmail: String (required): The email of the user that creates the post
  • /signup: Create a new user
    • Body:
      • email: String (required): The email address of the user
      • name: String (optional): The name of the user
      • postData: PostCreateInput[] (optional): The posts of the user

PUT

  • /publish/:id: Toggle the publish value of a post by its id
  • /post/:id/views: Increases the viewCount of a Post by one id

DELETE

  • /post/:id: Delete a post by its id

Evolving the App

Evolving the application typically requires two steps:

  1. Migrate your database using Prisma Migrate
  2. 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:

./prisma/schema.prisma
model User {
id Int @default(autoincrement()) @id
name String?
email String @unique
posts Post[]
profile Profile?
}
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
content String?
published Boolean @default(false)
viewCount Int @default(0)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
model Profile {
id Int @default(autoincrement()) @id
bio String?
user User @relation(fields: [userId], references: [id])
userId Int @unique
}

Once you’ve updated your data model, you can execute the changes against your database with the following command:

Terminal window
npx prisma migrate dev --name add-profile

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:

app.post('/user/:id/profile', async (req, res) => {
const { id } = req.params
const { bio } = req.body
const profile = await prisma.profile.create({
data: {
bio,
user: {
connect: {
id: Number(id)
}
}
}
})
res.send(profile)
})

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

Here are some more sample Prisma Client queries on the new Profile model:

Create a new profile for an existing user
const profile = await prisma.profile.create({
data: {
bio: 'Hello World',
user: {
connect: { email: '[email protected]' },
},
},
})
Create a new user with a new profile
const user = await prisma.user.create({
data: {
name: 'John',
profile: {
create: {
bio: 'Hello World',
},
},
},
})
Update the profile of an existing user
const userWithUpdatedProfile = await prisma.user.update({
where: { email: '[email protected]' },
data: {
profile: {
update: {
bio: 'Hello Friends',
},
},
},
})

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:

datasource db {
provider = "postgresql"
url = "postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA"
}

Here is an example connection string with a local PostgreSQL database:

datasource db {
provider = "postgresql"
url = "postgresql://janedoe:mypassword@localhost:5432/notesapi?schema=public"
}

MySQL

For MySQL, the connection URL has the following structure:

datasource db {
provider = "mysql"
url = "mysql://USER:PASSWORD@HOST:PORT/DATABASE"
}

Here is an example connection string with a local MySQL database:

datasource db {
provider = "mysql"
url = "mysql://janedoe:mypassword@localhost:3306/notesapi"
}

Microsoft SQL Server

Here is an example connection string with a local Microsoft SQL Server database:

datasource db {
provider = "sqlserver"
url = "sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"
}

MongoDB

Here is an example connection string with a local MongoDB database:

datasource db {
provider = "mongodb"
url = "mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority"
}

Learn More

Use the --help option for detailed information on using KraftCloud:

Terminal window
kraft cloud --help

Or visit the CLI Reference.