drizzle.config.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { defineConfig } from "drizzle-kit";
  2. import fs from "node:fs";
  3. import path from "node:path";
  4. function getLocalD1DB() {
  5. try {
  6. const basePath = path.resolve(".wrangler");
  7. const dbFile = fs
  8. .readdirSync(basePath, { encoding: "utf-8", recursive: true })
  9. .find(f => f.endsWith(".sqlite"));
  10. if (!dbFile) {
  11. throw new Error(`.sqlite file not found in ${basePath}`);
  12. }
  13. const url = path.resolve(basePath, dbFile);
  14. return url;
  15. } catch (err) {
  16. console.log(`Error ${err}`);
  17. }
  18. }
  19. export default defineConfig({
  20. dialect: "sqlite",
  21. schema: "./src/db/index.ts",
  22. out: "./drizzle",
  23. ...(process.env.NODE_ENV === "production"
  24. ? {
  25. driver: "d1-http",
  26. dbCredentials: {
  27. accountId: process.env.CLOUDFLARE_D1_ACCOUNT_ID,
  28. databaseId: process.env.CLOUDFLARE_DATABASE_ID,
  29. token: process.env.CLOUDFLARE_D1_API_TOKEN,
  30. },
  31. }
  32. : {
  33. dbCredentials: {
  34. url: getLocalD1DB(),
  35. },
  36. }),
  37. });