Skip to content

Commit

Permalink
Fix return type of findManyRandom with no second argument (#18)
Browse files Browse the repository at this point in the history
* Fix return type of findManyRandom with no second argument

* Add "strange" table seed to tests

* Fix tests
  • Loading branch information
nkeil authored May 13, 2024
1 parent 4cbe85e commit fd5fe96
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 5 deletions.
79 changes: 77 additions & 2 deletions example/db.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { prisma } from './db.js';
import { assert, beforeEach, test } from 'vitest';
import { assert, beforeEach, expect, expectTypeOf, test } from 'vitest';

interface User {
id: number;
firstName: string;
lastName: string;
}

interface Post {
id: number;
title: string;
content: string | null;
published: boolean;
createdAt: Date;
updatedAt: Date;
authorId: number;
}

interface Strange {
key: number;
value: string;
}

const POPULATION = 1000;
const NUM_TRIALS = 10000;
Expand All @@ -8,26 +29,80 @@ const STD_RATIO = 3.4641016151377544;

beforeEach(async () => {
await prisma.user.deleteMany();
await prisma.post.deleteMany();
await prisma.strange.deleteMany();

for (let i = 1; i <= POPULATION; ++i) {
await prisma.user.create({
const user = await prisma.user.create({
data: {
firstName: 'User' + (i % 2),
lastName: i.toString(),
},
});
for (let i = 1; i <= 1; ++i) {
await prisma.post.create({
data: {
author: { connect: { id: user.id } },
title: 'Title',
},
});
}
}
for (let i = 1; i <= 10; ++i) {
await prisma.strange.create({
data: { value: 'value ' + i },
});
}
});

test('empty findRandom', async () => {
await prisma.user.deleteMany();
await prisma.post.deleteMany();
await prisma.strange.deleteMany();

const user = await prisma.user.findRandom();
const post = await prisma.post.findRandom();
const strange = await prisma.strange.findRandom();

expectTypeOf(user).toMatchTypeOf<User | null>();
expectTypeOf(post).toMatchTypeOf<Post | null>();
expectTypeOf(strange).toMatchTypeOf<Strange | null>();

assert.isNull(user);
assert.isNull(post);
assert.isNull(strange);
});

test('empty findManyRandom', async () => {
await prisma.user.deleteMany();
await prisma.post.deleteMany();
await prisma.strange.deleteMany();

const users = await prisma.user.findManyRandom(10000);
const posts = await prisma.post.findManyRandom(10000);

expectTypeOf(users).toMatchTypeOf<{ id: number }[]>();
expectTypeOf(posts).toMatchTypeOf<{ id: number }[]>();

assert.isEmpty(users);
assert.isEmpty(posts);

// 'strange' table doesn't have an `id` column, so this should fail
expect(
async () => await prisma.strange.findManyRandom(10000),
).rejects.toThrowError();
});

test('custom unique key', async () => {
const strange = await prisma.strange.findManyRandom(5, {
custom_uniqueKey: 'key',
});

expectTypeOf(strange).toMatchTypeOf<{ key: number }[]>();
assert(strange.length === 5);
assert(
strange[0] && 'key' in strange[0] && typeof strange[0].key === 'number',
);
});

test('findRandom distribution', async () => {
Expand Down
13 changes: 11 additions & 2 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const main = async () => {
select: { id: true, firstName: true },
});

const post = await prisma.post.findManyRandom(10, {
const posts = await prisma.post.findManyRandom(10, {
select: { title: true },
where: {
OR: [
Expand All @@ -18,7 +18,16 @@ const main = async () => {
custom_uniqueKey: 'id',
});

console.log({ user, post });
const posts2 = await prisma.post.findManyRandom(10);

console.log({ user, posts, posts2 });

try {
const strange = await prisma.strange.findManyRandom(5);
console.log(strange);
} catch (e) {
console.log('there was an error');
}
};

main();
6 changes: 6 additions & 0 deletions example/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ const main = async () => {
});
}
}

for (let i = 1; i <= 1000; ++i) {
await prisma.strange.create({
data: { value: 'value ' + i },
});
}
};

main();
5 changes: 5 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ model Post {
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
}

model Strange {
key Int @id @default(autoincrement())
value String
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default (_extensionArgs?: Args) =>
)) as Prisma.Result<T, A, 'findFirst'>;
},

async findManyRandom<T, TWhere, TSelect, TUnique extends string>(
async findManyRandom<T, TWhere, TSelect, TUnique extends string = 'id'>(
this: T,
num: number,
args?: {
Expand Down

0 comments on commit fd5fe96

Please sign in to comment.