Back to Prisma1

01 Setting Up Prisma Demo Server GO G001

docs/1.23/get-started/01-setting-up-prisma-demo-server-GO-g001.mdx

1.34.124.8 KB
Original Source

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

export const meta = { title: 'Set up Prisma', position: 1, gettingStartedOrder: 3, gettingStartedTitle: 'Demo database', nextText: 'Great work! 👏 Move on to learn how you can change your datamodel and regenerate your Prisma client.', technology: 'go', technologyOrder: 4, articleGroup: 'Set up Prisma', }

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 Go client

Install the Prisma CLI

The Prisma CLI is used for various Prisma workflows. You can install it using Homebrew or NPM:

<Code languages={["Homebrew", "NPM"]}>

bash
brew tap prisma/prisma
brew install prisma
bash
npm install -g prisma
</Code>

Set up Prisma

To bootstrap the configuration files for your Prisma setup, run the prisma init command in your GOPATH:

bash
mkdir hello-world
cd hello-world
prisma init

After running prisma init, the Prisma CLI prompts you to select how you want to deploy Prisma:

  1. Select Demo server from the list.
  2. When your browser opens, register with Prisma Cloud. This is needed because that's where the Demo server is hosted.
  3. Go back to your terminal.
  4. Confirm the suggested values for the following questions with Return:
    1. The region where Prisma service should be hosted
    2. The name for Prisma service
    3. The stage for Prisma service
  5. Select Prisma Go Client to generate Prisma client for Golang.
<Collapse title="Learn about the generated files">
  • 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 Go client.
</Collapse>

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:

bash
prisma deploy

Congratulations, you have successfully deployed Prisma. You can now start using the Prisma client to talk to your database from code.

Prepare Go application

bash
touch index.go

You'll be using dep for dependency management in this tutorial. Run the following command to create the required setup:

bash
dep init

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

go
package main

import (
	"context"
	"fmt"
	prisma "hello-world/generated/prisma-client"
)

func main() {
	client := prisma.New(nil)
	ctx := context.TODO()

	// Create a new user
	name := "Alice"
	newUser, err := client.CreateUser(prisma.UserCreateInput{
		Name: name,
	}).Exec(ctx)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Created new user: %+v\\n", newUser)

	users, err := client.Users(nil).Exec(ctx)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%+v\\n", users)
}

Before executing the script, you need to ensure all dependencies are available. Run the following command:

bash
dep ensure

Now execute the script with the following command:

bash
go run index.go

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:

<QueryChooser titles={["Fetch single user", "Filter user list", "Update a user's name", "Delete user"]}>

go
id := "__USER_ID__"
userById, err := client.User(prisma.UserWhereUniqueInput{
  ID: &id,
}).Exec(ctx)
go
filter := "Alice"
posts, err := client.Users(&prisma.UsersParams{
  Where: &prisma.UserWhereInput{
    Name: &filter,
  },
}).Exec(ctx)
go
id := "__USER_ID__"
newName := "Bob"
updatedUser, err := client.UpdateUser(prisma.UserUpdateParams{
  Where: prisma.UserWhereUniqueInput{
    ID: &id,
  },
  Data: prisma.UserUpdateInput{
    Name: &newName,
  },
}).Exec(ctx)
go
id := "__USER_ID__"
deletedUser, err := client.DeleteUser(prisma.UserWhereUniqueInput{
  ID: &id,
}).Exec(ctx)
</QueryChooser>

In some snippets, you need to replace the __USER__ID__ placeholder with the ID of an actual user.