Eco 🌤
Eco 🌤
DTDrizzle Team
Created by Eco 🌤 on 11/23/2024 in #help
Read Replica Type Annotation
Just inferred the types instead..
import "../../envConfig";
import { env } from "@/env/server";
import { withReplicas } from "drizzle-orm/mysql-core";
import { drizzle } from "drizzle-orm/mysql2";
import config from "./config";
import * as relations from "./relations";
import * as schema from "./schema";

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

const primary = drizzle({ ...DRIZZLE_CONFIG, connection: config.primary });
const replica1 = drizzle({ ...DRIZZLE_CONFIG, connection: config.read[0] });
const replica2 = drizzle({ ...DRIZZLE_CONFIG, connection: config.read[1] });
const drizzleDb = withReplicas(primary, [replica1, replica2]);

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

const db = global.drizzleGlobal ?? drizzleDb;

export { db };

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

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

const primary = drizzle({ ...DRIZZLE_CONFIG, connection: config.primary });
const replica1 = drizzle({ ...DRIZZLE_CONFIG, connection: config.read[0] });
const replica2 = drizzle({ ...DRIZZLE_CONFIG, connection: config.read[1] });
const drizzleDb = withReplicas(primary, [replica1, replica2]);

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

const db = global.drizzleGlobal ?? drizzleDb;

export { db };

if (env.NODE_ENV !== "production") global.drizzleGlobal = db;
2 replies
DTDrizzle Team
Created by Eco 🌤 on 10/14/2024 in #help
How to automatically run migration on NextJS start
I was able to make it work using instrumentation.ts instrumentation.ts
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
await import("./sentry.server.config");
await import("./server/db/migrate");
await import("./server/db/seed");
}

if (process.env.NEXT_RUNTIME === "edge") {
await import("./sentry.edge.config");
}
}
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
await import("./sentry.server.config");
await import("./server/db/migrate");
await import("./server/db/seed");
}

if (process.env.NEXT_RUNTIME === "edge") {
await import("./sentry.edge.config");
}
}
migrate.ts
import path from "path";
import { migrate } from "drizzle-orm/mysql2/migrator";
import { db } from "./";

async function run() {
console.log("Running migrations...");
await migrate(db, {
migrationsFolder: path.join(process.cwd(), "server/db/migrations"),
});
console.log("Migrations done!");
}

run().catch((error) => {
console.error(error, "MIGRATION_FAILED");
process.exit(1);
});
import path from "path";
import { migrate } from "drizzle-orm/mysql2/migrator";
import { db } from "./";

async function run() {
console.log("Running migrations...");
await migrate(db, {
migrationsFolder: path.join(process.cwd(), "server/db/migrations"),
});
console.log("Migrations done!");
}

run().catch((error) => {
console.error(error, "MIGRATION_FAILED");
process.exit(1);
});
5 replies
DTDrizzle Team
Created by Eco 🌤 on 10/14/2024 in #help
How to automatically run migration on NextJS start
instructions is not clear.
5 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 10/14/2024 in #help
How to automatically run migration on NextJS start
No description
5 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
I have tried that and it only works if no eager loaded relationship is returned on the query.
with: {
items: {
with: {
product: true,
},
},
},
with: {
items: {
with: {
product: true,
},
},
},
6 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
I do have that and currently I'm just using getCart also on CartSummary since it can be cached. but I wanna know how to add typings on a prop way.
6 replies