Skip to content

Commit 85dc182

Browse files
committed
Avoid D1 placeholders for PostgreSQL apps (#73)
1 parent 6633ab0 commit 85dc182

2 files changed

Lines changed: 92 additions & 7 deletions

File tree

‎packages/void/src/index.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,12 +1060,19 @@ export function voidPlugin(options?: {
10601060
if (resolvedConfig.command !== 'serve') {
10611061
return;
10621062
}
1063-
const synced = syncWranglerBindings(root, bindings, {
1064-
bindingNames,
1065-
websockets: websocketRoutes,
1066-
sandbox: sandboxConfig,
1067-
live: liveConfig,
1068-
});
1063+
const synced = syncWranglerBindings(
1064+
root,
1065+
{
1066+
...bindings,
1067+
needsD1: dialect === 'sqlite' && bindings.needsD1,
1068+
},
1069+
{
1070+
bindingNames,
1071+
websockets: websocketRoutes,
1072+
sandbox: sandboxConfig,
1073+
live: liveConfig,
1074+
},
1075+
);
10691076
if (synced) {
10701077
voidLog('Synced bindings to wrangler.jsonc');
10711078
}
@@ -1305,7 +1312,8 @@ export function voidPlugin(options?: {
13051312
const d1Name = bindingNames?.d1 ?? 'DB';
13061313
const d1: Array<Record<string, unknown>> = [];
13071314
if (
1308-
(bindings.needsD1 || (authEnabled && dialect === 'sqlite')) &&
1315+
dialect === 'sqlite' &&
1316+
(bindings.needsD1 || authEnabled) &&
13091317
!hasBinding(resolved.d1_databases, d1Name)
13101318
) {
13111319
d1.push({ binding: d1Name, database_name: 'default', database_id: 'local' });
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
2+
import { tmpdir } from 'node:os';
3+
import { join } from 'pathslash';
4+
import type { Plugin, ResolvedConfig } from 'vite';
5+
import { afterEach, describe, expect, it, vi } from 'vite-plus/test';
6+
7+
type MockCloudflarePluginConfig = {
8+
config: (resolved: Record<string, unknown>) => Record<string, unknown>;
9+
};
10+
11+
vi.mock('@cloudflare/vite-plugin', () => ({
12+
cloudflare(pluginConfig: MockCloudflarePluginConfig): Array<Plugin> {
13+
return [{ name: 'mock:cloudflare', api: { pluginConfig } } as Plugin];
14+
},
15+
}));
16+
17+
const roots: Array<string> = [];
18+
19+
function project(framework = false): string {
20+
const root = mkdtempSync(join(tmpdir(), 'void-pg-bindings-'));
21+
roots.push(root);
22+
writeFileSync(join(root, 'void.json'), JSON.stringify({ database: 'pg' }));
23+
writeFileSync(
24+
join(root, 'package.json'),
25+
JSON.stringify({
26+
type: 'module',
27+
devDependencies: framework ? { '@sveltejs/kit': '2.70.3' } : {},
28+
}),
29+
);
30+
const route = framework ? 'src/routes/+page.server.ts' : 'routes/api/users.ts';
31+
mkdirSync(join(root, framework ? 'src/routes' : 'routes/api'), { recursive: true });
32+
writeFileSync(join(root, route), "import { db } from 'void/db'; export const GET = () => db;");
33+
return root;
34+
}
35+
36+
afterEach(() => {
37+
vi.restoreAllMocks();
38+
for (const root of roots.splice(0)) {
39+
rmSync(root, { recursive: true, force: true });
40+
}
41+
});
42+
43+
describe('PostgreSQL binding generation', () => {
44+
it('does not emit a D1 placeholder for a Void app using void/db', async () => {
45+
const root = project();
46+
vi.spyOn(process, 'cwd').mockReturnValue(root);
47+
const { voidPlugin } = await import('../../src/index.ts');
48+
const cloudflarePlugin = voidPlugin().find((plugin) => plugin.name === 'mock:cloudflare');
49+
if (!cloudflarePlugin) {
50+
throw new Error('Expected Cloudflare plugin.');
51+
}
52+
const config = (cloudflarePlugin.api as { pluginConfig: MockCloudflarePluginConfig })
53+
.pluginConfig.config;
54+
55+
const patch = config({});
56+
expect(patch.d1_databases).toBeUndefined();
57+
});
58+
59+
it('does not write a D1 placeholder into a SvelteKit project using void/db', async () => {
60+
const root = project(true);
61+
vi.spyOn(process, 'cwd').mockReturnValue(root);
62+
const { voidPlugin } = await import('../../src/index.ts');
63+
const syncPlugin = voidPlugin().find((plugin) => plugin.name === 'void:wrangler-sync');
64+
const hook = syncPlugin?.configResolved;
65+
const handler = typeof hook === 'function' ? hook : hook?.handler;
66+
if (!handler) {
67+
throw new Error('Expected Wrangler sync plugin.');
68+
}
69+
handler.call({} as never, { command: 'serve' } as ResolvedConfig);
70+
71+
const config = JSON.parse(readFileSync(join(root, 'wrangler.jsonc'), 'utf8')) as Record<
72+
string,
73+
unknown
74+
>;
75+
expect(config.d1_databases).toBeUndefined();
76+
});
77+
});

0 commit comments

Comments
 (0)