Skip to content

Instantly share code, notes, and snippets.

View millsp's full-sized avatar
🌴
🌞

pierre millsp

🌴
🌞
View GitHub Profile
@millsp
millsp / prisma-timing-extension.ts
Last active June 3, 2023 02:55
Example of a Prisma Client extensions that creates type-safe timing methods
import { PrismaClient, Prisma } from "@prisma/client"
type Operation =
| 'findFirst'
| 'findFirstOrThrow'
| 'findUnique'
| 'findUniqueOrThrow'
| 'findMany'
| 'create'
| 'createMany'
import { PrismaClient } from ".prisma/client";
/*
model CommentOptionalProp {
id String @id @default(auto()) @map("_id") @db.ObjectId
country String?
content CommentContent?
}
import { PrismaClient } from ".prisma/client";
/**
model Comment {
id String @id @default(auto()) @map("_id") @db.ObjectId
country String?
content CommentContent?
contents CommentContent[]
}
import { PrismaClient } from ".prisma/client";
(async () => {
const prisma = new PrismaClient();
await prisma.user.create({
data: {
email: Date.now() + "",
},
});
@millsp
millsp / unionizeDeep.ts
Created May 17, 2021 17:43
Unionize Deep
type MarkUndefinable<A, B> = {
[K in keyof A]: A[K] extends O.Object
? MarkUndefinable<A[K], A.At<B, K>>
: K extends keyof B ? A[K] | undefined : A[K]
}
type UnionizeDeep<A extends object, B extends object> =
O.Merge<MarkUndefinable<A, B>, B, 'deep', M.BuiltIn, undefined>
type test = A.Compute<UnionizeDeep<{
@millsp
millsp / error.ts
Created April 23, 2021 14:59
Go-style functional type-safe error handling
const checkNumber = check((thing: unknown) => {
if (typeof thing === 'number') {
return thing;
}
return ko(new E.NOT_NUMBER(thing));
});
const main = check(() => {
const [number0, e0] = checkNumber(9);
@millsp
millsp / index.ts
Created April 14, 2021 08:47
Prisma UserArgs
import { PrismaClient, Prisma } from '@prisma/client'
async function main() {
const prisma = new PrismaClient()
// Define a type that includes the relation to `Post`
const userWithPosts = Prisma.validator<Prisma.UserArgs>()({
include: { posts: true }
})
import { PrismaClient, Prisma } from '@prisma/client'
async function main() {
const prisma = new PrismaClient()
// Define a type that includes the relation to `Post`
const userWithPosts = Prisma.validator<Prisma.UserArgs>()({
include: { posts: true }
})
class PrismaUser<T> implements PrismaPromise<T> {
[prisma]: true;
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2> {
throw new Error("Method not implemented.");
}
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<T | TResult> {
throw new Error("Method not implemented.");
}
[Symbol.toStringTag]: string;
@millsp
millsp / N Tuple
Created January 17, 2021 19:33
Create a tuple of N size in TypeScript
type Tuple<A, N extends number, T extends unknown[] = []> = {
0: Tuple<A, N, [...T, A]>
1: T
}[
T['length'] extends N
? 1
: 0
];
type test0 = Tuple<'coucou', 4>;