Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3c19e9c
fix: move recreateTables() to integration hooks
bholmesdev Apr 30, 2024
4427383
feat: recreate and seed at load, not in virtual runtime
bholmesdev Apr 30, 2024
333131b
feat: eager build db on startup and seed file change
bholmesdev Apr 30, 2024
2c6de6b
fix: respect database_file in dbUrl
bholmesdev Apr 30, 2024
56c4c97
chore: remove duplicate recreateTables call
bholmesdev Apr 30, 2024
82ad425
chore: remove now self-explanatory comments
bholmesdev Apr 30, 2024
a901140
fix: remove invalidateModule call for eager loading
bholmesdev Apr 30, 2024
69b1f77
feat: respect seed package paths
bholmesdev Apr 30, 2024
4abe41d
fix: remove duplicate recreateTables() call
bholmesdev Apr 30, 2024
1ed4c02
refactor: move recreateTables() to vite-plugin-db
bholmesdev Apr 30, 2024
4694c8e
refactor: move queries.ts from runtime/ to core/
bholmesdev Apr 30, 2024
98b1491
fix: update test import to core/queries
bholmesdev Apr 30, 2024
026062e
refactor: move executeSeedFile to vite-plugin-db
bholmesdev Apr 30, 2024
fe7a947
refactor: extract seeding and recreating to helper fns
bholmesdev Apr 30, 2024
c15b585
chore: changeset
bholmesdev Apr 30, 2024
533edb2
chore: revert connectToStudio refactor
bholmesdev Apr 30, 2024
d8b46db
wip: log db url
bholmesdev Apr 30, 2024
6f6fef2
fix(test): normalize astro_database_file flag for windows
bholmesdev Apr 30, 2024
01cca1c
Revert "wip: log db url"
bholmesdev Apr 30, 2024
53ed848
Revert "Revert "wip: log db url""
bholmesdev Apr 30, 2024
33747cb
fix: correctly resolve relative paths with unit test
bholmesdev Apr 30, 2024
a390dbd
chore: remove unused dbDirPath
bholmesdev May 1, 2024
f81cf8b
chore: remove unused import
bholmesdev May 1, 2024
9ba3d60
chore: remove unused type
bholmesdev May 1, 2024
1b41be4
fix: remove bad import
bholmesdev May 2, 2024
3b014fd
[db] Load seed files with vite dev server (#10941)
bholmesdev May 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: recreate and seed at load, not in virtual runtime
  • Loading branch information
bholmesdev committed May 2, 2024
commit 4427383ab6a9a9c379ac6704c8eeafe946e29258
35 changes: 32 additions & 3 deletions packages/db/src/core/cli/commands/execute/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
} from '../../../integration/vite-plugin-db.js';
import { bundleFile, importBundledFile } from '../../../load-file.js';
import { getManagedAppTokenOrExit } from '../../../tokens.js';
import { type DBConfig } from '../../../types.js';
import { type DBConfig, type DBTables } from '../../../types.js';
import { AstroDbError } from '../../../../runtime/utils.js';
import { fileURLToPath } from 'node:url';

export async function cmd({
astroConfig,
Expand Down Expand Up @@ -51,8 +53,6 @@ export async function cmd({
virtualModContents = getLocalVirtualModContents({
tables: dbConfig.tables ?? {},
root: astroConfig.root,
shouldSeed: false,
seedFiles: [],
});
}
const { code } = await bundleFile({ virtualModContents, root: astroConfig.root, fileUrl });
Expand All @@ -72,3 +72,32 @@ export async function cmd({
throw e;
}
}

export async function executeSeedFile({
tables,
root,
fileUrl,
}: {
tables: DBTables;
root: URL;
fileUrl: URL;
}) {
const virtualModContents = getLocalVirtualModContents({
tables: tables ?? {},
root,
});
const { code } = await bundleFile({ virtualModContents, root, fileUrl });
const mod = await importBundledFile({ code, root });
if (typeof mod.default !== 'function') {
// TODO: format error
throw new AstroDbError(EXEC_DEFAULT_EXPORT_ERROR(fileURLToPath(fileUrl)));
}
try {
await mod.default();
} catch (e) {
if (e instanceof LibsqlError) {
throw new AstroDbError(EXEC_ERROR(e.message));
}
throw e;
}
}
2 changes: 1 addition & 1 deletion packages/db/src/core/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function astroDBIntegration(): AstroIntegration {
logger.info(
connectToStudio ? 'Connected to remote database.' : 'New local database created.'
);
if (connectToStudio) return;
if (true) return;

const localSeedPaths = SEED_DEV_FILE_NAME.map(
(name) => new URL(name, getDbDirectoryUrl(root))
Expand Down
81 changes: 32 additions & 49 deletions packages/db/src/core/integration/vite-plugin-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { SEED_DEV_FILE_NAME } from '../../runtime/queries.js';
import { DB_PATH, RUNTIME_IMPORT, RUNTIME_VIRTUAL_IMPORT, VIRTUAL_MODULE_ID } from '../consts.js';
import type { DBTables } from '../types.js';
import { type VitePlugin, getDbDirectoryUrl, getRemoteDatabaseUrl } from '../utils.js';
import { createLocalDatabaseClient } from '../../runtime/db-client.js';
import { recreateTables } from '../../runtime/seed-local.js';
import { executeSeedFile } from '../cli/commands/execute/index.js';
import { existsSync } from 'node:fs';

const WITH_SEED_VIRTUAL_MODULE_ID = 'astro:db:seed';

Expand Down Expand Up @@ -48,18 +52,8 @@ export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
configResolved(resolvedConfig) {
command = resolvedConfig.command;
},
async resolveId(id, rawImporter) {
async resolveId(id) {
if (id !== VIRTUAL_MODULE_ID) return;
if (params.connectToStudio) return resolved.virtual;

const importer = rawImporter ? await this.resolve(rawImporter) : null;
if (!importer) return resolved.virtual;

if (importer.id.startsWith(srcDirPath) && !importer.id.startsWith(dbDirPath)) {
// Seed only if the importer is in the src directory.
// Otherwise, we may get recursive seed calls (ex. import from db/seed.ts).
return resolved.seedVirtual;
}
return resolved.virtual;
},
async load(id) {
Expand All @@ -73,11 +67,36 @@ export function vitePluginDb(params: VitePluginDBParams): VitePlugin {
output: params.output,
});
}
const tables = params.tables.get() ?? {};
const localDbUrl = new URL(DB_PATH, params.root);
const db = createLocalDatabaseClient({ dbUrl: localDbUrl.href });
await recreateTables({ db, tables: params.tables.get() ?? {} });

const localSeedPaths = SEED_DEV_FILE_NAME.map(
(name) => new URL(name, getDbDirectoryUrl(params.root))
);
const integrationSeedPaths = params.seedFiles
.get()
// TODO: add resolver for package paths
.map((s) => (typeof s === 'string' && s.startsWith('.') ? new URL(s, params.root) : s))
.filter((s): s is URL => s instanceof URL);
const seedFiles = [...integrationSeedPaths, ...localSeedPaths];
let hasSeeded = false;
for await (const seedFile of seedFiles) {
// Invalidate the `astro:db` module when a seed file changes.
this.addWatchFile(fileURLToPath(seedFile));
if (existsSync(seedFile)) {
hasSeeded = true;
await executeSeedFile({ tables, fileUrl: seedFile, root: params.root });
}
}
if (seedFiles.length) {
// TODO: format log
console.log('Seeded database.');
}
return getLocalVirtualModContents({
root: params.root,
tables: params.tables.get(),
seedFiles: params.seedFiles.get(),
shouldSeed: id === resolved.seedVirtual,
});
},
};
Expand All @@ -90,53 +109,17 @@ export function getConfigVirtualModContents() {
export function getLocalVirtualModContents({
tables,
root,
seedFiles,
shouldSeed,
}: {
tables: DBTables;
seedFiles: Array<string | URL>;
root: URL;
shouldSeed: boolean;
}) {
const userSeedFilePaths = SEED_DEV_FILE_NAME.map(
// Format as /db/[name].ts
// for Vite import.meta.glob
(name) => new URL(name, getDbDirectoryUrl('file:///')).pathname
);
const resolveId = (id: string) =>
id.startsWith('.') ? normalizePath(fileURLToPath(new URL(id, root))) : id;
// Use top-level imports to correctly resolve `astro:db` within seed files.
// Dynamic imports cause a silent build failure,
// potentially because of circular module references.
const integrationSeedImportStatements: string[] = [];
const integrationSeedImportNames: string[] = [];
seedFiles.forEach((pathOrUrl, index) => {
const path = typeof pathOrUrl === 'string' ? resolveId(pathOrUrl) : pathOrUrl.pathname;
const importName = 'integration_seed_' + index;
integrationSeedImportStatements.push(`import ${importName} from ${JSON.stringify(path)};`);
integrationSeedImportNames.push(importName);
});

const dbUrl = new URL(DB_PATH, root);
return `
import { asDrizzleTable, createLocalDatabaseClient, normalizeDatabaseUrl } from ${RUNTIME_IMPORT};
${shouldSeed ? `import { seedLocal } from ${RUNTIME_IMPORT};` : ''}
${shouldSeed ? integrationSeedImportStatements.join('\n') : ''}

const dbUrl = normalizeDatabaseUrl(import.meta.env.ASTRO_DATABASE_FILE, ${JSON.stringify(dbUrl)});
export const db = createLocalDatabaseClient({ dbUrl });

${
shouldSeed
? `await seedLocal({
db,
tables: ${JSON.stringify(tables)},
userSeedGlob: import.meta.glob(${JSON.stringify(userSeedFilePaths)}, { eager: true }),
integrationSeedFunctions: [${integrationSeedImportNames.join(',')}],
});`
: ''
}

export * from ${RUNTIME_VIRTUAL_IMPORT};

${getStringifiedTableExports(tables)}`;
Expand Down
1 change: 1 addition & 0 deletions packages/db/src/core/load-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export async function bundleFile({
metafile: true,
define: {
'import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL': 'undefined',
'import.meta.env.ASTRO_DATABASE_FILE': JSON.stringify(process.env.ASTRO_DATABASE_FILE ?? ''),
},
plugins: [
{
Expand Down