Uncle
Uncle
PPrisma
Created by Uncle on 1/29/2025 in #help-and-questions
Help with related filtering with some
With the following schema how do I search for all leases based on the size of a unit?
model Unit {
num String @id @unique
building String
size String
description String
leasedPrice Int?
advertisedPrice Int
notes String?
unavailable Boolean @default(false)
lease Lease[]

@@index([num(sort: Desc)])
}
model Lease {
leaseId String @id @unique @default(cuid(2))
unitNum String
price Int
leaseCreatedAt DateTime @default(now())
leaseEnded DateTime?
dropboxURL String?
anvilEID String? @unique
stripeSubscriptionId String?
invoices Invoice[]
unit Unit @relation(fields: [unitNum], references: [num])


@@unique([leaseId, unitNum, price])
@@index([leaseId, leaseCreatedAt(sort: Desc)])
}
model Unit {
num String @id @unique
building String
size String
description String
leasedPrice Int?
advertisedPrice Int
notes String?
unavailable Boolean @default(false)
lease Lease[]

@@index([num(sort: Desc)])
}
model Lease {
leaseId String @id @unique @default(cuid(2))
unitNum String
price Int
leaseCreatedAt DateTime @default(now())
leaseEnded DateTime?
dropboxURL String?
anvilEID String? @unique
stripeSubscriptionId String?
invoices Invoice[]
unit Unit @relation(fields: [unitNum], references: [num])


@@unique([leaseId, unitNum, price])
@@index([leaseId, leaseCreatedAt(sort: Desc)])
}
I get an error when I do
const leases = await prisma.lease.findMany({
where: {
unit: {
some: {
size: '04x06'
}
}
}
})
const leases = await prisma.lease.findMany({
where: {
unit: {
some: {
size: '04x06'
}
}
}
})
4 replies
PPrisma
Created by Uncle on 1/27/2025 in #help-and-questions
cuid2 length
What is the length of the generated cuid(2) ids? I'm looking to validate them via zod.
9 replies
PPrisma
Created by Uncle on 12/2/2024 in #help-and-questions
Cannot read properties of undefined (reading 'create')
I'm getting this error with the following schema a creation process:
generator client {
provider = "prisma-client-js"
previewFeatures = ["omitApi"]
}

datasource db {
provider = "postgresql"
url = env("POSTGRES_PRISMA_URL")
}
model MagicLink {
tokenHash String @unique @map("token_hash")
email String
expiresAt DateTime @map("expires_at")
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["omitApi"]
}

datasource db {
provider = "postgresql"
url = env("POSTGRES_PRISMA_URL")
}
model MagicLink {
tokenHash String @unique @map("token_hash")
email String
expiresAt DateTime @map("expires_at")
}
export async function generateMagicLink(email:string):Promise<string> {
const random:RandomReader = {
read(bytes) {
crypto.getRandomValues(bytes)
}
}
const code = generateRandomString(random, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', 25);
console.log('code: ', code)
const codeHash = encodeBase32LowerCaseNoPadding(sha256(new TextEncoder().encode(code)));
console.log('codeHash: ', codeHash);
const magicLink = await prisma.magicLink.create({
data: {
email,
expiresAt: dayjs(Date.now()).add(5, 'minute').toDate(),
tokenHash: codeHash
}
})
console.log(magicLink)
return code;
}
export async function generateMagicLink(email:string):Promise<string> {
const random:RandomReader = {
read(bytes) {
crypto.getRandomValues(bytes)
}
}
const code = generateRandomString(random, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', 25);
console.log('code: ', code)
const codeHash = encodeBase32LowerCaseNoPadding(sha256(new TextEncoder().encode(code)));
console.log('codeHash: ', codeHash);
const magicLink = await prisma.magicLink.create({
data: {
email,
expiresAt: dayjs(Date.now()).add(5, 'minute').toDate(),
tokenHash: codeHash
}
})
console.log(magicLink)
return code;
}
7 replies
PPrisma
Created by Uncle on 9/2/2024 in #help-and-questions
Validator questions
I'm trying to operate partial type of my User model as I omit the passwordHash field at instantiation. When I follow the docs I end up with the select included in the type, this is from my sveltekit app:
import { Prisma, type User } from 'prisma/prisma-client'
const partialUser = Prisma.validator<Prisma.UserDefaultArgs>()({
select:{ email: true, givenName: true }
})
export let nameBlock: typeof partialUser;
import { Prisma, type User } from 'prisma/prisma-client'
const partialUser = Prisma.validator<Prisma.UserDefaultArgs>()({
select:{ email: true, givenName: true }
})
export let nameBlock: typeof partialUser;
Property 'givenName' does not exist on type '{ select: { email: true; givenName: true; }; }'.ts(2339)
13 replies
PPrisma
Created by Uncle on 8/17/2024 in #help-and-questions
Module '"prisma/prisma-client"' has no exported member 'PrismaClient'.
I'm suddenly getting this error from typescript and I don't know what's changed. I'm also no longer getting autocomplete in VSCode. I've restarted my computer and regenerated the client a few times. I'm at a loss on how to troubleshoot this. Studio works fine. My seed file works fine. import { PrismaClient } from '@prisma/client' const prisma = new PrismaClient({ omit:{ user: { passwordHash: true } } }) export default prisma
4 replies
PPrisma
Created by Uncle on 7/21/2024 in #help-and-questions
Help with findFirst and none relations
I'm working on seeding my database for a self storage and I'm trying to randomly select a customer with no leases and assign them a lease. This is my code and it's alway returning the same customer: const customer = await prisma.user.findFirst({ where: { customerLeases: { none: {} } }, select: { id: true, contactInfo: true, }, }); When I seed the database one customer ends up with all the leases. I assume from the docs that this is how I should be searching for a customer with no leases so I don't understand what I'm doing wrong. Any help would be greatly appreciated
17 replies