From fa6a7316ae93d06af95fc7c161e5d1f08f1ab410 Mon Sep 17 00:00:00 2001 From: Nicolas Keil Date: Wed, 11 Oct 2023 04:02:26 -0700 Subject: [PATCH] add simple prettier config. run pnpm format. fix example tsconfig --- README.md | 6 +-- example/.gitignore | 3 -- example/index.ts | 4 +- example/prisma/seed.ts | 120 ++++++++++++++++++++--------------------- example/prisma/test.ts | 6 +-- example/tsconfig.json | 2 +- package.json | 2 +- prettier.config.mjs | 8 +++ src/index.ts | 20 +++---- 9 files changed, 88 insertions(+), 83 deletions(-) delete mode 100644 example/.gitignore create mode 100644 prettier.config.mjs diff --git a/README.md b/README.md index 05c8f44..346b198 100644 --- a/README.md +++ b/README.md @@ -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()); ``` @@ -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' } } }, }); ``` diff --git a/example/.gitignore b/example/.gitignore deleted file mode 100644 index 11ddd8d..0000000 --- a/example/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -node_modules -# Keep environment variables out of version control -.env diff --git a/example/index.ts b/example/index.ts index f679a9c..8e18dce 100644 --- a/example/index.ts +++ b/example/index.ts @@ -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()); diff --git a/example/prisma/seed.ts b/example/prisma/seed.ts index 14abd07..76a746d 100644 --- a/example/prisma/seed.ts +++ b/example/prisma/seed.ts @@ -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, }, ]; diff --git a/example/prisma/test.ts b/example/prisma/test.ts index 5a50aa7..490248a 100644 --- a/example/prisma/test.ts +++ b/example/prisma/test.ts @@ -1,4 +1,4 @@ -import { prisma } from "../"; +import { prisma } from '../'; const main = async () => { const user = await prisma.user.findRandom(); @@ -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, }, diff --git a/example/tsconfig.json b/example/tsconfig.json index d0c4805..75f7982 100644 --- a/example/tsconfig.json +++ b/example/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "lib": ["es2023"], + "lib": ["esnext"], "module": "esnext", "target": "es2022", diff --git a/package.json b/package.json index 3289f02..9406a26 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/prettier.config.mjs b/prettier.config.mjs new file mode 100644 index 0000000..21e5015 --- /dev/null +++ b/prettier.config.mjs @@ -0,0 +1,8 @@ +/** @typedef {import("prettier").Config} PrettierConfig*/ + +/** @type { PrettierConfig } */ +const config = { + singleQuote: true, +}; + +export default config; diff --git a/src/index.ts b/src/index.ts index 589d5de..0af92fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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( this: T, - args?: Prisma.Exact> & object + args?: Prisma.Exact> & object, ) { const context = Prisma.getExtensionContext(this); @@ -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; + })) as Prisma.Result; }, async findManyRandom( @@ -29,19 +29,19 @@ export default (_extensionArgs?: Args) => args?: { where?: Prisma.Exact< TWhere, - Prisma.Args["where"] + Prisma.Args['where'] >; select?: Prisma.Exact< TSelect, - Prisma.Args["select"] & { id: true } + Prisma.Args['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 }; @@ -67,8 +67,8 @@ export default (_extensionArgs?: Args) => if (!row) { console.error( `get random row failed. Where clause: ${JSON.stringify( - where - )}` + where, + )}`, ); break; }