Repository: withastro/astro
File: packages/db/src/core/integration/vite-plugin-db.ts (TypeScript)

Code before:
				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(),
			});
		},
	};
}

export function getConfigVirtualModContents() {
	return `export * from ${RUNTIME_VIRTUAL_IMPORT}`;
}

export function getLocalVirtualModContents({
	tables,
	root,
	localExecution = false,
}: {
	tables: DBTables;
	root: URL;
	// Request module to be loaded immediately in process  
    localExecution?: boolean; 
}) {
	const { ASTRO_DATABASE_FILE } = getAstroEnv();
	const dbUrl = new URL(DB_PATH, root);

	// If this is for the execute command, we need to import the client directly instead of using the runtime only virtual module.
	const clientImport = localExecution
		? `import { createClient } from '${DB_CLIENTS.node}';`
		: `import { createClient } from '${VIRTUAL_CLIENT_MODULE_ID}';`;

	return `
import { asDrizzleTable, normalizeDatabaseUrl } from ${RUNTIME_IMPORT};

${clientImport}

const dbUrl = normalizeDatabaseUrl(${JSON.stringify(ASTRO_DATABASE_FILE)}, ${JSON.stringify(dbUrl)});
export const db = createClient({ url: dbUrl });

export * from ${RUNTIME_VIRTUAL_IMPORT};

${getStringifiedTableExports(tables)}`;
}

Review comment (suggestion):
Can you add some JSDocs that explain the business logic of the function? The new `localExecution` adds some logic that isn't clear to me. For example, it doesn't explain why, when `localExecution` is `true`, we need the node client. It's an assumption that isn't explained. 

Code after:
				});
			}

			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,
			});
		},
	};
}

export function getConfigVirtualModContents() {
	return `export * from ${RUNTIME_VIRTUAL_IMPORT}`;
}

/**
 * Get the module import for the DB client.
 * This is used to pick which module to import based on whether
 * the DB client is being used by the CLI, or in the Astro runtime.
 *
 * This is important for the `astro db execute` command to work correctly.
 *
 * @param localExecution - Whether the DB client is being used in a local execution context (e.g. CLI commands).
 * @returns The module import string for the DB client.
 */
function getDBModule(localExecution: boolean) {
	return localExecution
		? `import { createClient } from '${DB_CLIENTS.node}';`
		: `import { createClient } from '${VIRTUAL_CLIENT_MODULE_ID}';`;
}

export function getLocalVirtualModContents({
	tables,
	root,
	localExecution,