<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prisma</title>
    <description>The latest articles on DEV Community by Prisma (@prisma).</description>
    <link>https://dev.to/prisma</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F1608%2F0f93b179-76bf-4ee7-a838-e8222fbef062.png</url>
      <title>DEV Community: Prisma</title>
      <link>https://dev.to/prisma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prisma"/>
    <language>en</language>
    <item>
      <title>Extend your Platformatic API with Prisma Client</title>
      <dc:creator>Ruheni Alex</dc:creator>
      <pubDate>Fri, 16 Dec 2022 11:22:17 +0000</pubDate>
      <link>https://dev.to/prisma/extend-your-platformatic-api-with-prisma-client-3p92</link>
      <guid>https://dev.to/prisma/extend-your-platformatic-api-with-prisma-client-3p92</guid>
      <description>&lt;p&gt;In this part, you will learn how to extend the auto-generated GraphQL and REST API by Platformatic using &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-client" rel="noopener noreferrer"&gt;Prisma Client&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You will create a GraphQL mutation and a REST API endpoint to increment the views on a post record.&lt;/p&gt;

&lt;p&gt;If you're interested in learning how to use Prisma to model your database schema and auto-generate database migrations when working with Platformatic, refer to &lt;a href="https://dev.to/prisma/friendly-data-modeling-auto-generated-editable-migrations-for-platformatic-with-prisma-dib"&gt;part 2&lt;/a&gt; of this series.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: It is recommended you type out the changes over copying and pasting the code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Your dev toolbox
&lt;/h3&gt;

&lt;p&gt;To follow along, ensure you have the following installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js v18 or later&lt;/li&gt;
&lt;li&gt;npm v7 or later&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://marketplace.visualstudio.com/items?itemName=Prisma.prisma" rel="noopener noreferrer"&gt;Prisma VSCode Extension&lt;/a&gt; (&lt;em&gt;optional&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://marketplace.visualstudio.com/items?itemName=rangav.vscode-thunder-client" rel="noopener noreferrer"&gt;Thunder Client VSCode Extension&lt;/a&gt; (&lt;em&gt;optional&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Docker (&lt;em&gt;optional&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If you don't have Docker installed, you can set up a free hosted database on &lt;a href="https://railway.app/" rel="noopener noreferrer"&gt;Railway&lt;/a&gt; or install &lt;a href="https://www.postgresql.org/download/" rel="noopener noreferrer"&gt;PostgreSQL&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Assumed knowledge
&lt;/h3&gt;

&lt;p&gt;This guide will require you have basic familiarity in the following technologies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript&lt;/li&gt;
&lt;li&gt;GraphQL APIs&lt;/li&gt;
&lt;li&gt;REST APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Set up your Platformatic API
&lt;/h2&gt;

&lt;p&gt;The starting point of this tutorial is &lt;a href="https://dev.to/prisma/friendly-data-modeling-auto-generated-editable-migrations-for-platformatic-with-prisma-dib"&gt;part 2&lt;/a&gt; of this series. If you're following along from the previous part, you can jump to Extend the functionality of your API with a plugin section of this guide.&lt;/p&gt;

&lt;p&gt;In this tutorial, you'll use the following starter &lt;a href="https://github.com/ruheni/prisma-platformatic/tree/automated-migrations" rel="noopener noreferrer"&gt;repository&lt;/a&gt;. It contains the setup files for a new Platformatic project.&lt;/p&gt;

&lt;p&gt;To get started, clone the repository and checkout to the &lt;a href="https://github.com/ruheni/prisma-platformatic/tree/extend-plugin" rel="noopener noreferrer"&gt;&lt;code&gt;automated-migrations&lt;/code&gt;&lt;/a&gt; branch.&lt;/p&gt;

&lt;p&gt;Clone the repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone &lt;span class="nt"&gt;-b&lt;/span&gt; extend-plugin https://github.com/ruheni/prisma-platformatic.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, perform the following actions to get started:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Navigate to the cloned directory:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd prisma-platformatic
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Install dependencies:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a &lt;code&gt;.env&lt;/code&gt; file based-off of the &lt;code&gt;.env.example&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cp .env.example .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Start the PostgreSQL database with docker:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up -d
&lt;/code&gt;&lt;/pre&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you already have an existing database server running locally, update the value of the &lt;code&gt;DATABASE_URL&lt;/code&gt; in the &lt;code&gt;.env&lt;/code&gt; file with your database's user and password values:&lt;/p&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASE_URL="postgres://&amp;lt;USER&amp;gt;:&amp;lt;PASSWORD&amp;gt;@localhost:5432/blog"
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Connect to your database instance using &lt;code&gt;psql&lt;/code&gt; or your preferred SQL client. Copy and run the following SQL to create a database:&lt;/p&gt;


&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;DATABASE&lt;/span&gt; &lt;span class="n"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Generate an &lt;code&gt;up&lt;/code&gt; and &lt;code&gt;down&lt;/code&gt; migration:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx db-diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Apply the &lt;code&gt;up&lt;/code&gt; migration to your database:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx platformatic db migrations apply
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Populate the database with sample data:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx platformatic db seed seed.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you're following along from the previous part, you can skip this step.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Generate Prisma Client:
&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;```
npx prisma generate
```
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Project structure and files
&lt;/h3&gt;

&lt;p&gt;The project has the following structure:&lt;br&gt;
The important files in the directory are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.env&lt;/code&gt;: Contains the database connection string for your PostgreSQL database.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker-compose.yml&lt;/code&gt;: Defines the Docker image and configuration for your PostgreSQL database.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;package.json&lt;/code&gt;: Defines your application dependencies. &lt;a href="http://npmjs.com/package/platformatic" rel="noopener noreferrer"&gt;&lt;code&gt;platformatic&lt;/code&gt;&lt;/a&gt; is currently the only dependency in the project.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;platformatic.db.json&lt;/code&gt;: Defines &lt;a href="https://oss.platformatic.dev/docs/reference/service/configuration" rel="noopener noreferrer"&gt;Platformatic's configuration&lt;/a&gt; such as the server's hostname and port, migration directory, and your database's connection string.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;seed.js&lt;/code&gt;: Contains a script for populating the database with seed data.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;schema.prisma&lt;/code&gt;: Defines the structure of your database schema.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;schema.prisma&lt;/code&gt; file contains two models: &lt;code&gt;User&lt;/code&gt; and &lt;code&gt;Post&lt;/code&gt;, with a one-to-many relation.&lt;/p&gt;
&lt;h2&gt;
  
  
  Extend the functionality of your API with plugins
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Define the &lt;code&gt;plugin&lt;/code&gt; property in your &lt;code&gt;platformatic.db.json&lt;/code&gt; file
&lt;/h3&gt;

&lt;p&gt;The first step is defining the plugin configuration in your &lt;code&gt;platformatic.db.json&lt;/code&gt; file. Paste the following snippet to in your configuration file right after the &lt;code&gt;migrations&lt;/code&gt; property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nl"&gt;"plugin"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./plugin.js"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
  Expand to view the entire file
  &lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"server"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"logger"&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"level"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"info"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"hostname"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"127.0.0.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"port"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"3042"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"core"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"connectionString"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"{DATABASE_URL}"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"migrations"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./migrations"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"plugin"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"plugin.js"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;/p&gt;

&lt;p&gt;The snippet defines the path to your plugin. The plugin will be registered when your server starts, exposing the custom logic you've defined.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a plugin
&lt;/h3&gt;

&lt;p&gt;At the root of your directory, create a new file called &lt;code&gt;plugin.js&lt;/code&gt;. Paste  the following snippet to your plugin file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ./plugin.js&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;plugin loaded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The file exports an async function with an &lt;code&gt;app&lt;/code&gt; parameter that's a &lt;a href="https://www.fastify.io/docs/latest/Reference/Server/#instance" rel="noopener noreferrer"&gt;&lt;code&gt;FastifyInstance&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When you you start up Platformatic with &lt;code&gt;npx platformatic db start&lt;/code&gt;, Platformatic will register the plugin. "plugin loaded" will also be logged to your console.&lt;/p&gt;

&lt;p&gt;With everything set up, let's take a look at how you can extend your API using Prisma Client.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extend the GraphQL API
&lt;/h2&gt;

&lt;p&gt;You'll extend the GraphQL API by defining a mutation called &lt;code&gt;incrementPostViewCount&lt;/code&gt;. The mutation will have one argument, &lt;code&gt;id&lt;/code&gt; that is an &lt;code&gt;ID&lt;/code&gt; type. The mutation response type will be a &lt;code&gt;Post&lt;/code&gt; object type.&lt;/p&gt;

&lt;p&gt;To implement the GraphQL mutation, make the following changes to your &lt;code&gt;plugin.js&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ./plugin.js&lt;/span&gt;

&lt;span class="c1"&gt;// 1. &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PrismaClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@prisma/client&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PrismaClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;plugin loaded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;//3.&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;graphql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extendSchema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`
    extend type Mutation {
      incrementPostViewCount(id: ID): Post
    }
  `&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;//4.&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;graphql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;defineResolvers&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;Mutation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;incrementPostViewCount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// 5. &lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
          &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt;
          &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;viewCount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="na"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;

        &lt;span class="c1"&gt;// 6.&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Post with id:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; was not found`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;// 7.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above snippet does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Imports Prisma Client.&lt;/li&gt;
&lt;li&gt;Creates a new instance of Prisma Client.&lt;/li&gt;
&lt;li&gt;Defines the &lt;code&gt;incrementPostViewCount&lt;/code&gt; mutation type definition. It also defines the corresponding argument and the return type.&lt;/li&gt;
&lt;li&gt;Defines the resolver for your mutation.&lt;/li&gt;
&lt;li&gt;Makes a query to the database on the &lt;code&gt;Post&lt;/code&gt; model to increment a post's view count.&lt;/li&gt;
&lt;li&gt;Adds error handling in the event a record with the matching id was not found.&lt;/li&gt;
&lt;li&gt;Returns the updated post on success.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you're defining the &lt;code&gt;incrementPostMutation&lt;/code&gt; resolver, you will notice Prisma Client provides auto-completion. On hovering on the &lt;code&gt;incrementPostViewCount&lt;/code&gt; resolver and &lt;code&gt;post&lt;/code&gt; variable, VS Code infers the types for you.&lt;/p&gt;

&lt;p&gt;You can head over to &lt;a href="http://localhost:3042/graphiql" rel="noopener noreferrer"&gt;&lt;code&gt;http://localhost:3042/graphiql&lt;/code&gt;&lt;/a&gt; to try out the mutation you just created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="k"&gt;mutation&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;INCREMENT_POST_VIEWS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;incrementPostViewCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt; 
    &lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;viewCount&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
  Expand to view the response
  &lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"incrementPostViewCount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Prisma 💚 Platformatic"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"viewCount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;/p&gt;

&lt;h2&gt;
  
  
  Extend the REST API
&lt;/h2&gt;

&lt;p&gt;Next, you'll extend the REST API by defining an endpoint that will accept &lt;code&gt;PUT&lt;/code&gt; requests to increment the views of a post.&lt;/p&gt;

&lt;p&gt;To implement the REST endpoint, update &lt;code&gt;./plugin.js&lt;/code&gt; file with the following snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ./plugin.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PrismaClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@prisma/client&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PrismaClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;plugin loaded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;// ...GraphQL API&lt;/span&gt;

  &lt;span class="c1"&gt;// 1. &lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/post/:id/views&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 2. &lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;
    &lt;span class="c1"&gt;// 3. &lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;viewCount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="c1"&gt;// 4.&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;code&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Post with id:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; was not found`&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
     &lt;span class="c1"&gt;// 5.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Defines the route for incrementing a post's views.&lt;/li&gt;
&lt;li&gt;Retrieves the &lt;code&gt;id&lt;/code&gt; from the request parameters.&lt;/li&gt;
&lt;li&gt;Makes a query to the database on the &lt;code&gt;Post&lt;/code&gt; model to increment the post's view count.&lt;/li&gt;
&lt;li&gt;Adds error handling in the event a record with the matching id was not found.&lt;/li&gt;
&lt;li&gt;Returns the updated post on success.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
  Expand to see the diff
  &lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;const { PrismaClient } = require("@prisma/client")
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;const prisma = new PrismaClient()
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;module.exports = async (app) =&amp;gt; {
&lt;/span&gt;  app.log.info('plugin loaded')
&lt;span class="err"&gt;
&lt;/span&gt;  app.graphql.extendSchema(`
    extend type Mutation {
      incrementPostViewCount(id: ID): Post
    }
  `)
&lt;span class="err"&gt;
&lt;/span&gt;  //4.
  app.graphql.defineResolvers({
    Mutation: {
      incrementPostViewCount: async (_, { id }) =&amp;gt; {
        // 5. 
        const post = await prisma.post.update({
          where: {
            id: Number(id)
          },
          data: {
            viewCount: {
              increment: 1
            }
          }
        })
&lt;span class="err"&gt;
&lt;/span&gt;        // 6.
        if (!post) throw new Error(`Post with id:${id} was not found`)
        // 7.
        return post
      }
    }
  })
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="gi"&gt;+  // 1. 
+  app.put('/post/:id/views', async (req, reply) =&amp;gt; {
+    // 2.
+    const { id } = req.params
+    // 3.
+    const post = await prisma.post.update({
+      where: {
+        id: Number(id)
+      },
+      data: {
+        viewCount: {
+          increment: 1
+        }
+      }
+    })
+    // 4. 
+    if (!post) reply.code(404).send({ error: `Post with id:${id} was not found` })
+    // 5. 
+    return reply.send(post)
+  })
&lt;/span&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;br&gt;
You can head over to &lt;a href="http://localhost:3042/documentation" rel="noopener noreferrer"&gt;&lt;code&gt;http://localhost:3042/documentation&lt;/code&gt;&lt;/a&gt; to view the Swagger docs. You should see the endpoint you just created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F33921841%2F204011764-c569e9a4-96f2-47dd-bdeb-9c29bd52d964.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F33921841%2F204011764-c569e9a4-96f2-47dd-bdeb-9c29bd52d964.png" alt="Platformatic  auto-generated Swagger documentation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To try out the endpoint you just created, you can make a request to your endpoint using &lt;code&gt;curl&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -X PUT \
  'localhost:3042/post/2/views' \
  --header 'Accept: */*' \
  --header 'Content-Type: application/json' 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use Thunder Client on VS Code to run the request. To make a request:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open up Thunder Client on VS Code.&lt;/li&gt;
&lt;li&gt;Create a new request by clicking &lt;strong&gt;New Request&lt;/strong&gt; button on the side bar.&lt;/li&gt;
&lt;li&gt;Select the &lt;code&gt;PUT&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;Enter it to the following URL: &lt;code&gt;http://localhost:3042/post/1/views&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Send&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should see the following output on your editor:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F33921841%2F204013519-a088f09e-586b-4f25-b8cd-68d194133cbd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F33921841%2F204013519-a088f09e-586b-4f25-b8cd-68d194133cbd.png" alt="Thunder Client  raw `PUT` endraw  request"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Recap and next steps
&lt;/h2&gt;

&lt;p&gt;Congratulations! 🎉&lt;br&gt;
Using Prisma Client, you've successfully extended the GraphQL and REST APIs generated with Platformatic API.&lt;/p&gt;

&lt;p&gt;In case you missed it, check out &lt;a href="https://dev.to/prisma/friendly-data-modeling-auto-generated-editable-migrations-for-platformatic-with-prisma-dib"&gt;part 2&lt;/a&gt; to learn how you can use Prisma to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model your database schema&lt;/li&gt;
&lt;li&gt;Auto-generate fully customizable SQL migrations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can find the source code for this project on &lt;a href="https://github.com/ruheni/platformatic-prisma" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;. If you run into any challenges, feel free to create a GitHub issue or contribute to the repository. You can also reach out to me directly on &lt;a href="https://twitter.com/ruheni_alex" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>prisma</category>
      <category>platformatic</category>
    </item>
    <item>
      <title>Friendly data modeling &amp; auto-generated, editable migrations for Platformatic with Prisma</title>
      <dc:creator>Ruheni Alex</dc:creator>
      <pubDate>Thu, 15 Dec 2022 09:38:54 +0000</pubDate>
      <link>https://dev.to/prisma/friendly-data-modeling-auto-generated-editable-migrations-for-platformatic-with-prisma-dib</link>
      <guid>https://dev.to/prisma/friendly-data-modeling-auto-generated-editable-migrations-for-platformatic-with-prisma-dib</guid>
      <description>&lt;p&gt;A wise man once said...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F33921841%2F194832698-08959b77-9490-48ec-91c4-15f8fd9c6dc5.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F33921841%2F194832698-08959b77-9490-48ec-91c4-15f8fd9c6dc5.jpeg" alt="automate-all-the-things"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;... that was me.&lt;/p&gt;

&lt;p&gt;But all jokes and memes aside, automation helps cut down the amount of time spent on tedious and repetitive tasks.&lt;/p&gt;

&lt;p&gt;This guide will teach you how to model your database schema and auto-generate customizable SQL migrations using &lt;a href="https://www.prisma.io" rel="noopener noreferrer"&gt;Prisma&lt;/a&gt; when working with Platformatic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Your dev toolbox
&lt;/h3&gt;

&lt;p&gt;To follow along, ensure you have the following installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js  v16.17.0&lt;/li&gt;
&lt;li&gt;npm v7 or later&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://marketplace.visualstudio.com/items?itemName=Prisma.prisma" rel="noopener noreferrer"&gt;Prisma VSCode Extension&lt;/a&gt; (&lt;em&gt;optional&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://marketplace.visualstudio.com/items?itemName=rangav.vscode-thunder-client" rel="noopener noreferrer"&gt;Thunder Client VSCode Extension&lt;/a&gt;  (&lt;em&gt;optional&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Docker (&lt;em&gt;optional&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If you don't have Docker installed, you can set up a free hosted database on &lt;a href="https://railway.app/" rel="noopener noreferrer"&gt;Railway&lt;/a&gt; or install &lt;a href="https://www.postgresql.org/download/" rel="noopener noreferrer"&gt;PostgreSQL&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Assumed knowledge
&lt;/h3&gt;

&lt;p&gt;This guide will require you to have basic familiarity with the following technologies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript&lt;/li&gt;
&lt;li&gt;GraphQL APIs&lt;/li&gt;
&lt;li&gt;REST APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Set up your Platformatic app
&lt;/h2&gt;

&lt;p&gt;In this tutorial, you'll use the following starter &lt;a href="https://github.com/ruheni/prisma-platformatic/tree/automated-migrations" rel="noopener noreferrer"&gt;repository&lt;/a&gt;. It contains the setup files for a new Platformatic project.&lt;/p&gt;

&lt;p&gt;To get started, clone the repository and checkout to the &lt;a href="https://github.com/ruheni/prisma-platformatic/tree/automated-migrations" rel="noopener noreferrer"&gt;&lt;code&gt;automated-migrations&lt;/code&gt;&lt;/a&gt; branch.&lt;/p&gt;

&lt;p&gt;Clone the repository:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git clone &lt;span class="nt"&gt;-b&lt;/span&gt; automated-migrations https://github.com/ruheni/prisma-platformatic.git


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, perform the following actions to get started:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Navigate to the cloned directory:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

cd prisma-platformatic
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
1. Install dependencies:

    ```bash


    npm install


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a &lt;code&gt;.env&lt;/code&gt; file based-off of the &lt;code&gt;.env.example&lt;/code&gt; file:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

cp .env.example .env
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
1. Start the PostgreSQL database with docker:

    ```


    docker-compose up -d


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you already have an existing database server running locally, update the value of the &lt;code&gt;DATABASE_URL&lt;/code&gt; in your &lt;code&gt;.env&lt;/code&gt; file with your database's user and password values:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# .env
DATABASE_URL="postgres://&amp;lt;USER&amp;gt;:&amp;lt;PASSWORD&amp;gt;@localhost:5432/blog"


&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Connect to your database instance using &lt;code&gt;psql&lt;/code&gt; or your preferred SQL client. Copy and run the following SQL to create a database:&lt;/p&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;DATABASE&lt;/span&gt; &lt;span class="n"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Project structure and files
&lt;/h3&gt;

&lt;p&gt;The project has the following structure:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

prisma-platformatic
  ├── .env.example
  ├── .env
  ├── .gitignore
  ├── README.md
  ├── docker-compose.yml
  ├── package-lock.json
  ├── package.json
  └── platformatic.db.json


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The noteworthy files in the directory are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.env&lt;/code&gt;: Contains the database connection string for your PostgreSQL database.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker-compose.yml&lt;/code&gt;: Defines the Docker image and configuration for your PostgreSQL database.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;package.json&lt;/code&gt;: Defines your application dependencies. &lt;a href="http://npmjs.com/package/platformatic" rel="noopener noreferrer"&gt;&lt;code&gt;platformatic&lt;/code&gt;&lt;/a&gt; is currently the only dependency in the project.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;platformatic.db.json&lt;/code&gt;: Defines &lt;a href="https://oss.platformatic.dev/docs/reference/service/configuration" rel="noopener noreferrer"&gt;Platformatic's configuration&lt;/a&gt; such as the server's hostname and port, migration directory, and your database's connection string.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Data modeling and automated migrations
&lt;/h2&gt;

&lt;p&gt;Now that you've set up your application, it's time to get your hands dirty with Prisma!&lt;/p&gt;

&lt;h3&gt;
  
  
  Set Prisma in your project
&lt;/h3&gt;

&lt;p&gt;To get started, first install the &lt;a href="https://www.prisma.io/docs/reference/api-reference/command-reference" rel="noopener noreferrer"&gt;Prisma CLI&lt;/a&gt; as a development dependency in your project:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm install prisma --save-dev


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The Prisma CLI provides the tools that allow you to evolve your database schema in your project.&lt;/p&gt;

&lt;p&gt;You can now initialize Prisma in your project with the following command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npx prisma init


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The command creates a &lt;code&gt;prisma&lt;/code&gt; folder at the root containing a &lt;code&gt;schema.prisma&lt;/code&gt; file. The &lt;code&gt;schema.prisma&lt;/code&gt; file serves as a source of truth for your database schema.&lt;/p&gt;

&lt;p&gt;When you open up the &lt;code&gt;schema.prisma&lt;/code&gt; file, you should see the following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// prisma/schema.prisma&lt;/span&gt;
&lt;span class="n"&gt;generator&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"prisma-client-js"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;datasource&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"postgres"&lt;/span&gt;
  &lt;span class="n"&gt;url&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"DATABASE_URL"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The Prisma schema uses an intuitive and human-readable language called the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-schema#syntax" rel="noopener noreferrer"&gt;&lt;em&gt;Prisma Schema language&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The schema file is composed of three main components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data source&lt;/strong&gt;: Defines your database connection details such as the provider and database's connection string.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Generator&lt;/strong&gt;: Defines the &lt;em&gt;assets&lt;/em&gt; generated when specific Prisma commands are invoked. In this case, Prisma Client, a type-safe query builder for your database, will be generated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data model&lt;/strong&gt;: Defines the entities of your application that map to your database's tables (for relational databases) or collections (MongoDB). The schema doesn't have any yet, but models are denoted with the &lt;code&gt;model&lt;/code&gt; keyword, followed by the entity name.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Model your database schema
&lt;/h3&gt;

&lt;p&gt;For this guide, you will create a &lt;code&gt;Post&lt;/code&gt; model with the following fields in your &lt;code&gt;schema.prisma&lt;/code&gt; file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// ./prisma/schema.prisma&lt;/span&gt;
&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;        &lt;span class="n"&gt;Int&lt;/span&gt;      &lt;span class="nd"&gt;@id&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autoincrement&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;title&lt;/span&gt;     &lt;span class="n"&gt;String&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt;   &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;published&lt;/span&gt; &lt;span class="n"&gt;Boolean&lt;/span&gt;  &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;viewCount&lt;/span&gt; &lt;span class="n"&gt;Int&lt;/span&gt;      &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;createdAt&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The snippet above defines a model called &lt;code&gt;Post&lt;/code&gt; with the following fields and properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;id&lt;/code&gt;: An auto-incrementing integer that will be the primary key for the model.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;title&lt;/code&gt;: A non-null &lt;code&gt;String&lt;/code&gt; field.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;content&lt;/code&gt;: A nullable &lt;code&gt;String&lt;/code&gt; field.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;published&lt;/code&gt;: A &lt;code&gt;Boolean&lt;/code&gt; field with a default value of &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;viewCount&lt;/code&gt;: An &lt;code&gt;Int&lt;/code&gt; field with a default value of &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;createdAt&lt;/code&gt;: A &lt;code&gt;DateTime&lt;/code&gt; field with a timestamp of when the value is created as its default value.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Refer to the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-schema/data-model" rel="noopener noreferrer"&gt;Prisma documentation&lt;/a&gt; for further details on how to model your data using Prisma.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Generate a migration with &lt;code&gt;migrate diff&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;With the schema defined, you will now auto-generate a database migration using &lt;a href="https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-diff" rel="noopener noreferrer"&gt;&lt;code&gt;prisma migrate diff&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;prisma migrate diff&lt;/code&gt; compares (or "diffs") two schemas, the &lt;em&gt;current&lt;/em&gt;, and the &lt;em&gt;anticipated&lt;/em&gt; version. The current version is the &lt;em&gt;from&lt;/em&gt; state, and the anticipated version is the &lt;em&gt;to&lt;/em&gt; state. The command generates a SQL script describing the changes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fun fact: If you've used the &lt;a href="https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-dev" rel="noopener noreferrer"&gt;&lt;code&gt;prisma migrate dev&lt;/code&gt;&lt;/a&gt; command before, it runs &lt;code&gt;prisma migrate diff&lt;/code&gt; under the hood.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The command, &lt;code&gt;prisma migrate diff&lt;/code&gt; accepts the following schema sources for comparison:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A live database&lt;/li&gt;
&lt;li&gt;A migration history&lt;/li&gt;
&lt;li&gt;Schema data model (defined in the Prisma schema)&lt;/li&gt;
&lt;li&gt;An empty schema&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;prisma migrate diff&lt;/code&gt; command will use the following arguments to generate a migration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--from-schema-datasource&lt;/code&gt;: Uses the URL defined in the &lt;code&gt;datasource&lt;/code&gt; block.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--to-schema-datamodel&lt;/code&gt;: Uses the data model defined in the Prisma schema for the diff.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--script&lt;/code&gt; (&lt;em&gt;optional&lt;/em&gt;): Outputs a SQL script.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;--from-schema-datasource&lt;/code&gt; and &lt;code&gt;--to-schema-datamodel&lt;/code&gt; also require a path to your Prisma schema file.&lt;/p&gt;

&lt;p&gt;Create the &lt;code&gt;migrations&lt;/code&gt; directory that you will use to store a history of migrations:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

mkdir migrations


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;migrations&lt;/code&gt; directory is used by Platformatic to store and track the history of applied migrations.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, open up a terminal within your project directory run the following command to auto-generate your first migration:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npx prisma migrate diff &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--from-schema-datasource&lt;/span&gt; ./prisma/schema.prisma &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--to-schema-datamodel&lt;/span&gt; ./prisma/schema.prisma &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--script&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; migrations/001.do.sql &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;--exit-code&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Notes&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Update the output filename for any future migrations to prevent overwriting the contents of &lt;code&gt;001.do.sql&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You can jump to the &lt;strong&gt;Side quest&lt;/strong&gt; section to learn how you can automate versioning and generating migrations with the &lt;a href="https://github.com/ruheni/db-diff" rel="noopener noreferrer"&gt;&lt;code&gt;@ruheni/db-diff&lt;/code&gt;&lt;/a&gt; utility library&lt;/li&gt;
&lt;li&gt;If you omit the &lt;code&gt;--script&lt;/code&gt; argument, the command will generate a human-readable summary that looks something like this:
```
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;[+] Added tables&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Post&lt;/li&gt;
&lt;/ul&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;The command creates a file called &lt;code&gt;001.do.sql&lt;/code&gt; inside the &lt;code&gt;migrations&lt;/code&gt; directory with the following contents:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;

&lt;span class="c1"&gt;-- migrations/001.do.sql&lt;/span&gt;
&lt;span class="c1"&gt;-- CreateTable&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nv"&gt;"Post"&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;"id"&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"createdAt"&lt;/span&gt; &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;CURRENT_TIMESTAMP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"title"&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"content"&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"published"&lt;/span&gt; &lt;span class="nb"&gt;BOOLEAN&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"viewCount"&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="nv"&gt;"Post_pkey"&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You'll notice that the command generated the SQL that describes the changes you defined in the Prisma schema file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start your API server
&lt;/h2&gt;

&lt;p&gt;In your project directory, apply the migrations to your database using the Platformatic CLI:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npx platformatic db migrations apply


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, start up your API server:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npx platformatic db start


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The command will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start the Platformatic API server&lt;/li&gt;
&lt;li&gt;Auto-generate a REST and GraphQL API from your SQL database&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Explore and interact with your API
&lt;/h3&gt;

&lt;p&gt;You can now explore your GraphQL API on &lt;a href="http://localhost:3042/graphiql" rel="noopener noreferrer"&gt;&lt;code&gt;http://localhost:3042/graphiql&lt;/code&gt;&lt;/a&gt; or your REST API on &lt;a href="http://localhost:3042/documentation" rel="noopener noreferrer"&gt;&lt;code&gt;http://localhost:3042/documentation&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Run the following mutation on GraphiQL to insert a record in your database:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;mutation&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;INSERT_POST&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;insertPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Prisma 💚 Platformatic"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Learn how you can auto-generate your database migrations using Prisma for Platformatic"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;createdAt&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;published&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You should see the following output with a different &lt;code&gt;createdAt&lt;/code&gt; value:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"insertPost"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Prisma 💚 Platformatic"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Learn how you can auto-generate your database migrations using Prisma for Platformatic"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"createdAt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2022-10-08T14:26:08.101Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"published"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Congratulations! 🎉&lt;/p&gt;

&lt;h3&gt;
  
  
  Introspect your database for the &lt;code&gt;versions&lt;/code&gt; model
&lt;/h3&gt;

&lt;p&gt;Under the hood, Platformatic uses &lt;a href="https://www.npmjs.com/package/postgrator" rel="noopener noreferrer"&gt;Postgrator&lt;/a&gt; to run migrations. Postgrator creates a table in the database called &lt;code&gt;versions&lt;/code&gt; to track the applied migrations.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;versions&lt;/code&gt; table is not yet captured in the Prisma schema. When auto-generating future migrations, Prisma might prompt you to drop the &lt;code&gt;versions&lt;/code&gt; table, which is not ideal.&lt;/p&gt;

&lt;p&gt;To prevent this, you can run &lt;code&gt;prisma db pull&lt;/code&gt; to introspect the database and populate the Prisma schema with the missing model:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npx prisma db pull


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Your Prisma schema should now contain a &lt;code&gt;version&lt;/code&gt; model:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="err"&gt;

&lt;/span&gt;// ./prisma/schema.prisma
&lt;span class="p"&gt;model Post {
&lt;/span&gt;  id        Int      @id @default(autoincrement())
  title     String
  content   String?
  published Boolean  @default(false)
  viewCount Int      @default(0)
  createdAt DateTime @default(now())
}
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="gi"&gt;+model versions {
+  version BigInt    @id
+  name    String?
+  md5     String?
+  run_at  DateTime? @db.Timestamptz(6)
+}
&lt;/span&gt;&lt;span class="err"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add the &lt;code&gt;@@ignore&lt;/code&gt; attribute function to the model to exclude it from the Prisma Client API:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="err"&gt;

&lt;/span&gt;// ./prisma/schema.prisma
&lt;span class="p"&gt;model Post {
&lt;/span&gt;  id        Int      @id @default(autoincrement())
  title     String
  content   String?
  published Boolean  @default(false)
  viewCount Int      @default(0)
  createdAt DateTime @default(now())
}
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;model versions {
&lt;/span&gt;  version BigInt    @id
  name    String?
  md5     String?
  run_at  DateTime? @db.Timestamptz(6)
&lt;span class="gi"&gt;+
+  @@ignore
&lt;/span&gt;}
&lt;span class="err"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Side quest 🧙🏽: Automate versioning and generation of your database migrations
&lt;/h2&gt;

&lt;p&gt;The approach for generating migrations in the previous section generally works fine. The only caveat is that you have to manually specify the version of the migration file with every migration, i.e., &lt;code&gt;001.do.sql&lt;/code&gt;, &lt;code&gt;002.do.sql&lt;/code&gt;, and so forth.&lt;/p&gt;

&lt;p&gt;Another friction point is that the command is very long, tedious and there is a possibility of making an error.&lt;/p&gt;

&lt;p&gt;To get around these friction points, I built a utility library called &lt;a href="https://github.com/ruheni/db-diff" rel="noopener noreferrer"&gt;&lt;code&gt;@ruheni/db-diff&lt;/code&gt;&lt;/a&gt;. The tool wraps around the &lt;code&gt;prisma migrate diff&lt;/code&gt; command. It can generate an &lt;code&gt;up&lt;/code&gt; and a &lt;code&gt;down&lt;/code&gt; migration. &lt;code&gt;@ruheni/db-diff&lt;/code&gt; also versions the generated migration file and are Postgrator-compatible. On top of that, you can generate an up and down migration for every schema change.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Alternatively, you can also use &lt;a href="https://github.com/kgajera/platformatic-prisma" rel="noopener noreferrer"&gt;&lt;code&gt;platformatic-prisma&lt;/code&gt;&lt;/a&gt; by &lt;a href="https://twitter.com/kgajera24" rel="noopener noreferrer"&gt;Kishan Gajera&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Install the helper utility
&lt;/h3&gt;

&lt;p&gt;To get started, you can install &lt;code&gt;@ruheni/db-diff&lt;/code&gt; as a development dependency in your project:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm install --save-dev @ruheni/db-diff


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Update your schema
&lt;/h3&gt;

&lt;p&gt;Next, update your Prisma schema by creating a &lt;code&gt;User&lt;/code&gt; model with the following fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;id&lt;/code&gt;: the primary key with an auto-incrementing integer&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;email&lt;/code&gt;: a string value with a &lt;code&gt;@unique&lt;/code&gt; constraint&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt;: a string value (nullable/ not-required)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;posts&lt;/code&gt;: a one-to-many relationship between the &lt;code&gt;Post&lt;/code&gt; and &lt;code&gt;User&lt;/code&gt; models, respectively&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your Prisma schema should resemble the schema in the code block below:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// ./prisma/schema.prisma&lt;/span&gt;
&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;    &lt;span class="n"&gt;Int&lt;/span&gt;     &lt;span class="nd"&gt;@id&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autoincrement&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;  &lt;span class="nd"&gt;@unique&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;  &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;posts&lt;/span&gt; &lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;        &lt;span class="n"&gt;Int&lt;/span&gt;      &lt;span class="nd"&gt;@id&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autoincrement&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;createdAt&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;title&lt;/span&gt;     &lt;span class="n"&gt;String&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt;   &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;published&lt;/span&gt; &lt;span class="n"&gt;Boolean&lt;/span&gt;  &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;viewCount&lt;/span&gt; &lt;span class="n"&gt;Int&lt;/span&gt;      &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;author&lt;/span&gt;    &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;    &lt;span class="nd"&gt;@relation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;fields:&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;authorId&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="nl"&gt;references:&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
  &lt;span class="n"&gt;authorId&lt;/span&gt;  &lt;span class="n"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;versions&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="n"&gt;BigInt&lt;/span&gt;    &lt;span class="nd"&gt;@id&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;    &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;md5&lt;/span&gt;     &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;run_at&lt;/span&gt;  &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nd"&gt;@db.Timestamptz&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;@ignore&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;
  Expand here to see the schema diff
  &lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="err"&gt;

&lt;/span&gt;// ./prisma/schema.prisma
&lt;span class="gi"&gt;+model User {
+  id    Int     @id @default(autoincrement())
+  email String  @unique
+  name  String?
+  posts Post[]
+}
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;model Post {
&lt;/span&gt;  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  title     String
  content   String?
  published Boolean  @default(false)
  viewCount Int      @default(0)
&lt;span class="gi"&gt;+  author    User?    @relation(fields: [authorId], references: [id])
+  authorId  Int?
&lt;/span&gt;}
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;model versions {
&lt;/span&gt;  version BigInt    @id
  name    String?
  md5     String?
  run_at  DateTime? @db.Timestamptz(6)
&lt;span class="err"&gt;
&lt;/span&gt;  @@ignore
}
&lt;span class="err"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;/p&gt;

&lt;h3&gt;
  
  
  Auto-generate an &lt;code&gt;up&lt;/code&gt; migration using &lt;code&gt;@ruheni/db-diff&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Next, use &lt;code&gt;@ruheni/db-diff&lt;/code&gt; to auto-generate an &lt;code&gt;up&lt;/code&gt; migration:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npx db-diff --up


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The command should generate a new file called &lt;code&gt;002.do.sql&lt;/code&gt; with the following contents:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;

&lt;span class="c1"&gt;-- migrations/002.do.sql&lt;/span&gt;
&lt;span class="c1"&gt;-- AlterTable&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nv"&gt;"Post"&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt;     &lt;span class="nv"&gt;"authorId"&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- CreateTable&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nv"&gt;"User"&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;"id"&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"email"&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"name"&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="nv"&gt;"User_pkey"&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- CreateIndex&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="nv"&gt;"User_email_key"&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="nv"&gt;"User"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- AddForeignKey&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nv"&gt;"Post"&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="nv"&gt;"Post_authorId_fkey"&lt;/span&gt; &lt;span class="k"&gt;FOREIGN&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"authorId"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nv"&gt;"User"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="k"&gt;CASCADE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can specify the type of migration you would like to generate by passing either &lt;code&gt;--up&lt;/code&gt; for only the &lt;code&gt;up&lt;/code&gt; migration or &lt;code&gt;--down&lt;/code&gt; for the down migration.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@ruheni/db-diff&lt;/code&gt; utility library will auto-generate an up and a down migration if you don't provide either the &lt;code&gt;--up&lt;/code&gt; or &lt;code&gt;--down&lt;/code&gt; flags. If you maintain down migrations, ensure the migration version name is at par with the up migration.&lt;/p&gt;

&lt;p&gt;Apply the generated migration using Platformatic CLI:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npx platformatic db migrations apply


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Restart and interact with your API using Platformatic
&lt;/h3&gt;

&lt;p&gt;Restart the API server:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npx platformatic db start


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Platformatic will regenerate the GraphQL and REST APIs.&lt;/p&gt;

&lt;p&gt;Open up GraphiQL on &lt;code&gt;http://localhost:3042/graphiql&lt;/code&gt; and run the following mutation to create a user record in your database:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;mutation&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;INSERT_USER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;insertUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alex"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"alex@prisma.io"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;
  Expand to view the response
  &lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"insertUser"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alex"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;/p&gt;

&lt;p&gt;Run another query to link the user record with the existing post record you created in a previous step:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight graphql"&gt;&lt;code&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="k"&gt;mutation&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SAVE_POST&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="n"&gt;savePost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;authorId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;
  Expand to view the response
  &lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"savePost"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Prisma 💚 Platformatic"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Learn how you can auto-generate your database migrations using Prisma for Platformatic"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alex"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;/p&gt;

&lt;p&gt;And you're all done! 🎉&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;To recap what was covered in this part, you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modeled your database schema using Prisma&lt;/li&gt;
&lt;li&gt;Used the &lt;code&gt;prisma migrate diff&lt;/code&gt; to auto-generate your SQL migrations&lt;/li&gt;
&lt;li&gt;Created a GraphQL and REST API using Platformatic&lt;/li&gt;
&lt;li&gt;Used the &lt;code&gt;@ruheni/db-diff&lt;/code&gt; utility to auto-generate and version your SQL migrations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The next article will cover how you can extend the generated GraphQL and REST API using Prisma Client.&lt;/p&gt;

&lt;p&gt;Feel free to refer to &lt;a href="https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-diff" rel="noopener noreferrer"&gt;&lt;code&gt;prisma migrate diff&lt;/code&gt; reference docs&lt;/a&gt; to learn how you can use it to automate your database migration workflows. If you build something cool you would like to share with the rest of the world, feel free to share it in this &lt;a href="https://github.com/prisma/prisma/discussions/14101" rel="noopener noreferrer"&gt;GitHub discussion thread&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the event you run into any issues working with &lt;a href="https://github.com/ruheni/db-diff" rel="noopener noreferrer"&gt;&lt;code&gt;@ruheni/db-diff&lt;/code&gt;&lt;/a&gt;, feel free to &lt;a href="https://github.com/ruheni/db-diff/issues/new" rel="noopener noreferrer"&gt;create a GitHub issue&lt;/a&gt; or contribute to the library.&lt;/p&gt;

&lt;p&gt;Happy hacking! 🚀&lt;/p&gt;

</description>
      <category>node</category>
      <category>prisma</category>
      <category>database</category>
      <category>migrations</category>
    </item>
    <item>
      <title>Why Prisma and Platformatic are a great match</title>
      <dc:creator>Ruheni Alex</dc:creator>
      <pubDate>Wed, 14 Dec 2022 11:27:35 +0000</pubDate>
      <link>https://dev.to/prisma/why-prisma-and-platformatic-are-a-great-match-2dkl</link>
      <guid>https://dev.to/prisma/why-prisma-and-platformatic-are-a-great-match-2dkl</guid>
      <description>&lt;p&gt;&lt;a href="https://oss.platformatic.dev/" rel="noopener noreferrer"&gt;Platformatic&lt;/a&gt; is a new tool that helps Node.js developers ship APIs faster. In this 3-part series, we’ll explore why &lt;a href="https://www.prisma.io/" rel="noopener noreferrer"&gt;Prisma&lt;/a&gt; and Platformatic are such a great fit and how Prisma can help improve your data modeling, migrations, and querying workflows when using Platformatic.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Platformatic DB
&lt;/h2&gt;

&lt;p&gt;Platformatic DB is an open-source tool that auto-generates CRUD APIs for your database. It supports both GraphQL and REST (with an OpenAPI schema) APIs. &lt;/p&gt;

&lt;p&gt;It also allows you to extend the API using &lt;a href="https://oss.platformatic.dev/docs/reference/db/plugin" rel="noopener noreferrer"&gt;plugins&lt;/a&gt; and add authentication as well as authorization for your APIs. Platformatic DB supports SQLite, MySQL, MariaDB, and PostgreSQL databases.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Prisma
&lt;/h2&gt;

&lt;p&gt;Prisma is an open-source ORM (Object Relational Mapper). It provides a declarative way to define the structure of your database using the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-schema" rel="noopener noreferrer"&gt;Prisma schema language&lt;/a&gt;. It also provides tools for evolving your database schema by auto-generating fully customizable SQL migrations.&lt;/p&gt;

&lt;p&gt;Prisma provides a type-safe and intuitive client for interacting with your database called &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-client" rel="noopener noreferrer"&gt;Prisma Client&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Prisma can improve your development workflow with Platformatic DB
&lt;/h2&gt;

&lt;p&gt;While Platformatic speeds up building REST and GraphQL APIs from your database, Prisma can complement your development workflow in several ways.&lt;/p&gt;

&lt;p&gt;In the following sections, we'll explore three concrete ways Prisma makes working with Platformatic DB easier.&lt;/p&gt;

&lt;h3&gt;
  
  
  Intuitive data modeling language
&lt;/h3&gt;

&lt;p&gt;Prisma provides a Prisma schema — an intuitive, human-readable declarative definition of your database schema. The Prisma schema serves as the source of truth for your database schema by describing its desired end state.&lt;/p&gt;

&lt;p&gt;One of the benefits of using the Prisma schema is the auto-completion, with the help of the Prisma extension, and type-checking when modeling your database schema, allowing you to catch errors early.&lt;/p&gt;

&lt;p&gt;The following schema represents:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;User&lt;/code&gt; and &lt;code&gt;Post&lt;/code&gt; models&lt;/li&gt;
&lt;li&gt;A one-to-many relationship between the &lt;code&gt;User&lt;/code&gt; and &lt;code&gt;Post&lt;/code&gt; models
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;        &lt;span class="n"&gt;Int&lt;/span&gt;      &lt;span class="nd"&gt;@id&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autoincrement&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;createdAt&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;      &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;email&lt;/span&gt;     &lt;span class="n"&gt;String&lt;/span&gt;   &lt;span class="nd"&gt;@unique&lt;/span&gt;

  &lt;span class="n"&gt;posts&lt;/span&gt;     &lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;        &lt;span class="n"&gt;Int&lt;/span&gt;      &lt;span class="nd"&gt;@id&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autoincrement&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;createdAt&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;title&lt;/span&gt;     &lt;span class="n"&gt;String&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt;   &lt;span class="n"&gt;String&lt;/span&gt;
  &lt;span class="n"&gt;published&lt;/span&gt; &lt;span class="n"&gt;Boolean&lt;/span&gt;  &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;author&lt;/span&gt;    &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;    &lt;span class="nd"&gt;@relation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;fields:&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;authorId&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="nl"&gt;references:&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
  &lt;span class="n"&gt;authorId&lt;/span&gt;  &lt;span class="n"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Refer to the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-schema/data-model" rel="noopener noreferrer"&gt;Prisma docs&lt;/a&gt; to learn more about data modeling.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Auto-generated and customizable SQL migrations
&lt;/h3&gt;

&lt;p&gt;To generate your API with Platformatic, you must first define your database schema. The database schema is defined using SQL in a set of migrations files that are written manually. However, maintaining SQL migrations by hand can be tedious, brittle, and error-prone.&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1575443725047812098-340" src="https://platform.twitter.com/embed/Tweet.html?id=1575443725047812098"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1575443725047812098-340');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1575443725047812098&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I also think writing your own migrations introduces risk for user error like forgetting to create a foreign key.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/kgajera24" rel="noopener noreferrer"&gt;Kishan Gajera&lt;/a&gt; from &lt;a href="https://kgajera.com/blog/2022/11/15/platformatic-db-using-prisma-for-migrations/" rel="noopener noreferrer"&gt;Generate Up and Down Migrations for Platformatic DB using Prisma&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Editors typically don't provide auto-completion and type-checking when working with SQL. This might make it challenging to spot errors such as missing constraints like foreign keys.&lt;/p&gt;

&lt;p&gt;Prisma can help automate this process by generating a fully-customizable migration based on the defined schema using the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-cli" rel="noopener noreferrer"&gt;CLI&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The following SQL migration will be generated based on the schema in the previous section:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- CreateTable&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nv"&gt;"User"&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;"id"&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"createdAt"&lt;/span&gt; &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;CURRENT_TIMESTAMP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"email"&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"name"&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="nv"&gt;"User_pkey"&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- CreateTable&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nv"&gt;"Post"&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;"id"&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"title"&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"content"&lt;/span&gt; &lt;span class="nb"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"published"&lt;/span&gt; &lt;span class="nb"&gt;BOOLEAN&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"createdAt"&lt;/span&gt; &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;CURRENT_TIMESTAMP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;"authorId"&lt;/span&gt; &lt;span class="nb"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="nv"&gt;"Post_pkey"&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- CreateIndex&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="nv"&gt;"User_email_key"&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="nv"&gt;"User"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- AddForeignKey&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nv"&gt;"Post"&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="nv"&gt;"Post_authorId_fkey"&lt;/span&gt; &lt;span class="k"&gt;FOREIGN&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"authorId"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nv"&gt;"User"&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="k"&gt;CASCADE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Type-safety and auto-completion for database queries
&lt;/h3&gt;

&lt;p&gt;Platformatic provides &lt;a href="https://oss.platformatic.dev/docs/reference/db/plugin" rel="noopener noreferrer"&gt;plugins&lt;/a&gt; for extending your API. It also provides a &lt;a href="https://oss.platformatic.dev/docs/reference/sql-mapper/entities/introduction" rel="noopener noreferrer"&gt;SQL mapper&lt;/a&gt; for querying the database.&lt;/p&gt;

&lt;p&gt;Besides the out-of-the-box SQL mapper, you can alternatively extend your Platformatic API using Prisma Client to query your database. Prisma Client provides fully type-safe queries with rich type definitions based on your Prisma schema.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Platformatic also provides TypeScript support. You can use the CLI to &lt;a href="https://oss.platformatic.dev/docs/reference/cli#types" rel="noopener noreferrer"&gt;generate TypeScript types&lt;/a&gt; for your entities from your database.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you're working with the VS Code editor, VS Code's intellisense and suggestions will pop up as you define your queries.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--staxrNn_--%2Fc_limit%252Cf_auto%252Cfl_progressive%252Cq_66%252Cw_880%2Fhttps%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F87cdnxf4y98g7rflagx5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--staxrNn_--%2Fc_limit%252Cf_auto%252Cfl_progressive%252Cq_66%252Cw_880%2Fhttps%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F87cdnxf4y98g7rflagx5.gif" alt="Prisma VS Code intellisense"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Additionally, VS Code can also run your JavaScript file, which uses Prisma Client through the TypeScript compiler and throws errors when a type is violated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--MFi7Afig--%2Fc_limit%252Cf_auto%252Cfl_progressive%252Cq_66%252Cw_880%2Fhttps%3A%2F%2Fi.imgur.com%2F3lYgGAI.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fpracticaldev%2Fimage%2Ffetch%2Fs--MFi7Afig--%2Fc_limit%252Cf_auto%252Cfl_progressive%252Cq_66%252Cw_880%2Fhttps%3A%2F%2Fi.imgur.com%2F3lYgGAI.gif" alt="Prisma VS Code type validation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Learn more how you can write type-safe JavaScript with JsDoc in &lt;a href="https://www.prisma.io/blog/type-safe-js-with-jsdoc-typeSaf3js" rel="noopener noreferrer"&gt;this article&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;Platformatic speeds up the development of REST and GraphQL APIs. Prisma pushes the development experience even further by providing intuitive data modeling, auto-generating customizable migrations, type-safety, and auto-completion.&lt;/p&gt;

&lt;p&gt;Look out for parts 2 and 3 — modeling, auto-generating database migrations, and extending your API using a plugin with Prisma Client for Platformatic. 🤙🏾&lt;/p&gt;

</description>
      <category>node</category>
      <category>prisma</category>
      <category>database</category>
      <category>migrations</category>
    </item>
    <item>
      <title>How Prisma Introspects a Schema from a MongoDB Database</title>
      <dc:creator>Jan Piotrowski</dc:creator>
      <pubDate>Tue, 03 May 2022 10:40:26 +0000</pubDate>
      <link>https://dev.to/prisma/how-prisma-introspects-a-schema-from-a-mongodb-database-1ggc</link>
      <guid>https://dev.to/prisma/how-prisma-introspects-a-schema-from-a-mongodb-database-1ggc</guid>
      <description>&lt;p&gt;Prisma ORM &lt;a href="https://www.prisma.io/blog/mongodb-general-availability-pixnun6mffmu"&gt;recently released support for MongoDB&lt;/a&gt;. This represents the first time Prisma has supported a database outside of the SQL world. Prisma has been known for supporting many relational databases, but how did it end up being able to support the quite different MongoDB?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🕴️ I work as the Engineering Manager of Prisma’s Schema team. We are responsible for the schema management parts of Prisma, which most prominently include our migrations and introspection tools, as well as the Prisma schema language and file (and our awesome &lt;a href="https://marketplace.visualstudio.com/items?itemName=Prisma.prisma"&gt;Prisma VS Code extension&lt;/a&gt;!).&lt;/p&gt;

&lt;p&gt;The other big team working on Prisma object relational-mapper (ORM) is the Client team that builds Prisma Client and the Query Engine. These let users interact with their database to read and manipulate data.&lt;/p&gt;

&lt;p&gt;In this blog post, I summarize how our team got Prisma’s schema introspection feature to work for the new MongoDB connector and the interesting challenges we solved along the way.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Prisma
&lt;/h2&gt;

&lt;p&gt;Prisma is a Node.js database ORM built around the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-schema"&gt;Prisma schema language and the Prisma schema file&lt;/a&gt; containing an abstract representation of a user’s database. When you have tables with columns of certain data types in your database, those are represented as models with fields of a type in your Prisma schema.&lt;/p&gt;

&lt;p&gt;Prisma uses that information to generate a fully type-safe &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-client"&gt;TypeScript/JavaScript Prisma Client&lt;/a&gt; that makes it easy to interact with the data in your database (meaning it will complain if you try to write a &lt;code&gt;String&lt;/code&gt; into a &lt;code&gt;Datetime&lt;/code&gt; field, and make sure you, for example, include information for all the non-nullable columns without a default and similar).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.prisma.io/docs/concepts/components/prisma-migrate"&gt;Prisma Migrate&lt;/a&gt; uses your changes to the Prisma schema to automatically generate the SQL required to migrate your database to reflect that new schema. You don’t need to think about the changes necessary. You just write what you want to achieve, and Prisma then intelligently generates the SQL DDL (Data Definition Language) for that.&lt;/p&gt;

&lt;p&gt;For users who want to start using Prisma with their existing database, Prisma has a feature called &lt;a href="https://www.prisma.io/docs/concepts/components/introspection"&gt;Introspection&lt;/a&gt;. You call the CLI command &lt;code&gt;prisma db pull&lt;/code&gt; to “pull in” the existing database schema, and Prisma then can create the Prisma schema for you automatically, so your existing database can be used with Prisma in seconds.&lt;/p&gt;

&lt;p&gt;This works the same for PostgreSQL, MySQL, MariaDB, SQL Server, CockroachDB, and even SQLite and relies on &lt;em&gt;relational databases&lt;/em&gt; being pretty similar, having tables and columns, understanding some dialect of SQL, having foreign keys, and concepts like referential integrity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prisma + MongoDB
&lt;/h2&gt;

&lt;p&gt;One of our most requested features was support for Prisma with MongoDB. The &lt;a href="https://github.com/prisma/prisma/issues/1277"&gt;feature request issue on GitHub for MongoDB support&lt;/a&gt; from January 2020 was for a long time by far the most popular one, having gained more than a total of 800 reactions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c27pwDyo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mongodb-devhub-cms.s3.us-west-1.amazonaws.com/1_c73f797f9a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c27pwDyo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mongodb-devhub-cms.s3.us-west-1.amazonaws.com/1_c73f797f9a.png" alt="GitHub issue reactions image" width="603" height="64"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;MongoDB is known for its flexible schema and the document model, where you can store JSON-like documents. MongoDB takes a different paradigm from relational databases when modeling data – there are no tables, no columns, schemas, or foreign keys to represent relations between tables. Data is often stored grouped in the same document with related data or “denormalized,” which is different from what you would see in a relational database.&lt;/p&gt;

&lt;p&gt;So, how could these very different worlds be brought together?&lt;/p&gt;

&lt;h2&gt;
  
  
  Prisma and MongoDB: Schema team edition
&lt;/h2&gt;

&lt;p&gt;For our team, this meant figuring out: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to represent a MongoDB structure and its documents in a Prisma schema.&lt;/li&gt;
&lt;li&gt;How to migrate said data structures.&lt;/li&gt;
&lt;li&gt;How to let people introspect their existing MongoDB database to easily be able to start using Prisma.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Fortunately solving 1 and 2 was relatively simple:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Where relational databases have tables, columns, and foreign keys that are mapped to Prisma’s models, with their fields and relations, MongoDB has equivalent collections, fields, and &lt;a href="https://www.mongodb.com/docs/manual/core/data-modeling-introduction/#references"&gt;references&lt;/a&gt; that could be mapped the same way. Prisma Client can use that information to provide the same type safety and functionality on the Client side.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Relational database&lt;/th&gt;
&lt;th&gt;Prisma&lt;/th&gt;
&lt;th&gt;MongoDB&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Table →&lt;/td&gt;
&lt;td&gt;Model&lt;/td&gt;
&lt;td&gt;← Collection&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Column →&lt;/td&gt;
&lt;td&gt;Field&lt;/td&gt;
&lt;td&gt;← Field&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Foreign Key →&lt;/td&gt;
&lt;td&gt;Relation&lt;/td&gt;
&lt;td&gt;← Reference&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;With no database-side schema to migrate, creating and updating indexes and constraints was all that was needed for evolving a MongoDB database schema. As there is no SQL to modify the database structure (which is not written down or defined anywhere), Prisma also did not have to create migration files with Data Definition Language (DDL) statements and could just scope it down to allowing &lt;code&gt;prisma db push&lt;/code&gt; to directly bring the database to the desired end state.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A bigger challenge turned out to be the Introspection feature.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introspecting a schema with MongoDB
&lt;/h2&gt;

&lt;p&gt;With relational databases with a schema, there is always a way to inquire for the schema. In PostgreSQL, for example, you can query multiple views in a &lt;code&gt;information_schema&lt;/code&gt; schema to figure out all the details about the structure of the database—to, for example, generate the DDL SQL required to recreate a database, or abstract it into a Prisma schema.&lt;/p&gt;

&lt;p&gt;Because MongoDB has a flexible schema (unless schemas are enforced through the schema validation feature), no such information store exists that could be easily queried. That, of course, poses a problem for how to implement introspection for MongoDB in Prisma.&lt;/p&gt;

&lt;h2&gt;
  
  
  Research
&lt;/h2&gt;

&lt;p&gt;As any good engineering team would, we started by ... Googling a bit. No need to reinvent the wheel, if someone else solved the problem in the past before. Searches for “MongoDB introspection,” “MongoDB schema reverse engineering,” and (as we learned the native term) “MongoDB infer schema” fortunately brought some interesting and worthwhile results.&lt;/p&gt;

&lt;h3&gt;
  
  
  MongoDB Compass
&lt;/h3&gt;

&lt;p&gt;MongoDB’s own database GUI Compass has a &lt;a href="https://www.mongodb.com/docs/compass/current/schema/"&gt;“Schema” tab&lt;/a&gt; in a collection that can analyze a collection to “provide an overview of the data type and shape of the fields in a particular collection.”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EPXF5zxH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mongodb-devhub-cms.s3.us-west-1.amazonaws.com/2_f3f060548e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EPXF5zxH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mongodb-devhub-cms.s3.us-west-1.amazonaws.com/2_f3f060548e.png" alt="MongoDB Compass GUI Schema tab" width="857" height="716"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It works by sampling 1000 documents from a collection that has at least 1000 documents in it, analyzing the individual fields and then presenting them to the user.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;mongodb-schema&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Another resource we found was Lucas Hrabovsky’s &lt;a href="https://github.com/imlucas/mongodb-infer"&gt;&lt;code&gt;mongodb-infer&lt;/code&gt; repository&lt;/a&gt; from 2014. Later that year, it seemed to have merged/been replaced by &lt;a href="https://github.com/mongodb-js/mongodb-schema"&gt;&lt;code&gt;mongodb-schema&lt;/code&gt;&lt;/a&gt;, which is updated to this day.&lt;/p&gt;

&lt;p&gt;It’s a CLI and library version of the same idea—and indeed, when checking the source code of MongoDB Compass, you see a dependency for &lt;code&gt;mongodb-schema&lt;/code&gt; that is used under the hood.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing introspection for MongoDB in Prisma
&lt;/h2&gt;

&lt;p&gt;Usually, finding an open source library with an Apache 2.0 license means you just saved the engineering team a lot of time, and the team can just become a user of the library. But in this case, we wanted to implement our introspection in the same introspection engine we also use for the SQL databases—and that is written in Rust. As there is no &lt;code&gt;mongodb-schema&lt;/code&gt; for Rust yet, we had to implement this ourselves. Knowing how &lt;code&gt;mongodb-schema&lt;/code&gt; works, this turned out to be straightforward:&lt;/p&gt;

&lt;p&gt;We start by simply getting all collections in a database. The &lt;a href="https://github.com/mongodb/mongo-rust-driver"&gt;MongoDB Rust driver&lt;/a&gt; provides a handy &lt;a href="https://github.com/mongodb/mongo-rust-driver#getting-a-handle-to-a-database"&gt;&lt;code&gt;db.list_collection_names()&lt;/code&gt;&lt;/a&gt; that we can call to get all collections—and each collection is turned into a model for Prisma schema. 🥂&lt;/p&gt;

&lt;p&gt;To fill in the fields with their type, we get a sample of up to 1000 random records from each collection and loop through them. For each entry, we note which fields exist, and what data type they have. We map the BSON type to our Prisma scalar types (and native type, if necessary). Optimally, all entries have the same fields with the same data type, which is easily and cleanly mappable—and we are done!&lt;/p&gt;

&lt;p&gt;Often, not all entries in a collection are that uniform. Missing fields, for example, are expected and equivalent to &lt;code&gt;NULL&lt;/code&gt; values in a relational database.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to present fields with different types
&lt;/h3&gt;

&lt;p&gt;But different types (for example, &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;Datetime&lt;/code&gt;) pose a problem: Which type should we put into the Prisma schema?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🎓 &lt;strong&gt;Learning 1: Just choosing the most common data type is not a good idea.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In an early iteration of MongoDB introspection, we defaulted to the most common type, and left a comment with the percentage of the occurrences in the Prisma schema. The idea was that this should work most of the time and give the developer the best development experience—the better the types in your Prisma schema, the more Prisma can help you.&lt;/p&gt;

&lt;p&gt;But we quickly figured out when testing this that there was a slight (but logical) problem: Any time the Prisma Client encounters a type that does &lt;em&gt;not&lt;/em&gt; match what it has been told via the Prisma schema, it has to throw an error and abort the query. Otherwise, it would return data that does not adhere to its own generated types for that data.&lt;/p&gt;

&lt;p&gt;While we were aware this would happen, it was not intuitive to us &lt;em&gt;how often&lt;/em&gt; that would cause the Prisma Client to fail. We quickly learned about this when using such a Prisma schema with conflicting types in the underlying database with Prisma Studio, the built-in database GUI that comes with Prisma CLI (just run &lt;code&gt;npx prisma studio&lt;/code&gt;). By default, it loads 100 entries of a model you view—and when there were ~5% entries with a different type in a database of 1000 entries, it was very common to hit that on the first page already. Prisma Studio (and also an app using these schemas) was essentially unusable for these data sets this way.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Fortunately, &lt;em&gt;everything&lt;/em&gt; in MongoDB is a &lt;code&gt;Document&lt;/code&gt;, which maps to a &lt;code&gt;Json&lt;/code&gt; type field in Prisma. So, when a field has different data types, we use &lt;code&gt;Json&lt;/code&gt; instead, output a warning in Prisma CLI, and put a comment above the field in the Prisma schema that we render, which includes information about the data types we found and how common they were.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5YdRXkX_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mongodb-devhub-cms.s3.us-west-1.amazonaws.com/3_fd4367364a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5YdRXkX_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mongodb-devhub-cms.s3.us-west-1.amazonaws.com/3_fd4367364a.png" alt="Output of Prisma CLI on conflicting data types" width="880" height="263"&gt;&lt;/a&gt;&lt;/p&gt;
Output of Prisma CLI on conflicting data types



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X2hRKGX1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mongodb-devhub-cms.s3.us-west-1.amazonaws.com/4_8ddf4af2fd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X2hRKGX1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mongodb-devhub-cms.s3.us-west-1.amazonaws.com/4_8ddf4af2fd.png" alt="Resulting Prisma schema with statistics on conflicting data types" width="880" height="418"&gt;&lt;/a&gt;&lt;/p&gt;
Resulting Prisma schema with statistics on conflicting data types



&lt;h3&gt;
  
  
  How to iterate on the data to get to a cleaner schema
&lt;/h3&gt;

&lt;p&gt;Using &lt;code&gt;Json&lt;/code&gt; instead of a specific data type, of course, substantially lowers the benefit you get from Prisma and effectively enables you to write any JSON into the field (making the data even less uniform and harder to handle over time!). But at least you can read all existing data in Prisma Studio or in your app and interact with it.&lt;/p&gt;

&lt;p&gt;The preferred way to fix conflicting data types is to read and update them manually with a script, and then run &lt;code&gt;prisma db pull&lt;/code&gt; again. The new Prisma schema should then show only the one type still present in the collection.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;🎓 &lt;strong&gt;Learning 2: Output Prisma types in Prisma schema, not MongoDB types.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Originally, we outputted the raw type information we got from the MongoDB Rust driver, the &lt;a href="https://www.mongodb.com/docs/manual/reference/bson-types/"&gt;BSON types&lt;/a&gt;, into our CLI warnings and Prisma schema comments to help our users iterate on their data and fix the type. It turned out that while this was technically correct and told the user what type the data was in, using the BSON type names was confusing in a Prisma context. We switched to output the Prisma type names instead and this now feels much more natural to users.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While Prisma recommends everyone to clean up their data and minimize the amount of conflicting types that fall back to &lt;code&gt;Json&lt;/code&gt;, that is, of course, also a valid choice.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to enrich the introspected Prisma schema with relations
&lt;/h3&gt;

&lt;p&gt;By adding relation information to your introspected Prisma schema, you can tell Prisma to handle a specific column like a foreign key and create a relation with the data in it. &lt;code&gt;user User @relation(fields: [userId], references: [id])&lt;/code&gt; creates a relation to the &lt;code&gt;User&lt;/code&gt; model via the local &lt;code&gt;userId&lt;/code&gt; field. So, if you are using MongoDB references to model relations, add &lt;code&gt;@relation&lt;/code&gt; to them for Prisma to be able to access those in Prisma Client, emulate referential actions, and help with referential integrity to keep data clean.&lt;/p&gt;

&lt;p&gt;Right now, Prisma does not offer a way to detect or confirm the potential relations between different collections. We want to learn first how MongoDB users actually use relations, and then help them the optimal way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Implementing a good introspection story for MongoDB was a fun challenge for our team. In the beginning, it felt like two very different worlds were clashing together, but in the end, it was straightforward to find the correct tradeoffs and solutions to get the optimal outcome for our users. We are confident we found a great combination that combines the best of MongoDB with what people want from Prisma.&lt;/p&gt;

&lt;p&gt;Try out &lt;a href="https://www.prisma.io/docs/getting-started/setup-prisma/add-to-existing-project/mongodb-typescript-mongodb"&gt;Prisma and MongoDB with an existing MongoDB database&lt;/a&gt;, or &lt;a href="https://www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch/mongodb-typescript-mongodb"&gt;start from scratch and create one along the way&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>prisma</category>
      <category>mongodb</category>
      <category>orm</category>
      <category>database</category>
    </item>
    <item>
      <title>Using Prisma to Address Connection Pooling Issues in Serverless Environments</title>
      <dc:creator>Oleksii Klochai</dc:creator>
      <pubDate>Mon, 06 Dec 2021 14:57:32 +0000</pubDate>
      <link>https://dev.to/prisma/using-prisma-to-address-connection-pooling-issues-in-serverless-environments-3g66</link>
      <guid>https://dev.to/prisma/using-prisma-to-address-connection-pooling-issues-in-serverless-environments-3g66</guid>
      <description>&lt;p&gt;This article discusses what “serverless” environments are, why developers choose to deploy their apps to them, and how the &lt;a href="https://cloud.prisma.io/" rel="noopener noreferrer"&gt;Prisma Data Platform&lt;/a&gt; solves the problems that can arise regarding database connection pooling.&lt;/p&gt;

&lt;p&gt;Choosing which tools you will use to build your application, and which platform you will be deploying to in production, are early and vital decisions that you will have to make when building a product. &lt;/p&gt;

&lt;p&gt;Using an ORM like &lt;a href="https://www.prisma.io/" rel="noopener noreferrer"&gt;Prisma&lt;/a&gt; speeds up the development process, while serverless hosting reduces the ongoing cost and resource overhead of ensuring your application’s availability to your customers.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Prisma?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.prisma.io/" rel="noopener noreferrer"&gt;Prisma&lt;/a&gt; is a next-generation ORM (&lt;a href="https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping" rel="noopener noreferrer"&gt;Object-Relational Mapping&lt;/a&gt;) library for Node.js and TypeScript.&lt;/p&gt;

&lt;p&gt;Prisma allows you to build your app in JavaScript or TypeScript and work with your data as objects rather than SQL or NoSQL queries. These objects are mapped to a database using a schema that generates all the types and conversions required to accurately store your objects in your database platform.&lt;/p&gt;

&lt;p&gt;The Prisma toolset includes developer tools for generating database migrations so that your database is always in-sync with your data mode as defined in the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-schema" rel="noopener noreferrer"&gt;Prisma schema&lt;/a&gt;. In addition, the cloud-based &lt;a href="https://cloud.prisma.io/" rel="noopener noreferrer"&gt;Prisma Data Platform&lt;/a&gt; provides tools like user/role management and a data browser that allow you and your team to collaboratively manage your data online.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Prisma in your projects means spending less time worrying about your database, and more time to focus on building your app.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In addition to being a great aid to development, the &lt;a href="https://cloud.prisma.io/" rel="noopener noreferrer"&gt;Prisma Data Platform&lt;/a&gt; discussed in this article makes Prisma a flexible and efficient way to manage database connections for your application with minimal fuss.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is “serverless” and why do developers “go serverless”?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Serverless_computing" rel="noopener noreferrer"&gt;Serverless hosting&lt;/a&gt; is a means of delivering your app to your user without having to build and maintain a server to deploy it to.&lt;/p&gt;

&lt;p&gt;It may sound strange, but it is a cost effective way to deploy applications. Serverless cloud hosting providers like &lt;a href="https://aws.amazon.com/lambda/" rel="noopener noreferrer"&gt;AWS Lambda&lt;/a&gt; and &lt;a href="https://vercel.com/" rel="noopener noreferrer"&gt;Vercel&lt;/a&gt; take care of all of the infrastructure required to run your application.&lt;/p&gt;

&lt;p&gt;Here are some of the top reasons why developers are going serverless.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reduced infrastructure requirements
&lt;/h3&gt;

&lt;p&gt;The hosting provider administers all of the infrastructure required to host your app—maintaining it, keeping it up-to-date, and most importantly, keeping it secure. The provider deals with the day-to-day burdens of hosting.&lt;/p&gt;

&lt;h3&gt;
  
  
  Streamlined deployment
&lt;/h3&gt;

&lt;p&gt;With serverless hosting you are provided tools to readily upload, run, and monitor your code. When a request comes in, your code is launched in a container and executed. The serverless environment discards the container when it is no longer needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reduced costs
&lt;/h3&gt;

&lt;p&gt;In addition to the time and resources saved by not having to maintain your own server infrastructure, you also save on the cost of running a full server 24/7. Many serverless hosting providers like AWS and Azure will only bill you for the resources you use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scalability
&lt;/h3&gt;

&lt;p&gt;Serverless hosting offerings like &lt;a href="https://aws.amazon.com/lambda/" rel="noopener noreferrer"&gt;AWS Lambda&lt;/a&gt; and &lt;a href="https://azure.microsoft.com/en-us/services/functions/?cdn=disable" rel="noopener noreferrer"&gt;Azure Functions&lt;/a&gt; can support sustained high load thanks to the cloud providers’ globally distributed infrastructure. In addition, such systems are well-suited for handling bursts of high load amid periods of lower use due to their automatic scaling functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  The issues with “connection pooling” in serverless environments
&lt;/h2&gt;

&lt;p&gt;Any database, serverless or not, has a limit on the number of open connections that it can handle at one time. For each open connection, the database system like &lt;a href="https://www.postgresql.org/" rel="noopener noreferrer"&gt;PostgreSQL&lt;/a&gt; or &lt;a href="http://www.mysql.org/" rel="noopener noreferrer"&gt;MySQL&lt;/a&gt; reserves network, memory, and compute resources so that it’s able to handle any incoming requests through each connection.&lt;/p&gt;

&lt;p&gt;In a serverless environment your code does not run constantly. A user makes a request (whether it’s viewing a webpage or requesting an API resource), and a tiny containerized executing environment (called an instance) running only the code required to serve the request is launched.  The request is served, and the instance is destroyed.&lt;/p&gt;

&lt;p&gt;This is efficient, but there is one problem. In traditional hosting environments with a single server that runs your application,  a single instance of the application can service multiple user requests.&lt;/p&gt;

&lt;p&gt;In a traditional hosting environment a single instance of the application will be managing a &lt;strong&gt;single&lt;/strong&gt; &lt;a href="https://en.wikipedia.org/wiki/Connection_pool" rel="noopener noreferrer"&gt;connection pool&lt;/a&gt; to your database. The connection pool ensures that any database queries are within the connection and timeout limits that you have configured. It coordinates the re-use of database connections and prevents the database server from being overloaded.&lt;/p&gt;

&lt;p&gt;In a serverless environment, a new instance of your code is launched for every user request. Each instance will be unaware of the others, resulting in &lt;strong&gt;multiple&lt;/strong&gt; connection pools. As these separate connection pools are not aware of one another, they cannot coordinate to ensure the database server is not overloaded.&lt;/p&gt;

&lt;p&gt;In a serverless environment, there is no shared connection pool. This creates the risk that a small number of user requests exhaust the database connection limit causing the application to stop working.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcu90f410g7ydf51tym17.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcu90f410g7ydf51tym17.png" alt="Each Serverless function handles one user request which can quickly exceed the database connection limit"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Methods of dealing with with connection pooling issues
&lt;/h2&gt;

&lt;p&gt;There are three key ways in which the modern developer can deal with connection pooling issues in a serverless environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  External connection poolers
&lt;/h3&gt;

&lt;p&gt;One method is to use an &lt;a href="https://www.prisma.io/docs/guides/performance-and-optimization/connection-management#external-connection-poolers" rel="noopener noreferrer"&gt;external connection pooler&lt;/a&gt;. By moving the task of coordinating database connections outside of your serverless app, an external connection pooler solves the issue of the separate execution instances not being able to coordinate with each other.&lt;br&gt;
Not all database servers have an external connection pooler. If this path you choose to mitigate the issue, you should carefully select which tools and platforms will be used in your project early in the development process.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://cloud.prisma.io/" rel="noopener noreferrer"&gt;Prisma Data Proxy&lt;/a&gt; is an example of an external connection pooler approach.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frt38aa2ho502ld80uvu0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frt38aa2ho502ld80uvu0.png" alt="Serverless with the Prisma Data Proxy"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Adjusting your code
&lt;/h3&gt;

&lt;p&gt;Another method to overcome connection pooling issues is to ensure that your code is &lt;a href="https://www.prisma.io/docs/guides/performance-and-optimization/connection-management#prismaclient-in-serverless-environments" rel="noopener noreferrer"&gt;written in a way&lt;/a&gt; that accounts for the serverless environment in which it will run. Some cloud providers &lt;em&gt;do&lt;/em&gt; reuse resources between requests, so structuring your code so that connections can be maintained between requests is a way to reduce database load. &lt;/p&gt;

&lt;p&gt;However, this approach is limited and will only help alleviate the problem to some degree. &lt;/p&gt;
&lt;h3&gt;
  
  
  Fine tuning your database server
&lt;/h3&gt;

&lt;p&gt;An important step in deploying to a production environment is to monitor and profile your application. Knowing how your application will be used and how busy it will be allows you to determine what resources to allocate. With this information, you can optimize your database connection to reduce the chance of overloading it. For example, you can reduce the connection timeout for inactive connections so that your database is less likely to be spending resources on a connection that’s not going to be used. Another option is to increase the connection limit on the database, but handling more connections will likely require more compute resources.&lt;/p&gt;

&lt;p&gt;In addition to supporting these traditional methods of dealing with connection pooling issues, the Prisma Data Platform introduces a reliable and readily configurable database proxy that solves connection pooling issues—regardless of the database server or database host in use.&lt;/p&gt;
&lt;h2&gt;
  
  
  Solving connection pooling issues with Prisma Data Proxy
&lt;/h2&gt;

&lt;p&gt;Prisma has introduced a new service that effectively solves the connection pooling issue when deploying to serverless environments like AWS Lambda and Vercel.&lt;/p&gt;

&lt;p&gt;The Prisma Data Proxy acts as an intermediary between your application code and the database, effectively acting as an external connection pooler for any &lt;a href="https://www.prisma.io/docs/reference/database-reference/supported-databases" rel="noopener noreferrer"&gt;database supported by Prisma&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Flexible ORM and proxy with external connection pooling
&lt;/h3&gt;

&lt;p&gt;Using the Prisma Data Proxy solves the connection pooling issue without you having to drastically alter your code or spend extra time tweaking your database connection configuration to account for the serverless environment.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/GWbzyyziH9A"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The Prisma Data Proxy also reduces the size of the data bundle that you must upload to your serverless hosting environment. Many of the database functions are outsourced to the highly performant Prisma Engine binary hosted by the Prisma Data Proxy, so they do not need to be included with your deployment bundle.&lt;/p&gt;

&lt;h3&gt;
  
  
  Part of the Prisma Data Platform
&lt;/h3&gt;

&lt;p&gt;The Prisma Data Proxy is part of the &lt;a href="https://www.prisma.io/dataplatform" rel="noopener noreferrer"&gt;Prisma Data Platform&lt;/a&gt;, a cloud based platform that helps manage and collaborate on application data.&lt;/p&gt;

&lt;p&gt;The Prisma Data Platform includes a powerful databas tools to assist you and your team during the development process. This includes the Prisma Query Console, which enables you to view and manage your live data through a simple interface. It allows you to gain insight into how your application is used and to quickly fix any arising data issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  The benefits of serverless hosting and Prisma
&lt;/h2&gt;

&lt;p&gt;Serverless hosting in combination with the &lt;a href="https://cloud.prisma.io/" rel="noopener noreferrer"&gt;Prisma Data Platform&lt;/a&gt; provides a set of collaborative development and deployment tools for developers who simply want to get on with building their product.&lt;br&gt;
Building and running serverless applications has become a very attractive option for developers due to the reduced resources and costs, and an easier scaling approach.&lt;/p&gt;

&lt;p&gt;However, when developing your application you will have to ensure its suitability to be run in a serverless environment. There is nothing inherently wrong with the serverless concept, but there are differences to traditional hosting configurations that must be considered when coding to deploy in such an environment.&lt;/p&gt;

&lt;p&gt;The Prisma Data Platform is being actively developed to address the challenges of deploying data-intensive apps to modern serverless platforms while allowing you to use the database and database host you are already comfortable with.&lt;/p&gt;

&lt;p&gt;To learn more about databases in the Serverless era and a sneak peak into Prisma's vision for the Prisma Data Platform, check out the talks from the &lt;a href="https://www.prisma.io/serverless" rel="noopener noreferrer"&gt;Prisma Serverless Conference&lt;/a&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Get started with the Prisma Data Platform
&lt;/h3&gt;

&lt;p&gt;The Prisma Data Platform, including the Prisma Data Proxy, is currently available in Early Access.&lt;/p&gt;

&lt;p&gt;Online documentation is available to get you started, so that you are ready to implement the Prisma Data Platform in your project when it is released.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=iyGZ8JFPgoY" rel="noopener noreferrer"&gt;See the Prisma Data Proxy in action&lt;/a&gt; or &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-data-platform#prisma-data-proxy" rel="noopener noreferrer"&gt;learn more about how to easily integrate the Prisma Data Platform into your project&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And if you are ready to try Prisma Data Platform out, &lt;a href="https://www.prisma.io/dataplatform" rel="noopener noreferrer"&gt;get started today&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>serverless</category>
      <category>database</category>
    </item>
    <item>
      <title>Database Migrations for .NET and Entity Framework with Prisma</title>
      <dc:creator>Ruheni Alex</dc:creator>
      <pubDate>Thu, 04 Nov 2021 12:51:58 +0000</pubDate>
      <link>https://dev.to/prisma/database-migrations-for-net-and-entity-framework-with-prisma-49e0</link>
      <guid>https://dev.to/prisma/database-migrations-for-net-and-entity-framework-with-prisma-49e0</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article, you will learn how to model your data using &lt;a href="https://www.prisma.io/" rel="noopener noreferrer"&gt;Prisma&lt;/a&gt;, run a migration with Prisma Migrate and then introspect your database with &lt;a href="https://dotnet.microsoft.com/" rel="noopener noreferrer"&gt;.NET&lt;/a&gt; to generate your &lt;a href="https://docs.microsoft.com/en-us/aspnet/entity-framework" rel="noopener noreferrer"&gt;Entity Framework&lt;/a&gt; models. This article will use Azure SQL/ SQL Server. &lt;/p&gt;

&lt;p&gt;This article only offers an alternative data modelling workflow that is succinct and offer a better development experience. Besides .NET and the Entity framework, you can also use Prisma with your tool/ frameworks of choice such as &lt;a href="https://www.djangoproject.com/" rel="noopener noreferrer"&gt;Django&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Prisma auto-generates migration files in SQL that you can modify before applying them on your database. The workflow following this approach would be:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Model your data&lt;/li&gt;
&lt;li&gt;Run a migration using &lt;a href="https://www.prisma.io/migrate" rel="noopener noreferrer"&gt;Prisma Migrate&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Use the Entity Framework to &lt;em&gt;&lt;a href="https://docs.microsoft.com/en-us/ef/core/managing-schemas/scaffolding?tabs=dotnet-core-cli" rel="noopener noreferrer"&gt;reverse engineer&lt;/a&gt;/ scaffold/&lt;/em&gt; introspect your database and generate models and your database context.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What is Prisma?
&lt;/h2&gt;

&lt;p&gt;Prisma is an object-relational mapper (ORM) that provides a declarative way to define your database models that are easy to read and comprehend. Prisma also provides &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-client" rel="noopener noreferrer"&gt;Prisma Client&lt;/a&gt; – an intuitive and  type-safe query builder based off your schema.&lt;/p&gt;

&lt;p&gt;At the core of Prisma is the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-schema" rel="noopener noreferrer"&gt;Prisma Schema&lt;/a&gt; – aka &lt;em&gt;schema file&lt;/em&gt;. The schema contains 3 parts: the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-schema/data-sources/" rel="noopener noreferrer"&gt;data sources&lt;/a&gt;, &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-schema/generators" rel="noopener noreferrer"&gt;generators&lt;/a&gt; and your &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-schema/data-model" rel="noopener noreferrer"&gt;data model definition&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;To follow along, ensure you have the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Familiarity with .NET and the &lt;a href="https://docs.microsoft.com/en-us/aspnet/entity-framework" rel="noopener noreferrer"&gt;Entity framework&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Installed Node.js v 12.6.x or higher&lt;/li&gt;
&lt;li&gt;Installed .NET locally&lt;/li&gt;
&lt;li&gt;A SQL Server or Azure SQL database&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://marketplace.visualstudio.com/items?itemName=Prisma.prisma" rel="noopener noreferrer"&gt;Prisma Extension&lt;/a&gt; for VS Code users&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you're not familiar with setting up SQL Server, check out &lt;a href="https://www.prisma.io/dataguide/mssql/setting-up-a-local-sql-server-database" rel="noopener noreferrer"&gt;Setting up a local SQL Server database&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 1: Create a new console application
&lt;/h2&gt;

&lt;p&gt;The first step is to create a new .NET console app in your working directory. This guide uses a console application to keep it simple. However, feel free to create any other type of application.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

dotnet new console &lt;span class="nt"&gt;--output&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;__app-name__&lt;span class="k"&gt;*&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once the command is done, navigate to the project and confirm that your project is running.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;__app-name__&lt;span class="k"&gt;*&lt;/span&gt;
dotnet run


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If the output on the terminal is &lt;code&gt;Hello World!&lt;/code&gt;, everything seems to be running just fine!  🎉&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Add Prisma to your project
&lt;/h2&gt;

&lt;p&gt;On a separate terminal window but still within the same project run the following commands.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;mkdir &lt;/span&gt;prisma-migrate
&lt;span class="nb"&gt;cd &lt;/span&gt;prisma-migrate
npm init &lt;span class="nt"&gt;-y&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The commands create a new directory, navigate to the folder and initializes it as a Node.js project. The &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-cli" rel="noopener noreferrer"&gt;Prisma CLI&lt;/a&gt; is the only dependency for this project. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; prisma


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Initialize Prisma
&lt;/h3&gt;

&lt;p&gt;Once the install is done, run the following command.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npx prisma init &lt;span class="nt"&gt;--database-provider&lt;/span&gt; sqlserver


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The command:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creates a &lt;code&gt;.env&lt;/code&gt; file at the root of the project for storing environment variables&lt;/li&gt;
&lt;li&gt;Creates &lt;code&gt;prisma&lt;/code&gt; folder containing a &lt;code&gt;schema.prisma&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Specifies the database provider as &lt;code&gt;sqlserver&lt;/code&gt; in &lt;code&gt;schema.prisma&lt;/code&gt; file and provides a dummy &lt;code&gt;sqlserver&lt;/code&gt; connection string in &lt;code&gt;.env&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;schema.prisma&lt;/code&gt; file defines the database connection and the Prisma Client generator. This is where you will also define your database models.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;

&lt;span class="c1"&gt;/// prisma/schema.prisma&lt;/span&gt;
&lt;span class="n"&gt;datasource&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"sqlserver"&lt;/span&gt;
  &lt;span class="n"&gt;url&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"DATABASE_URL"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;generator&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"prisma-client-js"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For this guide, you can delete &lt;code&gt;generator client&lt;/code&gt; as it is relevant in a TypeScript/ JavaScript project. The &lt;code&gt;generator client&lt;/code&gt; is used to generate the TypeScript definitions for the Prisma Client.&lt;/p&gt;

&lt;p&gt;Update &lt;code&gt;.env&lt;/code&gt; with your database connection string. Refer to &lt;a href="https://www.prisma.io/docs/concepts/database-connectors/sql-server#connection-details" rel="noopener noreferrer"&gt;connection details&lt;/a&gt; for information on how to configure the connection string.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="c"&gt;# .env&lt;/span&gt;
&lt;span class="nv"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"sqlserver://HOST:PORT;database=DATABASE;user=USER;password=PASSWORD;"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;
  If you're using a remote database during development like Azure SQL, open this to learn more about how to configure the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-migrate/shadow-database" rel="noopener noreferrer"&gt;shadow database&lt;/a&gt;.
  &lt;br&gt;
Create a new database which will be the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-migrate/shadow-database" rel="noopener noreferrer"&gt;shadow database&lt;/a&gt; on Azure. Update your &lt;code&gt;.env&lt;/code&gt; file to include the &lt;code&gt;SHADOW_DATABASE_URL&lt;/code&gt; as well. The shadow database will be used in the data modelling step 
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="c"&gt;# .env&lt;/span&gt;
&lt;span class="nv"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"sqlserver://HOST:PORT;database=DATABASE;user=USER;password=PASSWORD;"&lt;/span&gt;
&lt;span class="nv"&gt;SHADOW_DATABASE_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"sqlserver://HOST:PORT;database=DATABASE;user=USER;password=PASSWORD;"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Update your &lt;code&gt;schema.prisma&lt;/code&gt; file with the connection string for the shadow database. &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;

&lt;span class="c1"&gt;/// prisma/schema.prisma&lt;/span&gt;
&lt;span class="n"&gt;datasource&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;provider&lt;/span&gt;          &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"sqlserver"&lt;/span&gt;
  &lt;span class="n"&gt;url&lt;/span&gt;               &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"DATABASE_URL"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;shadowDatabaseUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SHADOW_DATABASE_URL"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;A shadow database is a second, &lt;em&gt;temporary&lt;/em&gt; database for use in the local development workflow. When running a migration, the secondary database is created and deleted automatically unless you're using a cloud-hosted database.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's an illustration of how the shadow database works:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.prisma.io%2Fdocs%2Fstatic%2Ffa8149049da32d83d8014e3d14d644c0%2Fa6d36%2Fshadow-database.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.prisma.io%2Fdocs%2Fstatic%2Ffa8149049da32d83d8014e3d14d644c0%2Fa6d36%2Fshadow-database.png" alt="https://www.prisma.io/docs/static/fa8149049da32d83d8014e3d14d644c0/a6d36/shadow-database.png"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Data modelling
&lt;/h3&gt;

&lt;p&gt;This example will define 2 models: &lt;code&gt;Post&lt;/code&gt; and &lt;code&gt;User&lt;/code&gt;.The relationship between &lt;code&gt;User&lt;/code&gt; and &lt;code&gt;Post&lt;/code&gt; is a one-to-many relationship.&lt;/p&gt;

&lt;p&gt;The models defined in &lt;code&gt;schema.prisma&lt;/code&gt; will resemble a GraphQL syntax . The models also map to a table in your database.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;

&lt;span class="c1"&gt;/// prisma/schema.prisma&lt;/span&gt;
&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;        &lt;span class="n"&gt;Int&lt;/span&gt;      &lt;span class="nd"&gt;@id&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autoincrement&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt;   &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;title&lt;/span&gt;     &lt;span class="n"&gt;String&lt;/span&gt;
  &lt;span class="n"&gt;createdAt&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;author&lt;/span&gt;    &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;    &lt;span class="nd"&gt;@relation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;fields:&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;authorId&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="nl"&gt;references:&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
  &lt;span class="n"&gt;authorId&lt;/span&gt;  &lt;span class="n"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;    &lt;span class="n"&gt;Int&lt;/span&gt;     &lt;span class="nd"&gt;@id&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autoincrement&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;  &lt;span class="nd"&gt;@unique&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;  &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;posts&lt;/span&gt; &lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Refer to the &lt;a href="https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference" rel="noopener noreferrer"&gt;Prisma Schema Reference&lt;/a&gt; for further information on database field types, attributes, conventions and functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a database migration
&lt;/h3&gt;

&lt;p&gt;After modelling your data, it's time to apply the changes to your database. On your terminal window, run &lt;code&gt;npx prisma migrate dev&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npx prisma migrate dev &lt;span class="nt"&gt;--name&lt;/span&gt; init


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If no database exists, a new database with the database name specified in your &lt;code&gt;.env&lt;/code&gt; file will be created. The command also creates a new database migration that is located in &lt;code&gt;./prisma/migrations&lt;/code&gt; directory. The migration is in SQL and is applied against your database. &lt;code&gt;init&lt;/code&gt; refers to the migration name which you can change to something more semantic.&lt;/p&gt;

&lt;p&gt;If you wish to only create a migration but not apply it to your database, pass the &lt;code&gt;--create-only&lt;/code&gt; option to Prisma Migrate. This will generate the SQL and allow you to customize it before applying it against your database.&lt;/p&gt;

&lt;p&gt;You can confirm that the changes have been applied, run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npx prisma studio


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fde9p51dk06cbmaviv5lt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fde9p51dk06cbmaviv5lt.png" alt="Screenshot 2021-10-17 at 19.49.28.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select the Model you would like to view/ edit data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftm5gi64m3xeb68utpt5l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftm5gi64m3xeb68utpt5l.png" alt="Screenshot 2021-10-17 at 19.51.25.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click &lt;strong&gt;Save 1 change&lt;/strong&gt; to apply the changes.&lt;/p&gt;

&lt;p&gt;Navigate to the User model, create a new user, link the post created in the previous step and save the change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fph7m5cz8jho4m5p921zw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fph7m5cz8jho4m5p921zw.png" alt="Screenshot 2021-10-17 at 19.53.24.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Introspect your database aka Reverse Engineering
&lt;/h2&gt;

&lt;p&gt;Entity framework allows you to introspect your database schema and scaffold entity type classes and the database context based off an existing database.&lt;/p&gt;

&lt;p&gt;Navigate out of &lt;code&gt;prisma-migrate&lt;/code&gt; directory to the root of your project.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;cd&lt;/span&gt; ../


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Install Entity Framework tools you will require to work with SQL Server in your project.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

dotnet add package Microsoft.EntityFrameworkCore.SqlServer 
dotnet add package Microsoft.EntityFrameworkCore.Tools 
dotnet add package Microsoft.EntityFrameworkCore.Design


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Scaffold entity type classes based on your database schema to your project.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

dotnet ef dbcontext scaffold &lt;span class="s2"&gt;"Server=**HOST**;Database=**DATABASE_NAME**;User Id=**USER**;Password=**PASSWORD**"&lt;/span&gt; Microsoft.EntityFrameworkCore.SqlServer &lt;span class="nt"&gt;--context-dir&lt;/span&gt; DbContext &lt;span class="nt"&gt;--context&lt;/span&gt; BlogExampleContext &lt;span class="nt"&gt;--output-dir&lt;/span&gt; Models


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Microsoft.EntityFrameworkCore.SqlServer&lt;/code&gt; refers to the package to be used to introspect the database.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--context-dir&lt;/code&gt; and &lt;code&gt;--output-dir&lt;/code&gt; refer to the location of the context and the entity type classes after introspection&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--context&lt;/code&gt; refers to the name of your database context&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On a successful run 2 folders with 4 files will be created in your project.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="k"&gt;*&lt;/span&gt;app-name&lt;span class="k"&gt;*&lt;/span&gt;/
┣ DbContext/
┃ ┗ BlogExampleContext.cs
┣ Models/
┃ ┣ Post.cs
┃ ┗ PrismaMigration.cs
┃ ┗ User.cs
┣ ...


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you will notice, &lt;code&gt;PrismaMigration.cs&lt;/code&gt; was not defined in &lt;code&gt;schema.prisma&lt;/code&gt;. This is a table created by Prisma to track the migrations that have been created and applied against your database.&lt;/p&gt;

&lt;p&gt;To exclude the &lt;code&gt;PrismaMigrations&lt;/code&gt; table, and re-run the previous command but specify the tables you would like using the &lt;code&gt;--table&lt;/code&gt; and &lt;code&gt;—force&lt;/code&gt; options to overwrite the existing models.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

dotnet ef dbcontext scaffold &lt;span class="s2"&gt;"Server=**HOST**;Database=**DATABASE_NAME**;User Id=**USER**;Password=**PASSWORD**"&lt;/span&gt; Microsoft.EntityFrameworkCore.SqlServer &lt;span class="nt"&gt;--context-dir&lt;/span&gt; DbContext &lt;span class="nt"&gt;--context&lt;/span&gt; BlogExampleContext &lt;span class="nt"&gt;--output-dir&lt;/span&gt; Models &lt;span class="nt"&gt;--force&lt;/span&gt; &lt;span class="nt"&gt;--table&lt;/span&gt; User &lt;span class="nt"&gt;--table&lt;/span&gt; Post


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This time, 3 files are generated and the &lt;code&gt;PrismaMigrations.cs&lt;/code&gt; file is excluded when scaffolding.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you're interested in migrating to Prisma with an existing database, the Prisma CLI provides &lt;code&gt;db pull&lt;/code&gt; for introspecting the database and populating &lt;code&gt;schema.prisma&lt;/code&gt; with your database models.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations! You have successfully modelled your entities, run a migration with Prisma and scaffolded your models and context with Entity Framework. &lt;/p&gt;

&lt;p&gt;Learn how you can set up and deploy a Prisma based Node.js REST API to Azure Functions together with Azure SQL in &lt;a href="https://www.prisma.io/docs/guides/deployment/deployment-guides/deploying-to-azure-functions" rel="noopener noreferrer"&gt;Deploying to Azure Functions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you would like to learn more about Prisma, check out the &lt;a href="https://www.prisma.io/docs" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; and feel free to create an &lt;a href="https://github.com/prisma/prisma/issues" rel="noopener noreferrer"&gt;issue&lt;/a&gt; or start a &lt;a href="https://github.com/prisma/prisma/discussions" rel="noopener noreferrer"&gt;discussion&lt;/a&gt; on GitHub.&lt;/p&gt;

</description>
      <category>prisma</category>
      <category>database</category>
      <category>backend</category>
      <category>entityframework</category>
    </item>
    <item>
      <title>Upcoming Serverless Events in 2021 and 2022</title>
      <dc:creator>Etel Sverdlov</dc:creator>
      <pubDate>Wed, 03 Nov 2021 16:00:12 +0000</pubDate>
      <link>https://dev.to/prisma/upcoming-serverless-events-in-2021-and-2022-117</link>
      <guid>https://dev.to/prisma/upcoming-serverless-events-in-2021-and-2022-117</guid>
      <description>&lt;p&gt;Between the online, hybrid, and in person conferences happening around the globe, the world's conference schedule is in full swing. &lt;/p&gt;

&lt;p&gt;Since 2014, there has been a huge increase in interest about Serverless, at least according to &lt;a href="https://trends.google.com/trends/explore?date=all&amp;amp;q=%2Fg%2F11c0q_754d"&gt;Google Trends&lt;/a&gt;, and lots of events to match. &lt;/p&gt;

&lt;p&gt;To complement the rise of interest in Serverless, we've quickly compiled some upcoming relevant conferences on the subject. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;November 18th - &lt;a href="https://www.prisma.io/serverless"&gt;Prisma's Serverless Conference&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;This is our own, first Serverless event. A short mini-conference chock full with top speakers from Netlify, Cloudflare, MongoDB, Vercel, CockroachDB, Planetscale, and of course Prisma. Prisma's Serverless Conference focuses on the challenges developers face when trying to adopt serverless databases. &lt;/p&gt;

&lt;p&gt;PS. Keep a look out for the new Prisma tooling that will also be highlighted in the keynote! 🤩&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;February 24th - &lt;a href="https://serverlessnashville.io/2022/index.html"&gt;ServerlessDays NASHVILLE&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An in-person event in Tennessee, Serverless Days NASHVILLE is a continuation of a number of Serverless Days events. Sign ups for this event are currently open as are the CFPs! ServerlessDays are a family of events around the world that's focused on fostering a community around serverless technologies. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;March 22, 2022 - &lt;a href="https://serverless-architecture.io/thehague/"&gt;Serverless Architecture Conference&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A packed two day hybrid event online or at The Hague, the event covers Serverless infrastructure, especially as how it relates to Cloud native architecture, and Kubernetes. This is a very large conferences with talks, speakers and workshops. Later in the year, the same production company will reprise a Serverless architecture conference once more in Berlin.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;April 4-6, 2022- &lt;a href="https://qconlondon.com/"&gt;QCon London&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Qcon is a behemoth of a conference and this QCon is no exception. Among its many tracks is one dedicated to &lt;em&gt;Serverless: Orchestration vs Choreography.&lt;/em&gt; With top-of-the-line speakers and an enormous list of talks to choose from, QCOn is an in person option to learn more about Serverless in the Springtime.&lt;/p&gt;




&lt;p&gt;These are some of the Serverless events we've spotted. Are there others that you are especially looking forward to? Share more ideas in the comments! 👇&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Set up a free PostgreSQL database on Supabase to use with Prisma</title>
      <dc:creator>Mahmoud Abdelwahab</dc:creator>
      <pubDate>Fri, 11 Jun 2021 20:13:04 +0000</pubDate>
      <link>https://dev.to/prisma/set-up-a-free-postgresql-database-on-supabase-to-use-with-prisma-3pk6</link>
      <guid>https://dev.to/prisma/set-up-a-free-postgresql-database-on-supabase-to-use-with-prisma-3pk6</guid>
      <description>&lt;p&gt;&lt;a href="https://supabase.com" rel="noopener noreferrer"&gt;Supabase&lt;/a&gt; is a backend-as-a-service built on top of open source technologies. It gives you a database, authentication, a REST API, real-time subscriptions, and storage.&lt;/p&gt;

&lt;p&gt;It offers a free plan which includes a hosted PostgreSQL database. This can be useful if you're getting started with a new project.&lt;/p&gt;

&lt;p&gt;This guide explains how to quickly connect the Postgres database provided by Supabase to a Prisma project. There are other services out there that provide hosted PostgreSQL databases like &lt;a href="https://dev.to/prisma/how-to-setup-a-free-postgresql-database-on-heroku-1dc1"&gt;Heroku&lt;/a&gt; or &lt;a href="https://www.digitalocean.com/products/managed-databases/" rel="noopener noreferrer"&gt;Digital Ocean&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://prisma.io" rel="noopener noreferrer"&gt;Prisma&lt;/a&gt; is an &lt;a href="https://github.com/prisma/prisma" rel="noopener noreferrer"&gt;open source&lt;/a&gt; next-generation ORM. It consists of the following parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prisma Client&lt;/strong&gt;: Auto-generated and type-safe query builder for Node.js &amp;amp; TypeScript.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prisma Migrate&lt;/strong&gt;: Migration system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prisma Studio&lt;/strong&gt;: GUI to view and edit data in your database.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Log into Supabase
&lt;/h2&gt;

&lt;p&gt;Navigate your browser to &lt;a href="https://app.supabase.com/api/login" rel="noopener noreferrer"&gt;https://app.supabase.com&lt;/a&gt; and log in with GitHub.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4esri9nkyvz7odpug78l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4esri9nkyvz7odpug78l.png" alt="Login screen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Create a new project
&lt;/h2&gt;

&lt;p&gt;You'll find that an organization has been created for you under the same GitHub username you used when logging in.&lt;/p&gt;

&lt;p&gt;Go ahead and create a new project by clicking on &lt;strong&gt;"New project"&lt;/strong&gt; and then pick the organization.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdtatpc6j848sjlqva7i8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdtatpc6j848sjlqva7i8.png" alt="Create a new project"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You'll then need to provide a name and set a password for your database (we'll need it later). Finally, click on &lt;strong&gt;"create new project"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F950bp5xw267ncu9oc6jy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F950bp5xw267ncu9oc6jy.png" alt="Set project name and database password"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After creating the project, you'll need to wait for ~2 minutes for Supabase to finish creating the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Get the connection string from the project settings
&lt;/h2&gt;

&lt;p&gt;Go to the settings page from the sidebar and navigate to the &lt;strong&gt;Database&lt;/strong&gt; tab. You'll find the database's connection string with a placeholder for the password you provided when creating the project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzntcsh3ic91gf1gy8j73.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzntcsh3ic91gf1gy8j73.png" alt="Getting the connection string"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Testing the connection
&lt;/h2&gt;

&lt;p&gt;To make sure that everything works correctly, let's try the connection string in a Prisma project.&lt;/p&gt;

&lt;p&gt;If you already have one, all you need to do is set the &lt;code&gt;DATABASE_URL&lt;/code&gt; to the connection string (including the password) in your &lt;code&gt;.env&lt;/code&gt; file, and you're good to go. &lt;/p&gt;

&lt;p&gt;In case you don't have a Prisma project or this is your first time working with Prisma, you're going to use with the repo from the &lt;a href="https://www.prisma.io/docs/getting-started/quickstart" rel="noopener noreferrer"&gt;quickstart&lt;/a&gt; guide.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cloning the starter project
&lt;/h3&gt;

&lt;p&gt;Navigate into a directory of your choice and run the following command in your terminal if you're on a Windows machine:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

curl https://pris.ly/quickstart &lt;span class="nt"&gt;-L&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; quickstart-master.tar.gz &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-zxvf&lt;/span&gt; quickstart-master.tar.gz quickstart-master/typescript/starter &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; move quickstart-master&lt;span class="se"&gt;\t&lt;/span&gt;ypescript&lt;span class="se"&gt;\s&lt;/span&gt;tarter starter &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;rmdir&lt;/span&gt; /S /Q quickstart-master &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; del /Q quickstart-master.tar.gz


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And if you're using Mac OS or Linux, run the following command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

curl &lt;span class="nt"&gt;-L&lt;/span&gt; https://pris.ly/quickstart | &lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-xz&lt;/span&gt; &lt;span class="nt"&gt;--strip&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2 quickstart-master/typescript/starter


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can now navigate into the directory and install the project's dependencies:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;cd &lt;/span&gt;starter &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; npm &lt;span class="nb"&gt;install&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  A look at the project's structure
&lt;/h3&gt;

&lt;p&gt;This project comes with TypeScript configured and has the following structure.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;prisma&lt;/code&gt; directory which contains:

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;dev.db&lt;/code&gt; file: This is a SQLite database.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;schema.prisma&lt;/code&gt; file: Where we define the different database models and relations between them.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;.env&lt;/code&gt; file: Contains the &lt;code&gt;DATABASE_URL&lt;/code&gt; variable, which Prisma will use.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;script.ts&lt;/code&gt; file: where we will run some queries using Prisma Client.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This starter also comes with the following packages installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/prisma" rel="noopener noreferrer"&gt;&lt;code&gt;@prisma/client&lt;/code&gt;&lt;/a&gt;: An auto-generated and type-safe query builder that's &lt;em&gt;tailored&lt;/em&gt; to your data. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/@prisma/client" rel="noopener noreferrer"&gt;&lt;code&gt;prisma&lt;/code&gt;&lt;/a&gt;: Prisma's command-line interface (CLI). It allows you to initialize new project assets, generate Prisma Client, and analyze existing database structures through introspection to automatically create your application models.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Prisma works with both JavaScript and TypeScript. However, to get the best possible development experience, using TypeScript is highly recommended.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Configuring the project to use PostgreSQL
&lt;/h3&gt;

&lt;p&gt;Go ahead and delete the &lt;code&gt;prisma/dev.db&lt;/code&gt; file because we will be switching to PostgreSQL.&lt;/p&gt;

&lt;p&gt;Next, inside the &lt;code&gt;prisma/.env&lt;/code&gt; file, update the value of the &lt;code&gt;DATABASE_URL&lt;/code&gt; variable to the connection string you got in &lt;strong&gt;step 3&lt;/strong&gt;. The URL might look as follows:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# prisma/.env

postgres://postgres:[YOUR-PASSWORD]@db.vdbnhqozmlzdsaejdxwr.supabase.co:5432/postgres



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Finally, inside your &lt;code&gt;schema.prisma&lt;/code&gt; file, change the &lt;code&gt;provider&lt;/code&gt; from "sqlite" to &lt;code&gt;"postgresql"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is what your &lt;code&gt;schema.prisma&lt;/code&gt; file should look like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;

&lt;span class="n"&gt;datasource&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"postgresql"&lt;/span&gt;
  &lt;span class="n"&gt;url&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DATABASE_URL"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;generator&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"prisma-client-js"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;        &lt;span class="n"&gt;Int&lt;/span&gt;     &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autoincrement&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;title&lt;/span&gt;     &lt;span class="n"&gt;String&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt;   &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="err"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;published&lt;/span&gt; &lt;span class="n"&gt;Boolean&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;author&lt;/span&gt;    &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="err"&gt;?&lt;/span&gt;   &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;relation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;authorId&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;references&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="n"&gt;authorId&lt;/span&gt;  &lt;span class="n"&gt;Int&lt;/span&gt;&lt;span class="err"&gt;?&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;    &lt;span class="n"&gt;Int&lt;/span&gt;     &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autoincrement&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;  &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;unique&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;  &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="err"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;posts&lt;/span&gt; &lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To test that everything works correctly, run the following command to create a migration:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

prisma migrate dev &lt;span class="nt"&gt;--name&lt;/span&gt; init


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can optionally give your migration a name, depending on the changes you made. Since this is the project's first migration, you're setting the &lt;code&gt;--name&lt;/code&gt; flag to "init".&lt;/p&gt;

&lt;p&gt;If everything works correctly, you should get the following message in your terminal:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Your database is now in sync with your schema.

✔ Generated Prisma Client (2.x.x) to ./node_modules/@prisma/client in 111ms


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will create a &lt;code&gt;prisma/migrations&lt;/code&gt; folder inside your &lt;code&gt;prisma&lt;/code&gt; directory and synchronize your Prisma schema with your database schema.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: if you want to skip the process of creating a migration history, you can use the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-migrate/db-push" rel="noopener noreferrer"&gt;&lt;code&gt;db push&lt;/code&gt;&lt;/a&gt; command instead of &lt;code&gt;migrate dev&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you go to your Supabase project, in the table editor, you should see that two tables have been created, a &lt;code&gt;Post&lt;/code&gt; and a &lt;code&gt;User&lt;/code&gt; table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7y4qq4wwvfrheti6r09u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7y4qq4wwvfrheti6r09u.png" alt="tables created in the UI"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it! You have now successfully connected a Prisma project to a PostgreSQL database hosted on Supabase and ran your first migration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connection pooling with Supabase
&lt;/h2&gt;

&lt;p&gt;If you're working in a serverless environment (for example Node.js functions hosted on AWS Lambda, Vercel or Netlify Functions), you need to set up &lt;a href="https://www.prisma.io/docs/guides/performance-and-optimization/connection-management#serverless-environments-faas" rel="noopener noreferrer"&gt;connection pooling&lt;/a&gt; using a tool like &lt;a href="https://www.pgbouncer.org/" rel="noopener noreferrer"&gt;PgBouncer&lt;/a&gt;. That's because every function invocation may result in a &lt;a href="https://www.prisma.io/docs/guides/performance-and-optimization/connection-management#the-serverless-challenge" rel="noopener noreferrer"&gt;new connection to the database&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Fortunately, Supabase projects &lt;a href="https://supabase.com/blog/supabase-pgbouncer#what-is-connection-pooling" rel="noopener noreferrer"&gt;support connection management using PgBouncer&lt;/a&gt; and are enabled by default.&lt;/p&gt;

&lt;p&gt;Go to the &lt;strong&gt;Database&lt;/strong&gt; page from the sidebar and navigate to &lt;strong&gt;connection pool&lt;/strong&gt; settings&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0oowg8vq435ob5c3gf0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0oowg8vq435ob5c3gf0.png" alt="Connection pool settings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When running migrations you need to use the non pooled connection URL (like the one we used in &lt;strong&gt;step 4&lt;/strong&gt;). However,  when deploying your app, you'll use the pooled connection URL.  and add the &lt;code&gt;?pgbouncer=true&lt;/code&gt; flag to the PostgreSQL connection URL. The URL might look as follows:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# prisma/.env

postgres://postgres:[YOUR-PASSWORD]@db.vdbnhqozmlzdsaejdxwr.supabase.co:6543/postgres?pgbouncer=true



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Prisma Migrate uses database transactions to check out the current state of the database and the migrations table. However, the Migration Engine is designed to use a single connection to the database, and does not support connection pooling with PgBouncer. If you attempt to run Prisma Migrate commands in any environment that uses PgBouncer for connection pooling, you might see the following error:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

Error: undefined: Database error
Error querying the database: db error: ERROR: prepared statement &lt;span class="s2"&gt;"s0"&lt;/span&gt; already exists


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This is a known issue and it is being worked on, you can follow the progress on this &lt;a href="https://github.com/prisma/prisma/issues/6485" rel="noopener noreferrer"&gt;GitHub issue&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want to learn more about Supabase, check out the &lt;a href="https://supabase.com/docs" rel="noopener noreferrer"&gt;docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want to learn more about Prisma, check out the &lt;a href="https://www.prisma.io/docs" rel="noopener noreferrer"&gt;docs&lt;/a&gt;. Also in case you have any questions or run into any issue, feel free to start a discussion in the repo's &lt;a href="https://github.com/prisma/prisma/discussions" rel="noopener noreferrer"&gt;discussions section&lt;/a&gt;. &lt;/p&gt;

</description>
      <category>supabase</category>
      <category>prisma</category>
      <category>postgres</category>
      <category>database</category>
    </item>
    <item>
      <title>Deploying Prisma to Azure Functions with Azure SQL</title>
      <dc:creator>Daniel Norman</dc:creator>
      <pubDate>Mon, 22 Mar 2021 12:24:28 +0000</pubDate>
      <link>https://dev.to/prisma/deploying-prisma-to-azure-functions-with-azure-sql-2g2j</link>
      <guid>https://dev.to/prisma/deploying-prisma-to-azure-functions-with-azure-sql-2g2j</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;In this guide, you will set up and deploy a Prisma based Node.js REST API to &lt;a href="https://azure.microsoft.com/en-us/services/functions/"&gt;Azure Functions&lt;/a&gt; together with &lt;a href="https://azure.microsoft.com/en-us/services/sql-database/"&gt;Azure SQL&lt;/a&gt; as the database. The application will expose a REST API and use Prisma Client to handle fetching, creating, and deleting records from a database.&lt;/p&gt;

&lt;p&gt;Azure Functions is a serverless deployment platform that allows you to deploy code without having to maintain infrastructure. Azure SQL Database is a relational database service built for the cloud with automatic scaling.&lt;/p&gt;

&lt;p&gt;In this guide, you will create the necessary resources in Azure, create the database schema using Prisma Migrate and deploy a Node.js REST API with resource endpoints that use Prisma Client to handle database operations against the Azure SQL database.&lt;/p&gt;

&lt;p&gt;This guide's focus is showing how Prisma can be used in the Azure cloud focusing on Azure Functions and Azure SQL. The starting point is the &lt;a href="https://github.com/prisma/prisma-examples/tree/latest/deployment-platforms/azure-functions"&gt;Prisma Azure Functions example&lt;/a&gt; – a REST API for a simple blog with two models: &lt;code&gt;User&lt;/code&gt; and &lt;code&gt;Post&lt;/code&gt; (&lt;em&gt;1:n&lt;/em&gt;). The example contains REST endpoints preconfigured as serverless functions.&lt;/p&gt;

&lt;p&gt;Note that Azure SQL support in Prisma is in &lt;a href="https://www.prisma.io/docs/about/releases#preview"&gt;Preview&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With Azure Functions, the fundamental building block is a &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference#function-app"&gt;&lt;strong&gt;Function App&lt;/strong&gt;&lt;/a&gt;. A function app provides an execution context in Azure in which your functions run. It is comprised of one or more individual functions that are managed, deployed, and scaled together. That way, you can organize and collectively manage multiple functions as a single logical unit.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Throughout the guide, you'll find various &lt;strong&gt;checkpoints&lt;/strong&gt; that enable you to validate whether you performed the steps correctly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Azure account.&lt;/li&gt;
&lt;li&gt;Git installed&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.microsoft.com/en-us/cli/azure/install-azure-cli"&gt;Azure CLI&lt;/a&gt; installed.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://nodejs.org/"&gt;Node.js&lt;/a&gt; installed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prisma workflow
&lt;/h2&gt;

&lt;p&gt;At the core of Prisma is the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-schema"&gt;Prisma schema&lt;/a&gt; – a declarative configuration where you define your data model and other Prisma-related configuration. The Prisma schema is also a single source of truth for both Prisma Client and Prisma Migrate.&lt;/p&gt;

&lt;p&gt;In this guide, you will use &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-migrate"&gt;Prisma Migrate&lt;/a&gt; to create the database schema. Prisma Migrate is based on the Prisma schema and works by generating &lt;code&gt;.sql&lt;/code&gt; migration files that are executed against the database.&lt;/p&gt;

&lt;p&gt;Migrate comes with two primary workflows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating migrations and applying during local development with &lt;code&gt;prisma migrate dev&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Applying generated migration to production with &lt;code&gt;prisma migrate deploy&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For brevity, the guide does not cover how migrations are created with &lt;code&gt;prisma migrate dev&lt;/code&gt;. Rather, it focuses on the production workflow with &lt;code&gt;prisma migrate deploy&lt;/code&gt; and uses the Prisma schema and SQL migration that are included in the example code.&lt;/p&gt;

&lt;p&gt;To learn more about how migrations are created with Prisma Migrate, check out the &lt;a href="https://www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch-typescript-postgres"&gt;start from scratch guide&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Required Azure resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Resource group&lt;/li&gt;
&lt;li&gt;Azure SQL Database server&lt;/li&gt;
&lt;li&gt;Database&lt;/li&gt;
&lt;li&gt;Firewall rule&lt;/li&gt;
&lt;li&gt;Storage account&lt;/li&gt;
&lt;li&gt;Function App&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. Download the example and install dependencies
&lt;/h2&gt;

&lt;p&gt;Open your terminal and navigate to a location of your choice.&lt;/p&gt;

&lt;p&gt;Create the directory for the application code and download the example code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;mkdir prisma-azure
cd prisma-azure
curl https://codeload.github.com/prisma/prisma-examples/tar.gz/latest | tar -xz --strip=3 prisma-examples-latest/deployment-platforms/azure-functions/
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Checkpoint:&lt;/strong&gt; Run the following command to list the contents of the folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;ls -1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see the following files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CreatePost/
CreateUser/
DeletePost/
FilterPosts/
GetFeed/
GetPost/
PublishPost/
host.json
lib/
node_modules/
package.json
prisma/
proxies.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;npm install
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Sign in to Azure using the Azure CLI
&lt;/h2&gt;

&lt;p&gt;Begin by signing in using the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;az login
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Create the Resource Group on Azure
&lt;/h2&gt;

&lt;p&gt;In Azure, a resource group is a way to group together different cloud resources. Whenever you create a resource, e.g., an Azure Function, you need to assign it a resource group.&lt;/p&gt;

&lt;p&gt;Since the REST API will use both Azure Functions and an Azure SQL database, you will first create the resource group with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;az group create --location germanywestcentral --name prisma-azure-example
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The command above creates the resource group in the &lt;code&gt;germanywestcentral&lt;/code&gt; region. You should create the resource group in the region closest to your users. For the full list of Azure regions, run the &lt;code&gt;az account list-locations&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checkpoint:&lt;/strong&gt; The command should output information about the created resource group:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/subscriptions/SUBSCRIPTION_ID/resourceGroups/prisma-azure-example"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"location"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"germanywestcentral"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"managedBy"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"prisma-azure-example"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"properties"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"provisioningState"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Succeeded"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"tags"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Microsoft.Resources/resourceGroups"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Create the Azure SQL database server
&lt;/h2&gt;

&lt;p&gt;To create the Azure SQL database server, copy the command below into your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;az sql server create -l germanywestcentral -g prisma-azure-example --name UNIQUE_DB_SERVER_NAME --admin-user prisma --admin-password CHOOSE_A_PASSWORD --enable-public-network true
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before running the command, replace a unique name for the database in place of &lt;code&gt;UNIQUE_DB_SERVER_NAME&lt;/code&gt;, set a password in place of &lt;code&gt;CHOOSE_A_PASSWORD&lt;/code&gt;, and note it down.&lt;/p&gt;

&lt;p&gt;The command does the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creates the database server in the &lt;code&gt;germanywestcentral&lt;/code&gt; region.&lt;/li&gt;
&lt;li&gt;Associates it with the &lt;code&gt;prisma-azure-example&lt;/code&gt; resource group created in the previous step.&lt;/li&gt;
&lt;li&gt;Sets a unique name for the Azure SQL server with &lt;code&gt;UNIQUE_DB_SERVER_NAME&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Sets the admin user to &lt;code&gt;prisma&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Sets the admin password.&lt;/li&gt;
&lt;li&gt;Enables public network access so you can create the database schema from your machine.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next step, you will create the database that Prisma will use in the REST API.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Create the database
&lt;/h2&gt;

&lt;p&gt;In this step, you will create a database in the server you created in the previous step.&lt;/p&gt;

&lt;p&gt;Run the following command in the terminal, replace &lt;code&gt;UNIQUE_DB_SERVER_NAME&lt;/code&gt; with the database name you chose in the previous step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;az sql db create --resource-group prisma-azure-example --server UNIQUE_DB_SERVER_NAME --name prisma-azure-example --service-objective Basic
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a breakdown of the command's parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--resource-group&lt;/code&gt; adds the database to the resource group created in step 3&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--server&lt;/code&gt; sets the Azure SQL database server to create it in&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--name&lt;/code&gt; sets the name of the database&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--service-objective&lt;/code&gt; sets the database's service tier that &lt;a href="https://azure.microsoft.com/en-us/pricing/details/sql-database/single/"&gt;determines the cost&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Create a firewall rule to allow local access to the database
&lt;/h2&gt;

&lt;p&gt;In this step, you will add two firewall rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allow remote access from your local computer's public IP to the Azure SQL database. This is necessary so you can create the database schema and use the database for testing locally.&lt;/li&gt;
&lt;li&gt;Allow access to the Azure SQL database from Azure Functions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Allow access from your local computer
&lt;/h3&gt;

&lt;p&gt;Begin by determining your public IP with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;curl ifconfig.me
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the IP from the output and run the following command, replacing &lt;code&gt;YOUR_PUBLIC_IP&lt;/code&gt; with the IP address and &lt;code&gt;UNIQUE_DB_SERVER_NAME&lt;/code&gt; with the name from step 4:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;az sql server firewall-rule create --resource-group prisma-azure-example --server UNIQUE_DB_SERVER_NAME --name allow-local-acccess --start-ip-address YOUR_PUBLIC_IP --end-ip-address YOUR_PUBLIC_IP
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Checkpoint:&lt;/strong&gt; After creating the firewall rule, the command should output the following:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"endIpAddress"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"YOUR_PUBLIC_IP"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/prisma-azure-example/providers/Microsoft.Sql/servers/prisma-db/firewallRules/allow-local-acccess"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"kind"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"v12.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"location"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Germany West Central"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"allow-local-acccess"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"resourceGroup"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"prisma-azure-example"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"startIpAddress"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"YOUR_PUBLIC_IP"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Microsoft.Sql/servers/firewallRules"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Allow access from Azure Functions
&lt;/h3&gt;

&lt;p&gt;To allow applications hosted inside Azure to connect to your SQL server, Azure connections must be enabled. To enable Azure connections, there must be a firewall rule with starting and ending IP addresses set to &lt;code&gt;0.0.0.0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Create the rule with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;az sql server firewall-rule create --resource-group prisma-azure-example --server UNIQUE_DB_SERVER_NAME --name allow-function-acccess --start-ip-address 0.0.0.0 --end-ip-address 0.0.0.0
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. Create a storage account
&lt;/h2&gt;

&lt;p&gt;In this step, you will create a storage account used to maintain state and other information about your functions.&lt;/p&gt;

&lt;p&gt;Run the following command to create the storage account, replacing &lt;code&gt;UNIQUE_STORAGE_ACCOUNT_NAME&lt;/code&gt; with a name for the stroage account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;az storage account create --name UNIQUE_STORAGE_ACCOUNT_NAME --location germanywestcentral --resource-group prisma-azure-example --sku Standard_LRS
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Checkpoint:&lt;/strong&gt; If the command succeeds, it will output a large json object. Verify that &lt;code&gt;provisioningState&lt;/code&gt; is &lt;code&gt;Succeeded&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/subscriptions/YOUR_SUBSCRIPTION_ID/resourceGroups/prisma-azure-example/providers/Microsoft.Storage/storageAccounts/UNIQUE_STORAGE_ACCOUNT_NAME"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"provisioningState"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Succeeded"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"resourceGroup"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"prisma-azure-example"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Microsoft.Storage/storageAccounts"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  8. Create the function app
&lt;/h2&gt;

&lt;p&gt;In this step, you will create the function app, which provides the environment for executing your function code. A function app maps to your local function project and lets you group functions as a logical unit for easier management, deployment, and sharing of resources.&lt;/p&gt;

&lt;p&gt;Copy the following command and replace &lt;code&gt;FUNCTION_APP_NAME&lt;/code&gt; with a unique name for your function app, and &lt;code&gt;STORAGE_ACCOUNT_NAME&lt;/code&gt; with the name you chose in the previous step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;az functionapp create --resource-group prisma-azure-example --consumption-plan-location germanywestcentral --runtime node --runtime-version 14 --functions-version 3 --name FUNCTION_APP_NAME --storage-account STORAGE_ACCOUNT_NAME --os-type Linux
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The Function App name must be globally unique as it will determine the subdomain of the deployed function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checkpoint:&lt;/strong&gt; If the command succeeds, you will see a large JSON object in the terminal. Verify that that the &lt;code&gt;state&lt;/code&gt; key in the object is set to &lt;code&gt;Running&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  9. Set the DATABASE_URL environment variable locally
&lt;/h2&gt;

&lt;p&gt;In this step, you will define the &lt;code&gt;DATABASE_URL&lt;/code&gt; environment variable locally to create the database schema and test the functions locally.&lt;/p&gt;

&lt;p&gt;To construct the connection string, copy the following connection string template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sqlserver://DB_SERVER_NAME.database.windows.net:1433;database=DB_NAME;user=DB_ADMIN_USER@DB_SERVER_NAME;password={DB_ADMIN_PASSWORD};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace the following parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;DB_SERVER_NAME&lt;/code&gt; with the database server name defined in step 4&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DB_NAME&lt;/code&gt; with the database name defined in step 5&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DB_ADMIN_USER&lt;/code&gt; with the database admin user set in step 4 to &lt;code&gt;prisma&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DB_ADMIN_PASSWORD&lt;/code&gt; with the database admin password set in step 4&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After setting all the values, set it as a local environment variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;export DATABASE_URL="sqlserver://DB_SERVER_NAME.database.windows.net:1433;&lt;/span&gt;&lt;span class="nv"&gt;database&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;DB_NAME&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nv"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;DB_ADMIN_USER@DB_SERVER_NAME&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nv"&gt;password&lt;/span&gt;&lt;span class="o"&gt;={&lt;/span&gt;DB_ADMIN_PASSWORD&lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nv"&gt;encrypt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nv"&gt;trustServerCertificate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nv"&gt;hostNameInCertificate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt;.database.windows.net&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nv"&gt;loginTimeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;30&lt;span class="s2"&gt;"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  10. Create the Azure Functions local configuration
&lt;/h2&gt;

&lt;p&gt;In this step, you will create the &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local#local-settings-file"&gt;local configuration&lt;/a&gt; file for Azure Functions. The file is used to define local configuration such as environment variables for the functions and the runtime – in this case Node.js.&lt;/p&gt;

&lt;p&gt;Create a file named &lt;code&gt;local.settings.json&lt;/code&gt; in the root of the project with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch local.settings.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And add the following contents to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"IsEncrypted"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"Values"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"AzureWebJobsStorage"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"FUNCTIONS_WORKER_RUNTIME"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  11. Create the database schema
&lt;/h2&gt;

&lt;p&gt;With the &lt;code&gt;DATABASE_URL&lt;/code&gt; environment variable set, you will create the database schema using the &lt;a href="https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-deploy"&gt;&lt;code&gt;prisma migrate deploy&lt;/code&gt;&lt;/a&gt; command.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The &lt;code&gt;prisma migrate deploy&lt;/code&gt; command will run migration files from the &lt;code&gt;prisma/migrations&lt;/code&gt; folder. The initial migration is already included in the example. To learn more about how to create migrations, check out the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-migrate"&gt;Prisma Migrate&lt;/a&gt; docs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Run the following command to create the database schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;npx prisma migrate deploy
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Checkpoint:&lt;/strong&gt; The &lt;code&gt;prisma migrate deploy&lt;/code&gt; should show the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 migration found in prisma/migrations

The following migration have been applied:

migrations/
  └─ 20210322111219_init/
    └─ migration.sql

All migrations have been successfully applied.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  12. Expose the DATABASE_URL environment variable to the functions
&lt;/h2&gt;

&lt;p&gt;In this step, you will expose the &lt;code&gt;DATABASE_URL&lt;/code&gt; environment variable to the functions so that Prisma can connect to the database. In Azure Functions, environment variables are set using &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-app-settings"&gt;app settings&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Run the following command, after replacing &lt;code&gt;FUNCTION_APP_NAME_FROM_STEP_8&lt;/code&gt; with the name of the &lt;strong&gt;Function App&lt;/strong&gt; created in step 8:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;az functionapp config appsettings set --name FUNCTION_APP_NAME_FROM_STEP_8 --resource-group prisma-azure-example --settings DATABASE_URL=$&lt;/span&gt;DATABASE_URL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command will set the &lt;code&gt;DATABASE_URL&lt;/code&gt; app setting using the locally defined &lt;code&gt;DATABASE_URL&lt;/code&gt; environment variable set in step 9.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; By default, app settings are not encrypted. Since the &lt;code&gt;DATABASE_URL&lt;/code&gt; contains sensitive information, it can be stored more securely using &lt;a href="https://docs.microsoft.com/en-us/azure/app-service/app-service-key-vault-references?toc=/azure/azure-functions/toc.json"&gt;Azure's Key Vault&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Congratulations! You have created all the necessary resources and configuration, which means your API is ready to be deployed.&lt;/p&gt;

&lt;h2&gt;
  
  
  13. Deploy the functions
&lt;/h2&gt;

&lt;p&gt;In this step, you will generate Prisma Client and deploy the functions.&lt;/p&gt;

&lt;p&gt;From the project folder, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;npx prisma generate
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command will generate Prisma Client into the &lt;code&gt;node_modules&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;To deploy the functions, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;npx func azure functionapp publish FUNCTION_APP_NAME
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Checkpoint:&lt;/strong&gt; If the functions have been successfully deployed you should see the following output:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Getting site publishing info...
Uploading package...
Uploading 67.24 MB [##############################################################################]
Upload completed successfully.
Deployment completed successfully.
Syncing triggers...
Functions in FUNCTION_APP_NAME:
    CreatePost - [httpTrigger]
        Invoke url: https://FUNCTION_APP_NAME.azurewebsites.net/api/post
    CreateUser - [httpTrigger]
        Invoke url: https://FUNCTION_APP_NAME.azurewebsites.net/api/user
    DeletePost - [httpTrigger]
        Invoke url: https://FUNCTION_APP_NAME.azurewebsites.net/api/post/{postid}
    FilterPosts - [httpTrigger]
        Invoke url: https://FUNCTION_APP_NAME.azurewebsites.net/api/filterposts
    GetFeed - [httpTrigger]
        Invoke url: https://FUNCTION_APP_NAME.azurewebsites.net/api/feed
    GetPost - [httpTrigger]
        Invoke url: https://FUNCTION_APP_NAME.azurewebsites.net/api/post/{postid}
    PublishPost - [httpTrigger]
        Invoke url: https://FUNCTION_APP_NAME.azurewebsites.net/api/publish/{postid}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations 🎊! If you've made it this far, you've successfully deployed a Prisma based REST API to Azure Functions which uses Azure SQL as the database.&lt;/p&gt;

&lt;p&gt;In the next step, you'll test the functions and take a closer look at how the functions are implemented.&lt;/p&gt;

&lt;h2&gt;
  
  
  14. Test the deployed functions
&lt;/h2&gt;

&lt;p&gt;In this step, you will test the API's different endpoints using the URLs from the previous step.&lt;/p&gt;

&lt;p&gt;Begin by making a POST HTTP request to the &lt;em&gt;CreateUser&lt;/em&gt; endpoint with curl:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;curl --request POST --data '{"email":"alice@prisma.io","name":"Alice"}' https://FUNCTION_APP_NAME.azurewebsites.net/api/user
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Replace the &lt;strong&gt;&lt;code&gt;FUNCTION_APP_NAME&lt;/code&gt;&lt;/strong&gt; in the URL with the app name you chose in step 8.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the request succeeds, you should see the created user object returned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"createdAt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2021-03-02T14:48:15.746Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"alice@prisma.io"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The files associated with the function can be found in the &lt;code&gt;CreateUser&lt;/code&gt; folder, which contains two files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;function.json&lt;/code&gt;: Function configuration, e.g. HTTP method, path, and return value&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;index.js&lt;/code&gt;: The function handler where Prisma Client is used to create the user in the Azure SQL database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, try creating a &lt;em&gt;post&lt;/em&gt; associated with the user you just created with the following comand:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;curl --request POST --data '{"title":"Prisma with Azure","content":"","authorEmail":"alice@prisma.io"}' https://FUNCTION_APP_NAME.azurewebsites.net/api/post
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the request succeeds, you should see the created &lt;em&gt;post&lt;/em&gt; object returned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"createdAt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2021-03-02T17:09:53.160Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"updatedAt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2021-03-02T17:09:53.161Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Prisma with Azure"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"published"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"authorId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To update the &lt;code&gt;published&lt;/code&gt; field of the post, make the following request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;curl --request PUT https://FUNCTION_APP_NAME.azurewebsites.net/api/publish/1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the request succeeds, you should see the updated &lt;em&gt;post&lt;/em&gt; object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"authorId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"createdAt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2021-03-02T17:09:53.160Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"published"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Prisma with Azure"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"updatedAt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2021-03-03T10:07:11.047Z"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, to test the &lt;em&gt;feed&lt;/em&gt; endpoint, make the following request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;curl https://FUNCTION_APP_NAME.azurewebsites.net/api/feed
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the request succeeds, you should see the post you created and the related author:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"createdAt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2021-03-02T14:48:15.746Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"email"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"alice@prisma.io"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"authorId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"createdAt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2021-03-02T17:09:53.160Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"published"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Prisma with Azure"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"updatedAt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2021-03-03T10:07:11.047Z"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Developing and debugging the functions locally
&lt;/h2&gt;

&lt;p&gt;When implementing Azure Functions, you can also start a local development environment using the Azure Functions Core tools' function runtime. That way, you can test and debug the implementation of the functions locally.&lt;/p&gt;

&lt;p&gt;To launch the functions runtime, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;npx func start
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command starts a local server and allows you to call any of the functions in the project.&lt;/p&gt;

&lt;p&gt;You can inject environment variables into the functions by adding them to the &lt;code&gt;Values&lt;/code&gt; object in the &lt;code&gt;local.settings.json&lt;/code&gt; file at the root of the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up a local database for development
&lt;/h2&gt;

&lt;p&gt;When developing locally, you should consider running a local Microsoft SQL Server instance. &lt;a href="https://docs.microsoft.com/en-us/azure/azure-sql/azure-sql-iaas-vs-paas-what-is-overview"&gt;While Microsoft SQL Server is not the same as Azure SQL, the two have high compatibility with each other.&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The quickest way to set up a local Microsoft SQL Server is with Docker. Check out the &lt;a href="https://github.com/prisma/prisma-examples/tree/latest/databases/sql-server"&gt;Microsoft SQL Server example&lt;/a&gt; for more information on how to set it up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bootstrapping a new function
&lt;/h2&gt;

&lt;p&gt;When you want to create a new function, you can use the following command to bootstrap a new function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;npx func function new --language JavaScript --template "HTTP trigger" --name FUNCTION_NAME
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The command creates a folder with the &lt;code&gt;index.js&lt;/code&gt; and &lt;code&gt;function.json&lt;/code&gt; files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Congratulations! You have successfully deployed the REST API to Azure Functions and used Prisma Client to handle database queries to the Azure SQL database.&lt;/p&gt;

&lt;p&gt;For more insight into Prisma Client's API, explore the function handlers and check out the &lt;a href="https://www.prisma.io/docs/reference/api-reference/prisma-client-reference"&gt;Prisma Client API Reference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's worth noting that while this guide used the Azure CLI to create all the resources, this can also be achieved via the Azure Portal UI or the VSCode extension, which supports deployments directly from VSCode.&lt;/p&gt;

&lt;p&gt;As a next step, you could look into implementing a continuous delivery pipeline using &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-github-actions?tabs=javascript"&gt;GitHub Actions&lt;/a&gt; to automate the deployment process from a GitHub repository.&lt;/p&gt;

</description>
      <category>azure</category>
      <category>serverless</category>
      <category>node</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Bundling Prisma with the CDK using aws-lambda-nodejs</title>
      <dc:creator>Ryan Dsouza</dc:creator>
      <pubDate>Thu, 11 Mar 2021 13:56:39 +0000</pubDate>
      <link>https://dev.to/prisma/bundling-prisma-with-the-cdk-using-aws-lambda-nodejs-2lkd</link>
      <guid>https://dev.to/prisma/bundling-prisma-with-the-cdk-using-aws-lambda-nodejs-2lkd</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this post, we shall see how we can bundle Prisma using &lt;a href="https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-nodejs-readme.html" rel="noopener noreferrer"&gt;aws-lambda-nodejs&lt;/a&gt; with the CDK.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;This is a walkthrough of the steps required to bundle Prisma with CDK and so cloning the repo below and following along would be ideal.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/ryands17" rel="noopener noreferrer"&gt;
        ryands17
      &lt;/a&gt; / &lt;a href="https://github.com/ryands17/prisma-lambda" rel="noopener noreferrer"&gt;
        prisma-lambda
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Integrating Prisma with `aws-lambda-nodejs` to run Prisma on Lambda
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Constructs
&lt;/h2&gt;

&lt;p&gt;In this project, we have a simple &lt;a href="https://github.com/ryands17/prisma-lambda/blob/main/functions/index.ts" rel="noopener noreferrer"&gt;Lambda function&lt;/a&gt; that uses Prisma to query data from a &lt;code&gt;User&lt;/code&gt; table and logs to the console. The only resource we create here is the Lambda function so let's look at that.&lt;/p&gt;

&lt;p&gt;I'm skipping the imports in these snippets to make it smaller but if you want to view those you can check those out in the file mentioned in the comments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// lib/prisma-lambda-stack.ts&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ln&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NodejsFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;prisma&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;lambda&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODEJS_14_X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;handler&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;lambdaFn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;DB_URL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DB_URL&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;bundling&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;nodeModules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@prisma/client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;prisma&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;commandHooks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;beforeBundling&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;inputDir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;outputDir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="nf"&gt;beforeInstall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;inputDir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;outputDir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;`cp -R ../prisma &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;outputDir&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/`&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="nf"&gt;afterBundling&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;inputDir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;outputDir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
          &lt;span class="s2"&gt;`cd &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;outputDir&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="s2"&gt;`yarn prisma generate`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="s2"&gt;`rm -rf node_modules/@prisma/engines`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here, we create a simple Node.js Lambda function and the parameters like &lt;code&gt;runtime&lt;/code&gt;, &lt;code&gt;handler&lt;/code&gt;, &lt;code&gt;entry&lt;/code&gt;, &lt;code&gt;timeout&lt;/code&gt; and &lt;code&gt;environment&lt;/code&gt; are the basic Lambda parameters that we pass.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws-lambda-nodejs&lt;/code&gt; uses &lt;a href="https://esbuild.github.io/" rel="noopener noreferrer"&gt;esbuild&lt;/a&gt; under the hood so your function is bundled using &lt;code&gt;esbuild&lt;/code&gt;. There are a couple of important parameters that we need to set for the bundling process.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first is &lt;code&gt;nodeModules&lt;/code&gt;. This will instruct &lt;code&gt;esbuild&lt;/code&gt; to not include &lt;code&gt;@prisma/client&lt;/code&gt; and &lt;code&gt;prisma&lt;/code&gt; into our function file and treat them as &lt;code&gt;node_modules&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The other section to be configured is &lt;code&gt;commandHooks&lt;/code&gt;. The main hooks we need here are &lt;code&gt;beforeInstall&lt;/code&gt; and &lt;code&gt;afterBundling&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;code&gt;beforeInstall&lt;/code&gt;, we run the command &lt;code&gt;cp -R ../prisma ${outputDir}/&lt;/code&gt;. This makes sure we have &lt;code&gt;schema.prisma&lt;/code&gt; present in the bundle. You can also directly copy &lt;code&gt;schema.prisma&lt;/code&gt; instead of the entire directory if you have the &lt;code&gt;migrations&lt;/code&gt; folder which is not required in the bundle.&lt;/li&gt;
&lt;li&gt;The other hook we use is &lt;code&gt;afterBundling&lt;/code&gt;. This is to generate &lt;code&gt;PrismaClient&lt;/code&gt; and to remove the &lt;code&gt;engines&lt;/code&gt; folder to reduce function size.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rest is a simple Prisma setup with a &lt;a href="https://github.com/ryands17/prisma-lambda/blob/main/prisma/schema.prisma" rel="noopener noreferrer"&gt;schema.prisma&lt;/a&gt; and a &lt;code&gt;User&lt;/code&gt; model.&lt;/p&gt;

&lt;p&gt;Now when we run &lt;code&gt;cdk synth&lt;/code&gt;, the function will be bundled and ready for deployment.&lt;/p&gt;
&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So this is how we can package Prisma with &lt;code&gt;aws-lambda-nodejs&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is the repo again for those who haven't checked it out:&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/ryands17" rel="noopener noreferrer"&gt;
        ryands17
      &lt;/a&gt; / &lt;a href="https://github.com/ryands17/prisma-lambda" rel="noopener noreferrer"&gt;
        prisma-lambda
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Integrating Prisma with `aws-lambda-nodejs` to run Prisma on Lambda
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>prisma</category>
      <category>aws</category>
      <category>lambda</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Building a GraphQL CRUD API for your Database with TypeGraphQL &amp; Prisma</title>
      <dc:creator>Ruheni Alex</dc:creator>
      <pubDate>Thu, 25 Feb 2021 13:21:34 +0000</pubDate>
      <link>https://dev.to/prisma/prototyping-a-crud-api-with-typegraphql-and-prisma-for-your-database-424c</link>
      <guid>https://dev.to/prisma/prototyping-a-crud-api-with-typegraphql-and-prisma-for-your-database-424c</guid>
      <description>&lt;p&gt;Building CRUD APIs can be a tedious chore. The time spent writing glue code, plumbing layers together and doing repetitive work is often better invested into tasks that actually add value and solve interesting problems.&lt;/p&gt;

&lt;p&gt;In this article, you'll explore how you can prototype an e-commerce GraphQL CRUD API using &lt;a href="https://typegraphql.com/" rel="noopener noreferrer"&gt;TypeGraphQL&lt;/a&gt;, &lt;a href="https://www.apollographql.com/docs/apollo-server/" rel="noopener noreferrer"&gt;Apollo Server&lt;/a&gt; and &lt;a href="https://prisma.io/" rel="noopener noreferrer"&gt;Prisma&lt;/a&gt; for database access. &lt;a href="https://sqlite.org/index.html" rel="noopener noreferrer"&gt;SQLite&lt;/a&gt; will be the database of choice in this tutorial because of its ease of setup. Feel free to use your choice database - Prisma currently supports PostgreSQL, MySQL, SQL Server, SQLite, MongoDB, and CockroachDB.&lt;/p&gt;

&lt;p&gt;The complete example used in this tutorial is available on &lt;a href="https://github.com/ruheni/typegraphql-crud-api" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is TypeGraphQL?
&lt;/h2&gt;

&lt;p&gt;TypeGraphQL is a framework that follows a &lt;a href="https://www.prisma.io/blog/the-problems-of-schema-first-graphql-development-x1mn4cb0tyl3" rel="noopener noreferrer"&gt;code-first&lt;/a&gt; and object-oriented approach towards building GraphQL APIs. It leverages &lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;TypeScript&lt;/a&gt; by using classes and decorators.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If you're not familiar with TypeScript yet, check out Learn TypeScript: A Pocketguide Tutorial.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is Prisma?
&lt;/h2&gt;

&lt;p&gt;Prisma is an ORM - Object-relational mapper. It provides a declarative way to define your database models that are easy to read and understand. It also offers a type-safe database client from your database schema that is intuitive and fun. 🙂&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before getting started, ensure you have the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Installed &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Basic understanding of JavaScript/TypeScript.&lt;/li&gt;
&lt;li&gt;Familiarity with GraphQL APIs.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: To get started on learning the basics of GraphQL, How To GraphQL will give you a great foundation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 1: Initialize Your Project
&lt;/h2&gt;

&lt;p&gt;The first step will be creating your working directory on your computer and initialize it by running &lt;code&gt;npm init -y&lt;/code&gt;. This command will generate a &lt;code&gt;package.json&lt;/code&gt; file with prepopulated values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir crud-api
cd crud-api
npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Set Up Dependencies
&lt;/h3&gt;

&lt;p&gt;Install the following dependencies in your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install apollo-server type-graphql reflect-metadata class-validator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://typegraphql.com/docs/installation.html#packages-installation" rel="noopener noreferrer"&gt;reflect-metadata&lt;/a&gt; allows you to do runtime reflection on types.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/typestack/class-validator" rel="noopener noreferrer"&gt;class-validator&lt;/a&gt; is a dependency used by TypeGraphQL to validate inputs and arguments using &lt;a href="https://www.apollographql.com/blog/backend/graphql-schema-decorators-3c01a445b628/" rel="noopener noreferrer"&gt;decorators&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Your project will be dependent on the following development dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev typescript ts-node-dev ts-node @types/node @types/ws
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;typescript&lt;/code&gt; and &lt;code&gt;ts-node-dev&lt;/code&gt; dependencies are required to transpile your TypeScript files and restart the app server when a change is made to a file.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Little More 🔧
&lt;/h3&gt;

&lt;p&gt;Create a &lt;code&gt;tsconfig.json&lt;/code&gt; file in your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch tsconfig.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;tsconfig.json&lt;/code&gt; allows you to define options that will let the TypeScript compiler take advantage of when transpiling your code to JavaScript.&lt;/p&gt;

&lt;p&gt;Paste in the following code to your &lt;code&gt;tsconfig.json&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// tsconfig.json
{
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "lib": [
      "es2018",
      "esnext.asynciterable"
    ],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To take advantage of &lt;code&gt;ts-node-dev&lt;/code&gt;'s live reloading, modify &lt;code&gt;scripts&lt;/code&gt; in your &lt;code&gt;package.json&lt;/code&gt; to this script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// package.json&lt;/span&gt;
&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scripts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dev&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ts-node-dev --no-notify --respawn --transpile-only src/index.ts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, create your &lt;code&gt;src&lt;/code&gt; directory - this will house the bulk of your application - and create an &lt;code&gt;index.ts&lt;/code&gt; file as well:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir src
touch src/index.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following code will be your initial setup of your GraphQL server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// index.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;reflect-metadata&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;tq&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;type-graphql&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ApolloServer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apollo-server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;tq&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;buildSchema&lt;/span&gt;&lt;span class="p"&gt;({})&lt;/span&gt;

  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ApolloServer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🚀 Server ready at: &amp;lt;http://localhost:4000&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;app&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;VS Code's TypeScript compiler will throw an error about the buildSchema missing the resolver argument. Not to worry, this will be covered in Step 3: Set up typegraphql-prisma. 🙂&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 2: Set Up Prisma
&lt;/h2&gt;

&lt;p&gt;Now that that's out of the way let's get to the fun part, data modeling.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initialize Prisma
&lt;/h3&gt;

&lt;p&gt;You'll need to setup Prisma in your project by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx prisma init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Prisma CLI creates a &lt;code&gt;.env&lt;/code&gt; file in your project's root and a new &lt;code&gt;prisma&lt;/code&gt; directory. The folder created will contain a &lt;code&gt;schema.prisma&lt;/code&gt; file that defines your database connection, the &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-client" rel="noopener noreferrer"&gt;Prisma Client&lt;/a&gt; generator and this is where you will define your database models.&lt;/p&gt;

&lt;p&gt;The default &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-schema/data-sources/" rel="noopener noreferrer"&gt;database source&lt;/a&gt; is PostgreSQL. However, you can switch this to your preferred provider - &lt;code&gt;sqlite&lt;/code&gt;, &lt;code&gt;mysql&lt;/code&gt;, &lt;code&gt;sqlserver&lt;/code&gt;, &lt;code&gt;cockroachdb&lt;/code&gt; or &lt;code&gt;mongodb&lt;/code&gt; - and modify the &lt;code&gt;url&lt;/code&gt; pointing to your database. For ease of setup, this tutorial will use SQLite. &lt;/p&gt;

&lt;p&gt;Navigate to &lt;code&gt;schema.prisma&lt;/code&gt; file and change the datasource provider and datasource url string values to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// schema.prisma&lt;/span&gt;
&lt;span class="nx"&gt;generator&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;prisma-client-js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// change the code below 👇&lt;/span&gt;
&lt;span class="nx"&gt;datasource&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sqlite&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="nx"&gt;url&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;file:./dev.db&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
  Alternative database connection string setup
  &lt;p&gt;In your &lt;code&gt;.env&lt;/code&gt; file, add this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#.env file
&lt;/span&gt;&lt;span class="n"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;file:./dev.db&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then modify the datasource url to point to &lt;code&gt;DATABASE_URL&lt;/code&gt; variable in your &lt;code&gt;.env&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// schema.prisma&lt;/span&gt;
&lt;span class="nx"&gt;generator&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;prisma-client-js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="nx"&gt;datasource&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sqlite&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="nx"&gt;url&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;DATABASE_URL&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;The database doesn’t currently exist. Prisma will create the database in &lt;code&gt;prisma/dev.db&lt;/code&gt; when running your first migration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Database Models
&lt;/h3&gt;

&lt;p&gt;For the e-commerce API, you will define 4 models: &lt;code&gt;Product&lt;/code&gt;, &lt;code&gt;Category&lt;/code&gt;, &lt;code&gt;Order&lt;/code&gt;, and &lt;code&gt;User&lt;/code&gt;. The relationships between these entities will be as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one-to-many relationship between &lt;code&gt;User&lt;/code&gt; and &lt;code&gt;Order&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;many-to-many relationship between &lt;code&gt;Product&lt;/code&gt; and &lt;code&gt;Category&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;many-to-many relationship between &lt;code&gt;Order&lt;/code&gt; and &lt;code&gt;Product&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a visual diagram representing the relationships between the different database models:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgtv3my83usnfcjz0t0zv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgtv3my83usnfcjz0t0zv.png" alt="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtv3my83usnfcjz0t0zv.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Your Prisma Schema
&lt;/h3&gt;

&lt;p&gt;With the relationships defined, your final database models should be similar to the one below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// schema.prisma
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

model Product {
  id          String     @id @default(uuid())
  createdAt   DateTime   @default(now())
  updatedAt   DateTime   @updatedAt
  name        String
  sku         String     @unique
  description String?
  quantity    Int
  categories  Category[]
  orders      Order[]
}

model Category {
  id        Int       @id @default(autoincrement())
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  name      String
  products  Product[]
}

model Order {
  id        String    @id @default(uuid())
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  userId    Int?
  customer  User?     @relation(fields: [userId], references: [id])
  products  Product[]
}

model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  email     String   @unique
  firstName String?
  lastName  String?
  address   String?
  orders    Order[]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Feel free to modify the fields provided to your preference.&lt;/p&gt;

&lt;h3&gt;
  
  
  Your First Migration
&lt;/h3&gt;

&lt;p&gt;Next, you'll sync your schema with your database with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx prisma migrate dev --name init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command creates a database migration and executes it against your database. A database migration refers to incremental, reversible changes that are made to a database schema over time. In that sense, you can think of migrations as a tool enabling "version control" for your database.&lt;/p&gt;

&lt;p&gt;The command creates a migration called &lt;code&gt;init&lt;/code&gt;. Once the migration is complete, a new &lt;code&gt;/prisma/migrations&lt;/code&gt; directory is created. Since you're working with SQLite, Prisma CLI will create your database and apply the changes against your database.&lt;/p&gt;

&lt;p&gt;After the first migration is created, the Prisma CLI installs the &lt;a href="https://www.npmjs.com/package/@prisma/client" rel="noopener noreferrer"&gt;@prisma/client&lt;/a&gt; package. In subsequent database migrations, the Prisma CLI will generate your Prisma Client. &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-client" rel="noopener noreferrer"&gt;Prisma Client&lt;/a&gt; is an auto-generated database client that allows you to interact with your database in a type-safe way.&lt;/p&gt;

&lt;p&gt;Pretty neat, right? 😜&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Modify Your GraphQL Server
&lt;/h2&gt;

&lt;p&gt;Create an instance of &lt;code&gt;PrismaClient&lt;/code&gt; and add it to the context of your GraphQL Server as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//index.ts
import 'reflect-metadata'
import * as tq from 'type-graphql'
import { ApolloServer } from 'apollo-server'
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient()

const app = async () =&amp;gt; {
  const schema = await tq.buildSchema({})

  const context = {
    prisma
  }

  new ApolloServer({ schema, context }).listen({ port: 4000 }, () =&amp;gt;
    console.log('🚀 Server ready at: &amp;lt;http://localhost:4000&amp;gt;')
  )
}

app()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding &lt;code&gt;prisma&lt;/code&gt; to your context makes sure that &lt;code&gt;PrismaClient&lt;/code&gt; is made available to your GraphQL operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Set Up &lt;code&gt;typegraphql-prisma&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;TypeGraphQL provides an integration with Prisma using the &lt;code&gt;[typegraphql-prisma](https://www.npmjs.com/package/typegraphql-prisma)&lt;/code&gt; package. This package will &lt;a href="https://prisma.typegraphql.com/docs/intro/#what-it-is" rel="noopener noreferrer"&gt;auto-generate types, enums and even CRUD resolvers&lt;/a&gt; based on your Prisma schema, which you otherwise would need to write manually.&lt;/p&gt;

&lt;p&gt;Install &lt;code&gt;typegraphql-prisma&lt;/code&gt; as a dev dependency with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev typegraphql-prisma
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;typegraphql-prisma&lt;/code&gt; is dependent on several packages for it to work properly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install graphql-type-json graphql-fields
npm install --save-dev @types/graphql-fields
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the dependencies are successfully installed, add a new generator in your &lt;code&gt;schema.prisma&lt;/code&gt; file below the &lt;code&gt;client&lt;/code&gt; generator as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// schema.prisma
generator client {
  provider = "prisma-client-js"
}

generator typegraphql {
  provider = "typegraphql-prisma"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;npx prisma generate&lt;/code&gt; to generate TypeGraphQL classes and CRUD resolvers based on your Prisma schema.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx prisma generate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;typegraphql-prisma&lt;/code&gt; emits the generated classes, enums and resolvers to &lt;code&gt;node_modules/@generated/typegraphql-prisma&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It should be noted that in your subsequent migrations, the classes and resolvers TypeGraphQL generates will be automatically updated on running &lt;code&gt;npx prisma migrate dev&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzby23fodrm0pp7phks67.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzby23fodrm0pp7phks67.png" alt="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zby23fodrm0pp7phks67.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The final step is updating our resolvers in our Apollo Server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// index.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;reflect-metadata&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PrismaClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@prisma/client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ApolloServer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;apollo-server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;resolvers&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@generated/type-graphql&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;tq&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;type-graphql&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PrismaClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;tq&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;buildSchema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;resolvers&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;prisma&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ApolloServer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;🚀 Server ready at: &amp;lt;http://localhost:4000&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;app&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5: Test Your New GraphQL API
&lt;/h2&gt;

&lt;p&gt;Start your GraphQL server with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open &lt;a href="http://localhost:4000/" rel="noopener noreferrer"&gt;localhost:4000&lt;/a&gt; to interact with the GraphQL playground...and congratulations. 🎉&lt;/p&gt;

&lt;p&gt;You've successfully automated building a CRUD API. Just to be sure explore &lt;strong&gt;Docs&lt;/strong&gt; section of the playground and try out some queries and mutations:&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a New &lt;code&gt;Category&lt;/code&gt;:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;mutation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;createCategory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;electronics&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;id&lt;/span&gt;
    &lt;span class="nx"&gt;name&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a New &lt;code&gt;Product&lt;/code&gt;:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;mutation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;createProduct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Google Pixel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;sku&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;90456123098&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
      &lt;span class="nx"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;id&lt;/span&gt;
    &lt;span class="nx"&gt;name&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Query all Products:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;id&lt;/span&gt;
    &lt;span class="nx"&gt;name&lt;/span&gt;
    &lt;span class="nx"&gt;quantity&lt;/span&gt;
    &lt;span class="nx"&gt;sku&lt;/span&gt;
    &lt;span class="nx"&gt;categories&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;name&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;id&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything is running smoothly. Go ahead and pat yourself on the back for getting this far.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/U7DFwka0lRgiIWKZnP/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/U7DFwka0lRgiIWKZnP/giphy.gif" alt="https://media.giphy.com/media/U7DFwka0lRgiIWKZnP/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Feel free to explore other mutations and queries on the playground. 🙂&lt;/p&gt;

&lt;h2&gt;
  
  
  Side Quest: Some &lt;code&gt;typegraphql-prisma&lt;/code&gt; Wizardry 🧙🏾‍♂️
&lt;/h2&gt;

&lt;p&gt;Alternatively, you can make specific CRUD operations, specific actions, modify generated functions that can be performed against your Prisma schema. This gives you more control of the operations exposed on your API. This approach also allows you to create your custom resolvers and add them to your GraphQL schema.&lt;/p&gt;

&lt;h3&gt;
  
  
  Expose Specific Prisma CRUD operations
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;typegraphql-prisma&lt;/code&gt; allows you to expose selected CRUD operations on your API on your Prisma models.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {
  ProductCrudResolver,
  CategoryCrudResolver
} from "@generated/type-graphql";

const schema = await tq.buildSchema({
  resolvers: [
    ProductCrudResolver,
    CategoryCrudResolver
  ]
})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Expose Specific Prisma Actions
&lt;/h3&gt;

&lt;p&gt;To get more control over the GraphQL operations, you can expose, &lt;code&gt;typegraphql-prisma&lt;/code&gt; allows you to expose specific Prisma operations from the generated types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {
  CreateProductResolver,
  UpdateProductResolver
} from "@generated/type-graphql";

const schema = await tq.buildSchema({
  resolvers: [
    CreateProductResolver,
    UpdateProductResolver
  ]
})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TypeGraphQL allows you to add custom queries and mutations to your schema using the generated Prisma Client.&lt;/p&gt;

&lt;h3&gt;
  
  
  Applying Custom Resolvers
&lt;/h3&gt;

&lt;p&gt;The beauty of TypeGraphQL is that you can create custom resolvers such as authorization by creating its decorators.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;typegraphql-prisma&lt;/code&gt; generates the &lt;code&gt;applyResolversEnhanceMap&lt;/code&gt; function and &lt;code&gt;ResolversEnhanceMap&lt;/code&gt; to aid in the creation of a config object that allows you to add decorator functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {
  ResolversEnhanceMap,
  applyResolversEnhanceMap,
} from "@generated/type-graphql";
import { Authorized } from "type-graphql";

const resolversEnhanceMap: ResolversEnhanceMap = {
  Category: {
    createCategory: [Authorized(Role.ADMIN)],
  },
};

applyResolversEnhanceMap(resolversEnhanceMap);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Learn more about other advanced operations you can apply to your GraphQL resolvers, such as custom resolvers, authorization, middleware and additional decorators to your Prisma schema and models &lt;a href="https://github.com/MichalLytek/typegraphql-prisma#advanced-usage" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Generating the SDL
&lt;/h3&gt;

&lt;p&gt;To enable viewing the SDL of your GraphQL API, make the following modification in your application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const schema = await tq.buildSchema({
    resolvers,
    emitSchemaFile: true
  })

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will automatically create a &lt;code&gt;schema.gql&lt;/code&gt; file with your GraphQL schema definitions in your working directory.&lt;/p&gt;

&lt;p&gt;You could also specify the exact path for your &lt;code&gt;schema.gql&lt;/code&gt; file. This modification will add it in &lt;code&gt;src/snapshots/schema&lt;/code&gt; folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;schema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;tq&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;buildSchema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;resolvers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;emitSchemaFile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;snapshots/schema&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;schema.gql&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the application you just built, here's a preview of the &lt;code&gt;type User&lt;/code&gt;'s queries and mutations that is generated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Query&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;aggregateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserWhereUniqueInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;orderBy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;UserOrderByInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;skip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;take&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserWhereInput&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;AggregateUser&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
  &lt;span class="nf"&gt;findFirstUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserWhereUniqueInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;distinct&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;UserScalarFieldEnum&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;orderBy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;UserOrderByInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;skip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;take&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserWhereInput&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;
  &lt;span class="nf"&gt;user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserWhereUniqueInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;
  &lt;span class="nf"&gt;users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserWhereUniqueInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;distinct&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;UserScalarFieldEnum&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;orderBy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;UserOrderByInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;skip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;take&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserWhereInput&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Mutation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserCreateInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
  &lt;span class="nf"&gt;deleteManyUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserWhereInput&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;AffectedRowsOutput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
  &lt;span class="nf"&gt;deleteUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserWhereUniqueInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;
  &lt;span class="nf"&gt;updateManyUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserUpdateManyMutationInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserWhereInput&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;AffectedRowsOutput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
  &lt;span class="nf"&gt;updateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserUpdateInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserWhereUniqueInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;
  &lt;span class="nf"&gt;upsertUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserCreateInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserUpdateInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserWhereUniqueInput&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can learn more about this feature &lt;a href="https://typegraphql.com/docs/0.16.0/emit-schema.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You have successfully built a CRUD GraphQL API by barely writing any code. Simple and fast. I hope you have enjoyed this tutorial. Feel free to share your thoughts, and share what kind of tutorials or examples you would like to see more.&lt;/p&gt;

&lt;p&gt;Explore the &lt;a href="https://github.com/prisma/prisma-examples" rel="noopener noreferrer"&gt;prisma-examples&lt;/a&gt; to see how Prisma can fit in your stack. If you feel an example is missing, create an &lt;a href="https://github.com/prisma/prisma/examples/issues/new" rel="noopener noreferrer"&gt;issue&lt;/a&gt;. 😊&lt;/p&gt;

&lt;p&gt;Happy hacking! 👩🏾‍💻👨🏾‍💻&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Adding an API and database to your Nuxt App with Prisma</title>
      <dc:creator>Ruheni Alex</dc:creator>
      <pubDate>Thu, 18 Feb 2021 12:02:09 +0000</pubDate>
      <link>https://dev.to/prisma/adding-an-api-and-database-to-your-nuxt-app-with-prisma-2nlp</link>
      <guid>https://dev.to/prisma/adding-an-api-and-database-to-your-nuxt-app-with-prisma-2nlp</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The application is built using Nuxt version 2 at the time it was written. Prisma runs on the server-side and you can use Nuxt version 3's &lt;a href="https://v3.nuxtjs.org/guide/features/server-routes" rel="noopener noreferrer"&gt;server routes&lt;/a&gt; to add database access to your application without using Express.&lt;/p&gt;

&lt;p&gt;If you would like to try out Prisma with Nuxt 3, check out &lt;a href="https://sidebase.io/" rel="noopener noreferrer"&gt;sidebase&lt;/a&gt;. You can quickly set up a project by running &lt;code&gt;npm create sidebase@latest&lt;/code&gt; and select Prisma in the modules prompt.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Building fullstack applications has never been easier using &lt;a href="https://nuxtjs.org" rel="noopener noreferrer"&gt;Nuxt&lt;/a&gt;. It provides a &lt;a href="https://vuejs.org" rel="noopener noreferrer"&gt;Vue&lt;/a&gt; framework for building blazing fast server-side rendered and static applications. &lt;a href="https://prisma.io" rel="noopener noreferrer"&gt;Prisma&lt;/a&gt; is an ORM that offers developers a type-safe database client (currently supports  PostgreSQL, MySQL, SQLite, SQL Server, CockroachDB and MongoDB). This is useful when building APIs and is intuitive for both beginners and experienced developers.&lt;/p&gt;

&lt;p&gt;Nuxt presents two options for building fullstack applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using Nuxt programmatically - setting up your own server with your &lt;a href="https://nuxtjs.org/docs/2.x/directory-structure/middleware" rel="noopener noreferrer"&gt;middleware&lt;/a&gt; and API.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-servermiddleware" rel="noopener noreferrer"&gt;&lt;code&gt;serverMiddleware&lt;/code&gt;&lt;/a&gt; - this is an extension of your Nuxt app that allows you to create additional routes without setting up an external server. The middleware is registered on application start.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, you’ll learn how to add an API in your Nuxt application using Prisma for your database access and Nuxt’s &lt;code&gt;serverMiddleware&lt;/code&gt; property.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before you start, ensure you have Node.js installed. If you are using Visual Studio Code, you can supercharge your development experience by installing the &lt;a href="https://marketplace.visualstudio.com/items?itemName=Prisma.prisma" rel="noopener noreferrer"&gt;Prisma extension&lt;/a&gt; for auto-completion, formatting, and syntax highlighting. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_6AF9ABEB955DB3AAD3B965E721CBE5603955F2FD47440251DB514D08F2D85D33_1609335122080_image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_6AF9ABEB955DB3AAD3B965E721CBE5603955F2FD47440251DB514D08F2D85D33_1609335122080_image.png" alt="Prisma VS Code Extension"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The completed project is available on &lt;a href="https://github.com/ruheni/prisma-nuxt" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 0: Initialize your Nuxt application
&lt;/h2&gt;

&lt;p&gt;The first step is initializing your Nuxt application. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npx create-nuxt-app awesome-nuxt-app


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You’ll be asked a couple of questions such as the name of the projects, linter, testing framework, etc. Keep the app simple and go with the default options provided. To learn more about the options, head over to &lt;a href="https://github.com/nuxt/create-nuxt-app/blob/master/README.md" rel="noopener noreferrer"&gt;Create Nuxt App&lt;/a&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;cd &lt;/span&gt;awesome-nuxt-app
npm run dev


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Congratulations! Your application is now running on &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; 🥳.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_73BF466ED2B54D3E86B7180578C8C4A2F8A05295E5EBFE61FC0C76AB74FCDA16_1612939180344_image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_73BF466ED2B54D3E86B7180578C8C4A2F8A05295E5EBFE61FC0C76AB74FCDA16_1612939180344_image.png" alt="Awesome Nuxt app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Add Prisma to your app
&lt;/h2&gt;

&lt;p&gt;Now that your Nuxt application is running, the next step is to set up Prisma. You’ll first install the Prisma CLI as a dev dependency by running the following command: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; prisma


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Initialize Prisma
&lt;/h3&gt;

&lt;p&gt;Once the installation is complete, run the following command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npx prisma init


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The command above creates a folder called &lt;code&gt;prisma&lt;/code&gt; at the root of your project which contains a file called &lt;code&gt;schema.prisma&lt;/code&gt; and a &lt;code&gt;.env&lt;/code&gt; file at the root of the project. The &lt;code&gt;schema.prisma&lt;/code&gt; defines your database connection and Prisma Client generator. For this example, you’ll use SQLite for ease of setting up. If you’d like to use another database provider, switching is as simple as renaming the provider from &lt;code&gt;sqlite&lt;/code&gt; to your provider of choice without any additional setup and updating the &lt;a href="https://www.prisma.io/docs/reference/database-reference/connection-urls" rel="noopener noreferrer"&gt;connection URL&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For our example, you’ll define two tables: &lt;code&gt;Post&lt;/code&gt;  and &lt;code&gt;User&lt;/code&gt; with a one-to-many relationship between &lt;code&gt;User&lt;/code&gt; and &lt;code&gt;Post&lt;/code&gt;. Update your &lt;code&gt;schema.prisma&lt;/code&gt;  file to resemble this one:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// schema.prisma&lt;/span&gt;
&lt;span class="n"&gt;datasource&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"sqlite"&lt;/span&gt;
  &lt;span class="n"&gt;url&lt;/span&gt;      &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"file:./dev.db"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;generator&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"prisma-client-js"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;        &lt;span class="n"&gt;Int&lt;/span&gt;      &lt;span class="nd"&gt;@id&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autoincrement&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;title&lt;/span&gt;     &lt;span class="n"&gt;String&lt;/span&gt;
  &lt;span class="n"&gt;content&lt;/span&gt;   &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;createdAt&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;updatedAt&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt; &lt;span class="nd"&gt;@updatedAt&lt;/span&gt;
  &lt;span class="n"&gt;published&lt;/span&gt; &lt;span class="n"&gt;Boolean&lt;/span&gt;  &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;author&lt;/span&gt;    &lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;    &lt;span class="nd"&gt;@relation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;fields:&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;authorId&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="nl"&gt;references:&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
  &lt;span class="n"&gt;authorId&lt;/span&gt;  &lt;span class="n"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;    &lt;span class="n"&gt;Int&lt;/span&gt;     &lt;span class="nd"&gt;@id&lt;/span&gt; &lt;span class="nd"&gt;@default&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autoincrement&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;  &lt;span class="nd"&gt;@unique&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;  &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;posts&lt;/span&gt; &lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Create your first database migration
&lt;/h3&gt;

&lt;p&gt;To sync your data model to your database schema, you’ll need to use &lt;a href="https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-dev" rel="noopener noreferrer"&gt;&lt;code&gt;prisma migrate&lt;/code&gt;&lt;/a&gt; CLI.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npx prisma migrate dev &lt;span class="nt"&gt;--name&lt;/span&gt; init


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above command will create a migration called &lt;code&gt;init&lt;/code&gt; located in the &lt;code&gt;/prisma/migrations&lt;/code&gt; directory. The migrations folder is used to keep track of schema changes for our database. Think of it as &lt;em&gt;version control&lt;/em&gt;, but for your database schema. The Prisma CLI also creates your &lt;code&gt;dev.db&lt;/code&gt; database. &lt;/p&gt;

&lt;p&gt;After &lt;code&gt;prisma migrate&lt;/code&gt; is done creating a migration, the Prisma CLI installs &lt;a href="https://www.npmjs.com/package/@prisma/client" rel="noopener noreferrer"&gt;&lt;code&gt;@prisma/client&lt;/code&gt;&lt;/a&gt; package and &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-client/generating-prisma-client" rel="noopener noreferrer"&gt;generates Prisma Client&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.prisma.io/docs/concepts/components/prisma-client" rel="noopener noreferrer"&gt;Prisma Client&lt;/a&gt; is an auto-generated and type-safe query builder &lt;em&gt;tailored&lt;/em&gt; to your database schema. &lt;code&gt;prisma migrate&lt;/code&gt; will update your Prisma Client every time you run a migration.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The above features are now in &lt;a href="https://www.prisma.io/docs/about/releases#generally-available-ga" rel="noopener noreferrer"&gt;General Availability&lt;/a&gt; and your feedback will be highly appreciated and will help us keep improving them. 🙂&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 2: Add your &lt;code&gt;serverMiddleware&lt;/code&gt; endpoints
&lt;/h2&gt;

&lt;p&gt;For your API, you will use &lt;a href="https://expressjs.com" rel="noopener noreferrer"&gt;Express&lt;/a&gt; inside Nuxt to create our API. Sounds crazy, right?&lt;/p&gt;

&lt;p&gt;Well, Express is used to allow your API endpoints to access the request and response objects.&lt;/p&gt;

&lt;p&gt;Go ahead and install Express:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm &lt;span class="nb"&gt;install &lt;/span&gt;express


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Create an &lt;code&gt;api&lt;/code&gt; folder and an &lt;code&gt;index.js&lt;/code&gt; file that will contain your API handlers:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;mkdir &lt;/span&gt;api
&lt;span class="nb"&gt;touch &lt;/span&gt;api/index.js


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After creating your &lt;code&gt;/api/index.js&lt;/code&gt; file, paste in the following code in  &lt;code&gt;index.js&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PrismaClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@prisma/client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PrismaClient&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="cm"&gt;/** 
* logic for our api will go here
*/&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above code initializes Express and Prisma and exports two properties, &lt;code&gt;path&lt;/code&gt; and &lt;code&gt;handler&lt;/code&gt;, which will be registered in &lt;code&gt;nuxt.config.js&lt;/code&gt; in &lt;strong&gt;Step 3&lt;/strong&gt;. The &lt;code&gt;path&lt;/code&gt; property specifies the route the middleware will be accessible, and &lt;code&gt;handler&lt;/code&gt; specifies the function executed when invoked. For the rest of this step, you’ll be working in &lt;code&gt;index.js&lt;/code&gt; setting up the endpoints and their respective handlers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a &lt;code&gt;User&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The first feature you’ll be implementing is creating a user/ author. The database will be expecting an &lt;code&gt;email&lt;/code&gt; and an optional &lt;code&gt;name&lt;/code&gt;. It’s implementation is as follows:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/user`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Creating a &lt;code&gt;Post&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Next, you’ll add the create post endpoint. The request body will expect a &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;content&lt;/code&gt; and &lt;code&gt;authorEmail&lt;/code&gt;. If an author doesn’t exist in the database, their user record will be created.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/post&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;authorEmail&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;connectOrCreate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;authorEmail&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Get drafts
&lt;/h3&gt;

&lt;p&gt;Once that is done, you’ll need to be able to view all unpublished posts. Prisma lets you specify all relations you’d like to be returned in the response with the &lt;a href="https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#include" rel="noopener noreferrer"&gt;&lt;code&gt;include&lt;/code&gt;&lt;/a&gt; property. This is where you’ll add the &lt;code&gt;author&lt;/code&gt; &lt;a href="https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries" rel="noopener noreferrer"&gt;relation query&lt;/a&gt; to view the respective posts as well as their authors.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/drafts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findMany&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;published&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;include&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Get &lt;code&gt;Post&lt;/code&gt; by &lt;code&gt;Id&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;You can get a post by it’s id using &lt;a href="https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#findunique" rel="noopener noreferrer"&gt;&lt;code&gt;findUnique&lt;/code&gt;&lt;/a&gt; as follows:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/post/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findUnique&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;include&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Publish a &lt;code&gt;Post&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;When a &lt;code&gt;Post&lt;/code&gt; is ready to go live update the published field:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/publish/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;published&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Get Feed
&lt;/h3&gt;

&lt;p&gt;All your published posts can be made available on the &lt;code&gt;/feed&lt;/code&gt; endpoint, filtering them by checking that the &lt;code&gt;published&lt;/code&gt; property is set to &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/feed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findMany&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;published&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;include&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Deleting a &lt;code&gt;Post&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The last CRUD feature is deleting a &lt;code&gt;Post&lt;/code&gt; record in your database:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/post/:id`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The final feature in your application is filtering posts, checking if the &lt;code&gt;searchString&lt;/code&gt; is found in either the &lt;code&gt;title&lt;/code&gt; or &lt;code&gt;content&lt;/code&gt; of your Posts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Search for a &lt;code&gt;Post&lt;/code&gt;
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// index.js&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/filterPosts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;searchString&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;draftPosts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;prisma&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findMany&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;OR&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;searchString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;searchString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;draftPosts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 3: Modify &lt;code&gt;nuxt.config.js&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The last step is registering your &lt;code&gt;serverMiddleware&lt;/code&gt; in &lt;code&gt;nuxt.config.js&lt;/code&gt; which is as simple as this.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// nuxt.config.js&lt;/span&gt;
  &lt;span class="nx"&gt;serverMiddleware&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;~/api/index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 4: Take your API for a spin
&lt;/h2&gt;

&lt;p&gt;Once you’ve modified &lt;code&gt;nuxt.config.js&lt;/code&gt;, make sure to restart your Nuxt app. You can use &lt;a href="https://www.prisma.io/studio" rel="noopener noreferrer"&gt;Prisma Studio&lt;/a&gt; to create your database records. Alternatively, you can use your favorite API testing tool - for example &lt;a href="https://www.postman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt;,&lt;a href="https://www.insomnia.rest/" rel="noopener noreferrer"&gt;Insomnia&lt;/a&gt; or &lt;a href="https://marketplace.visualstudio.com/items?itemName=humao.rest-client" rel="noopener noreferrer"&gt;REST Client&lt;/a&gt; - to test your API by making &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP" rel="noopener noreferrer"&gt;HTTP&lt;/a&gt; requests against your API.&lt;/p&gt;

&lt;p&gt;In a new terminal window, use the Prisma CLI to startup Prisma Studio.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npx prisma studio


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The command opens Prisma studio on &lt;a href="https://localhost:5555" rel="noopener noreferrer"&gt;&lt;code&gt;localhost:5555&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_73BF466ED2B54D3E86B7180578C8C4A2F8A05295E5EBFE61FC0C76AB74FCDA16_1612939344654_image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_73BF466ED2B54D3E86B7180578C8C4A2F8A05295E5EBFE61FC0C76AB74FCDA16_1612939344654_image.png" alt="Prisma Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a couple of &lt;code&gt;User&lt;/code&gt; and &lt;code&gt;Post&lt;/code&gt; records on Prisma Studio and save your changes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_73BF466ED2B54D3E86B7180578C8C4A2F8A05295E5EBFE61FC0C76AB74FCDA16_1612939474651_image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_73BF466ED2B54D3E86B7180578C8C4A2F8A05295E5EBFE61FC0C76AB74FCDA16_1612939474651_image.png" alt="Prisma studio - creating users"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_73BF466ED2B54D3E86B7180578C8C4A2F8A05295E5EBFE61FC0C76AB74FCDA16_1612971944780_image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_73BF466ED2B54D3E86B7180578C8C4A2F8A05295E5EBFE61FC0C76AB74FCDA16_1612971944780_image.png" alt="Prisma Studio- creating posts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the post isn’t published yet, fetch a list of the drafted posts using the GET &lt;code&gt;api/drafts&lt;/code&gt; endpoint.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_73BF466ED2B54D3E86B7180578C8C4A2F8A05295E5EBFE61FC0C76AB74FCDA16_1612939657477_image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropbox.com%2Fs_73BF466ED2B54D3E86B7180578C8C4A2F8A05295E5EBFE61FC0C76AB74FCDA16_1612939657477_image.png" alt="Nuxt app - draft posts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations!  Your app is up and running! 🎉&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Nuxt is a great tool for building fullstack applications using Vue. Pair it up with Prisma and working with databases while building a fullstack app becomes less of a hassle. This enables a developer to focus on what matters, shipping features!&lt;/p&gt;

&lt;p&gt;Using Nuxt to build fullstack applications is great for prototyping. However, if you’d like to build bigger applications, it’s recommended to separate your frontend from the backend.&lt;/p&gt;

&lt;p&gt;I hope you liked this tutorial and learned something useful. The completed project is available on &lt;a href="https://github.com/ruheni/prisma-nuxt" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Do you have any suggestions of any libraries/frameworks you’d like to see paired with Prisma? Feel free to let us know in the discussion or create an &lt;a href="https://github.com/prisma/prisma-examples/issues" rel="noopener noreferrer"&gt;issue on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy hacking!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>nuxt</category>
      <category>vue</category>
      <category>express</category>
    </item>
  </channel>
</rss>
