العلاقات والاستعلامات المتقدمة باستخدام Prisma
صمّم علاقات واحد إلى متعدد ومتعدد إلى متعدد، ثم نفّذ الاستعلامات عبرها باستخدام عمليات الكتابة المتداخلة وinclude وselect والتصفية وتقسيم الصفحات.
العلاقات والاستعلامات المتقدمة باستخدام Prisma درس مجاني في Next.js 15 Fullstack Web Apps على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Next.js 15 Fullstack Web Apps، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Next.js 15 Fullstack Web Apps 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Relations?
Real data is connected: a user has many posts, a post has many tags. Prisma models these relations in the schema so you can traverse them in type-safe queries.
One-to-Many in the Schema
A one-to-many relation links a parent to many children via a foreign key. Define both sides in the schema.
model User {
id Int @id @default(autoincrement())
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
authorId Int
author User @relation(fields: [authorId], references: [id])
}Many-to-Many
Prisma supports implicit many-to-many with just array fields on both sides; it manages the join table for you.
model Post { id Int @id @default(autoincrement()) tags Tag[] }
model Tag { id Int @id @default(autoincrement()) posts Post[] }Including Related Data
By default queries return only the model fields. Use include to fetch related records too.
const user = await prisma.user.findUnique({
where: { id: 1 },
include: { posts: true },
});Selecting Specific Fields
Use select to fetch only the columns you need — leaner payloads and faster queries. You can nest select for relations.
const posts = await prisma.post.findMany({
select: { id: true, title: true, author: { select: { name: true } } },
});Nested Writes
Create a parent and its children in one call with a nested create. Prisma runs it in a transaction.
await prisma.user.create({
data: {
name: 'Ada',
posts: { create: [{ title: 'Hello' }, { title: 'World' }] },
},
});Filtering with where
Build rich filters with operators like contains, gt, in, and combine them with AND/OR.
const recent = await prisma.post.findMany({
where: {
published: true,
title: { contains: 'Prisma' },
},
});Filtering on Relations
You can filter a parent by a condition on its children using some, every, or none.
const authors = await prisma.user.findMany({
where: { posts: { some: { published: true } } },
});Sorting and Pagination
Order results with orderBy and page with take/skip (offset) for classic pagination.
const page = await prisma.post.findMany({
orderBy: { createdAt: 'desc' },
take: 10,
skip: 20,
});Cursor Pagination
For large or live datasets, cursor-based pagination is more stable than offset. Pass the last seen id as a cursor.
const next = await prisma.post.findMany({
take: 10,
cursor: { id: lastId },
skip: 1,
});Avoiding N+1 Queries
Fetching a list and then querying each item separately causes the N+1 problem. Prisma avoids it: use a single include to load relations in one optimized query rather than looping.
Quick Check
Test your Prisma relations knowledge.
Recap
You learned relational querying with Prisma:
- Model one-to-many and many-to-many relations in the schema
- Fetch relations with
include; trim fields withselect - Nested writes create parents and children atomically
- Filter on relations, paginate with take/skip or cursors, and avoid the N+1 problem
تعلم TypeScript مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 12
- الدروس
- 48
الأسئلة الشائعة
هل درس «العلاقات والاستعلامات المتقدمة باستخدام Prisma» مجاني؟
نعم — نص درس «العلاقات والاستعلامات المتقدمة باستخدام Prisma» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Next.js 15 Fullstack Web Apps، انتقل إلى CoddyKit PRO. تتضمن دورة Next.js 15 Fullstack Web Apps 4 دروس في المجموع.
ماذا ستتعلم في «العلاقات والاستعلامات المتقدمة باستخدام Prisma»؟
صمّم علاقات واحد إلى متعدد ومتعدد إلى متعدد، ثم نفّذ الاستعلامات عبرها باستخدام عمليات الكتابة المتداخلة وinclude وselect والتصفية وتقسيم الصفحات. تتمرن على Next.js 15 Fullstack Web Apps مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Next.js 15 Fullstack Web Apps؟
لا تُشترط خبرة سابقة. Next.js 15 Fullstack Web Apps على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «العلاقات والاستعلامات المتقدمة باستخدام Prisma»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Next.js 15 Fullstack Web Apps هذا؟
نعم. كل درس في Next.js 15 Fullstack Web Apps يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مقدمة إلى Prisma ORM
- تصميم مخطط Prisma وعمليات الترحيل
- عمليات CRUD باستخدام Prisma Client
- العلاقات والاستعلامات المتقدمة باستخدام Prisma