Eternal Mori
Eternal Mori
Explore posts from servers
DTDrizzle Team
Created by Eternal Mori on 3/16/2024 in #help
drizzle-kit push error: Multiple primary key defined
My database and table in this example are using MariaDB with collation: utf8mb4_general_ci. When I use MySQL with utf8mb4_0900_ai_ci I dont experience this error.
3 replies
DTDrizzle Team
Created by Dustin@WunderGraph on 7/12/2023 in #help
How to do conditional joins with the query builder?
All equels with a undefined value are ignored
18 replies
DTDrizzle Team
Created by Dustin@WunderGraph on 7/12/2023 in #help
How to do conditional joins with the query builder?
Here is a where clause with conditional params:
.where(
and(
channel ? eq(s.tickets.channelId, channel) : undefined,
status ? eq(s.tickets.status, status) : undefined,
eq(s.channels.organizationId, organizationId)
)
)
.where(
and(
channel ? eq(s.tickets.channelId, channel) : undefined,
status ? eq(s.tickets.status, status) : undefined,
eq(s.channels.organizationId, organizationId)
)
)
18 replies
DTDrizzle Team
Created by Eternal Mori on 7/12/2023 in #help
How to implement a where clause on a joined table with the new relation builder?
Now I created almost an indentical output with the new builder. But now the problem is that I dont know how to filter on a joined table. in this example on "ticket.channel.organizationId". That part is missing here. How can I implement the filter?
const ticket = await db.query.tickets.findFirst({
columns: {
id: true,
status: true,
},
with: {
channel: {
columns: {
id: true,
type: true,
name: true,
organizationId: true,
},
},
contact: {
columns: {
id: true,
name: true,
email: true,
},
},
messages: {
columns: {
id: true,
subject: true,
text: true,
html: true,
createdAt: true,
},
},
},
where: (table, { and, eq, sql }) => and(eq(table.id, params.id)),
});
const ticket = await db.query.tickets.findFirst({
columns: {
id: true,
status: true,
},
with: {
channel: {
columns: {
id: true,
type: true,
name: true,
organizationId: true,
},
},
contact: {
columns: {
id: true,
name: true,
email: true,
},
},
messages: {
columns: {
id: true,
subject: true,
text: true,
html: true,
createdAt: true,
},
},
},
where: (table, { and, eq, sql }) => and(eq(table.id, params.id)),
});
4 replies
DTDrizzle Team
Created by Eternal Mori on 7/3/2023 in #help
Broken typescript in select
import { connect } from "@planetscale/database";
import { type DrizzleConfig } from "drizzle-orm";
import { drizzle as mysqlDrizzle } from "drizzle-orm/mysql2";
import { drizzle as planetscaleDrizzle } from "drizzle-orm/planetscale-serverless";
import mysql from "mysql2/promise";

import * as schema from "~/lib/drizzle/schema";
import { env } from "~/env.mjs";

const isDev = env.NODE_ENV === "development";

const mysqlConnection = await mysql.createConnection({
uri: env.DATABASE_URL,
});

const planetscaleConnection = connect({
url: env.DATABASE_URL,
});

const config: DrizzleConfig<typeof schema> = {
schema,
logger: env.NODE_ENV === "development",
};

export const db = isDev
? mysqlDrizzle(mysqlConnection, config)
: planetscaleDrizzle(planetscaleConnection, config);
import { connect } from "@planetscale/database";
import { type DrizzleConfig } from "drizzle-orm";
import { drizzle as mysqlDrizzle } from "drizzle-orm/mysql2";
import { drizzle as planetscaleDrizzle } from "drizzle-orm/planetscale-serverless";
import mysql from "mysql2/promise";

import * as schema from "~/lib/drizzle/schema";
import { env } from "~/env.mjs";

const isDev = env.NODE_ENV === "development";

const mysqlConnection = await mysql.createConnection({
uri: env.DATABASE_URL,
});

const planetscaleConnection = connect({
url: env.DATABASE_URL,
});

const config: DrizzleConfig<typeof schema> = {
schema,
logger: env.NODE_ENV === "development",
};

export const db = isDev
? mysqlDrizzle(mysqlConnection, config)
: planetscaleDrizzle(planetscaleConnection, config);
3 replies
DTDrizzle Team
Created by Eternal Mori on 7/3/2023 in #help
Broken typescript in select
I fixed it, it was because I used a dynamic db connection:
3 replies
DTDrizzle Team
Created by eatmoose on 6/2/2023 in #help
Auto update timestamp fields
Planetscale advices to update these values on app level, because if you use mysql functions like: "NOW()" or "CURRENT_TIMESTAMP()" they will use the mysql server time and timezone. If you want to use the timezone of your own app. set these values at app level. My Schema TS file:
const updatedAndCreatedAt = {
updatedAt: datetime("updatd_at").notNull(),
createdAt: datetime("created_at").notNull(),
};

export const users = table(
"users",
{
id: varchar("id", { length: 36 }).primaryKey(),
...all other fields,
...updatedAndCreatedAt,
}
);
const updatedAndCreatedAt = {
updatedAt: datetime("updatd_at").notNull(),
createdAt: datetime("created_at").notNull(),
};

export const users = table(
"users",
{
id: varchar("id", { length: 36 }).primaryKey(),
...all other fields,
...updatedAndCreatedAt,
}
);
When creating my record:
await db
.insert(schema.users)
.values({
id,
...al other fields,
createdAt: new Date(),
updatedAt: new Date(),
})
.execute();
await db
.insert(schema.users)
.values({
id,
...al other fields,
createdAt: new Date(),
updatedAt: new Date(),
})
.execute();
When updating:
await db
.update(schema.users)
.set({
...body,
updatedAt: new Date(),
})
.execute()
await db
.update(schema.users)
.set({
...body,
updatedAt: new Date(),
})
.execute()
17 replies