What is prisma?
Prisma is an open-source database toolkit that includes an ORM (Object-Relational Mapper), migrations, and a query builder for Node.js and TypeScript. It simplifies database access, ensures type safety, and can be used to build GraphQL and REST APIs.
What are prisma's main functionalities?
ORM (Object-Relational Mapping)
Prisma's ORM allows you to interact with your database through an object-oriented model. The code sample demonstrates how to fetch all users from the database using Prisma's ORM.
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
async function main() {
const allUsers = await prisma.user.findMany();
console.log(allUsers);
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
});
Migrations
Prisma Migrate generates and runs SQL migrations based on your Prisma schema. The code sample shows how to create and apply a new migration called 'init'.
npx prisma migrate dev --name init
Type Safety
Prisma ensures type safety by generating TypeScript types based on your database schema. The code sample demonstrates fetching a user with a specific ID and the result is typed as 'User'.
const user: User = await prisma.user.findUnique({ where: { id: 1 } });
Data Modeling
Prisma uses a schema file to model your database. The code sample shows a Prisma schema definition for a User model with an auto-incrementing ID, name, and unique email.
model User {
id Int @id @default(autoincrement())
name String
email String @unique
}
Query Builder
Prisma's query builder allows you to construct complex queries with ease. The code sample demonstrates ordering users by their name in ascending order.
const sortedUsers = await prisma.user.findMany({
orderBy: {
name: 'asc',
},
});
Other packages similar to prisma
sequelize
Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication, and more. Compared to Prisma, Sequelize has been around for longer and has a more traditional API, but it might not offer the same level of type safety and simplicity in schema management.
typeorm
TypeORM is an ORM that can run in Node.js, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, and Electron platforms and can be used with TypeScript and JavaScript. TypeORM is highly influenced by other ORMs, such as Hibernate, Doctrine, and Entity Framework. It provides a similar level of type safety as Prisma but with a different approach to defining models and running migrations.
knex
Knex.js is a SQL query builder for Postgres, MSSQL, MySQL, MariaDB, SQLite3, Oracle, and Amazon Redshift, designed to be flexible, portable, and fun to use. It does not include an ORM by default, but it can be paired with objection.js for ORM functionality. Knex is more of a query builder than a full-fledged ORM and lacks the type safety and model generation features of Prisma.
Website • Docs • Blog • Forum • Slack • Twitter
Prisma - turn your database into a GraphQL API. Prisma lets you design your data model and have a production ready GraphQL API online in minutes.
The Prisma GraphQL API provides powerful abstractions and building blocks to develop flexible, scalable GraphQL backends:
- Type-safe API that can be used from frontend and backend, including filters, aggregations and transactions.
- Data modeling with declarative SDL. Prisma migrates your underlying database automatically.
- Realtime API using GraphQL Subscriptions.
- Advanced API composition using GraphQL Bindings and schema stitching.
- Works with all frontend frameworks like React, Vue.js, Angular (Quickstart Examples).
Contents
Quickstart
Watch this 5 min tutorial or follow the steps below to get started with Prisma:
- Install the CLI via NPM:
npm install -g prisma
- Create a new service:
The following command creates all files you need for a new service.
prisma init
- Define your data model:
Edit datamodel.graphql
to define your data model using the GraphQL SDL notation.
type Tweet {
id: ID! @unique
createdAt: DateTime!
text: String!
owner: User!
location: Location!
}
type User {
id: ID! @unique
createdAt: DateTime!
updatedAt: DateTime!
handle: String! @unique
name: String
tweets: [Tweet!]!
}
type Location {
latitude: Float!
longitude: Float!
}
- Deploy your service:
To deploy your service simply run the following command and select one of the hosted development clusters or setup a local Docker-based development environment:
prisma deploy
- Connect to your GraphQL endpoint:
Use the endpoint from the previous step in your frontend (or backend) applications to connect to your GraphQL API.
7 Read more in the dedicated quickstarts for your favorite technology
Examples
Architecture
Prisma is a secure API layer that sits in front of your database. Acting as a proxy, Prisma exposes a powerful GraphQL API and manages Rate-Limiting, Authentication, Logging and a host of other features. Because Prisma is a standalone process, it can be scaled independently from your application layer and provide scalable subscriptions infrastructure.
Supported Databases
Prisma can be used for MySQL Databases out of the box. More databases connectors will follow:
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 GraphQL CRUD API
- Define and evolve your data model using GraphQL SDL
Try the online demo: open GraphQL Playground
Prisma has a community of thousands of amazing developers and contributors. Welcome, please join us! 👋
Contributing
Your feedback is very helpful, please share your opinion and thoughts!