0.0.33 • Published 5 years ago

@prisma/prototype v0.0.33

Weekly downloads
28
License
Apache-2.0
Repository
github
Last release
5 years ago

WebsiteDocsExamplesBlogSlackTwitterPrisma Framework (Preview)

CircleCI Slack Status Join the community on Spectrum

Prisma replaces traditional ORMs and simplifies database workflows:

  • Access: Type-safe database access with the auto-generated Prisma client (in JavaScript, TypeScript, Go)
  • Migrate: Declarative data modelling and migrations (optional)
  • Manage: Visual data management with Prisma Admin

It is used to build GraphQL, REST, gRPC APIs and more. Prisma currently supports MySQL, PostgreSQL, MongoDB.

A new version of Prisma, the Prisma Framework (initially called Prisma 2), is currently in Preview. It doesn't require a database proxy server and features a more modular architecture based on three tools: Photon.js, Lift and Studio. Follow the development of the Prisma Framework on: isprisma2ready.com.

Get started with the tutorial or some ready-to-run examples.

Contents

Quickstart

Get started with Prisma from scratch (or use your existing database):

1. Install Prisma via Homebrew

brew tap prisma/prisma
brew install prisma
npm install -g prisma
# or
yarn global add prisma

2. Connect Prisma to a database

To setup Prisma, you need to have Docker installed. Run the following command to get started with Prisma:

prisma init hello-world

If you don't want to use Docker to host the Prisma server as a database proxy, be sure to check out the new Prisma Framework which removes the need for the Prisma server.

The interactive CLI wizard now helps you with the required setup:

  • Select Create new database (you can also use an existing database or a hosted demo database)
  • Select the database type: MySQL or PostgreSQL
  • Select the language for the generated Prisma client: TypeScript, Flow, JavaScript or Go

Once the wizard has terminated, run the following commands to setup Prisma:

cd hello-world
docker-compose up -d

3. Define your datamodel

Edit datamodel.prisma to define your datamodel using SDL syntax. Each model is mapped to a table in your database schema:

type User {
  id: ID! @id
  email: String @unique
  name: String!
  posts: [Post!]!
}

type Post {
  id: ID! @id
  title: String!
  published: Boolean! @default(value: false)
  author: User
}

4. Deploy datamodel & migrate database

To deploy your Prisma API, run the following command:

prisma deploy

The Prisma API is deployed based on the datamodel and exposes CRUD & realtime operations for each model in that file.

5. Use the Prisma client (Node.js)

The Prisma client connects to the Prisma API and lets you perform read and write operations against your database. This section explains how to use the Prisma client from Node.js.

Inside the hello-world directory, install the prisma-client-lib dependency:

npm install --save prisma-client-lib

To generate the Prisma client, run the following command:

prisma generate

Create a new Node script inside the hello-world directory:

touch index.js

Add the following code to it:

const { prisma } = require('./generated/prisma-client')

// A `main` function so that we can use async/await
async function main() {
  // Create a new user with a new post
  const newUser = await prisma.createUser({
    name: 'Alice',
    posts: {
      create: { title: 'The data layer for modern apps' }
    }
  })
  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)

  // Read all posts from the database and print them to the console
  const allPosts = await prisma.posts()
  console.log(allPosts)
}

main().catch(e => console.error(e))

Finally, run the code using the following command:

node index.js
const usersCalledAlice = await prisma
  .users({
    where: {
      name: "Alice"
    }
  })
// replace the __USER_ID__ placeholder with an actual user ID
const updatedUser = await prisma
  .updateUser({
    where: { id: "__USER_ID__" },
    data: { email: "alice@prisma.io" }
  })
// replace the __USER_ID__ placeholder with an actual user ID
const deletedUser = await prisma
  .deleteUser({ id: "__USER_ID__" })
const postsByAuthor = await prisma
  .user({ email: "alice@prisma.io" })
  .posts()

6. Next steps

Here is what you can do next:

Examples (Prisma 1)

You can find the examples for the Prisma Framework (originally called Prisma 2) here. These example are based on the new Prisma tools: Photon.js and Lift.

TypeScript

DemoDescription
scriptSimple usage of Prisma client in script
graphqlSimple GraphQL server based on graphql-yoga
graphql-apollo-serverSimple GraphQL server based on apollo-server
graphql-crudGraphQL server with full CRUD API
graphql-authGraphQL server with email-password authentication & permissions
graphql-subscriptionsGraphQL server with realtime subscriptions
rest-expressSimple REST API with Express.JS
grpcSimple gRPC API
docker-mongodbSet up Prisma locally with MongoDB
docker-mysqlSet up Prisma locally with MySQL
docker-postgresSet up Prisma locally with PostgreSQL
cli-appSimple CLI TODO list app

Node.js

DemoDescription
scriptSimple usage of Prisma client in script
graphqlSimple GraphQL server
graphql-authGraphQL server with email-password authentication & permissions
graphql-subscriptionsGraphQL server with realtime subscriptions
rest-expressSimple REST API with Express.JS
grpcSimple gRPC API
docker-mongodbSet up Prisma locally with MongoDB
docker-mysqlSet up Prisma locally with MySQL
docker-postgresSet up Prisma locally with PostgreSQL
cli-appSimple CLI TODO list app

Golang

DemoDescription
cli-appSimple CLI TODO list app
graphqlSimple GraphQL server
http-muxSimple REST API with gorilla/mux
rest-ginSimple REST API with Gin
scriptSimple usage of Prisma client in script

Flow

DemoDescription
graphqlSimple GraphQL server
scriptSimple usage of Prisma client in script

Database Connectors

Database connectors provide the link between Prisma and the underlying database.

You can connect the following databases to Prisma already:

  • MySQL
  • PostgreSQL
  • MongoDB

Upcoming Connectors

Prisma 1 is currently in maintenance mode in favor of the development of the Prisma Framework. The data source connectors listed below will be implemented in the scope of the Prisma Framework instead of Prisma 1.

If you are interested to participate in the preview for one of the following connectors, please reach out in our Slack.

Join the discussion or contribute to influence which we'll work on next!

Community

Prisma has a community of thousands of amazing developers and contributors. Welcome, please join us! 👋

Channels

Events

Resources

Prisma Framework Preview

Prisma 2 splits up Prisma's core functionality into 2 standalone tools:

  • Photon: Type-safe database access
  • Lift: Declarative data modeling and migrations

Photon and Lift are currently in Preview! Get started with the tutorial or some ready-to-run examples.

You can track the progress of Prisma 2 on isprisma2ready.com or follow the development of the technical specification.

0.0.33

5 years ago

0.0.32

5 years ago

0.0.31

5 years ago

0.0.30

5 years ago

0.0.29

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago