Dari
Dari
Explore posts from servers
DTDrizzle Team
Created by Dari on 10/11/2024 in #help
Resolved: Need help with my multi column primary and foreign key table
Setup:
Turso / libsql, dialect is set to turso
Versions:
"@libsql/client": "^0.14.0"
"drizzle-orm": "^0.34.1"
"drizzle-kit": "^0.25.0"
Turso / libsql, dialect is set to turso
Versions:
"@libsql/client": "^0.14.0"
"drizzle-orm": "^0.34.1"
"drizzle-kit": "^0.25.0"
Hey I have created this table:
export const userAsyncTasks = createTable(
"userAsyncTask",
{
userId: text("userId").notNull(),
identifier: text("identifier").notNull(),
type: text("type", { enum: userAsyncTaskTypeEnum }).notNull(),
data: text("data", { mode: "json" }),
startedAt: int("startedAt", { mode: "timestamp" }).notNull(),
timeoutAt: int("timeoutAt", { mode: "timestamp" }),
},
(table) => ({
pk: primaryKey({ columns: [table.userId, table.type, table.identifier] }),
fk: foreignKey({
columns: [table.userId],
foreignColumns: [users.id],
})
.onDelete("cascade")
.onUpdate("cascade"),
}),
);
export const userAsyncTasks = createTable(
"userAsyncTask",
{
userId: text("userId").notNull(),
identifier: text("identifier").notNull(),
type: text("type", { enum: userAsyncTaskTypeEnum }).notNull(),
data: text("data", { mode: "json" }),
startedAt: int("startedAt", { mode: "timestamp" }).notNull(),
timeoutAt: int("timeoutAt", { mode: "timestamp" }),
},
(table) => ({
pk: primaryKey({ columns: [table.userId, table.type, table.identifier] }),
fk: foreignKey({
columns: [table.userId],
foreignColumns: [users.id],
})
.onDelete("cascade")
.onUpdate("cascade"),
}),
);
12 replies
DTDrizzle Team
Created by Dari on 9/20/2024 in #help
Using function in "and" instead of dynamic query
Hey i was wondering if this would work or if there would be any downsides
await db.query.myTable.findFirst({
where: and(
eq(myTable.identifier, params.identifier),
eq(myTable.userId, params.userId),
this.optionalAnd()
),
});

function optionalAnd() {
// some complex synchronous condition which will result in true or false
const doNothing = false; // simplified
if (doNothing) {
return undefined;
}

return and(
eq(myTable.someField, "someValue"),
eq(myTable.someOtherField, "someOtherValue")
);
}
await db.query.myTable.findFirst({
where: and(
eq(myTable.identifier, params.identifier),
eq(myTable.userId, params.userId),
this.optionalAnd()
),
});

function optionalAnd() {
// some complex synchronous condition which will result in true or false
const doNothing = false; // simplified
if (doNothing) {
return undefined;
}

return and(
eq(myTable.someField, "someValue"),
eq(myTable.someOtherField, "someOtherValue")
);
}
This is a simplified example. I prefered writing it like this instead of a dynamic query because i prefer its readability. But I'm not sure if I might run into issues with this. For now the code seems to work. Any suggestions? Im using turso as my db (sqlite aka libsql to be more precise). Thanks for your help 🙂
1 replies
TTCTheo's Typesafe Cult
Created by Dari on 7/24/2024 in #questions
Why is HydrateClient in page.tsx and not in layout.tsx?
Hey I was wondering why the HydrateClient Component is not in the root layout. I couldn't think about a scenario where I wouldn't want to hydrate it 🤔
1 replies
DTDrizzle Team
Created by Dari on 7/3/2024 in #help
Drizzle Push to Turso fails on third attempt
If I use drizzle push on an empty turso db the first push works fine. (command: "pnpm db:push") After that if I run push again, it thinks that the schema doesn't match and it applies some changes. On the third db:push attempt it tries to do the changes again but this time it fails and it leaves the db in an "broken" state. (Because It has some _old tables left and the new ones are missing the values of the _old one.) I see two issues here. 1) Drizzle kit sees schema changes which are actually not there. 2) Drizzle kit is not pushing in a transaction and on error it is not getting rolled back Does anybody else have this issue / a solution? Here is a link to a minimal repo: https://github.com/Cuhadari-Deniz/testing-drizzle-turso drizzle-orm: 0.30.10 drizzle-kit: 0.21.4 @libsql/client: 0.6.0
3 replies
TTCTheo's Typesafe Cult
Created by Dari on 2/22/2024 in #questions
Is it bad to get db without context?
Hey, i was doing some code splitting in my create-t3-turbo app. I found out that i can access the (drizzle) db client without the context. So instead of ctx.db i just imported db from '@acme/db'. I was wondering if there is a downside to this approach and why db was added to the context then? It certainly makes my code cleaner because i don't have to pass the db client to functions.
4 replies
TTCTheo's Typesafe Cult
Created by Dari on 3/22/2023 in #questions
How to chain useQuery calls? | Use result from one query as input for another?
23 replies
TTCTheo's Typesafe Cult
Created by Dari on 1/9/2023 in #questions
How bad is revealing ids really?
Hey, I am currently not using DTOs or sth similar. And I was wondering how bad it really is if i expose my primary keys of a table in my frontend by giving over the whole object returned by the prisma client. I know it's bad practice and seen as a security breach ... but I never fully understood why. If I send a DTO with the same values but some uuid instead of the id (primary key) i still have to persist the uuid in the table and although it is not the primary key ... it is still an identifier that i have to use and a "hacker" could use too.
9 replies