<?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: Daniel Norman</title>
    <description>The latest articles on DEV Community by Daniel Norman (@2color).</description>
    <link>https://dev.to/2color</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%2Fuser%2Fprofile_image%2F280152%2F7be0b2f5-0d61-43b9-8728-8fba872573a6.jpeg</url>
      <title>DEV Community: Daniel Norman</title>
      <link>https://dev.to/2color</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/2color"/>
    <language>en</language>
    <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>Productive Development With Prisma’s Zero-Cost Type Safety</title>
      <dc:creator>Daniel Norman</dc:creator>
      <pubDate>Tue, 09 Jun 2020 11:49:39 +0000</pubDate>
      <link>https://dev.to/prisma/productive-development-with-prisma-s-zero-cost-type-safety-4od2</link>
      <guid>https://dev.to/prisma/productive-development-with-prisma-s-zero-cost-type-safety-4od2</guid>
      <description>&lt;p&gt;Handling data is at the core of web applications and comes with many challenges as data travels through different components of the application's code.&lt;br&gt;
In this article, we'll look at &lt;a href="https://prisma.io/" rel="noopener noreferrer"&gt;Prisma&lt;/a&gt;'s zero-cost type safety and how it boosts productivity and increases developer confidence in applications that use a relational database.&lt;/p&gt;
&lt;h2&gt;
  
  
  The journey of data in web applications
&lt;/h2&gt;

&lt;p&gt;If you've been building web applications, there's a good chance you've spent a lot of your time handling data. As a developer, your concerns with data start in the UI, as users enter information or interact in a way that creates information. This is where the long data journey begins. The journey usually ends in a database; from which it may go on many more journeys as it's fetched, updated, and saved again.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 The post assumes a basic understanding of relational databases and full-stack development.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In a typical &lt;a href="https://en.wikipedia.org/wiki/Multitier_architecture" rel="noopener noreferrer"&gt;three-tier&lt;/a&gt; application, the journey looks as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The data is sent via HTTP from the user's browser by the frontend to the backend server (or a serverless function) via an API, for example a GraphQL or REST API.&lt;/li&gt;
&lt;li&gt;The backend finds the matching route and its handler.&lt;/li&gt;
&lt;li&gt;The backend authenticates the user, deserializes the data, and validates the data.&lt;/li&gt;
&lt;li&gt;The route handler applies business logic to the data.&lt;/li&gt;
&lt;li&gt;The database access layer is used to safely store the data in the database.&lt;/li&gt;
&lt;/ol&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%2Fi%2F3vh16hr6vo2w3h4scf9a.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%2Fi%2F3vh16hr6vo2w3h4scf9a.png" alt="Typical architecture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each of the components that the data moves through may manipulate and transform the data. With JavaScript, there's a common problem when multiple components interact with data: &lt;em&gt;type errors&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures" rel="noopener noreferrer"&gt;type&lt;/a&gt; error is an error that occurs when a value in an operation is of a different type from what the code expects.&lt;/p&gt;

&lt;p&gt;For example, a function that concatenates the first and last name of a user object may run into a type error:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getFullName&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&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="nx"&gt;firstName&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="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calling the function without passing in a parameter raises a type error:&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="nf"&gt;getFullName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Uncaught TypeError: Cannot read property 'firstName' of undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calling the function with an object missing the properties will &lt;em&gt;not&lt;/em&gt; raise an error:&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="nf"&gt;getFullName&lt;/span&gt;&lt;span class="p"&gt;({})&lt;/span&gt; &lt;span class="c1"&gt;// "undefined undefined"&lt;/span&gt;

&lt;span class="nf"&gt;getFullName&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Shakuntala&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="c1"&gt;// "Shakuntala undefined"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is due to JavaScript's ability to convert types during runtime. In this case, &lt;code&gt;undefined&lt;/code&gt; is converted to &lt;code&gt;string&lt;/code&gt;. This feature is known as &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion" rel="noopener noreferrer"&gt;implicit type coercion&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With JavaScript, these errors occur at runtime. In practice, this means that these errors are discovered during testing or after the application has been deployed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Type safety with TypeScript
&lt;/h3&gt;

&lt;p&gt;In recent years, TypeScript became popular amongst JavaScript developers as a typed language that compiles to JavaScript. One of the main benefits that TypeScript offers is the ability to detect type errors at compile time which increases confidence in the applications you're building.&lt;/p&gt;

&lt;p&gt;For example, we can define the &lt;code&gt;getFullName&lt;/code&gt; function from above as follows:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getFullName&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="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;firstName&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="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&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="s2"&gt;`&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="nx"&gt;firstName&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="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastName&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="nf"&gt;getFullName&lt;/span&gt;&lt;span class="p"&gt;({})&lt;/span&gt; &lt;span class="c1"&gt;// Type error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the call below the function definition is invalid, the error will be caught when the TypeScript compiler is run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;tsc example.ts

example.ts:5:13 - error TS2345: Argument of &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="s1"&gt;'{}'&lt;/span&gt; is not assignable to parameter of &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="s1"&gt;'{ firstName: string; lastName: number; }'&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
  Type &lt;span class="s1"&gt;'{}'&lt;/span&gt; is missing the following properties from &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="s1"&gt;'{ firstName: string; lastName: number; }'&lt;/span&gt;: firstName, lastName

5 getFullName&lt;span class="o"&gt;({})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Benefits of TypeScript aside, when comparing TypeScript to JavaScript, it comes at a cost of defining types which often reduces productivity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changing data and type errors
&lt;/h2&gt;

&lt;p&gt;Type errors are especially common during rapid development and prototyping where introducing new features requires changes to the structure of the data.&lt;/p&gt;

&lt;p&gt;For example, a blog may have the concept of &lt;code&gt;Users&lt;/code&gt; and &lt;code&gt;Posts&lt;/code&gt;, whereby, an &lt;code&gt;author&lt;/code&gt; can have many &lt;code&gt;posts&lt;/code&gt;. Typically, each of these two entities would have a structure like in the following diagram:&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%2Fi%2Ftnrb57bgpwp6jf0weup8.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%2Fi%2Ftnrb57bgpwp6jf0weup8.png" alt="Database schema"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you decide to rename the &lt;code&gt;name&lt;/code&gt; field to &lt;code&gt;firstName&lt;/code&gt; and add a &lt;code&gt;lastName&lt;/code&gt; field you will need to update the database schema. But once the database schema has been migrated (updated to have a new structure), the backend may fail as its queries still point to the &lt;code&gt;name&lt;/code&gt; field which doesn't exist.&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%2Fi%2Fkofujzkq4yyo37r4odo0.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%2Fi%2Fkofujzkq4yyo37r4odo0.png" alt="Altered database schema"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This kind of change is called a &lt;em&gt;schema migration&lt;/em&gt;, and there are many ways to deal with such changes. For example, the naive approach could look as follows:&lt;/p&gt;

&lt;p&gt;You schedule a maintenance window and use the time before to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Update the backend code to use the new field.&lt;/li&gt;
&lt;li&gt;Migrate the database schema in a test environment.&lt;/li&gt;
&lt;li&gt;Test the updated backend with the migrated database schema.&lt;/li&gt;
&lt;li&gt;If the testing succeeds, use the maintenance window to take down the old version of the backend, migrate the database schema, and then deploy the updated backend.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One of the problems with this approach (besides having to take the service down) is that updating the code to use the new field is a manual process. Because code accessing the old &lt;code&gt;name&lt;/code&gt; field is still &lt;em&gt;syntactically valid&lt;/em&gt;, type errors will happen when the code runs. Specifically, no error will be thrown, as accessing undefined fields doesn't throw a &lt;code&gt;TypeError&lt;/code&gt; like in the &lt;code&gt;getFullName&lt;/code&gt; example above.&lt;/p&gt;

&lt;p&gt;Adapting the code to the new schema can be done in a couple of ways, which can be combined:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manually searching the code for all occurrences of &lt;code&gt;name&lt;/code&gt; and adjusting them to work with the schema change.&lt;/li&gt;
&lt;li&gt;With unit and integration tests. You can start the process by creating new tests to describe the expected behavior after the change. The tests fail initially and as the code is updated, they gradually pass as the code is adapted to make use of the new fields.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Depending on how you're accessing your database, either approach can be a cumbersome task. With an SQL query builder like &lt;a href="https://knexjs.org/" rel="noopener noreferrer"&gt;knex.js&lt;/a&gt;, you have to search for queries using the old &lt;code&gt;name&lt;/code&gt; field and update them. With ORMs, you typically have to update the &lt;code&gt;User&lt;/code&gt; model and ensure the model isn't used to access or manipulate the old &lt;code&gt;name&lt;/code&gt; field.&lt;/p&gt;

&lt;p&gt;In an application using knex.js, the change looks as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;const user = await db('users')
&lt;/span&gt;&lt;span class="gd"&gt;-  .select('userId', 'name', 'twitter', 'email)
&lt;/span&gt;&lt;span class="gi"&gt;+  .select('userId', 'firstName', 'lastName', 'twitter', 'email)
&lt;/span&gt;  .where({
    userId: requestedUserId
  })
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;await db('users')
&lt;/span&gt;  .where({ userId: userIdToUpdate })
&lt;span class="gd"&gt;-  .update({ name: newName })
&lt;/span&gt;&lt;span class="gi"&gt;+  .update({ firstName: newFirstName, lastName: newLastName })
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The challenge here, irrespective of the specific database abstraction is that you need to coordinate changes between the database and your codebase.&lt;/p&gt;

&lt;p&gt;Prisma approach eases the coordination work between the codebase and the database schema.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prisma – modern database toolkit
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/prisma/prisma2" rel="noopener noreferrer"&gt;Prisma 2&lt;/a&gt; is an open-source database toolkit that was built with the benefits of type safety in mind.&lt;/p&gt;

&lt;p&gt;In this post, we'll look at Prisma Client, the toolkit's type-safe database client for Node.js and TypeScript.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 Prisma Migrate, the Prisma's migration tool is currently in an experimental state. For more information, check out the &lt;a href="https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-migrate" rel="noopener noreferrer"&gt;Prisma Migrate documentation&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Prisma is database agnostic and supports different databases including PostgreSQL, MySQL, and SQLite.&lt;/p&gt;

&lt;p&gt;The generated Prisma Client is in TypeScript, which makes type safety possible. **The good news is that you can reap some of the rewards of type safety in a Node.js application written in JavaScript without having to invest the time defining types for the database layer.&lt;/p&gt;

&lt;p&gt;Moreover, Prisma can serve as a gateway to a deeper understanding of TypeScript's benefits.&lt;/p&gt;

&lt;h2&gt;
  
  
  Schema centric workflow
&lt;/h2&gt;

&lt;p&gt;Prisma uses the &lt;a href="https://github.com/prisma/prisma2/blob/master/docs/prisma-schema-file.md" rel="noopener noreferrer"&gt;Prisma schema&lt;/a&gt; as a declarative and typed schema for your database. It serves as the source of truth for both the database and the client, which is auto-generated from the Prisma schema. The Prisma schema is just another representation of your database. For the example above, the corresponding Prisma schema would look as follows:&lt;br&gt;
&lt;/p&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;@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="nd"&gt;@id&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;twitter&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;postId&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="n"&gt;autoincrement&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="nd"&gt;@id&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;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;p&gt;Prisma supports different workflows depending on whether you are starting from scratch or with an existing database.&lt;/p&gt;

&lt;p&gt;Assuming you have a database schema already defined (with SQL or with a migration tool), Prisma's workflow looks as follows from a high level:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You introspect the database using the Prisma CLI which creates the Prisma schema.&lt;/li&gt;
&lt;li&gt;You use the CLI to generate the Prisma Client (which uses the Prisma schema as a representation of the database schema). You get a node module that is tailored to your database schema.&lt;/li&gt;
&lt;/ol&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%2Fimgur.com%2FToNkpb2.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%2Fimgur.com%2FToNkpb2.png" alt="Introspection workflow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the database introspected and the Prisma Client generated, you can now use Prisma Client as follows:&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="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="c1"&gt;// An example route handler for POST /api/user&lt;/span&gt;
&lt;span class="c1"&gt;// Required fields in body: name, email&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handle&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="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;name&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="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;createdUser&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="nx"&gt;name&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="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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;createdUser&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="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;createdUser&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="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;createdUser&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="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 appeal of generated Prisma Client (as imported from &lt;code&gt;@prisma/client&lt;/code&gt;) is that all input parameters and return values of the &lt;code&gt;prisma&lt;/code&gt; methods are fully typed. For example, in VSCode you can right-click on &lt;code&gt;createdUser&lt;/code&gt; and &lt;code&gt;Go to Type Definition&lt;/code&gt; which will lead to the generated TypeScript code:&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
  &lt;span class="na"&gt;twitter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because of those types, it's possible for tooling, e.g. code editors and CLI tools to do a lot of checks behind the scenes and provide you with actionable feedback as you're writing code. For example, trying to access a non-existent field, e.g. &lt;code&gt;createdUser.address&lt;/code&gt; would be quickly detectable and could be notified.&lt;/p&gt;

&lt;p&gt;With a better understanding of the relationship between the database schema, the Prisma schema, and the generated Prisma Client, let's look at the tools that provide such actionable feedback with JavaScript by using the generated types behind the scenes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Productive and safe development with zero-cost type safety
&lt;/h2&gt;

&lt;p&gt;The benefits of type safety can be had at zero cost in a project using JavaScript with Prisma. This means that you become more confident in your code without any additional effort.&lt;/p&gt;

&lt;p&gt;There are several levels to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Level 1: Autocomplete suggestions
&lt;/h3&gt;

&lt;p&gt;The first example of zero-cost type safety is the way that VSCode IntelliSense suggestions pop up as you type:&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%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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F87cdnxf4y98g7rflagx5.gif" alt="autocomplete in action"&gt;&lt;/a&gt;&lt;/p&gt;
Autocomplete in action



&lt;p&gt;The generated &lt;code&gt;@prisma/client&lt;/code&gt; is a CRUD API that is tailored to your database schema and is fully typed in TypeScript. This allows VSCode's IntelliSense to give typed autocomplete suggestions during development.&lt;/p&gt;




&lt;h3&gt;
  
  
  Level 2: Type safety validations in VSCode
&lt;/h3&gt;

&lt;p&gt;Suggestions are a nice feature that improves productivity and reduces juggling between reading documentation and coding. You can get errors –the same way linters work in VSCode– when your code uses the Prisma API in unintended ways, thereby violating types.&lt;/p&gt;

&lt;p&gt;Add &lt;code&gt;// @ts-check&lt;/code&gt; to the top of JavaScript files which use the Prisma Client. VSCode will run your code through the TypeScript compiler and report back errors:&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%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%2Fi.imgur.com%2F3lYgGAI.gif" alt="With @ts-check the input parameters  raw `prisma.user.create()` endraw  and the return value  raw `createdUser` endraw  are typed and match the DB field types"&gt;&lt;/a&gt;&lt;/p&gt;
With @ts-check the input parameters `prisma.user.create()` and the return value `createdUser` are typed and match the DB field types



&lt;p&gt;If you narrow the returned fields with &lt;a href="https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/field-selection#select" rel="noopener noreferrer"&gt;&lt;code&gt;select&lt;/code&gt;&lt;/a&gt; in the call to &lt;code&gt;prisma.user.create()&lt;/code&gt; the returned &lt;code&gt;createdUser&lt;/code&gt; will be typed accordingly:&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%2Fi.imgur.com%2FQ5FxuMJ.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%2Fi.imgur.com%2FQ5FxuMJ.gif" alt="Selection set and type errors when accessing unselected fields"&gt;&lt;/a&gt;&lt;/p&gt;
Selection set and type errors when accessing unselected fields



&lt;p&gt;For this to work enable syntax checking in VSCode:&lt;/p&gt;

&lt;p&gt;Set &lt;code&gt;javascript.validate.enable&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt; in your VSCode configuration:&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;"javascript.validate.enable"&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="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;While this provides valuable feedback during development, nothing stops you from committing or deploying code with errors. This is where automated type checks can be useful.&lt;/p&gt;




&lt;h3&gt;
  
  
  Level 3: Automated type checks in CI
&lt;/h3&gt;

&lt;p&gt;In a similar fashion to how VSCode runs the TypeScript compiler for type checks, you can run the type checks in your CI or as a commit hook.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the TypeScript compiler as a development dependency:
&lt;/li&gt;
&lt;/ol&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;typescript &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Run the TypeScript compiler:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx tsc &lt;span class="nt"&gt;--noEmit&lt;/span&gt; &lt;span class="nt"&gt;--allowJs&lt;/span&gt; &lt;span class="nt"&gt;--checkJs&lt;/span&gt; pages/api/&lt;span class="k"&gt;*&lt;/span&gt;.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run as a commit hook:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/typicode/husky" rel="noopener noreferrer"&gt;Husky&lt;/a&gt; allows you to define commit hooks in your &lt;code&gt;package.json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can install Husky:&lt;br&gt;
&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;husky &lt;span class="nt"&gt;--save-dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And add the hook:&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="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;package.json&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"husky"&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;"hooks"&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;"pre-commit"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"tsc --noEmit --allowJs --checkJs pages/api/*.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;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;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Type errors are a common problem in JavaScript and because they are noticed at runtime, detecting can be difficult without rigorous testing. When working with data that travels through many components and a database, the risk associated with such type errors increases.&lt;/p&gt;

&lt;p&gt;TypeScript's type safety alleviate some of those risks but come at a cost of learning TypeScript and defining types upfront.&lt;/p&gt;

&lt;p&gt;In applications that rapidly change to accommodate for new features, the database schema must be adapted with schema migrations and in turn the application code.&lt;/p&gt;

&lt;p&gt;Having to manually manage such migrations can be error-prone and cumbersome, which reduces the ability to iterate on an application quickly without introducing errors.&lt;/p&gt;

&lt;p&gt;Prisma addresses these challenges with a &lt;strong&gt;schema centric workflow&lt;/strong&gt; and an &lt;strong&gt;auto-generated TypeScript database client&lt;/strong&gt;. These features make for a pleasant developer experience as they boost productivity and increase confidence, with autocompletion and automated type checks during build time.&lt;/p&gt;

&lt;p&gt;These benefits come at zero cost because as a developer you are not required to take any extra precautions or steps to benefit from type safety using Prisma. Most importantly, all of this is available in projects written exclusively in JavaScript.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>database</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
