piscopancer
piscopancer
Explore posts from servers
PPrisma
Created by piscopancer on 11/17/2024 in #help-and-questions
Infer type from create query?
here's the function that creates a new Schedule. Attention to the shape, not data
async function createSchedule() {
return db.schedule.create({
data: {
name: '',
days: {
create: [
{
holiday: true,
independentWorkDay: true,
lessons: {
create: [
{
place: 'some place',
type: 'lecture' satisfies LessonType,
subject: {
connect: {
id: 12, // the important part that the type would have this subject's id. There's no such field in `Prisma.LessonCreateArgs` ofc, so I need to infer the type from this function
},
},
},
],
},
},
],
},
},
})
}
async function createSchedule() {
return db.schedule.create({
data: {
name: '',
days: {
create: [
{
holiday: true,
independentWorkDay: true,
lessons: {
create: [
{
place: 'some place',
type: 'lecture' satisfies LessonType,
subject: {
connect: {
id: 12, // the important part that the type would have this subject's id. There's no such field in `Prisma.LessonCreateArgs` ofc, so I need to infer the type from this function
},
},
},
],
},
},
],
},
},
})
}
I want to get the type of the Lesson of the shape in this function. so it has to look like this
/**
* place: string
* type: string
* subject: {
* id: number
* }
*/
/**
* place: string
* type: string
* subject: {
* id: number
* }
*/
so that I can use somewhere else
const [lessons, setLessons] = useState<LessonToCreate[]>([])
setLessons([
{
place: '402',
type: 'seminar',
subject: {
id: 1
}
}
])
const [lessons, setLessons] = useState<LessonToCreate[]>([])
setLessons([
{
place: '402',
type: 'seminar',
subject: {
id: 1
}
}
])
Is there a utility type in prisma to infer this shape?
7 replies
PPrisma
Created by piscopancer on 11/15/2024 in #help-and-questions
narrow down String type using prisma client's extension?
can I explain to prisma client and typescript that this particular field needs to be a string union?
model Lesson {
id Int @id @default(autoincrement())
type String? // null | "practical" | "seminar" | "lecture"
model Lesson {
id Int @id @default(autoincrement())
type String? // null | "practical" | "seminar" | "lecture"
4 replies
PPrisma
Created by piscopancer on 11/15/2024 in #help-and-questions
`Prisma.skip` seen as `undefined`?
No description
5 replies
PPrisma
Created by piscopancer on 11/12/2024 in #help-and-questions
[React Native] DB duplicated to the emulator. Intentional?
No description
4 replies
PPrisma
Created by piscopancer on 11/12/2024 in #help-and-questions
[React Native] Creating client with Supabase crashes
with db.ts like so (pretty basic)
import { PrismaClient } from '@prisma/client/react-native'
import '@prisma/react-native'

export const db = new PrismaClient({
log: ['error'],
})

export async function initializeDb() {
try {
db.$applyPendingMigrations()
console.log('migrations complete')
} catch (e) {
console.error(`failed to apply migrations: ${e}`)
throw new Error('Applying migrations failed, your app is now in an inconsistent state. We cannot guarantee safety, it is now your responsibility to reset the database or tell the user to re-install the app')
}
}
import { PrismaClient } from '@prisma/client/react-native'
import '@prisma/react-native'

export const db = new PrismaClient({
log: ['error'],
})

export async function initializeDb() {
try {
db.$applyPendingMigrations()
console.log('migrations complete')
} catch (e) {
console.error(`failed to apply migrations: ${e}`)
throw new Error('Applying migrations failed, your app is now in an inconsistent state. We cannot guarantee safety, it is now your responsibility to reset the database or tell the user to re-install the app')
}
}
the app crashes after calling this function
useEffect(() => {
async function init() {
await initializeDb() // <-
}
init()
}, [])
useEffect(() => {
async function init() {
await initializeDb() // <-
}
init()
}, [])
with the following schema
generator client {
provider = "prisma-client-js"
previewFeatures = ["reactNative", "driverAdapters"]
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
}

model User {
id Int @id @default(autoincrement())
username String
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["reactNative", "driverAdapters"]
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
}

model User {
id Int @id @default(autoincrement())
username String
}
11 replies
PPrisma
Created by piscopancer on 11/12/2024 in #help-and-questions
Prisma extension in vscode too slow
vscode: 1.95.2 prisma ext: v5.22.0
10 replies
PPrisma
Created by piscopancer on 6/14/2024 in #help-and-questions
Get type of `create` with `connect`?
db.course.create({
data: {
title: 'New course',
tutor: {
connect: {
id: 1
}
}
}
})
db.course.create({
data: {
title: 'New course',
tutor: {
connect: {
id: 1
}
}
}
})
I would like to get the type for object I pass to create function. How can I do that? I assume I should use internal Prisma utility types, right? The trick here is that tutor is a required property so it must be either created or connected (in my case, connected)
2 replies