在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:Prisma开源软件地址:https://gitee.com/mirrors/Prisma开源软件介绍:What is Prisma?Prisma is a next-generation ORM that consists of these tools:
Prisma Client can be used in any Node.js or TypeScript backend application (including serverless applications and microservices). This can be a REST API, a GraphQL API a gRPC API, or anything else that needs a database.
Getting startedThe fastest way to get started with Prisma is by following the Quickstart (5 min). The Quickstart is based on a preconfigured SQLite database. You can also get started with your own database (PostgreSQL and MySQL) by following one of these guides: How does Prisma workThis section provides a high-level overview of how Prisma works and its most important technical components. For a more thorough introduction, visit the Prisma documentation. The Prisma schemaEvery project that uses a tool from the Prisma toolkit starts with a Prisma schema file. The Prisma schema allows developers to define their application models in an intuitive data modeling language. It also contains the connection to a database and defines a generator: // Data sourcedatasource db { provider = "postgresql" url = env("DATABASE_URL")}// Generatorgenerator client { provider = "prisma-client-js"}// Data modelmodel Post { id Int @id @default(autoincrement()) title String content String? published Boolean @default(false) author User? @relation(fields: [authorId], references: [id]) authorId Int?}model User { id Int @id @default(autoincrement()) email String @unique name String? posts Post[]} In this schema, you configure three things:
The Prisma data modelOn this page, the focus is on the data model. You can learn more about Data sources and Generators on the respective docs pages. Functions of Prisma modelsThe data model is a collection of models. A model has two major functions:
Getting a data modelThere are two major workflows for "getting" a data model into your Prisma schema:
Once the data model is defined, you can generate Prisma Client which will expose CRUD and more queries for the defined models. If you're using TypeScript, you'll get full type-safety for all queries (even when only retrieving the subsets of a model's fields). Accessing your database with Prisma ClientGenerating Prisma ClientThe first step when using Prisma Client is installing its npm package: npm install @prisma/client Note that the installation of this package invokes the After you change your data model, you'll need to manually re-generate Prisma Client to ensure the code inside prisma generate Refer to the documentation for more information about "generating the Prisma client". Using Prisma Client to send queries to your databaseOnce the Prisma Client is generated, you can import it in your code and send queries to your database. This is what the setup code looks like. Import and instantiate Prisma ClientYou can import and instantiate Prisma Client as follows: import { PrismaClient } from '@prisma/client'const prisma = new PrismaClient() or const { PrismaClient } = require('@prisma/client')const prisma = new PrismaClient() Now you can start sending queries via the generated Prisma Client API, here are few sample queries. Note that all Prisma Client queries return plain old JavaScript objects. Learn more about the available operations in the Prisma Client docs or watch this demo video (2 min). Retrieve all |
请发表评论