Skip to content
Open
55 changes: 32 additions & 23 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@ enum Role {
}

model User {
id Int @id @default(autoincrement())
email String @unique
password String
role Role @default(USER)
warnings Int?
banned Boolean @default(false)
money BigInt @default(0)
friends Float @default(0)
birthday DateTime?
signal Bytes?
parameters Json @default("{}")
posts Post[]
Blog Blog[]
profile Profile?
service Service[]
subscriptions Subscription[]
reactions Reaction[]
id Int @id @default(autoincrement())
email String @unique
password String
role Role @default(USER)
warnings Int?
banned Boolean @default(false)
money BigInt @default(0)
friends Float @default(0)
birthday DateTime?
signal Bytes?
parameters Json @default("{}")
posts Post[]
Blog Blog[]
profile Profile?
service Service[]
subscriptions Subscription[]
reactions Reaction[]
connections User[] @relation("Connections")
symmetricalConnections User[] @relation("Connections")
}

model Profile {
Expand All @@ -49,14 +51,21 @@ enum Gender {
}

model Post {
id Int @id @default(autoincrement())
title String @unique @db.VarChar(255)
createdAt DateTime @default(now())
imprint String @default(uuid())
author User @relation(fields: [authorId], references: [id])
id Int @id @default(autoincrement())
title String @unique @db.VarChar(255)
createdAt DateTime @default(now())
imprint String @default(uuid())
author User @relation(fields: [authorId], references: [id])
authorId Int
blog Blog @relation(fields: [blogId], references: [id], onDelete: Cascade)
blog Blog @relation(fields: [blogId], references: [id], onDelete: Cascade)
blogId Int
comments Comment[]
}

model Comment {
id Int @id @default(autoincrement())
body String @db.VarChar(255)
posts Post[]
}

model Blog {
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/client/client-custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('client (custom)', () => {
profile: [],
service: [],
subscription: [],
comment: [],
};

if (provider !== 'mongodb') {
Expand Down Expand Up @@ -63,6 +64,7 @@ describe('client (custom)', () => {
profile: [],
service: [],
subscription: [],
comment: [],
};

if (provider !== 'mongodb') {
Expand Down
27 changes: 25 additions & 2 deletions src/__tests__/create/create-connect.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { PrismaClient } from '@prisma/client';

import { resetDb, seededPosts, simulateSeed } from '../../../testing';
import { PrismockClient, PrismockClientType } from '../../lib/client';
import { resetDb, seededPosts, simulateSeed, type PostWithComments } from '../../../testing';
import { PrismockClient, PrismockClientType, relationshipStore } from '../../lib/client';

jest.setTimeout(40000);

describe('create (connect)', () => {
let prismock: PrismockClientType;
let prisma: PrismaClient;

beforeEach(() => relationshipStore.resetValues());

beforeAll(async () => {
await resetDb();

Expand Down Expand Up @@ -92,4 +94,25 @@ describe('create (connect)', () => {
expect(realBlog).toEqual(expected);
expect(mockBlog).toEqual(expected);
});

it('Should handle many to many relationship', async () => {
const payload = {
data: {
id: 99,
title: 'Title',
authorId: 1,
blogId: 1,
comments: {
connect: [{ id: 1 }, { id: 2 }],
},
},
};
await prisma.post.create(payload);
await prismock.post.create(payload);

const realPost = await prisma.post.findFirst({ where: { id: 99 }, include: { comments: true } });
const mockPost = await prismock.post.findFirst({ where: { id: 99 }, include: { comments: true } });

expect((mockPost as PostWithComments).comments).toMatchObject((realPost as PostWithComments).comments);
});
});
53 changes: 53 additions & 0 deletions src/__tests__/delete/delete-many-to-many.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { PrismaClient } from '@prisma/client';

import { resetDb, simulateSeed, type PostWithComments } from '../../../testing';
import { PrismockClient, PrismockClientType, relationshipStore } from '../../lib/client';

describe('deleteMany', () => {
let prismock: PrismockClientType;
let prisma: PrismaClient;

beforeEach(() => relationshipStore.resetValues());

beforeAll(async () => {
await resetDb();

prisma = new PrismaClient();
prismock = new PrismockClient() as PrismockClientType;
await simulateSeed(prismock);
});

it('Should reset many to many relationships', async () => {
const connectPayload = {
where: { id: 1 },
data: {
comments: {
connect: [{ id: 1 }, { id: 2 }],
},
},
};

await prisma.post.update(connectPayload);
await prismock.post.update(connectPayload);

await prisma.comment.delete({ where: { id: 1 } });
await prismock.comment.delete({ where: { id: 1 } });

await prisma.comment.create({
data: {
body: 'yo',
},
});
await prismock.comment.create({
data: {
id: 1,
body: 'yo',
},
});

const post = await prisma.post.findFirst({ where: { id: 1 }, include: { comments: true } });
const mockedPost = await prismock.post.findFirst({ where: { id: 1 }, include: { comments: true } });

expect(post).toMatchObject(mockedPost as PostWithComments);
});
});
Loading