Back to Prisma1

01 Setting Up Prisma New Database A002

docs/1.15/get-started/01-setting-up-prisma-new-database-a002.mdx

1.34.124.4 KB
Original Source

import QueryChooser from 'components/Markdown/QueryChooser' import Code from 'components/Markdown/Code'

export const meta = { title: 'Setting up Prisma', gettingStartedTitle: 'New database', position: 1, gettingStartedOrder: 2, nextText: 'Great work! 👏 Move on to learn how you can extend your data model and make changes to your Prisma API.' }

Goals

On this page, you will learn how to:

  • Install the Prisma CLI
  • Start a local Prisma server using Docker
  • Create a Prisma service configuration
  • Deploy a Prisma service to the local Prisma server
  • Explore the Prisma API in a GraphQL Playground

Install the Prisma CLI

The Prisma CLI is used to deploy and manage Prisma services. You can install it using NPM:

bash
npm install -g prisma

Install Docker

To use Prisma locally, you need to have Docker installed on your machine. If you don't have Docker yet, you can download the Docker Community Edition for your operating system here.

Set up database and Prisma server

Create Docker Compose file

To launch a Prisma server on your machine, you need a Docker Compose file that configures the Prisma server and specifies the database it can connect to.

bash
touch docker-compose.yml

Add Prisma and database Docker images

Paste the following contents into the Docker Compose file you just created:

<Code languages={["MySQL", "PostgreSQL"]}>

yml
version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.15
    restart: always
    ports:
    - "4466:4466"
    environment:
      PRISMA_CONFIG: |
        port: 4466
        databases:
          default:
            connector: mysql
            host: mysql
            port: 3306
            user: root
            password: prisma
            migrations: true
  mysql:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: prisma
    volumes:
      - mysql:/var/lib/mysql
volumes:
  mysql: ~
yml
version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.15
    restart: always
    ports:
    - "4466:4466"
    environment:
      PRISMA_CONFIG: |
        port: 4466
        databases:
          default:
            connector: postgres
            host: postgres
            port: 5432
            user: prisma
            password: prisma
            migrations: true
  postgres:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: prisma
    volumes:
      - postgres:/var/lib/postgresql/data
volumes:
  postgres: ~
</Code>

You can switch between MySQL and PostgreSQL by using the tabs above the code block.

Start database and Prisma server

To start the Prisma server and launch the connected database, run the following command:

bash
docker-compose up -d

Create a Prisma service

To bootstrap the service configuration files for your first Prisma service, create a new directory and initalize it using the prisma init command:

bash
mkdir hello-world
cd hello-world
prisma init --endpoint http://localhost:4466

Deploy the Prisma service

The prisma init command created the minimal service configuration needed to deploy a Prisma service: prisma.yml and datamodel.graphql.

This service configuration now needs to be deployed so you can use the Prisma API of your service:

bash
prisma deploy

Explore the Prisma API in a Playground

The Prisma API of your service exposes CRUD and realtime operations for the User type defined in datamodel.graphql. You can explore the API in a GraphQL Playground using the following command:

bash
prisma playground

Here are a few sample queries and mutations you can send to explore the API.

<QueryChooser titles={["Create a new User", "Query all Users", "Update a User's name", "Delete a User"]}>

graphql
mutation {
  createUser(data: {
    name: "Alice"
  }) {
    id
  }
}
graphql
query {
  users {
    id
    name
  }
}
graphql
mutation {
  updateUser(
    where: { id: "__USER_ID__" },
    data: { name: "Sarah" }
  ) {
    id
    name
  }
}
graphql
mutation {
  deleteUser(where: {
    id: "__USER_ID__"
  }) {
    id
    name
  }
}
</QueryChooser>