philbookst
philbookst
Explore posts from servers
DTDrizzle Team
Created by philbookst on 12/20/2023 in #help
push:sqlite error _old_push table with turso
is there any known workaround for this issue: https://github.com/drizzle-team/drizzle-orm/issues/1313 adding a column to a table and calling db:push gives a 500 error due to a _oldpushtablename being created causing some issues
6 replies
DTDrizzle Team
Created by philbookst on 6/7/2023 in #help
Query/select statement of column via sql`` for extras field
is it possible to make a normal select to another table inside a extras object in a relational query? i'm trying to do something like this:
const lessonWithComments = await db.query.Lesson.findFirst({
where: eq(Lesson.id, lessonId),
with: {
comments: {
with: {
reactions: true,
user: {
columns: {
id: true,
title: true,
firstName: true,
lastName: true,
image: true
},
extras: {
memberKind: sql`
SELECT ${MemberOnCourse.memberKind}
FROM ${MemberOnCourse}
WHERE ${MemberOnCourse.userId} = ${User.id}
`.as("memberKind")
}
}
}
}
},
orderBy: (Comment, { asc }) => [asc(Comment.createdAt)]
});
const lessonWithComments = await db.query.Lesson.findFirst({
where: eq(Lesson.id, lessonId),
with: {
comments: {
with: {
reactions: true,
user: {
columns: {
id: true,
title: true,
firstName: true,
lastName: true,
image: true
},
extras: {
memberKind: sql`
SELECT ${MemberOnCourse.memberKind}
FROM ${MemberOnCourse}
WHERE ${MemberOnCourse.userId} = ${User.id}
`.as("memberKind")
}
}
}
}
},
orderBy: (Comment, { asc }) => [asc(Comment.createdAt)]
});
i'm getting a syntax error at position 2270 near 'SELECT' is this possible or should i just add the necessary relation and query it on the same level as the user object?
2 replies
DTDrizzle Team
Created by philbookst on 6/2/2023 in #help
Problem with infering json type in zod schema
When using the following table/schema:
const Test = mysqlTable(
"Test",
{
id: serial("id").primaryKey().notNull(),
value: json("value").$type<{ a: string, b: number }>().notNull()
},
)

const TestInsertSchema = createInsertSchema(Test)

const ValueFromSchema = TestInsertSchema.pick({
value: true
})

type Value = z.infer<typeof ValueFromSchema>
// Value is typed as following:
// type Value = {
// value: ((string | number | boolean | {
// [key: string]: Json;
// } | Json[]) & (string | number | boolean | {
// [key: string]: Json;
// } | Json[] | undefined)) | null;
// }
const Test = mysqlTable(
"Test",
{
id: serial("id").primaryKey().notNull(),
value: json("value").$type<{ a: string, b: number }>().notNull()
},
)

const TestInsertSchema = createInsertSchema(Test)

const ValueFromSchema = TestInsertSchema.pick({
value: true
})

type Value = z.infer<typeof ValueFromSchema>
// Value is typed as following:
// type Value = {
// value: ((string | number | boolean | {
// [key: string]: Json;
// } | Json[]) & (string | number | boolean | {
// [key: string]: Json;
// } | Json[] | undefined)) | null;
// }
Am I doing something wrong? How can I pick the value key with correct types from the schema?
3 replies
DTDrizzle Team
Created by philbookst on 5/30/2023 in #help
Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020
Hey I just upgraded to the v^0.26.2 and I get the following error:
Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 2 overrides)

node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]/node_modules/drizzle-orm/relations-d3070559.mjs:301:11:
301 │ otel = await import('@opentelemetry/api');
╵ ~~~~~
Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 2 overrides)

node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected]/node_modules/drizzle-orm/relations-d3070559.mjs:301:11:
301 │ otel = await import('@opentelemetry/api');
╵ ~~~~~
this breaks my local development and (sveltekit) build as well
2 replies
DTDrizzle Team
Created by philbookst on 5/11/2023 in #help
uuid missing in drizzle-orm/mysql-core?
There is a uuid method in the drizzle-orm/pg-core package, but none in the mysql-core package. What is the reason behind this? What should we use instead to generate uuids automatically?
12 replies
DTDrizzle Team
Created by philbookst on 5/11/2023 in #help
DatabaseError: Duplicate column name 'id'
hey guys, i have a problem in my query but cannot figure out what is causing it.
const coursesSq = db
.select({
id: Course.id,
name: Course.name,
image: Course.image,
status: Course.status,
description: Course.description,
guides: Course.guides,
price: Course.price,
moduleCount: sql<number>`COUNT(${Module.id})`.as("moduleCount")
})
.from(Course)
.leftJoin(Module, eq(Course.id, Module.courseId))
.groupBy(Course.id)
.as("courses");

const membersOnCourse = db
.select({
memberId: MemberOnCourse.id,
courseId: MemberOnCourse.courseId,
memberKind: MemberOnCourse.memberKind,
user: {
id: User.id,
title: User.title,
firstName: User.firstName,
lastName: User.lastName,
image: User.image
}
})
.from(MemberOnCourse)
.innerJoin(User, eq(MemberOnCourse.userId, User.id))
.as("membersOnCourse");

const courses = await db
.select()
.from(coursesSq)
.leftJoin(membersOnCourse, eq(coursesSq.id, membersOnCourse.courseId));
const coursesSq = db
.select({
id: Course.id,
name: Course.name,
image: Course.image,
status: Course.status,
description: Course.description,
guides: Course.guides,
price: Course.price,
moduleCount: sql<number>`COUNT(${Module.id})`.as("moduleCount")
})
.from(Course)
.leftJoin(Module, eq(Course.id, Module.courseId))
.groupBy(Course.id)
.as("courses");

const membersOnCourse = db
.select({
memberId: MemberOnCourse.id,
courseId: MemberOnCourse.courseId,
memberKind: MemberOnCourse.memberKind,
user: {
id: User.id,
title: User.title,
firstName: User.firstName,
lastName: User.lastName,
image: User.image
}
})
.from(MemberOnCourse)
.innerJoin(User, eq(MemberOnCourse.userId, User.id))
.as("membersOnCourse");

const courses = await db
.select()
.from(coursesSq)
.leftJoin(membersOnCourse, eq(coursesSq.id, membersOnCourse.courseId));
any idea what's going on?
5 replies
DTDrizzle Team
Created by philbookst on 5/11/2023 in #help
Need help transform a nested prisma query to drizzle
Hey I'm trying to translate the following prisma query to drizzle:
const invite = await prisma.invite.findUnique({
where: {
id: token
},
include: {
invitedBy: {
select: {
title: true,
firstName: true,
lastName: true,
}
},
course: {
select: {
name: true,
settings: {
select: {
hostAlias: true,
moderatorAlias: true,
memberAlias: true
}
}
}
}
}
});
const invite = await prisma.invite.findUnique({
where: {
id: token
},
include: {
invitedBy: {
select: {
title: true,
firstName: true,
lastName: true,
}
},
course: {
select: {
name: true,
settings: {
select: {
hostAlias: true,
moderatorAlias: true,
memberAlias: true
}
}
}
}
}
});
closest i've come to is this:
const invite = await db
.select({
inviteExpiresAt: Invite.inviteExpiresAt,
inviteAs: Invite.inviteAs,
limit: Invite.limit,
invitedBy: {
title: User.title,
firstName: User.firstName,
lastName: User.lastName,
},
course: {
id: Course.id,
name: Course.name,
},
settings: {
hostAlias: CourseSettings.hostAlias,
moderatorAlias: CourseSettings.moderatorAlias,
memberAlias: CourseSettings.memberAlias
}
})
.from(Invite)
.where(eq(Invite.id, token))
.innerJoin(User, eq(Invite.invitedById, User.id))
.innerJoin(Course, eq(Invite.courseId, Course.id))
.innerJoin(CourseSettings, eq(Course.id, CourseSettings.courseId))
const invite = await db
.select({
inviteExpiresAt: Invite.inviteExpiresAt,
inviteAs: Invite.inviteAs,
limit: Invite.limit,
invitedBy: {
title: User.title,
firstName: User.firstName,
lastName: User.lastName,
},
course: {
id: Course.id,
name: Course.name,
},
settings: {
hostAlias: CourseSettings.hostAlias,
moderatorAlias: CourseSettings.moderatorAlias,
memberAlias: CourseSettings.memberAlias
}
})
.from(Invite)
.where(eq(Invite.id, token))
.innerJoin(User, eq(Invite.invitedById, User.id))
.innerJoin(Course, eq(Invite.courseId, Course.id))
.innerJoin(CourseSettings, eq(Course.id, CourseSettings.courseId))
but when trying to move the settings object into the course object as it is in the prisma query I get an error Object literal may only specify known properties, and 'hostAlias' does not exist in type 'SQL<unknown> | Aliased<unknown> | AnyMySqlColumn<{}>' is there any way to get the same result with drizzle? thank you very much for your help! drizzle is awesome!
4 replies