Home / Function/ vitePluginDb() — astro Function Reference

vitePluginDb() — astro Function Reference

Architecture documentation for the vitePluginDb() function in vite-plugin-db.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  54a8b5c7_dd97_53d0_6372_b72f4f1ba7ee["vitePluginDb()"]
  10c96ff6_9978_85bd_85f5_ced507d506e2["vite-plugin-db.ts"]
  54a8b5c7_dd97_53d0_6372_b72f4f1ba7ee -->|defined in| 10c96ff6_9978_85bd_85f5_ced507d506e2
  8595edb1_700a_631d_5f61_1f6442f606d1["getRemoteVirtualModContents()"]
  54a8b5c7_dd97_53d0_6372_b72f4f1ba7ee -->|calls| 8595edb1_700a_631d_5f61_1f6442f606d1
  2febfa4d_ec4e_d812_680f_a8e74106b0dd["getLocalVirtualModContents()"]
  54a8b5c7_dd97_53d0_6372_b72f4f1ba7ee -->|calls| 2febfa4d_ec4e_d812_680f_a8e74106b0dd
  dd29af9e_6b76_c953_7091_c9ee6b0f8ac9["recreateTables()"]
  54a8b5c7_dd97_53d0_6372_b72f4f1ba7ee -->|calls| dd29af9e_6b76_c953_7091_c9ee6b0f8ac9
  fc1913f5_dcbd_84fd_9bb4_c7a9a2405272["getResolvedSeedFiles()"]
  54a8b5c7_dd97_53d0_6372_b72f4f1ba7ee -->|calls| fc1913f5_dcbd_84fd_9bb4_c7a9a2405272
  style 54a8b5c7_dd97_53d0_6372_b72f4f1ba7ee fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/db/src/core/integration/vite-plugin-db.ts lines 63–131

export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
	let command: 'build' | 'serve' = 'build';
	return {
		name: 'astro:db',
		enforce: 'pre',
		configResolved(resolvedConfig) {
			command = resolvedConfig.command;
		},
		resolveId: {
			filter: {
				id: new RegExp(`^${VIRTUAL_MODULE_ID}$`),
			},
			handler() {
				if (params.seedHandler.inProgress) {
					return resolved.importedFromSeedFile;
				}
				return resolved.module;
			},
		},
		load: {
			filter: {
				id: new RegExp(`^(${resolved.module}|${resolved.importedFromSeedFile})$`),
			},
			async handler(id) {
				if (params.connectToRemote) {
					return getRemoteVirtualModContents({
						appToken: params.appToken,
						tables: params.tables.get(),
						isBuild: command === 'build',
						output: params.output,
						localExecution: false,
					});
				}

				// When seeding, we resolved to a different virtual module.
				// this prevents an infinite loop attempting to rerun seed files.
				// Short circuit with the module contents in this case.
				if (id === resolved.importedFromSeedFile) {
					return getLocalVirtualModContents({
						root: params.root,
						tables: params.tables.get(),
						localExecution: false,
					});
				}

				await recreateTables(params);
				const seedFiles = getResolvedSeedFiles(params);
				for await (const seedFile of seedFiles) {
					// Use `addWatchFile()` to invalidate the `astro:db` module
					// when a seed file changes.
					this.addWatchFile(fileURLToPath(seedFile));
					if (existsSync(seedFile)) {
						params.seedHandler.inProgress = true;
						await params.seedHandler.execute(seedFile);
					}
				}
				if (params.seedHandler.inProgress) {
					(params.logger ?? console).info('Seeded database.');
					params.seedHandler.inProgress = false;
				}
				return getLocalVirtualModContents({
					root: params.root,
					tables: params.tables.get(),
					localExecution: false,
				});
			},
		},
	};
}

Domain

Subdomains

Frequently Asked Questions

What does vitePluginDb() do?
vitePluginDb() is a function in the astro codebase, defined in packages/db/src/core/integration/vite-plugin-db.ts.
Where is vitePluginDb() defined?
vitePluginDb() is defined in packages/db/src/core/integration/vite-plugin-db.ts at line 63.
What does vitePluginDb() call?
vitePluginDb() calls 4 function(s): getLocalVirtualModContents, getRemoteVirtualModContents, getResolvedSeedFiles, recreateTables.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free