Eco 🌤
Eco 🌤
DTDrizzle Team
Created by Eco 🌤 on 4/20/2025 in #help
drizzle-seed thinks I'm not using MySQL.
Error: The drizzle-seed package currently supports only PostgreSQL, MySQL, and SQLite databases. Please ensure your database is one of these supported types
at seedFunc (/Users/user/Code/starter/node_modules/src/index.ts:393:9)
at SeedPromise.then (/Users/user/Code/starter/node_modules/src/index.ts:167:10)
Error: The drizzle-seed package currently supports only PostgreSQL, MySQL, and SQLite databases. Please ensure your database is one of these supported types
at seedFunc (/Users/user/Code/starter/node_modules/src/index.ts:393:9)
at SeedPromise.then (/Users/user/Code/starter/node_modules/src/index.ts:167:10)
seed.ts
import "@/server/envConfig";
import { seed } from "drizzle-seed";
import { db } from ".";
import { account, user } from "./schema";

async function main() {
await seed(db, { user, account });
}

main();
seed.ts
import "@/server/envConfig";
import { seed } from "drizzle-seed";
import { db } from ".";
import { account, user } from "./schema";

async function main() {
await seed(db, { user, account });
}

main();
// db.ts
import { withReplicas } from "drizzle-orm/mysql-core";
import { drizzle } from "drizzle-orm/mysql2";
import { serverEnv } from "@/env/server";
import config from "./config";
import * as relations from "./relations";
import * as schema from "./schema";

const DRIZZLE_CONFIG = {
schema: {
...schema,
...relations,
},
mode: "default" as const,
casing: "snake_case" as const,
} as const;

const primaryDb = drizzle({
...DRIZZLE_CONFIG,
connection: { uri: config.primary },
});

const replicaDbs =
config.read.length > 0
? config.read.map((url) =>
drizzle({
...DRIZZLE_CONFIG,
connection: { uri: url },
}),
)
: [primaryDb];

const drizzleDb = withReplicas(
primaryDb,
replicaDbs as [typeof primaryDb, ...(typeof primaryDb)[]],
);

declare const global: {
drizzleGlobal: typeof drizzleDb;
};

const db = global.drizzleGlobal ?? drizzleDb;

export { db, primaryDb, replicaDbs };

export type TransactionType = Parameters<
Parameters<typeof db.transaction>[0]
>[0];

if (serverEnv.NODE_ENV !== "production") global.drizzleGlobal = db;
// db.ts
import { withReplicas } from "drizzle-orm/mysql-core";
import { drizzle } from "drizzle-orm/mysql2";
import { serverEnv } from "@/env/server";
import config from "./config";
import * as relations from "./relations";
import * as schema from "./schema";

const DRIZZLE_CONFIG = {
schema: {
...schema,
...relations,
},
mode: "default" as const,
casing: "snake_case" as const,
} as const;

const primaryDb = drizzle({
...DRIZZLE_CONFIG,
connection: { uri: config.primary },
});

const replicaDbs =
config.read.length > 0
? config.read.map((url) =>
drizzle({
...DRIZZLE_CONFIG,
connection: { uri: url },
}),
)
: [primaryDb];

const drizzleDb = withReplicas(
primaryDb,
replicaDbs as [typeof primaryDb, ...(typeof primaryDb)[]],
);

declare const global: {
drizzleGlobal: typeof drizzleDb;
};

const db = global.drizzleGlobal ?? drizzleDb;

export { db, primaryDb, replicaDbs };

export type TransactionType = Parameters<
Parameters<typeof db.transaction>[0]
>[0];

if (serverEnv.NODE_ENV !== "production") global.drizzleGlobal = db;
// drizzle.config.ts
import { type Config, defineConfig } from "drizzle-kit";
import config from "./server/db/config";

export default defineConfig({
dialect: "mysql",
dbCredentials: {
url: config.primary,
},
casing: "snake_case",
}) satisfies Config;
// drizzle.config.ts
import { type Config, defineConfig } from "drizzle-kit";
import config from "./server/db/config";

export default defineConfig({
dialect: "mysql",
dbCredentials: {
url: config.primary,
},
casing: "snake_case",
}) satisfies Config;
3 replies
DTDrizzle Team
Created by Eco 🌤 on 2/12/2025 in #help
Primary and Replica Query Load Balancing
https://orm.drizzle.team/docs/read-replicas I have a setup for 1 primary and 1 replica. I have it configured similarly to the docs and can confirm that queries are going into the replica. The issue I have at the moment is that all queries are going into the replica, and I want it to be like 70% using the replica while 30% uses the primary. It seems like the configuration only allows weighted decisions based on replicas, not including the primary.
1 replies
DTDrizzle Team
Created by Eco 🌤 on 11/23/2024 in #help
Read Replica Health Check
Is there a way to health check read replica or not needed because it switches to primary? also is there a better way to check if connection is working properly? at the moment I query a table.
import { db } from "@/server/db";

interface DatabaseError {
code?: string;
message?: string;
}

// Type guard to check if the error is a DatabaseError
function isDatabaseError(error: unknown): error is DatabaseError {
return (
typeof error === "object" &&
error !== null &&
"code" in error &&
typeof (error as { code?: unknown }).code === "string"
);
}

export async function GET(): Promise<Response> {
try {
await db.$primary.query.users.findFirst();
} catch (error) {
console.error(error);

if (isDatabaseError(error)) {
return new Response(error.code || "Internal Server Error", {
status: 500,
});
}

return new Response("Internal Server Error", { status: 500 });
}

return new Response("Ok", { status: 200 });
}
import { db } from "@/server/db";

interface DatabaseError {
code?: string;
message?: string;
}

// Type guard to check if the error is a DatabaseError
function isDatabaseError(error: unknown): error is DatabaseError {
return (
typeof error === "object" &&
error !== null &&
"code" in error &&
typeof (error as { code?: unknown }).code === "string"
);
}

export async function GET(): Promise<Response> {
try {
await db.$primary.query.users.findFirst();
} catch (error) {
console.error(error);

if (isDatabaseError(error)) {
return new Response(error.code || "Internal Server Error", {
status: 500,
});
}

return new Response("Internal Server Error", { status: 500 });
}

return new Response("Ok", { status: 200 });
}
1 replies
DTDrizzle Team
Created by Eco 🌤 on 11/23/2024 in #help
Read Replica Type Annotation
No description
2 replies
DTDrizzle Team
Created by Eco 🌤 on 10/14/2024 in #help
How to automatically run migration on NextJS start
No description
5 replies
DTDrizzle Team
Created by Eco 🌤 on 8/14/2024 in #help
can we have production migrations recommendations docs for drizzle?
No description
2 replies
DTDrizzle Team
Created by Eco 🌤 on 7/25/2024 in #help
separating relations to different file results in typescript issues
I have updated my config to this and just move the relations to relations.ts
schema: ["./server/db/schema.ts", "./server/db/relations.ts"],
schema: ["./server/db/schema.ts", "./server/db/relations.ts"],
It results in this kind of type error.
error TS2339: Property 'name' does not exist on type 'never'.

29 .map((p) => p.permission.name);
error TS2339: Property 'name' does not exist on type 'never'.

29 .map((p) => p.permission.name);
3 replies
DTDrizzle Team
Created by Eco 🌤 on 7/6/2024 in #help
Is it possible to return has many relationship load to return a single object instead of an array.
db.query.products.findMany({
orderBy: (model, { desc }) => desc(model.createdAt),
with: {
discounts: {
where: (model, { eq }) =>
eq(model.organizationId, session.organizationId),
limit: 1,
},
},
}),
db.query.products.findMany({
orderBy: (model, { desc }) => desc(model.createdAt),
with: {
discounts: {
where: (model, { eq }) =>
eq(model.organizationId, session.organizationId),
limit: 1,
},
},
}),
output
[
{
id: 1,
discounts: [ [Object] ]
}
]
[
{
id: 1,
discounts: [ [Object] ]
}
]
desired output
[
{
id: 1,
discounts: Object
}
]
[
{
id: 1,
discounts: Object
}
]
2 replies
DTDrizzle Team
Created by Eco 🌤 on 5/26/2024 in #help
How to properly infer type or add typings of a returned model with relationship in a component props
getCart.tsx
const session = await getSession();

let cart = await db.query.carts.findFirst({
where: (model, { eq }) => eq(model.userId, session.uuid),
with: {
items: {
with: {
product: true,
},
},
},
});

return cart;
const session = await getSession();

let cart = await db.query.carts.findFirst({
where: (model, { eq }) => eq(model.userId, session.uuid),
with: {
items: {
with: {
product: true,
},
},
},
});

return cart;
page.tsx
const cart = await getCart();
<CartSummary cart={cart} />
const cart = await getCart();
<CartSummary cart={cart} />
cart-summary.tsx
export default async function CartSummary({ cart }) {
}
export default async function CartSummary({ cart }) {
}
6 replies