Skip to content

Commit

Permalink
add simple prettier config. run pnpm format. fix example tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
nkeil committed Oct 11, 2023
1 parent b0104fa commit fa6a731
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 83 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pnpm add prisma-extension-random # pnpm
1. Import the extension and add it to your Prisma client:

```typescript
import { PrismaClient } from "@prisma/client";
import prismaRandom from "prisma-extension-random";
import { PrismaClient } from '@prisma/client';
import prismaRandom from 'prisma-extension-random';

const prisma = new PrismaClient().$extends(prismaRandom());
```
Expand All @@ -35,7 +35,7 @@ const prisma = new PrismaClient().$extends(prismaRandom());
// Find a random post from an author whose firstname starts with "B"
const post = await prisma.post.findRandom({
select: { id: true, title: true },
where: { author: { firstName: { startsWith: "B" } } },
where: { author: { firstName: { startsWith: 'B' } } },
});
```

Expand Down
3 changes: 0 additions & 3 deletions example/.gitignore

This file was deleted.

4 changes: 2 additions & 2 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrismaClient } from "@prisma/client";
import prismaRandom from "../dist";
import { PrismaClient } from '@prisma/client';
import prismaRandom from '../dist';

export const prisma = new PrismaClient().$extends(prismaRandom());
120 changes: 60 additions & 60 deletions example/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
import { prisma } from "../";
import { prisma } from '../';

const users = [
{ firstName: "Ida", lastName: "Lupino" },
{ firstName: "Bong", lastName: "Joon Ho" },
{ firstName: "Guillermo", lastName: "del Toro" },
{ firstName: "David", lastName: "Cronenberg" },
{ firstName: "Sidney", lastName: "Lumet" },
{ firstName: "Woody", lastName: "Allen" },
{ firstName: "Kathryn", lastName: "Bigelow" },
{ firstName: "Tim", lastName: "Burton" },
{ firstName: "Terry", lastName: "Gilliam" },
{ firstName: "Wes", lastName: "Anderson" },
{ firstName: "Lois", lastName: "Weber" },
{ firstName: "William", lastName: "Friedkin" },
{ firstName: "Darren", lastName: "Aronofsky" },
{ firstName: "Spike", lastName: "Lee" },
{ firstName: "Pedro", lastName: "Almodóvar" },
{ firstName: "Lars", lastName: "von Trier" },
{ firstName: "Abbas", lastName: "Kiarostami" },
{ firstName: "Brian", lastName: "De Palma" },
{ firstName: "Alejandro", lastName: "González Iñárritu" },
{ firstName: "Satyajit", lastName: "Ray" },
{ firstName: "Ridley", lastName: "Scott" },
{ firstName: "Alice", lastName: "Guy-Blaché" },
{ firstName: "Frank", lastName: "Capra" },
{ firstName: "D.W.", lastName: "Griffith" },
{ firstName: "Terrence", lastName: "Malick" },
{ firstName: "Fritz", lastName: "Lang" },
{ firstName: "Andrei", lastName: "Tarkovsky" },
{ firstName: "David", lastName: "Lean" },
{ firstName: "Alfonso", lastName: "Cuarón" },
{ firstName: "John", lastName: "Cassavetes" },
{ firstName: "Roman", lastName: "Polanski" },
{ firstName: "Billy", lastName: "Wilder" },
{ firstName: "Christopher", lastName: "Nolan" },
{ firstName: "F.W.", lastName: "Murnau" },
{ firstName: "David", lastName: "Fincher" },
{ firstName: "Jea", lastName: "-Luc Godard" },
{ firstName: "David", lastName: "Lynch" },
{ firstName: "Yasujiro", lastName: "Ozu" },
{ firstName: "Orson", lastName: "Welles" },
{ firstName: "Francis", lastName: "Ford Coppola" },
{ firstName: "Federico", lastName: "Fellini" },
{ firstName: "Paul", lastName: "Thomas Anderson" },
{ firstName: "Denis", lastName: "Villeneuve" },
{ firstName: "Charlie", lastName: "Chaplin" },
{ firstName: "Sergei", lastName: "Eisenstein" },
{ firstName: "John", lastName: "Ford" },
{ firstName: "Ingmar", lastName: "Bergman" },
{ firstName: "Quentin", lastName: "Tarantino" },
{ firstName: "Martin", lastName: "Scorsese" },
{ firstName: "Steven", lastName: "Spielberg" },
{ firstName: "Akira", lastName: "Kurosawa" },
{ firstName: "Alfred", lastName: "Hitchcock" },
{ firstName: "Stanley", lastName: "Kubrick" },
{ firstName: 'Ida', lastName: 'Lupino' },
{ firstName: 'Bong', lastName: 'Joon Ho' },
{ firstName: 'Guillermo', lastName: 'del Toro' },
{ firstName: 'David', lastName: 'Cronenberg' },
{ firstName: 'Sidney', lastName: 'Lumet' },
{ firstName: 'Woody', lastName: 'Allen' },
{ firstName: 'Kathryn', lastName: 'Bigelow' },
{ firstName: 'Tim', lastName: 'Burton' },
{ firstName: 'Terry', lastName: 'Gilliam' },
{ firstName: 'Wes', lastName: 'Anderson' },
{ firstName: 'Lois', lastName: 'Weber' },
{ firstName: 'William', lastName: 'Friedkin' },
{ firstName: 'Darren', lastName: 'Aronofsky' },
{ firstName: 'Spike', lastName: 'Lee' },
{ firstName: 'Pedro', lastName: 'Almodóvar' },
{ firstName: 'Lars', lastName: 'von Trier' },
{ firstName: 'Abbas', lastName: 'Kiarostami' },
{ firstName: 'Brian', lastName: 'De Palma' },
{ firstName: 'Alejandro', lastName: 'González Iñárritu' },
{ firstName: 'Satyajit', lastName: 'Ray' },
{ firstName: 'Ridley', lastName: 'Scott' },
{ firstName: 'Alice', lastName: 'Guy-Blaché' },
{ firstName: 'Frank', lastName: 'Capra' },
{ firstName: 'D.W.', lastName: 'Griffith' },
{ firstName: 'Terrence', lastName: 'Malick' },
{ firstName: 'Fritz', lastName: 'Lang' },
{ firstName: 'Andrei', lastName: 'Tarkovsky' },
{ firstName: 'David', lastName: 'Lean' },
{ firstName: 'Alfonso', lastName: 'Cuarón' },
{ firstName: 'John', lastName: 'Cassavetes' },
{ firstName: 'Roman', lastName: 'Polanski' },
{ firstName: 'Billy', lastName: 'Wilder' },
{ firstName: 'Christopher', lastName: 'Nolan' },
{ firstName: 'F.W.', lastName: 'Murnau' },
{ firstName: 'David', lastName: 'Fincher' },
{ firstName: 'Jea', lastName: '-Luc Godard' },
{ firstName: 'David', lastName: 'Lynch' },
{ firstName: 'Yasujiro', lastName: 'Ozu' },
{ firstName: 'Orson', lastName: 'Welles' },
{ firstName: 'Francis', lastName: 'Ford Coppola' },
{ firstName: 'Federico', lastName: 'Fellini' },
{ firstName: 'Paul', lastName: 'Thomas Anderson' },
{ firstName: 'Denis', lastName: 'Villeneuve' },
{ firstName: 'Charlie', lastName: 'Chaplin' },
{ firstName: 'Sergei', lastName: 'Eisenstein' },
{ firstName: 'John', lastName: 'Ford' },
{ firstName: 'Ingmar', lastName: 'Bergman' },
{ firstName: 'Quentin', lastName: 'Tarantino' },
{ firstName: 'Martin', lastName: 'Scorsese' },
{ firstName: 'Steven', lastName: 'Spielberg' },
{ firstName: 'Akira', lastName: 'Kurosawa' },
{ firstName: 'Alfred', lastName: 'Hitchcock' },
{ firstName: 'Stanley', lastName: 'Kubrick' },
];

const posts = [
{
title: "My first blog",
content: "This is my first ever blog post about prisma",
title: 'My first blog',
content: 'This is my first ever blog post about prisma',
published: true,
},
{
title: "My second blog",
content: "This is my second blog post",
title: 'My second blog',
content: 'This is my second blog post',
published: false,
},
{
title: "I love prisma",
content: "This is my third blog post",
title: 'I love prisma',
content: 'This is my third blog post',
published: true,
},
];
Expand Down
6 changes: 3 additions & 3 deletions example/prisma/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { prisma } from "../";
import { prisma } from '../';

const main = async () => {
const user = await prisma.user.findRandom();
Expand All @@ -7,8 +7,8 @@ const main = async () => {
select: { id: true, title: true },
where: {
OR: [
{ title: { contains: "prisma" } },
{ content: { contains: "prisma" } },
{ title: { contains: 'prisma' } },
{ content: { contains: 'prisma' } },
],
published: true,
},
Expand Down
2 changes: 1 addition & 1 deletion example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"lib": ["es2023"],
"lib": ["esnext"],
"module": "esnext",
"target": "es2022",

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
],
"scripts": {
"build": "rimraf dist && tsc",
"format": "prettier --write src/**/*.ts *.md --ignore-unknown --no-error-on-unmatched-pattern",
"format": "prettier --write \"**/*.ts\" \"**/*.md\" --ignore-unknown --no-error-on-unmatched-pattern",
"type-check": "tsc --noEmit"
},
"peerDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions prettier.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @typedef {import("prettier").Config} PrettierConfig*/

/** @type { PrettierConfig } */
const config = {
singleQuote: true,
};

export default config;
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Prisma } from "@prisma/client/extension";
import { Prisma } from '@prisma/client/extension';

type Args = {};

export default (_extensionArgs?: Args) =>
Prisma.defineExtension((prisma) => {
return prisma.$extends({
name: "prisma-extension-random",
name: 'prisma-extension-random',
model: {
$allModels: {
async findRandom<T, A>(
this: T,
args?: Prisma.Exact<A, Prisma.Args<T, "findFirst">> & object
args?: Prisma.Exact<A, Prisma.Args<T, 'findFirst'>> & object,
) {
const context = Prisma.getExtensionContext(this);

Expand All @@ -20,7 +20,7 @@ export default (_extensionArgs?: Args) =>
return (await (context as any).findFirst({
...args,
skip: Math.max(0, Math.floor(Math.random() * numRows)),
})) as Prisma.Result<T, A, "findFirst">;
})) as Prisma.Result<T, A, 'findFirst'>;
},

async findManyRandom<T, TWhere, TSelect>(
Expand All @@ -29,19 +29,19 @@ export default (_extensionArgs?: Args) =>
args?: {
where?: Prisma.Exact<
TWhere,
Prisma.Args<T, "findFirst">["where"]
Prisma.Args<T, 'findFirst'>['where']
>;
select?: Prisma.Exact<
TSelect,
Prisma.Args<T, "findFirst">["select"] & { id: true }
Prisma.Args<T, 'findFirst'>['select'] & { id: true }
>;
}
},
) {
const context = Prisma.getExtensionContext(this);
type FindFirstResult = Prisma.Result<
T,
{ where: TWhere; select: TSelect },
"findFirst"
'findFirst'
>;

const select = args?.select ?? { id: true as const };
Expand All @@ -67,8 +67,8 @@ export default (_extensionArgs?: Args) =>
if (!row) {
console.error(
`get random row failed. Where clause: ${JSON.stringify(
where
)}`
where,
)}`,
);
break;
}
Expand Down

0 comments on commit fa6a731

Please sign in to comment.