Skip to content
/ prisma1 Public
forked from prisma/prisma1

πŸ’Ύ Database Tools incl. ORM, Migrations and Admin UI (Postgres, MySQL & MongoDB) [deprecated]

License

Notifications You must be signed in to change notification settings

jw/prisma1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Prisma

Website β€’ Docs β€’ Blog β€’ Forum β€’ Slack β€’ Twitter β€’ OSS β€’ Learn

CircleCI Slack Status npm version

Prisma the data layer for modern applications. It replaces traditional ORMs and data access layers with a universal database abstraction used via the Prisma client. Prisma is to build GraphQL servers, REST APIs & more.

  • Prisma client for various languages such as JavaScript, TypeScript,Flow, Go.
  • Supports multiple databases such as MySQL, PostgreSQL, MongoDB (see all supported databases).
  • Type-safe database access including filters, aggregations, pagination and transactions.
  • Realtime event systems for your database to get notified about database events.
  • Declarative data modeling & migrations (optional) with simple SDL syntax.

Contents

Quickstart

1. Install Prisma via Homebrew

brew install prisma
brew tap prisma/prisma
Alternative: Install with NPM or Yarn
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

The interactice 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 data model

Edit datamodel.prisma to define your data model using SDL syntax:

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

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

Each model is mapped to a table in your database schema.

4. Deploy your Prisma API

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 (JavaScript)

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

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

touch index.js

Now add the following code to it:

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

// 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
See more API operations

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

Examples

Collection of Prisma example projects πŸ’‘

You can also check the AirBnB clone example we built as a fully-featured demo app for Prisma.

Architecture

Prisma takes the role of a data access layer in your backend architecture by connecting your API server to your databases. It enables a layered architecture which leads to better separation of concerns and improves maintainability of the entire backend.

Acting as a GraphQL database proxy, Prisma provides a GraphQL-based abstraction for your databases enabling you to read and write data with GraphQL queries and mutations. Using Prisma bindings, you can access Prisma's GraphQL API from your programming language.

Prisma servers run as standalone processes which allows for them to be scaled independently from your API server.

Is Prisma an ORM?

Prisma provides a mapping from your API to your database. In that sense, it solves similar problems as conventional ORMs. The big difference between Prisma and other ORMs is how the mapping is implemented.

Prisma takes a radically different approach which avoids the shortcomings and limitations commonly experienced with ORMs. The core idea is that Prisma turns your database into a GraphQL API which is then consumed by your API server (via GraphQL binding). While this makes Prisma particularly well-suited for building GraphQL servers, it can definitely be used in other contexts as well.

Here is how Prisma compares to conventional ORMs:

  • Expressiveness: Full flexibility thanks to Prisma's GraphQL API, including relational filters and nested mutations.
  • Performance: Prisma uses various optimization techniques to ensure top performance in complex scenarios.
  • Architecture: Using Prisma enables a layered and clean architecture, allowing you to focus on your API layer.
  • Type safety: Thanks to GraphQL's strong type system you're getting a strongly typed API layer for free.
  • Realtime: Out-of-the-box support for realtime updates for all events happening in the database.

Database Connectors

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

You can connect the following databases to Prisma already:

  • MySQL
  • Postgres
  • MongoDB (alpha)

More database connectors will follow.

Upcoming Connectors

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

Further Connectors

We are still collecting use cases and feedback for the API design and feature set of the following connectors:

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

GraphQL API

The most important component in Prisma is the GraphQL API:

  • Query, mutate & stream data via a auto-generated GraphQL CRUD API
  • Define your data model and perform migrations using GraphQL SDL

Prisma's auto-generated GraphQL APIs are fully compatible with the OpenCRUD standard.

Try the online demo!

Community

Prisma has a community of thousands of amazing developers and contributors. Welcome, please join us! πŸ‘‹

Contributing

Contributions are welcome and extremely helpful πŸ™Œ Please refer to the contribution guide for more information.

Releases are separated into three channels: alpha, beta and stable. You can learn more about these three channels and Prisma's release process here.

Prisma

About

πŸ’Ύ Database Tools incl. ORM, Migrations and Admin UI (Postgres, MySQL & MongoDB) [deprecated]

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Scala 68.4%
  • TypeScript 30.7%
  • Shell 0.4%
  • Java 0.2%
  • JavaScript 0.2%
  • HTML 0.1%