Eco 🌤
Eco 🌤
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