Skip to content

Commit c7f3e83

Browse files
feat: Make DATABASE_ID no longer necessary as env var
1 parent b76c796 commit c7f3e83

File tree

9 files changed

+89
-7
lines changed

9 files changed

+89
-7
lines changed

.env.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
## Github Actions Secrets - NOT NECESSARY FOR LOCAL DEV ENVIRONMENT
22
CLOUDFLARE_ACCOUNT_ID=
3-
DATABASE_ID=
43
CLOUDFLARE_API_TOKEN=
54
NEXT_PUBLIC_TURNSTILE_SITE_KEY=
65

.github/workflows/deploy.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,16 @@ jobs:
4040
- name: Install dependencies
4141
run: pnpm install
4242

43+
- name: Set D1 environment variables
44+
id: set-d1-vars
45+
run: |
46+
echo "D1_DATABASE_NAME=$(node scripts/get-db-name.js)" >> $GITHUB_ENV
47+
echo "D1_DATABASE_ID=$(node scripts/get-db-id.js)" >> $GITHUB_ENV
48+
4349
- name: Deploy
4450
run: pnpm run deploy
4551
env:
46-
DATABASE_ID: ${{ vars.DATABASE_ID }}
52+
DATABASE_ID: ${{ env.D1_DATABASE_ID }}
4753
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
4854
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
4955
# Here you can add env variables that will be available in Next.js
@@ -56,7 +62,7 @@ jobs:
5662
with:
5763
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
5864
accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
59-
command: d1 migrations apply ${{ vars.DATABASE_ID }} --remote
65+
command: d1 migrations apply ${{ env.D1_DATABASE_NAME }} --remote
6066

6167
- name: Purge Cloudflare CDN cache
6268
if: ${{ vars.CLOUDFLARE_ZONE_ID != '' }}

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ After making a change to wrangler.jsonc, you need to run `pnpm cf-typegen` to ge
137137
7. Add the API token to the Github repository secrets as `CLOUDFLARE_API_TOKEN`
138138
8. Add the Cloudflare account id to the Github repository variables as `CLOUDFLARE_ACCOUNT_ID`
139139
9. Optional: If you want clear the CDN cache on deploy, add `CLOUDFLARE_ZONE_ID` to the Github repository variables for the zone id of your domain. This is the zone id of your domain, not the account id.
140-
10. Add the database name to the Github repository variables as `DATABASE_ID`. This should match the database name in the `wrangler.jsonc` file.
141-
11. Push to the main branch
140+
10. Push to the main branch
142141

143142
## Email templates
144143
If you want to preview and edit the email templates you can:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"preview": "pnpm run opennext:build && wrangler dev",
1515
"cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts",
1616
"db:generate": "drizzle-kit generate --name",
17-
"db:migrate:dev": "wrangler d1 migrations apply cloudflare-workers-nextjs-saas --local",
17+
"db:migrate:dev": "wrangler d1 migrations apply $(node scripts/get-db-name.mjs) --local",
1818
"email:dev": "email dev -d src/react-email -p 3001"
1919
},
2020
"dependencies": {

scripts/get-db-id.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { parseWranglerConfig } from './utils/parse-wrangler.js';
2+
3+
try {
4+
const config = parseWranglerConfig();
5+
const dbId = config.d1_databases?.[0]?.database_id;
6+
7+
if (!dbId) {
8+
console.error('Database ID not found in wrangler.jsonc');
9+
process.exit(1);
10+
}
11+
12+
console.log(dbId);
13+
} catch (error) {
14+
console.error(error.message);
15+
process.exit(1);
16+
}

scripts/get-db-name.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { parseWranglerConfig } from './utils/parse-wrangler.js';
2+
3+
try {
4+
const config = parseWranglerConfig();
5+
const dbName = config.d1_databases?.[0]?.database_name;
6+
7+
if (!dbName) {
8+
console.error('Database name not found in wrangler.jsonc');
9+
process.exit(1);
10+
}
11+
12+
console.log(dbName);
13+
} catch (error) {
14+
console.error(error.message);
15+
process.exit(1);
16+
}

scripts/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "scripts",
3+
"type": "module"
4+
}

scripts/utils/parse-wrangler.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
5+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
6+
7+
/**
8+
* Parses the wrangler.jsonc file and returns the configuration object
9+
* @returns {object} The parsed wrangler configuration
10+
* @throws {Error} If the file cannot be read or parsed
11+
*/
12+
export function parseWranglerConfig() {
13+
const wranglerPath = path.join(__dirname, '..', '..', 'wrangler.jsonc');
14+
const wranglerContent = fs.readFileSync(wranglerPath, 'utf8');
15+
16+
// Remove comments from the JSONC content
17+
const jsonContent = wranglerContent.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '');
18+
19+
try {
20+
return JSON.parse(jsonContent);
21+
} catch (error) {
22+
throw new Error(`Failed to parse wrangler.jsonc: ${error.message}`);
23+
}
24+
}
25+
26+
/**
27+
* Gets the D1 database configuration from wrangler.jsonc
28+
* @returns {{ name: string, id: string } | null} The database configuration or null if not found
29+
*/
30+
export function getD1Database() {
31+
const config = parseWranglerConfig();
32+
const d1Config = config.d1_databases?.[0];
33+
34+
if (!d1Config) {
35+
return null;
36+
}
37+
38+
return {
39+
name: d1Config.database_name,
40+
id: d1Config.database_id
41+
};
42+
}

src/utils/webauthn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export async function verifyPasskeyAuthentication(
132132
await db
133133
.update(passKeyCredentialTable)
134134
.set({ counter: verification.authenticationInfo.newCounter })
135-
.where(eq(passKeyCredentialTable.id, credential.id));
135+
.where(eq(passKeyCredentialTable.credentialId, credential.credentialId));
136136

137137
return {
138138
verification,

0 commit comments

Comments
 (0)