@better-t-stack/template-generator 3.26.1 → 3.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -388,7 +388,7 @@ function updatePackageJsonsWithCatalogs(vfs, packagesInfo, catalog) {
388
388
  //#endregion
389
389
  //#region src/utils/db-scripts.ts
390
390
  function getDbScriptSupport(config) {
391
- const isD1Alchemy = config.dbSetup === "d1" && config.serverDeploy === "cloudflare";
391
+ const isD1Alchemy = config.dbSetup === "d1" && (config.serverDeploy === "cloudflare" || config.backend === "self" && config.webDeploy === "cloudflare");
392
392
  if (!(config.backend !== "convex" && config.backend !== "none" && config.database !== "none" && config.orm !== "none" && config.orm !== "mongoose")) return {
393
393
  hasDbScripts: false,
394
394
  hasDbPush: false,
@@ -800,15 +800,15 @@ const dependencyVersionMap = {
800
800
  "@tanstack/solid-query": "^5.87.4",
801
801
  "@tanstack/solid-query-devtools": "^5.87.4",
802
802
  "@tanstack/solid-router-devtools": "^1.131.44",
803
- wrangler: "^4.54.0",
803
+ wrangler: "^4.77.0",
804
804
  "@cloudflare/vite-plugin": "^1.17.1",
805
- "@opennextjs/cloudflare": "^1.14.6",
805
+ "@opennextjs/cloudflare": "^1.17.3",
806
806
  "nitro-cloudflare-dev": "^0.2.2",
807
807
  "@sveltejs/adapter-cloudflare": "^7.2.4",
808
808
  "@cloudflare/workers-types": "^4.20251213.0",
809
809
  "@astrojs/cloudflare": "^13.0.1",
810
810
  "@astrojs/node": "^10.0.0-beta.9",
811
- alchemy: "^0.87.0",
811
+ alchemy: "^0.90.0",
812
812
  dotenv: "^17.2.2",
813
813
  tsdown: "^0.16.5",
814
814
  zod: "^4.1.13",
@@ -1632,7 +1632,10 @@ function processAuthPlugins(vfs, config) {
1632
1632
  namedImports: [named]
1633
1633
  });
1634
1634
  });
1635
- const betterAuthCall = sourceFile.getVariableDeclaration("auth")?.getInitializerIfKind(SyntaxKind.CallExpression);
1635
+ const betterAuthCall = sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression).find((callExpression) => {
1636
+ const expression = callExpression.getExpression();
1637
+ return Node.isIdentifier(expression) && expression.getText() === "betterAuth";
1638
+ });
1636
1639
  if (betterAuthCall) {
1637
1640
  const configObject = betterAuthCall.getArguments()[0]?.asKind(SyntaxKind.ObjectLiteralExpression);
1638
1641
  if (configObject) {
@@ -1877,7 +1880,7 @@ function processDeployDeps(vfs, config) {
1877
1880
  function processEnvDeps(vfs, config) {
1878
1881
  const envPath = "packages/env/package.json";
1879
1882
  if (!vfs.exists(envPath)) return;
1880
- const { frontend, backend, runtime } = config;
1883
+ const { frontend, backend, runtime, webDeploy } = config;
1881
1884
  const deps = ["zod"];
1882
1885
  const hasNative = frontend.some((value) => [
1883
1886
  "native-bare",
@@ -1890,6 +1893,7 @@ function processEnvDeps(vfs, config) {
1890
1893
  else if (hasNuxt) deps.push("@t3-oss/env-nuxt");
1891
1894
  if (hasNative || !hasNextJs && !hasNuxt) deps.push("@t3-oss/env-core");
1892
1895
  if (backend !== "convex" && backend !== "none" && runtime !== "workers" && !deps.includes("@t3-oss/env-core")) deps.push("@t3-oss/env-core");
1896
+ if (backend === "self" && webDeploy === "cloudflare" && hasNextJs) deps.push("@opennextjs/cloudflare");
1893
1897
  addPackageDependency({
1894
1898
  vfs,
1895
1899
  packagePath: envPath,
@@ -4523,12 +4527,16 @@ async function authenticateClerkRequest(request: Request): Promise<ClerkContextA
4523
4527
  {{#if (and (eq backend 'self') (includes frontend "next"))}}
4524
4528
  import type { NextRequest } from "next/server";
4525
4529
  {{#if (eq auth "better-auth")}}
4530
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
4531
+ import { createAuth } from "@{{projectName}}/auth";
4532
+ {{else}}
4526
4533
  import { auth } from "@{{projectName}}/auth";
4527
4534
  {{/if}}
4535
+ {{/if}}
4528
4536
 
4529
4537
  export async function createContext(req: NextRequest){{#if (eq auth "clerk")}}: Promise<ClerkRequestContext>{{/if}} {
4530
4538
  {{#if (eq auth "better-auth")}}
4531
- const session = await auth.api.getSession({
4539
+ const session = await {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}createAuth(){{else}}auth{{/if}}.api.getSession({
4532
4540
  headers: req.headers,
4533
4541
  });
4534
4542
  return {
@@ -4551,12 +4559,16 @@ export async function createContext(req: NextRequest){{#if (eq auth "clerk")}}:
4551
4559
 
4552
4560
  {{else if (and (eq backend 'self') (includes frontend "tanstack-start"))}}
4553
4561
  {{#if (eq auth "better-auth")}}
4562
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
4563
+ import { createAuth } from "@{{projectName}}/auth";
4564
+ {{else}}
4554
4565
  import { auth } from "@{{projectName}}/auth";
4555
4566
  {{/if}}
4567
+ {{/if}}
4556
4568
 
4557
4569
  export async function createContext({ req }: { req: Request }){{#if (eq auth "clerk")}}: Promise<ClerkRequestContext>{{/if}} {
4558
4570
  {{#if (eq auth "better-auth")}}
4559
- const session = await auth.api.getSession({
4571
+ const session = await {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}createAuth(){{else}}auth{{/if}}.api.getSession({
4560
4572
  headers: req.headers,
4561
4573
  });
4562
4574
  return {
@@ -4579,8 +4591,12 @@ export async function createContext({ req }: { req: Request }){{#if (eq auth "cl
4579
4591
 
4580
4592
  {{else if (and (eq backend 'self') (includes frontend "nuxt"))}}
4581
4593
  {{#if (eq auth "better-auth")}}
4594
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
4595
+ import { createAuth } from "@{{projectName}}/auth";
4596
+ {{else}}
4582
4597
  import { auth } from "@{{projectName}}/auth";
4583
4598
  {{/if}}
4599
+ {{/if}}
4584
4600
 
4585
4601
  export type CreateContextOptions = {
4586
4602
  headers: Headers;
@@ -4588,7 +4604,7 @@ export type CreateContextOptions = {
4588
4604
 
4589
4605
  export async function createContext({ headers }: CreateContextOptions) {
4590
4606
  {{#if (eq auth "better-auth")}}
4591
- const session = await auth.api.getSession({ headers });
4607
+ const session = await {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}createAuth(){{else}}auth{{/if}}.api.getSession({ headers });
4592
4608
  return {
4593
4609
  auth: null,
4594
4610
  session,
@@ -4603,8 +4619,12 @@ export async function createContext({ headers }: CreateContextOptions) {
4603
4619
 
4604
4620
  {{else if (and (eq backend 'self') (includes frontend "astro"))}}
4605
4621
  {{#if (eq auth "better-auth")}}
4622
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
4623
+ import { createAuth } from "@{{projectName}}/auth";
4624
+ {{else}}
4606
4625
  import { auth } from "@{{projectName}}/auth";
4607
4626
  {{/if}}
4627
+ {{/if}}
4608
4628
 
4609
4629
  export type CreateContextOptions = {
4610
4630
  headers: Headers;
@@ -4612,7 +4632,7 @@ export type CreateContextOptions = {
4612
4632
 
4613
4633
  export async function createContext({ headers }: CreateContextOptions) {
4614
4634
  {{#if (eq auth "better-auth")}}
4615
- const session = await auth.api.getSession({ headers });
4635
+ const session = await {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}createAuth(){{else}}auth{{/if}}.api.getSession({ headers });
4616
4636
  return {
4617
4637
  auth: null,
4618
4638
  session,
@@ -4628,8 +4648,12 @@ export async function createContext({ headers }: CreateContextOptions) {
4628
4648
  {{else if (eq backend 'hono')}}
4629
4649
  import type { Context as HonoContext } from "hono";
4630
4650
  {{#if (eq auth "better-auth")}}
4651
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
4652
+ import { createAuth } from "@{{projectName}}/auth";
4653
+ {{else}}
4631
4654
  import { auth } from "@{{projectName}}/auth";
4632
4655
  {{/if}}
4656
+ {{/if}}
4633
4657
 
4634
4658
  export type CreateContextOptions = {
4635
4659
  context: HonoContext;
@@ -4637,7 +4661,7 @@ export type CreateContextOptions = {
4637
4661
 
4638
4662
  export async function createContext({ context }: CreateContextOptions){{#if (eq auth "clerk")}}: Promise<ClerkRequestContext>{{/if}} {
4639
4663
  {{#if (eq auth "better-auth")}}
4640
- const session = await auth.api.getSession({
4664
+ const session = await {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}createAuth(){{else}}auth{{/if}}.api.getSession({
4641
4665
  headers: context.req.raw.headers,
4642
4666
  });
4643
4667
  return {
@@ -5371,12 +5395,16 @@ async function authenticateClerkRequest(request: Request): Promise<ClerkContextA
5371
5395
  {{#if (and (eq backend 'self') (includes frontend "next"))}}
5372
5396
  import type { NextRequest } from "next/server";
5373
5397
  {{#if (eq auth "better-auth")}}
5398
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
5399
+ import { createAuth } from "@{{projectName}}/auth";
5400
+ {{else}}
5374
5401
  import { auth } from "@{{projectName}}/auth";
5375
5402
  {{/if}}
5403
+ {{/if}}
5376
5404
 
5377
5405
  export async function createContext(req: NextRequest){{#if (eq auth "clerk")}}: Promise<ClerkRequestContext>{{/if}} {
5378
5406
  {{#if (eq auth "better-auth")}}
5379
- const session = await auth.api.getSession({
5407
+ const session = await {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}createAuth(){{else}}auth{{/if}}.api.getSession({
5380
5408
  headers: req.headers,
5381
5409
  });
5382
5410
  return {
@@ -5399,12 +5427,16 @@ export async function createContext(req: NextRequest){{#if (eq auth "clerk")}}:
5399
5427
 
5400
5428
  {{else if (and (eq backend 'self') (includes frontend "tanstack-start"))}}
5401
5429
  {{#if (eq auth "better-auth")}}
5430
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
5431
+ import { createAuth } from "@{{projectName}}/auth";
5432
+ {{else}}
5402
5433
  import { auth } from "@{{projectName}}/auth";
5403
5434
  {{/if}}
5435
+ {{/if}}
5404
5436
 
5405
5437
  export async function createContext({ req }: { req: Request }){{#if (eq auth "clerk")}}: Promise<ClerkRequestContext>{{/if}} {
5406
5438
  {{#if (eq auth "better-auth")}}
5407
- const session = await auth.api.getSession({
5439
+ const session = await {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}createAuth(){{else}}auth{{/if}}.api.getSession({
5408
5440
  headers: req.headers,
5409
5441
  });
5410
5442
  return {
@@ -5428,8 +5460,12 @@ export async function createContext({ req }: { req: Request }){{#if (eq auth "cl
5428
5460
  {{else if (eq backend 'hono')}}
5429
5461
  import type { Context as HonoContext } from "hono";
5430
5462
  {{#if (eq auth "better-auth")}}
5463
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
5464
+ import { createAuth } from "@{{projectName}}/auth";
5465
+ {{else}}
5431
5466
  import { auth } from "@{{projectName}}/auth";
5432
5467
  {{/if}}
5468
+ {{/if}}
5433
5469
 
5434
5470
  export type CreateContextOptions = {
5435
5471
  context: HonoContext;
@@ -5437,7 +5473,7 @@ export type CreateContextOptions = {
5437
5473
 
5438
5474
  export async function createContext({ context }: CreateContextOptions){{#if (eq auth "clerk")}}: Promise<ClerkRequestContext>{{/if}} {
5439
5475
  {{#if (eq auth "better-auth")}}
5440
- const session = await auth.api.getSession({
5476
+ const session = await {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}createAuth(){{else}}auth{{/if}}.api.getSession({
5441
5477
  headers: context.req.raw.headers,
5442
5478
  });
5443
5479
  return {
@@ -8834,10 +8870,17 @@ declare namespace App {
8834
8870
  }
8835
8871
  }
8836
8872
  `],
8837
- ["auth/better-auth/fullstack/astro/src/middleware.ts.hbs", `import { auth } from "@{{projectName}}/auth";
8873
+ ["auth/better-auth/fullstack/astro/src/middleware.ts.hbs", `{{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
8874
+ import { createAuth } from "@{{projectName}}/auth";
8875
+ {{else}}
8876
+ import { auth } from "@{{projectName}}/auth";
8877
+ {{/if}}
8838
8878
  import { defineMiddleware } from "astro:middleware";
8839
8879
 
8840
8880
  export const onRequest = defineMiddleware(async (context, next) => {
8881
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
8882
+ const auth = createAuth();
8883
+ {{/if}}
8841
8884
  const isAuthed = await auth.api.getSession({
8842
8885
  headers: context.request.headers,
8843
8886
  });
@@ -8853,34 +8896,72 @@ export const onRequest = defineMiddleware(async (context, next) => {
8853
8896
  return next();
8854
8897
  });
8855
8898
  `],
8856
- ["auth/better-auth/fullstack/astro/src/pages/api/auth/[...all].ts.hbs", `import { auth } from "@{{projectName}}/auth";
8899
+ ["auth/better-auth/fullstack/astro/src/pages/api/auth/[...all].ts.hbs", `{{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
8900
+ import { createAuth } from "@{{projectName}}/auth";
8901
+ {{else}}
8902
+ import { auth } from "@{{projectName}}/auth";
8903
+ {{/if}}
8857
8904
  import type { APIRoute } from "astro";
8858
8905
 
8859
8906
  export const ALL: APIRoute = async (ctx) => {
8907
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
8908
+ const auth = createAuth();
8909
+ {{/if}}
8860
8910
  return auth.handler(ctx.request);
8861
8911
  };
8862
8912
  `],
8863
- ["auth/better-auth/fullstack/next/src/app/api/auth/[...all]/route.ts.hbs", `import { auth } from "@{{projectName}}/auth";
8913
+ ["auth/better-auth/fullstack/next/src/app/api/auth/[...all]/route.ts.hbs", `{{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
8914
+ import { createAuth } from "@{{projectName}}/auth";
8915
+ {{else}}
8916
+ import { auth } from "@{{projectName}}/auth";
8917
+ {{/if}}
8864
8918
  import { toNextJsHandler } from "better-auth/next-js";
8865
8919
 
8866
- export const { GET, POST } = toNextJsHandler(auth.handler);
8920
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
8921
+ export async function GET(request: Request) {
8922
+ return toNextJsHandler(createAuth()).GET(request);
8923
+ }
8924
+
8925
+ export async function POST(request: Request) {
8926
+ return toNextJsHandler(createAuth()).POST(request);
8927
+ }
8928
+ {{else}}
8929
+ export const { GET, POST } = toNextJsHandler(auth);
8930
+ {{/if}}
8867
8931
  `],
8868
- ["auth/better-auth/fullstack/nuxt/server/api/auth/[...all].ts.hbs", `import { auth } from "@{{projectName}}/auth";
8932
+ ["auth/better-auth/fullstack/nuxt/server/api/auth/[...all].ts.hbs", `{{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
8933
+ import { createAuth } from "@{{projectName}}/auth";
8934
+ {{else}}
8935
+ import { auth } from "@{{projectName}}/auth";
8936
+ {{/if}}
8869
8937
 
8870
8938
  export default defineEventHandler((event) => {
8939
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
8940
+ const auth = createAuth();
8941
+ {{/if}}
8871
8942
  return auth.handler(toWebRequest(event));
8872
8943
  });
8873
8944
  `],
8874
- ["auth/better-auth/fullstack/tanstack-start/src/routes/api/auth/$.ts.hbs", `import { auth } from '@{{projectName}}/auth'
8945
+ ["auth/better-auth/fullstack/tanstack-start/src/routes/api/auth/$.ts.hbs", `{{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
8946
+ import { createAuth } from '@{{projectName}}/auth'
8947
+ {{else}}
8948
+ import { auth } from '@{{projectName}}/auth'
8949
+ {{/if}}
8875
8950
  import { createFileRoute } from '@tanstack/react-router'
8876
8951
 
8877
8952
  export const Route = createFileRoute('/api/auth/$')({
8878
8953
  server: {
8879
8954
  handlers: {
8880
8955
  GET: ({ request }) => {
8956
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
8957
+ const auth = createAuth()
8958
+ {{/if}}
8881
8959
  return auth.handler(request)
8882
8960
  },
8883
8961
  POST: ({ request }) => {
8962
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
8963
+ const auth = createAuth()
8964
+ {{/if}}
8884
8965
  return auth.handler(request)
8885
8966
  },
8886
8967
  },
@@ -10800,67 +10881,75 @@ import { env } from "@{{projectName}}/env/server";
10800
10881
  import { polar, checkout, portal } from "@polar-sh/better-auth";
10801
10882
  import { polarClient } from "./lib/payments";
10802
10883
  {{/if}}
10803
- import prisma from "@{{projectName}}/db";
10884
+ import { createPrismaClient } from "@{{projectName}}/db";
10804
10885
 
10805
- export const auth = betterAuth({
10806
- database: prismaAdapter(prisma, {
10886
+ export function createAuth() {
10887
+ const prisma = createPrismaClient();
10888
+
10889
+ return betterAuth({
10890
+ database: prismaAdapter(prisma, {
10807
10891
  {{#if (eq database "postgres")}}provider: "postgresql",{{/if}}
10808
10892
  {{#if (eq database "sqlite")}}provider: "sqlite",{{/if}}
10809
10893
  {{#if (eq database "mysql")}}provider: "mysql",{{/if}}
10810
10894
  {{#if (eq database "mongodb")}}provider: "mongodb",{{/if}}
10811
- }),
10895
+ }),
10812
10896
 
10813
- trustedOrigins: [
10814
- env.CORS_ORIGIN,
10897
+ trustedOrigins: [
10898
+ env.CORS_ORIGIN,
10815
10899
  {{#if (or (includes frontend "native-bare") (includes frontend "native-uniwind") (includes frontend "native-unistyles"))}}
10816
- "{{projectName}}://",
10817
- ...(env.NODE_ENV === "development"
10818
- ? [
10819
- "exp://",
10820
- "exp://**",
10821
- "exp://192.168.*.*:*/**",
10822
- "http://localhost:8081",
10823
- ]
10824
- : []),
10900
+ "{{projectName}}://",
10901
+ ...(env.NODE_ENV === "development"
10902
+ ? [
10903
+ "exp://",
10904
+ "exp://**",
10905
+ "exp://192.168.*.*:*/**",
10906
+ "http://localhost:8081",
10907
+ ]
10908
+ : []),
10825
10909
  {{/if}}
10826
- ],
10827
- emailAndPassword: {
10828
- enabled: true,
10829
- },
10830
- secret: env.BETTER_AUTH_SECRET,
10831
- baseURL: env.BETTER_AUTH_URL,
10910
+ ],
10911
+ emailAndPassword: {
10912
+ enabled: true,
10913
+ },
10914
+ secret: env.BETTER_AUTH_SECRET,
10915
+ baseURL: env.BETTER_AUTH_URL,
10832
10916
  {{#if (ne backend "self")}}
10833
- advanced: {
10834
- defaultCookieAttributes: {
10835
- sameSite: "none",
10836
- secure: true,
10837
- httpOnly: true,
10917
+ advanced: {
10918
+ defaultCookieAttributes: {
10919
+ sameSite: "none",
10920
+ secure: true,
10921
+ httpOnly: true,
10922
+ },
10838
10923
  },
10839
- },
10840
10924
  {{/if}}
10841
- plugins: [
10925
+ plugins: [
10842
10926
  {{#if (eq payments "polar")}}
10843
- polar({
10844
- client: polarClient,
10845
- createCustomerOnSignUp: true,
10846
- enableCustomerPortal: true,
10847
- use: [
10848
- checkout({
10849
- products: [
10850
- {
10851
- productId: "your-product-id",
10852
- slug: "pro",
10853
- },
10854
- ],
10855
- successUrl: env.POLAR_SUCCESS_URL,
10856
- authenticatedUsersOnly: true,
10857
- }),
10858
- portal(),
10859
- ],
10860
- }),
10927
+ polar({
10928
+ client: polarClient,
10929
+ createCustomerOnSignUp: true,
10930
+ enableCustomerPortal: true,
10931
+ use: [
10932
+ checkout({
10933
+ products: [
10934
+ {
10935
+ productId: "your-product-id",
10936
+ slug: "pro",
10937
+ },
10938
+ ],
10939
+ successUrl: env.POLAR_SUCCESS_URL,
10940
+ authenticatedUsersOnly: true,
10941
+ }),
10942
+ portal(),
10943
+ ],
10944
+ }),
10945
+ {{/if}}
10946
+ ],
10947
+ });
10948
+ }
10949
+
10950
+ {{#if (and (ne runtime "workers") (ne serverDeploy "cloudflare") (or (ne backend "self") (ne webDeploy "cloudflare")))}}
10951
+ export const auth = createAuth();
10861
10952
  {{/if}}
10862
- ],
10863
- });
10864
10953
  {{/if}}
10865
10954
 
10866
10955
  {{#if (eq orm "drizzle")}}
@@ -10872,68 +10961,76 @@ import { env } from "@{{projectName}}/env/server";
10872
10961
  import { polar, checkout, portal } from "@polar-sh/better-auth";
10873
10962
  import { polarClient } from "./lib/payments";
10874
10963
  {{/if}}
10875
- import { db } from "@{{projectName}}/db";
10964
+ import { createDb } from "@{{projectName}}/db";
10876
10965
  import * as schema from "@{{projectName}}/db/schema/auth";
10877
10966
 
10878
10967
 
10879
- export const auth = betterAuth({
10880
- database: drizzleAdapter(db, {
10968
+ export function createAuth() {
10969
+ const db = createDb();
10970
+
10971
+ return betterAuth({
10972
+ database: drizzleAdapter(db, {
10881
10973
  {{#if (eq database "postgres")}}provider: "pg",{{/if}}
10882
10974
  {{#if (eq database "sqlite")}}provider: "sqlite",{{/if}}
10883
10975
  {{#if (eq database "mysql")}}provider: "mysql",{{/if}}
10884
- schema: schema,
10885
- }),
10886
- trustedOrigins: [
10887
- env.CORS_ORIGIN,
10976
+ schema: schema,
10977
+ }),
10978
+ trustedOrigins: [
10979
+ env.CORS_ORIGIN,
10888
10980
  {{#if (or (includes frontend "native-bare") (includes frontend "native-uniwind") (includes frontend "native-unistyles"))}}
10889
- "{{projectName}}://",
10890
- ...(env.NODE_ENV === "development"
10891
- ? [
10892
- "exp://",
10893
- "exp://**",
10894
- "exp://192.168.*.*:*/**",
10895
- "http://localhost:8081",
10896
- ]
10897
- : []),
10981
+ "{{projectName}}://",
10982
+ ...(env.NODE_ENV === "development"
10983
+ ? [
10984
+ "exp://",
10985
+ "exp://**",
10986
+ "exp://192.168.*.*:*/**",
10987
+ "http://localhost:8081",
10988
+ ]
10989
+ : []),
10898
10990
  {{/if}}
10899
- ],
10900
- emailAndPassword: {
10901
- enabled: true,
10902
- },
10903
- secret: env.BETTER_AUTH_SECRET,
10904
- baseURL: env.BETTER_AUTH_URL,
10991
+ ],
10992
+ emailAndPassword: {
10993
+ enabled: true,
10994
+ },
10995
+ secret: env.BETTER_AUTH_SECRET,
10996
+ baseURL: env.BETTER_AUTH_URL,
10905
10997
  {{#if (ne backend "self")}}
10906
- advanced: {
10907
- defaultCookieAttributes: {
10908
- sameSite: "none",
10909
- secure: true,
10910
- httpOnly: true,
10998
+ advanced: {
10999
+ defaultCookieAttributes: {
11000
+ sameSite: "none",
11001
+ secure: true,
11002
+ httpOnly: true,
11003
+ },
10911
11004
  },
10912
- },
10913
11005
  {{/if}}
10914
- plugins: [
11006
+ plugins: [
10915
11007
  {{#if (eq payments "polar")}}
10916
- polar({
10917
- client: polarClient,
10918
- createCustomerOnSignUp: true,
10919
- enableCustomerPortal: true,
10920
- use: [
10921
- checkout({
10922
- products: [
10923
- {
10924
- productId: "your-product-id",
10925
- slug: "pro",
10926
- },
10927
- ],
10928
- successUrl: env.POLAR_SUCCESS_URL,
10929
- authenticatedUsersOnly: true,
10930
- }),
10931
- portal(),
10932
- ],
10933
- }),
11008
+ polar({
11009
+ client: polarClient,
11010
+ createCustomerOnSignUp: true,
11011
+ enableCustomerPortal: true,
11012
+ use: [
11013
+ checkout({
11014
+ products: [
11015
+ {
11016
+ productId: "your-product-id",
11017
+ slug: "pro",
11018
+ },
11019
+ ],
11020
+ successUrl: env.POLAR_SUCCESS_URL,
11021
+ authenticatedUsersOnly: true,
11022
+ }),
11023
+ portal(),
11024
+ ],
11025
+ }),
11026
+ {{/if}}
11027
+ ],
11028
+ });
11029
+ }
11030
+
11031
+ {{#if (and (ne runtime "workers") (ne serverDeploy "cloudflare") (or (ne backend "self") (ne webDeploy "cloudflare")))}}
11032
+ export const auth = createAuth();
10934
11033
  {{/if}}
10935
- ],
10936
- });
10937
11034
  {{/if}}
10938
11035
 
10939
11036
  {{#if (eq runtime "workers")}}
@@ -10944,79 +11041,83 @@ import { env } from "@{{projectName}}/env/server";
10944
11041
  import { polar, checkout, portal } from "@polar-sh/better-auth";
10945
11042
  import { polarClient } from "./lib/payments";
10946
11043
  {{/if}}
10947
- import { db } from "@{{projectName}}/db";
11044
+ import { createDb } from "@{{projectName}}/db";
10948
11045
  import * as schema from "@{{projectName}}/db/schema/auth";
10949
11046
 
10950
11047
 
10951
- export const auth = betterAuth({
10952
- database: drizzleAdapter(db, {
11048
+ export function createAuth() {
11049
+ const db = createDb();
11050
+
11051
+ return betterAuth({
11052
+ database: drizzleAdapter(db, {
10953
11053
  {{#if (eq database "postgres")}}provider: "pg",{{/if}}
10954
11054
  {{#if (eq database "sqlite")}}provider: "sqlite",{{/if}}
10955
11055
  {{#if (eq database "mysql")}}provider: "mysql",{{/if}}
10956
- schema: schema,
10957
- }),
10958
- trustedOrigins: [
10959
- env.CORS_ORIGIN,
11056
+ schema: schema,
11057
+ }),
11058
+ trustedOrigins: [
11059
+ env.CORS_ORIGIN,
10960
11060
  {{#if (or (includes frontend "native-bare") (includes frontend "native-uniwind") (includes frontend "native-unistyles"))}}
10961
- "{{projectName}}://",
10962
- ...(env.NODE_ENV === "development"
10963
- ? [
10964
- "exp://",
10965
- "exp://**",
10966
- "exp://192.168.*.*:*/**",
10967
- "http://localhost:8081",
10968
- ]
10969
- : []),
11061
+ "{{projectName}}://",
11062
+ ...(env.NODE_ENV === "development"
11063
+ ? [
11064
+ "exp://",
11065
+ "exp://**",
11066
+ "exp://192.168.*.*:*/**",
11067
+ "http://localhost:8081",
11068
+ ]
11069
+ : []),
10970
11070
  {{/if}}
10971
- ],
10972
- emailAndPassword: {
10973
- enabled: true,
10974
- },
10975
- // uncomment cookieCache setting when ready to deploy to Cloudflare using *.workers.dev domains
10976
- // session: {
10977
- // cookieCache: {
10978
- // enabled: true,
10979
- // maxAge: 60,
10980
- // },
10981
- // },
10982
- secret: env.BETTER_AUTH_SECRET,
10983
- baseURL: env.BETTER_AUTH_URL,
10984
- advanced: {
10985
- defaultCookieAttributes: {
10986
- sameSite: "none",
10987
- secure: true,
10988
- httpOnly: true,
11071
+ ],
11072
+ emailAndPassword: {
11073
+ enabled: true,
10989
11074
  },
10990
- // uncomment crossSubDomainCookies setting when ready to deploy and replace <your-workers-subdomain> with your actual workers subdomain
10991
- // https://developers.cloudflare.com/workers/wrangler/configuration/#workersdev
10992
- // crossSubDomainCookies: {
10993
- // enabled: true,
10994
- // domain: "<your-workers-subdomain>",
11075
+ // uncomment cookieCache setting when ready to deploy to Cloudflare using *.workers.dev domains
11076
+ // session: {
11077
+ // cookieCache: {
11078
+ // enabled: true,
11079
+ // maxAge: 60,
11080
+ // },
10995
11081
  // },
10996
- },
11082
+ secret: env.BETTER_AUTH_SECRET,
11083
+ baseURL: env.BETTER_AUTH_URL,
11084
+ advanced: {
11085
+ defaultCookieAttributes: {
11086
+ sameSite: "none",
11087
+ secure: true,
11088
+ httpOnly: true,
11089
+ },
11090
+ // uncomment crossSubDomainCookies setting when ready to deploy and replace <your-workers-subdomain> with your actual workers subdomain
11091
+ // https://developers.cloudflare.com/workers/wrangler/configuration/#workersdev
11092
+ // crossSubDomainCookies: {
11093
+ // enabled: true,
11094
+ // domain: "<your-workers-subdomain>",
11095
+ // },
11096
+ },
10997
11097
  {{#if (eq payments "polar")}}
10998
- plugins: [
10999
- polar({
11000
- client: polarClient,
11001
- createCustomerOnSignUp: true,
11002
- enableCustomerPortal: true,
11003
- use: [
11004
- checkout({
11005
- products: [
11006
- {
11007
- productId: "your-product-id",
11008
- slug: "pro",
11009
- },
11010
- ],
11011
- successUrl: env.POLAR_SUCCESS_URL,
11012
- authenticatedUsersOnly: true,
11013
- }),
11014
- portal(),
11015
- ],
11016
- }),
11017
- ],
11098
+ plugins: [
11099
+ polar({
11100
+ client: polarClient,
11101
+ createCustomerOnSignUp: true,
11102
+ enableCustomerPortal: true,
11103
+ use: [
11104
+ checkout({
11105
+ products: [
11106
+ {
11107
+ productId: "your-product-id",
11108
+ slug: "pro",
11109
+ },
11110
+ ],
11111
+ successUrl: env.POLAR_SUCCESS_URL,
11112
+ authenticatedUsersOnly: true,
11113
+ }),
11114
+ portal(),
11115
+ ],
11116
+ }),
11117
+ ],
11018
11118
  {{/if}}
11019
- });
11119
+ });
11120
+ }
11020
11121
  {{/if}}
11021
11122
  {{/if}}
11022
11123
 
@@ -11030,59 +11131,65 @@ import { polarClient } from "./lib/payments";
11030
11131
  {{/if}}
11031
11132
  import { client } from "@{{projectName}}/db";
11032
11133
 
11033
- export const auth = betterAuth({
11034
- database: mongodbAdapter(client),
11035
- trustedOrigins: [
11036
- env.CORS_ORIGIN,
11134
+ export function createAuth() {
11135
+ return betterAuth({
11136
+ database: mongodbAdapter(client),
11137
+ trustedOrigins: [
11138
+ env.CORS_ORIGIN,
11037
11139
  {{#if (or (includes frontend "native-bare") (includes frontend "native-uniwind") (includes frontend "native-unistyles"))}}
11038
- "{{projectName}}://",
11039
- ...(env.NODE_ENV === "development"
11040
- ? [
11041
- "exp://",
11042
- "exp://**",
11043
- "exp://192.168.*.*:*/**",
11044
- "http://localhost:8081",
11045
- ]
11046
- : []),
11140
+ "{{projectName}}://",
11141
+ ...(env.NODE_ENV === "development"
11142
+ ? [
11143
+ "exp://",
11144
+ "exp://**",
11145
+ "exp://192.168.*.*:*/**",
11146
+ "http://localhost:8081",
11147
+ ]
11148
+ : []),
11047
11149
  {{/if}}
11048
- ],
11049
- emailAndPassword: {
11050
- enabled: true,
11051
- },
11052
- secret: env.BETTER_AUTH_SECRET,
11053
- baseURL: env.BETTER_AUTH_URL,
11150
+ ],
11151
+ emailAndPassword: {
11152
+ enabled: true,
11153
+ },
11154
+ secret: env.BETTER_AUTH_SECRET,
11155
+ baseURL: env.BETTER_AUTH_URL,
11054
11156
  {{#if (ne backend "self")}}
11055
- advanced: {
11056
- defaultCookieAttributes: {
11057
- sameSite: "none",
11058
- secure: true,
11059
- httpOnly: true,
11157
+ advanced: {
11158
+ defaultCookieAttributes: {
11159
+ sameSite: "none",
11160
+ secure: true,
11161
+ httpOnly: true,
11162
+ },
11060
11163
  },
11061
- },
11062
11164
  {{/if}}
11063
11165
  {{#if (eq payments "polar")}}
11064
- plugins: [
11065
- polar({
11066
- client: polarClient,
11067
- createCustomerOnSignUp: true,
11068
- enableCustomerPortal: true,
11069
- use: [
11070
- checkout({
11071
- products: [
11072
- {
11073
- productId: "your-product-id",
11074
- slug: "pro",
11075
- },
11076
- ],
11077
- successUrl: env.POLAR_SUCCESS_URL,
11078
- authenticatedUsersOnly: true,
11079
- }),
11080
- portal(),
11081
- ],
11082
- }),
11083
- ],
11166
+ plugins: [
11167
+ polar({
11168
+ client: polarClient,
11169
+ createCustomerOnSignUp: true,
11170
+ enableCustomerPortal: true,
11171
+ use: [
11172
+ checkout({
11173
+ products: [
11174
+ {
11175
+ productId: "your-product-id",
11176
+ slug: "pro",
11177
+ },
11178
+ ],
11179
+ successUrl: env.POLAR_SUCCESS_URL,
11180
+ authenticatedUsersOnly: true,
11181
+ }),
11182
+ portal(),
11183
+ ],
11184
+ }),
11185
+ ],
11186
+ {{/if}}
11187
+ });
11188
+ }
11189
+
11190
+ {{#if (and (ne runtime "workers") (ne serverDeploy "cloudflare") (or (ne backend "self") (ne webDeploy "cloudflare")))}}
11191
+ export const auth = createAuth();
11084
11192
  {{/if}}
11085
- });
11086
11193
  {{/if}}
11087
11194
 
11088
11195
  {{#if (eq orm "none")}}
@@ -11094,59 +11201,65 @@ import { polarClient } from "./lib/payments";
11094
11201
  {{/if}}
11095
11202
 
11096
11203
 
11097
- export const auth = betterAuth({
11098
- database: "", // Invalid configuration
11099
- trustedOrigins: [
11100
- env.CORS_ORIGIN,
11204
+ export function createAuth() {
11205
+ return betterAuth({
11206
+ database: "", // Invalid configuration
11207
+ trustedOrigins: [
11208
+ env.CORS_ORIGIN,
11101
11209
  {{#if (or (includes frontend "native-bare") (includes frontend "native-uniwind") (includes frontend "native-unistyles"))}}
11102
- "{{projectName}}://",
11103
- ...(env.NODE_ENV === "development"
11104
- ? [
11105
- "exp://",
11106
- "exp://**",
11107
- "exp://192.168.*.*:*/**",
11108
- "http://localhost:8081",
11109
- ]
11110
- : []),
11210
+ "{{projectName}}://",
11211
+ ...(env.NODE_ENV === "development"
11212
+ ? [
11213
+ "exp://",
11214
+ "exp://**",
11215
+ "exp://192.168.*.*:*/**",
11216
+ "http://localhost:8081",
11217
+ ]
11218
+ : []),
11111
11219
  {{/if}}
11112
- ],
11113
- emailAndPassword: {
11114
- enabled: true,
11115
- },
11116
- secret: env.BETTER_AUTH_SECRET,
11117
- baseURL: env.BETTER_AUTH_URL,
11220
+ ],
11221
+ emailAndPassword: {
11222
+ enabled: true,
11223
+ },
11224
+ secret: env.BETTER_AUTH_SECRET,
11225
+ baseURL: env.BETTER_AUTH_URL,
11118
11226
  {{#if (ne backend "self")}}
11119
- advanced: {
11120
- defaultCookieAttributes: {
11121
- sameSite: "none",
11122
- secure: true,
11123
- httpOnly: true,
11227
+ advanced: {
11228
+ defaultCookieAttributes: {
11229
+ sameSite: "none",
11230
+ secure: true,
11231
+ httpOnly: true,
11232
+ },
11124
11233
  },
11125
- },
11126
11234
  {{/if}}
11127
11235
  {{#if (eq payments "polar")}}
11128
- plugins: [
11129
- polar({
11130
- client: polarClient,
11131
- createCustomerOnSignUp: true,
11132
- enableCustomerPortal: true,
11133
- use: [
11134
- checkout({
11135
- products: [
11136
- {
11137
- productId: "your-product-id",
11138
- slug: "pro",
11139
- },
11140
- ],
11141
- successUrl: env.POLAR_SUCCESS_URL,
11142
- authenticatedUsersOnly: true,
11143
- }),
11144
- portal(),
11145
- ],
11146
- }),
11147
- ],
11236
+ plugins: [
11237
+ polar({
11238
+ client: polarClient,
11239
+ createCustomerOnSignUp: true,
11240
+ enableCustomerPortal: true,
11241
+ use: [
11242
+ checkout({
11243
+ products: [
11244
+ {
11245
+ productId: "your-product-id",
11246
+ slug: "pro",
11247
+ },
11248
+ ],
11249
+ successUrl: env.POLAR_SUCCESS_URL,
11250
+ authenticatedUsersOnly: true,
11251
+ }),
11252
+ portal(),
11253
+ ],
11254
+ }),
11255
+ ],
11256
+ {{/if}}
11257
+ });
11258
+ }
11259
+
11260
+ {{#if (and (ne runtime "workers") (ne serverDeploy "cloudflare") (or (ne backend "self") (ne webDeploy "cloudflare")))}}
11261
+ export const auth = createAuth();
11148
11262
  {{/if}}
11149
- });
11150
11263
  {{/if}}
11151
11264
  `],
11152
11265
  ["auth/better-auth/server/base/tsconfig.json.hbs", `{
@@ -12623,13 +12736,19 @@ export default function Dashboard({
12623
12736
  import Dashboard from "./dashboard";
12624
12737
  import { headers } from "next/headers";
12625
12738
  {{#if (eq backend "self")}}
12739
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
12740
+ import { createAuth } from "@{{projectName}}/auth";
12741
+ {{else}}
12626
12742
  import { auth } from "@{{projectName}}/auth";
12627
12743
  {{/if}}
12744
+ {{/if}}
12745
+ {{#if (or (ne backend "self") (eq payments "polar"))}}
12628
12746
  import { authClient } from "@/lib/auth-client";
12747
+ {{/if}}
12629
12748
 
12630
12749
  export default async function DashboardPage() {
12631
12750
  {{#if (eq backend "self")}}
12632
- const session = await auth.api.getSession({
12751
+ const session = await {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}createAuth(){{else}}auth{{/if}}.api.getSession({
12633
12752
  headers: await headers(),
12634
12753
  });
12635
12754
  {{else}}
@@ -14313,12 +14432,16 @@ export const getUser = createServerFn({ method: "GET" }).middleware([authMiddlew
14313
14432
  return context.session
14314
14433
  })`],
14315
14434
  ["auth/better-auth/web/react/tanstack-start/src/middleware/auth.ts.hbs", `{{#if (eq backend "self")}}
14435
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
14436
+ import { createAuth } from "@{{projectName}}/auth";
14437
+ {{else}}
14316
14438
  import { auth } from "@{{projectName}}/auth";
14439
+ {{/if}}
14317
14440
  import { createMiddleware } from "@tanstack/react-start";
14318
14441
 
14319
14442
 
14320
14443
  export const authMiddleware = createMiddleware().server(async ({ next, request }) => {
14321
- const session = await auth.api.getSession({
14444
+ const session = await {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}createAuth(){{else}}auth{{/if}}.api.getSession({
14322
14445
  headers: request.headers,
14323
14446
  })
14324
14447
  return next({
@@ -17521,8 +17644,12 @@ import { createContext } from "@{{projectName}}/api/context";
17521
17644
  import { appRouter } from "@{{projectName}}/api/routers/index";
17522
17645
  {{/if}}
17523
17646
  {{#if (eq auth "better-auth")}}
17647
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
17648
+ import { createAuth } from "@{{projectName}}/auth";
17649
+ {{else}}
17524
17650
  import { auth } from "@{{projectName}}/auth";
17525
17651
  {{/if}}
17652
+ {{/if}}
17526
17653
  import { Hono } from "hono";
17527
17654
  import { cors } from "hono/cors";
17528
17655
  import { logger } from "hono/logger";
@@ -17555,7 +17682,16 @@ app.use(
17555
17682
  );
17556
17683
 
17557
17684
  {{#if (eq auth "better-auth")}}
17558
- app.on(["POST", "GET"], "/api/auth/*", (c) => auth.handler(c.req.raw));
17685
+ app.on(
17686
+ ["POST", "GET"],
17687
+ "/api/auth/*",
17688
+ (c) =>
17689
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
17690
+ createAuth().handler(c.req.raw)
17691
+ {{else}}
17692
+ auth.handler(c.req.raw)
17693
+ {{/if}}
17694
+ );
17559
17695
  {{/if}}
17560
17696
 
17561
17697
  {{#if (eq api "orpc")}}
@@ -17910,23 +18046,31 @@ import * as schema from "./schema";
17910
18046
  {{#if (eq dbSetup "planetscale")}}
17911
18047
  import { drizzle } from "drizzle-orm/planetscale-serverless";
17912
18048
 
17913
- export const db = drizzle({
17914
- connection: {
17915
- host: env.DATABASE_HOST,
17916
- username: env.DATABASE_USERNAME,
17917
- password: env.DATABASE_PASSWORD,
17918
- },
17919
- schema,
17920
- });
18049
+ export function createDb() {
18050
+ return drizzle({
18051
+ connection: {
18052
+ host: env.DATABASE_HOST,
18053
+ username: env.DATABASE_USERNAME,
18054
+ password: env.DATABASE_PASSWORD,
18055
+ },
18056
+ schema,
18057
+ });
18058
+ }
17921
18059
  {{else}}
17922
18060
  import { drizzle } from "drizzle-orm/mysql2";
17923
18061
 
17924
- export const db = drizzle({
17925
- connection: {
17926
- uri: env.DATABASE_URL,
17927
- },
17928
- schema,
17929
- });
18062
+ export function createDb() {
18063
+ return drizzle({
18064
+ connection: {
18065
+ uri: env.DATABASE_URL,
18066
+ },
18067
+ schema,
18068
+ });
18069
+ }
18070
+ {{/if}}
18071
+
18072
+ {{#if (and (ne serverDeploy "cloudflare") (or (ne backend "self") (ne webDeploy "cloudflare")))}}
18073
+ export const db = createDb();
17930
18074
  {{/if}}
17931
18075
  {{/if}}
17932
18076
 
@@ -17937,24 +18081,28 @@ import * as schema from "./schema";
17937
18081
  import { drizzle } from "drizzle-orm/planetscale-serverless";
17938
18082
  import { env } from "@{{projectName}}/env/server";
17939
18083
 
17940
- export const db = drizzle({
17941
- connection: {
17942
- host: env.DATABASE_HOST,
17943
- username: env.DATABASE_USERNAME,
17944
- password: env.DATABASE_PASSWORD,
17945
- },
17946
- schema,
17947
- });
18084
+ export function createDb() {
18085
+ return drizzle({
18086
+ connection: {
18087
+ host: env.DATABASE_HOST,
18088
+ username: env.DATABASE_USERNAME,
18089
+ password: env.DATABASE_PASSWORD,
18090
+ },
18091
+ schema,
18092
+ });
18093
+ }
17948
18094
  {{else}}
17949
18095
  import { drizzle } from "drizzle-orm/mysql2";
17950
18096
  import { env } from "@{{projectName}}/env/server";
17951
18097
 
17952
- export const db = drizzle({
17953
- connection: {
17954
- uri: env.DATABASE_URL,
17955
- },
17956
- schema,
17957
- });
18098
+ export function createDb() {
18099
+ return drizzle({
18100
+ connection: {
18101
+ uri: env.DATABASE_URL,
18102
+ },
18103
+ schema,
18104
+ });
18105
+ }
17958
18106
  {{/if}}
17959
18107
  {{/if}}
17960
18108
  `],
@@ -17986,12 +18134,32 @@ import * as schema from "./schema";
17986
18134
  import { neon } from '@neondatabase/serverless';
17987
18135
  import { drizzle } from 'drizzle-orm/neon-http';
17988
18136
 
17989
- const sql = neon(env.DATABASE_URL);
17990
- export const db = drizzle(sql, { schema });
18137
+ export function createDb() {
18138
+ const sql = neon(env.DATABASE_URL);
18139
+ return drizzle(sql, { schema });
18140
+ }
17991
18141
  {{else}}
17992
18142
  import { drizzle } from "drizzle-orm/node-postgres";
18143
+ {{#if (and (eq backend "self") (eq webDeploy "cloudflare"))}}
18144
+ import { Pool } from "pg";
18145
+ {{/if}}
17993
18146
 
17994
- export const db = drizzle(env.DATABASE_URL, { schema });
18147
+ export function createDb() {
18148
+ {{#if (and (eq backend "self") (eq webDeploy "cloudflare"))}}
18149
+ const pool = new Pool({
18150
+ connectionString: env.DATABASE_URL,
18151
+ maxUses: 1,
18152
+ });
18153
+
18154
+ return drizzle({ client: pool, schema });
18155
+ {{else}}
18156
+ return drizzle(env.DATABASE_URL, { schema });
18157
+ {{/if}}
18158
+ }
18159
+ {{/if}}
18160
+
18161
+ {{#if (and (ne serverDeploy "cloudflare") (or (ne backend "self") (ne webDeploy "cloudflare")))}}
18162
+ export const db = createDb();
17995
18163
  {{/if}}
17996
18164
  {{/if}}
17997
18165
 
@@ -18003,15 +18171,26 @@ import { neon } from '@neondatabase/serverless';
18003
18171
  import { drizzle } from 'drizzle-orm/neon-http';
18004
18172
  import { env } from "@{{projectName}}/env/server";
18005
18173
 
18006
- const sql = neon(env.DATABASE_URL || "");
18007
- export const db = drizzle(sql, { schema });
18174
+ export function createDb() {
18175
+ const sql = neon(env.DATABASE_URL || "");
18176
+ return drizzle(sql, { schema });
18177
+ }
18008
18178
  {{else}}
18009
18179
  import { drizzle } from "drizzle-orm/node-postgres";
18010
18180
  import { env } from "@{{projectName}}/env/server";
18181
+ import { Pool } from "pg";
18182
+
18183
+ export function createDb() {
18184
+ const pool = new Pool({
18185
+ connectionString: env.DATABASE_URL || "",
18186
+ maxUses: 1,
18187
+ });
18011
18188
 
18012
- export const db = drizzle(env.DATABASE_URL || "", { schema });
18189
+ return drizzle({ client: pool, schema });
18190
+ }
18013
18191
  {{/if}}
18014
- {{/if}}`],
18192
+ {{/if}}
18193
+ `],
18015
18194
  ["db/drizzle/sqlite/drizzle.config.ts.hbs", `import { defineConfig } from "drizzle-kit";
18016
18195
  import dotenv from "dotenv";
18017
18196
 
@@ -18041,44 +18220,50 @@ export default defineConfig({
18041
18220
  {{/if}}
18042
18221
  });
18043
18222
  `],
18044
- ["db/drizzle/sqlite/src/index.ts.hbs", `{{#if (or (eq runtime "bun") (eq runtime "node") (eq runtime "none"))}}
18223
+ ["db/drizzle/sqlite/src/index.ts.hbs", `{{#if (eq dbSetup "d1")}}
18224
+ import * as schema from "./schema";
18225
+ import { drizzle } from "drizzle-orm/d1";
18226
+ import { env } from "@{{projectName}}/env/server";
18227
+
18228
+ export function createDb() {
18229
+ return drizzle(env.DB, { schema });
18230
+ }
18231
+ {{else if (or (eq runtime "bun") (eq runtime "node") (eq runtime "none"))}}
18045
18232
  import { env } from "@{{projectName}}/env/server";
18046
18233
  import * as schema from "./schema";
18047
18234
  import { drizzle } from "drizzle-orm/libsql";
18048
18235
  import { createClient } from "@libsql/client";
18049
18236
 
18050
- const client = createClient({
18051
- url: env.DATABASE_URL,
18237
+ export function createDb() {
18238
+ const client = createClient({
18239
+ url: env.DATABASE_URL,
18052
18240
  {{#if (eq dbSetup "turso")}}
18053
- authToken: env.DATABASE_AUTH_TOKEN,
18241
+ authToken: env.DATABASE_AUTH_TOKEN,
18054
18242
  {{/if}}
18055
- });
18243
+ });
18056
18244
 
18057
- export const db = drizzle({ client, schema });
18058
- {{/if}}
18245
+ return drizzle({ client, schema });
18246
+ }
18059
18247
 
18060
- {{#if (eq runtime "workers")}}
18248
+ {{#if (and (ne serverDeploy "cloudflare") (or (ne backend "self") (ne webDeploy "cloudflare")))}}
18249
+ export const db = createDb();
18250
+ {{/if}}
18251
+ {{else if (eq runtime "workers")}}
18061
18252
  import * as schema from "./schema";
18062
-
18063
- {{#if (eq dbSetup "d1")}}
18064
- import { drizzle } from "drizzle-orm/d1";
18065
- import { env } from "@{{projectName}}/env/server";
18066
-
18067
- export const db = drizzle(env.DB, { schema });
18068
- {{else}}
18069
18253
  import { drizzle } from "drizzle-orm/libsql";
18070
18254
  import { env } from "@{{projectName}}/env/server";
18071
18255
  import { createClient } from "@libsql/client";
18072
18256
 
18073
- const client = createClient({
18074
- url: env.DATABASE_URL || "",
18257
+ export function createDb() {
18258
+ const client = createClient({
18259
+ url: env.DATABASE_URL || "",
18075
18260
  {{#if (eq dbSetup "turso")}}
18076
- authToken: env.DATABASE_AUTH_TOKEN,
18261
+ authToken: env.DATABASE_AUTH_TOKEN,
18077
18262
  {{/if}}
18078
- });
18263
+ });
18079
18264
 
18080
- export const db = drizzle({ client, schema });
18081
- {{/if}}
18265
+ return drizzle({ client, schema });
18266
+ }
18082
18267
  {{/if}}
18083
18268
  `],
18084
18269
  ["db/mongoose/mongodb/src/index.ts.hbs", `import mongoose from "mongoose";
@@ -18186,26 +18371,28 @@ import { env } from "@{{projectName}}/env/server";
18186
18371
  {{#if (eq dbSetup "planetscale")}}
18187
18372
  import { PrismaPlanetScale } from "@prisma/adapter-planetscale";
18188
18373
 
18189
- const adapter = new PrismaPlanetScale({ url: env.DATABASE_URL });
18190
- const prisma = new PrismaClient({ adapter });
18374
+ export function createPrismaClient() {
18375
+ const adapter = new PrismaPlanetScale({ url: env.DATABASE_URL });
18376
+ return new PrismaClient({ adapter });
18377
+ }
18191
18378
  {{else}}
18192
18379
  import { PrismaMariaDb } from "@prisma/adapter-mariadb";
18193
18380
 
18194
- const databaseUrl: string = env.DATABASE_URL;
18195
- const url: URL = new URL(databaseUrl);
18196
- const connectionConfig = {
18197
- host: url.hostname,
18198
- port: parseInt(url.port || "3306"),
18199
- user: url.username,
18200
- password: url.password,
18201
- database: url.pathname.slice(1),
18202
- };
18381
+ export function createPrismaClient() {
18382
+ const databaseUrl: string = env.DATABASE_URL;
18383
+ const url: URL = new URL(databaseUrl);
18384
+ const connectionConfig = {
18385
+ host: url.hostname,
18386
+ port: parseInt(url.port || "3306"),
18387
+ user: url.username,
18388
+ password: url.password,
18389
+ database: url.pathname.slice(1),
18390
+ };
18203
18391
 
18204
- const adapter = new PrismaMariaDb(connectionConfig);
18205
- const prisma = new PrismaClient({ adapter });
18392
+ const adapter = new PrismaMariaDb(connectionConfig);
18393
+ return new PrismaClient({ adapter });
18394
+ }
18206
18395
  {{/if}}
18207
-
18208
- export default prisma;
18209
18396
  {{else}}
18210
18397
  import { PrismaClient } from "../prisma/generated/client";
18211
18398
  import { env } from "@{{projectName}}/env/server";
@@ -18213,27 +18400,35 @@ import { env } from "@{{projectName}}/env/server";
18213
18400
  {{#if (eq dbSetup "planetscale")}}
18214
18401
  import { PrismaPlanetScale } from "@prisma/adapter-planetscale";
18215
18402
 
18216
- const adapter = new PrismaPlanetScale({ url: env.DATABASE_URL });
18217
- const prisma = new PrismaClient({ adapter });
18403
+ export function createPrismaClient() {
18404
+ const adapter = new PrismaPlanetScale({ url: env.DATABASE_URL });
18405
+ return new PrismaClient({ adapter });
18406
+ }
18218
18407
  {{else}}
18219
18408
  import { PrismaMariaDb } from "@prisma/adapter-mariadb";
18220
18409
 
18221
- const databaseUrl: string = env.DATABASE_URL;
18222
- const url: URL = new URL(databaseUrl);
18223
- const connectionConfig = {
18224
- host: url.hostname,
18225
- port: parseInt(url.port || "3306"),
18226
- user: url.username,
18227
- password: url.password,
18228
- database: url.pathname.slice(1),
18229
- };
18410
+ export function createPrismaClient() {
18411
+ const databaseUrl: string = env.DATABASE_URL;
18412
+ const url: URL = new URL(databaseUrl);
18413
+ const connectionConfig = {
18414
+ host: url.hostname,
18415
+ port: parseInt(url.port || "3306"),
18416
+ user: url.username,
18417
+ password: url.password,
18418
+ database: url.pathname.slice(1),
18419
+ };
18230
18420
 
18231
- const adapter = new PrismaMariaDb(connectionConfig);
18232
- const prisma = new PrismaClient({ adapter });
18421
+ const adapter = new PrismaMariaDb(connectionConfig);
18422
+ return new PrismaClient({ adapter });
18423
+ }
18233
18424
  {{/if}}
18234
18425
 
18426
+ {{#if (and (ne serverDeploy "cloudflare") (or (ne backend "self") (ne webDeploy "cloudflare")))}}
18427
+ const prisma = createPrismaClient();
18235
18428
  export default prisma;
18236
- {{/if}}`],
18429
+ {{/if}}
18430
+ {{/if}}
18431
+ `],
18237
18432
  ["db/prisma/postgres/prisma.config.ts.hbs", `import path from "node:path";
18238
18433
  import { defineConfig, env } from 'prisma/config'
18239
18434
  import dotenv from 'dotenv'
@@ -18287,61 +18482,86 @@ import { neonConfig } from "@neondatabase/serverless";
18287
18482
 
18288
18483
  neonConfig.poolQueryViaFetch = true;
18289
18484
 
18290
- const prisma = new PrismaClient({
18291
- adapter: new PrismaNeon({
18292
- connectionString: env.DATABASE_URL,
18293
- }),
18294
- });
18485
+ export function createPrismaClient() {
18486
+ return new PrismaClient({
18487
+ adapter: new PrismaNeon({
18488
+ connectionString: env.DATABASE_URL,
18489
+ }),
18490
+ });
18491
+ }
18295
18492
 
18296
18493
  {{else if (eq dbSetup "prisma-postgres")}}
18297
18494
  import { PrismaPg } from "@prisma/adapter-pg";
18298
18495
 
18299
- const adapter = new PrismaPg({
18300
- connectionString: env.DATABASE_URL,
18301
- });
18496
+ export function createPrismaClient() {
18497
+ const adapter = new PrismaPg({
18498
+ connectionString: env.DATABASE_URL,
18499
+ maxUses: 1,
18500
+ });
18302
18501
 
18303
- const prisma = new PrismaClient({ adapter });
18502
+ return new PrismaClient({ adapter });
18503
+ }
18304
18504
 
18305
18505
  {{else}}
18306
18506
  import { PrismaPg } from "@prisma/adapter-pg";
18307
18507
 
18308
- const adapter = new PrismaPg({ connectionString: env.DATABASE_URL });
18309
- const prisma = new PrismaClient({ adapter });
18508
+ export function createPrismaClient() {
18509
+ const adapter = new PrismaPg({
18510
+ connectionString: env.DATABASE_URL,
18511
+ maxUses: 1,
18512
+ });
18513
+ return new PrismaClient({ adapter });
18514
+ }
18310
18515
 
18311
18516
  {{/if}}
18312
-
18313
- export default prisma;
18314
18517
  {{else}}
18315
18518
  import { PrismaClient } from "../prisma/generated/client";
18316
18519
  import { env } from "@{{projectName}}/env/server";
18317
18520
  {{#if (eq dbSetup "neon")}}
18318
18521
  import { PrismaNeon } from "@prisma/adapter-neon";
18319
18522
 
18320
- const adapter = new PrismaNeon({
18321
- connectionString: env.DATABASE_URL,
18322
- });
18523
+ export function createPrismaClient() {
18524
+ const adapter = new PrismaNeon({
18525
+ connectionString: env.DATABASE_URL,
18526
+ });
18323
18527
 
18324
- const prisma = new PrismaClient({ adapter });
18528
+ return new PrismaClient({ adapter });
18529
+ }
18325
18530
 
18326
18531
  {{else if (eq dbSetup "prisma-postgres")}}
18327
18532
  import { PrismaPg } from "@prisma/adapter-pg";
18328
18533
 
18329
- const adapter = new PrismaPg({
18330
- connectionString: env.DATABASE_URL,
18331
- });
18534
+ export function createPrismaClient() {
18535
+ const adapter = new PrismaPg({
18536
+ connectionString: env.DATABASE_URL,
18537
+ {{#if (and (eq backend "self") (eq webDeploy "cloudflare"))}}
18538
+ maxUses: 1,
18539
+ {{/if}}
18540
+ });
18332
18541
 
18333
- const prisma = new PrismaClient({ adapter });
18542
+ return new PrismaClient({ adapter });
18543
+ }
18334
18544
 
18335
18545
  {{else}}
18336
18546
  import { PrismaPg } from "@prisma/adapter-pg";
18337
18547
 
18338
- const adapter = new PrismaPg({ connectionString: env.DATABASE_URL });
18339
- const prisma = new PrismaClient({ adapter });
18340
-
18548
+ export function createPrismaClient() {
18549
+ const adapter = new PrismaPg({
18550
+ connectionString: env.DATABASE_URL,
18551
+ {{#if (and (eq backend "self") (eq webDeploy "cloudflare"))}}
18552
+ maxUses: 1,
18341
18553
  {{/if}}
18554
+ });
18555
+ return new PrismaClient({ adapter });
18556
+ }
18342
18557
 
18558
+ {{/if}}
18559
+ {{#if (and (ne serverDeploy "cloudflare") (or (ne backend "self") (ne webDeploy "cloudflare")))}}
18560
+ const prisma = createPrismaClient();
18343
18561
  export default prisma;
18344
- {{/if}}`],
18562
+ {{/if}}
18563
+ {{/if}}
18564
+ `],
18345
18565
  ["db/prisma/sqlite/prisma.config.ts.hbs", `import path from "node:path";
18346
18566
  import { defineConfig, env } from "prisma/config";
18347
18567
  import dotenv from "dotenv";
@@ -18392,25 +18612,36 @@ datasource db {
18392
18612
  import { PrismaD1 } from "@prisma/adapter-d1";
18393
18613
  import { env } from "@{{projectName}}/env/server";
18394
18614
 
18395
- const adapter = new PrismaD1(env.DB);
18396
- const prisma = new PrismaClient({ adapter });
18615
+ export function createPrismaClient() {
18616
+ const adapter = new PrismaD1(env.DB);
18617
+ return new PrismaClient({ adapter });
18618
+ }
18397
18619
 
18620
+ {{#if (and (ne runtime "workers") (ne serverDeploy "cloudflare") (or (ne backend "self") (ne webDeploy "cloudflare")))}}
18621
+ const prisma = createPrismaClient();
18398
18622
  export default prisma;
18623
+ {{/if}}
18399
18624
  {{else}}
18400
18625
  import { PrismaLibSql } from "@prisma/adapter-libsql";
18401
18626
  import { env } from "@{{projectName}}/env/server";
18402
18627
 
18403
- const adapter = new PrismaLibSql({
18404
- url: env.DATABASE_URL,
18628
+ export function createPrismaClient() {
18629
+ const adapter = new PrismaLibSql({
18630
+ url: env.DATABASE_URL,
18405
18631
  {{#if (eq dbSetup "turso")}}
18406
- authToken: env.DATABASE_AUTH_TOKEN || "",
18632
+ authToken: env.DATABASE_AUTH_TOKEN || "",
18407
18633
  {{/if}}
18408
- });
18634
+ });
18409
18635
 
18410
- const prisma = new PrismaClient({ adapter });
18636
+ return new PrismaClient({ adapter });
18637
+ }
18411
18638
 
18639
+ {{#if (and (ne runtime "workers") (ne serverDeploy "cloudflare") (or (ne backend "self") (ne webDeploy "cloudflare")))}}
18640
+ const prisma = createPrismaClient();
18412
18641
  export default prisma;
18413
- {{/if}}`],
18642
+ {{/if}}
18643
+ {{/if}}
18644
+ `],
18414
18645
  ["examples/ai/convex/packages/backend/convex/agent.ts.hbs", `import { Agent } from "@convex-dev/agent";
18415
18646
  import { google } from "@ai-sdk/google";
18416
18647
  import { components } from "./_generated/api";
@@ -22596,18 +22827,28 @@ export default function TodosScreen() {
22596
22827
  ["examples/todo/server/drizzle/base/src/routers/todo.ts.hbs", `{{#if (eq api "orpc")}}
22597
22828
  import { eq } from "drizzle-orm";
22598
22829
  import z from "zod";
22830
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
22831
+ import { createDb } from "@{{projectName}}/db";
22832
+ {{else}}
22599
22833
  import { db } from "@{{projectName}}/db";
22834
+ {{/if}}
22600
22835
  import { todo } from "@{{projectName}}/db/schema/todo";
22601
22836
  import { publicProcedure } from "../index";
22602
22837
 
22603
22838
  export const todoRouter = {
22604
22839
  getAll: publicProcedure.handler(async () => {
22840
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
22841
+ const db = createDb();
22842
+ {{/if}}
22605
22843
  return await db.select().from(todo);
22606
22844
  }),
22607
22845
 
22608
22846
  create: publicProcedure
22609
22847
  .input(z.object({ text: z.string().min(1) }))
22610
22848
  .handler(async ({ input }) => {
22849
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
22850
+ const db = createDb();
22851
+ {{/if}}
22611
22852
  return await db
22612
22853
  .insert(todo)
22613
22854
  .values({
@@ -22618,6 +22859,9 @@ export const todoRouter = {
22618
22859
  toggle: publicProcedure
22619
22860
  .input(z.object({ id: z.number(), completed: z.boolean() }))
22620
22861
  .handler(async ({ input }) => {
22862
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
22863
+ const db = createDb();
22864
+ {{/if}}
22621
22865
  return await db
22622
22866
  .update(todo)
22623
22867
  .set({ completed: input.completed })
@@ -22627,6 +22871,9 @@ export const todoRouter = {
22627
22871
  delete: publicProcedure
22628
22872
  .input(z.object({ id: z.number() }))
22629
22873
  .handler(async ({ input }) => {
22874
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
22875
+ const db = createDb();
22876
+ {{/if}}
22630
22877
  return await db.delete(todo).where(eq(todo.id, input.id));
22631
22878
  }),
22632
22879
  };
@@ -22637,16 +22884,26 @@ import z from "zod";
22637
22884
  import { router, publicProcedure } from "../index";
22638
22885
  import { todo } from "@{{projectName}}/db/schema/todo";
22639
22886
  import { eq } from "drizzle-orm";
22887
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
22888
+ import { createDb } from "@{{projectName}}/db";
22889
+ {{else}}
22640
22890
  import { db } from "@{{projectName}}/db";
22891
+ {{/if}}
22641
22892
 
22642
22893
  export const todoRouter = router({
22643
22894
  getAll: publicProcedure.query(async () => {
22895
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
22896
+ const db = createDb();
22897
+ {{/if}}
22644
22898
  return await db.select().from(todo);
22645
22899
  }),
22646
22900
 
22647
22901
  create: publicProcedure
22648
22902
  .input(z.object({ text: z.string().min(1) }))
22649
22903
  .mutation(async ({ input }) => {
22904
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
22905
+ const db = createDb();
22906
+ {{/if}}
22650
22907
  return await db.insert(todo).values({
22651
22908
  text: input.text,
22652
22909
  });
@@ -22655,6 +22912,9 @@ export const todoRouter = router({
22655
22912
  toggle: publicProcedure
22656
22913
  .input(z.object({ id: z.number(), completed: z.boolean() }))
22657
22914
  .mutation(async ({ input }) => {
22915
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
22916
+ const db = createDb();
22917
+ {{/if}}
22658
22918
  return await db
22659
22919
  .update(todo)
22660
22920
  .set({ completed: input.completed })
@@ -22664,6 +22924,9 @@ export const todoRouter = router({
22664
22924
  delete: publicProcedure
22665
22925
  .input(z.object({ id: z.number() }))
22666
22926
  .mutation(async ({ input }) => {
22927
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
22928
+ const db = createDb();
22929
+ {{/if}}
22667
22930
  return await db.delete(todo).where(eq(todo.id, input.id));
22668
22931
  }),
22669
22932
  });
@@ -22787,11 +23050,18 @@ export { Todo };
22787
23050
  `],
22788
23051
  ["examples/todo/server/prisma/base/src/routers/todo.ts.hbs", `{{#if (eq api "orpc")}}
22789
23052
  import z from "zod";
23053
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
23054
+ import { createPrismaClient } from "@{{projectName}}/db";
23055
+ {{else}}
22790
23056
  import prisma from "@{{projectName}}/db";
23057
+ {{/if}}
22791
23058
  import { publicProcedure } from "../index";
22792
23059
 
22793
23060
  export const todoRouter = {
22794
23061
  getAll: publicProcedure.handler(async () => {
23062
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
23063
+ const prisma = createPrismaClient();
23064
+ {{/if}}
22795
23065
  return await prisma.todo.findMany({
22796
23066
  orderBy: {
22797
23067
  id: "asc",
@@ -22802,6 +23072,9 @@ export const todoRouter = {
22802
23072
  create: publicProcedure
22803
23073
  .input(z.object({ text: z.string().min(1) }))
22804
23074
  .handler(async ({ input }) => {
23075
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
23076
+ const prisma = createPrismaClient();
23077
+ {{/if}}
22805
23078
  return await prisma.todo.create({
22806
23079
  data: {
22807
23080
  text: input.text,
@@ -22816,6 +23089,9 @@ export const todoRouter = {
22816
23089
  .input(z.object({ id: z.number(), completed: z.boolean() }))
22817
23090
  {{/if}}
22818
23091
  .handler(async ({ input }) => {
23092
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
23093
+ const prisma = createPrismaClient();
23094
+ {{/if}}
22819
23095
  return await prisma.todo.update({
22820
23096
  where: { id: input.id },
22821
23097
  data: { completed: input.completed },
@@ -22829,6 +23105,9 @@ export const todoRouter = {
22829
23105
  .input(z.object({ id: z.number() }))
22830
23106
  {{/if}}
22831
23107
  .handler(async ({ input }) => {
23108
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
23109
+ const prisma = createPrismaClient();
23110
+ {{/if}}
22832
23111
  return await prisma.todo.delete({
22833
23112
  where: { id: input.id },
22834
23113
  });
@@ -22839,11 +23118,18 @@ export const todoRouter = {
22839
23118
  {{#if (eq api "trpc")}}
22840
23119
  import { TRPCError } from "@trpc/server";
22841
23120
  import z from "zod";
23121
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
23122
+ import { createPrismaClient } from "@{{projectName}}/db";
23123
+ {{else}}
22842
23124
  import prisma from "@{{projectName}}/db";
23125
+ {{/if}}
22843
23126
  import { publicProcedure, router } from "../index";
22844
23127
 
22845
23128
  export const todoRouter = router({
22846
23129
  getAll: publicProcedure.query(async () => {
23130
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
23131
+ const prisma = createPrismaClient();
23132
+ {{/if}}
22847
23133
  return await prisma.todo.findMany({
22848
23134
  orderBy: {
22849
23135
  id: "asc"
@@ -22854,6 +23140,9 @@ export const todoRouter = router({
22854
23140
  create: publicProcedure
22855
23141
  .input(z.object({ text: z.string().min(1) }))
22856
23142
  .mutation(async ({ input }) => {
23143
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
23144
+ const prisma = createPrismaClient();
23145
+ {{/if}}
22857
23146
  return await prisma.todo.create({
22858
23147
  data: {
22859
23148
  text: input.text,
@@ -22868,6 +23157,9 @@ export const todoRouter = router({
22868
23157
  .input(z.object({ id: z.number(), completed: z.boolean() }))
22869
23158
  {{/if}}
22870
23159
  .mutation(async ({ input }) => {
23160
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
23161
+ const prisma = createPrismaClient();
23162
+ {{/if}}
22871
23163
  try {
22872
23164
  return await prisma.todo.update({
22873
23165
  where: { id: input.id },
@@ -22888,6 +23180,9 @@ export const todoRouter = router({
22888
23180
  .input(z.object({ id: z.number() }))
22889
23181
  {{/if}}
22890
23182
  .mutation(async ({ input }) => {
23183
+ {{#if (or (eq runtime "workers") (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
23184
+ const prisma = createPrismaClient();
23185
+ {{/if}}
22891
23186
  try {
22892
23187
  return await prisma.todo.delete({
22893
23188
  where: { id: input.id },
@@ -31613,7 +31908,69 @@ export const env = createEnv({
31613
31908
  emptyStringAsUndefined: true,
31614
31909
  });
31615
31910
  `],
31616
- ["packages/env/src/server.ts.hbs", `{{#if (or (eq serverDeploy "cloudflare") (and (eq backend "self") (eq webDeploy "cloudflare")))}}
31911
+ ["packages/env/src/server.ts.hbs", `{{#if (eq serverDeploy "cloudflare")}}
31912
+ /// <reference path="../env.d.ts" />
31913
+ // For Cloudflare Workers, env is accessed via cloudflare:workers module
31914
+ // Types are defined in env.d.ts based on your alchemy.run.ts bindings
31915
+ export { env } from "cloudflare:workers";
31916
+ {{else if (and (eq backend "self") (eq webDeploy "cloudflare") (includes frontend "next"))}}
31917
+ /// <reference path="../env.d.ts" />
31918
+ import { getCloudflareContext } from "@opennextjs/cloudflare";
31919
+
31920
+ function getNodeEnvValue(key: string) {
31921
+ return process.env[key];
31922
+ }
31923
+
31924
+ function getCloudflareEnvSync() {
31925
+ try {
31926
+ return getCloudflareContext().env as Env;
31927
+ } catch {
31928
+ return undefined;
31929
+ }
31930
+ }
31931
+
31932
+ type EnvValue = Env[keyof Env];
31933
+
31934
+ function createEnvProxy(getValue: (key: keyof Env & string) => EnvValue | undefined) {
31935
+ return new Proxy({} as Env, {
31936
+ get(_target, prop) {
31937
+ if (typeof prop !== "string") {
31938
+ return undefined;
31939
+ }
31940
+
31941
+ return getValue(prop as keyof Env & string);
31942
+ },
31943
+ });
31944
+ }
31945
+
31946
+ function resolveEnvValue(key: keyof Env & string): EnvValue | undefined {
31947
+ const nodeValue = getNodeEnvValue(key);
31948
+ if (nodeValue !== undefined) {
31949
+ return nodeValue as EnvValue;
31950
+ }
31951
+
31952
+ return getCloudflareEnvSync()?.[key as keyof Env];
31953
+ }
31954
+
31955
+ // Next.js local dev runs in Node.js, where env vars are exposed on process.env.
31956
+ // In the Cloudflare runtime, fall back to OpenNext's Cloudflare context bindings.
31957
+ // For static routes (ISR/SSG), use getEnvAsync() so OpenNext can resolve bindings
31958
+ // with the async Cloudflare context API.
31959
+ export async function getEnvAsync() {
31960
+ const cloudflareEnv = (await getCloudflareContext({ async: true })).env as Env;
31961
+
31962
+ return createEnvProxy((key) => {
31963
+ const nodeValue = getNodeEnvValue(key);
31964
+ if (nodeValue !== undefined) {
31965
+ return nodeValue;
31966
+ }
31967
+
31968
+ return cloudflareEnv[key as keyof Env];
31969
+ });
31970
+ }
31971
+
31972
+ export const env = createEnvProxy(resolveEnvValue);
31973
+ {{else if (and (eq backend "self") (eq webDeploy "cloudflare"))}}
31617
31974
  /// <reference path="../env.d.ts" />
31618
31975
  // For Cloudflare Workers, env is accessed via cloudflare:workers module
31619
31976
  // Types are defined in env.d.ts based on your alchemy.run.ts bindings