Goals
On this page, you will learn how to:
- Install the Prisma CLI
- Set up Prisma with a sandboxed demo database
- Read and write data using the Prisma client
Install the Prisma CLI
The Prisma CLI is used for various Prisma workflows. You can install it using Homebrew or NPM:
brew tap prisma/prisma brew install prisma
Copy
Set up Prisma
To bootstrap the configuration files for your Prisma setup, create a new directory and initalize it using the prisma init
command:
mkdir hello-world cd hello-world prisma init
Copy
After running prisma init
, the Prisma CLI prompts you to select how you want to deploy Prisma:
- Select Demo server from the list.
- When your browser opens, register with Prisma Cloud. This is needed because that's where the Demo server is hosted.
- Go back to your terminal.
Confirm the suggested values for the following questions by hitting Return:
- The region where Prisma service should be hosted
- The name for Prisma service
- The stage for Prisma service
- Select Prisma TypeScript Client to generate Prisma client for TypeScript.
prisma.yml
: The root configuration file for your Prisma setup.datamodel.prisma
: Specifies the datamodel for your application that will be mapped to the database (it basically defines your database schema).generated/
: Contains the generated source files for the Prisma TypeScript client.
Deploy Prisma
The interactive wizard created the minimal Prisma configuration based on a hosted demo database: prisma.yml
and datamodel.prisma
. Prisma now needs to be deployed so you can use the Prisma API:
prisma deploy
Copy
Congratulations, you have successfully deployed Prisma. You now have a free and hosted demo database (AWS Aurora) available in Prisma Cloud and are ready to use the Prisma client to read and write to it from your code.
View and edit your data in Prisma Admin
If you want to view and edit the data in your database, you can use Prisma Admin. To access Prisma Admin, you need to append /_admin
to your Prisma endpoint, for example: https://eu1.prisma.sh/alice/helloworld/dev/_admin
.
Prepare TypeScript application
Create your tsconfig.json
with the following command:
touch tsconfig.json
Copy
Add the following configuration to the tsconfig.json
file:
{ "compilerOptions": { "lib": ["es2016", "esnext.asynciterable"] } }
Copy
Next, initialize an empty NPM project in the current directory and install the required dependencies:
npm init -y npm install --save prisma-client-lib npm install --save-dev typescript ts-node
Copy
Almost done! Run the following command to create an empty TypeScript script:
touch index.ts
Copy
Great, you're now ready to write some code and talk to your database programmatically!
Read and write data using the Prisma client
Add the following code in index.ts
:
import { prisma } from './generated/prisma-client' // A `main` function so that we can use async/await async function main() { // Create a new user called `Alice` const newUser = await prisma.createUser({ name: 'Alice' }) console.log(`Created new user: ${newUser.name} (ID: ${newUser.id})`) // Read all users from the database and print them to the console const allUsers = await prisma.users() console.log(allUsers) } main().catch(e => console.error(e))
Copy
Before executing the code, go ahead and add a start
script to package.json
so you can comfortably run the code:
{ "name": "hello-world", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "ts-node index.ts" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "graphql": "^14.0.2", "prisma-client-lib": "^1.20.0" }, "devDependencies": { "ts-node": "^7.0.1", "typescript": "^3.1.6" } }
Copy
Now execute the script with the following command:
npm run start
Copy
Whenever you run the script with that command, a new user record is created in the demo database (because of the call to createUser
).
Feel free to play around with the Prisma client API and try out some of the following operations by adding the following code snippets to the file (at the end of the main
function) and re-executing the script:
const user = await prisma.user({ id: '__USER_ID__' })
Copy
In some snippets, you need to replace the
__USER__ID__
placeholder with the ID of an actual user.