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
refactor: move executeSeedFile to vite-plugin-db
  • Loading branch information
bholmesdev committed May 2, 2024
commit 026062ef3b0590093134c5fb2bbbb757edc1ec7a
33 changes: 1 addition & 32 deletions packages/db/src/core/cli/commands/execute/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import {
} from '../../../integration/vite-plugin-db.js';
import { bundleFile, importBundledFile } from '../../../load-file.js';
import { getManagedAppTokenOrExit } from '../../../tokens.js';
import { type DBConfig, type DBTables } from '../../../types.js';
import { AstroDbError } from '../../../../runtime/utils.js';
import { fileURLToPath } from 'node:url';
import type { DBConfig } from '../../../types.js';

export async function cmd({
astroConfig,
Expand Down Expand Up @@ -72,32 +70,3 @@ 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;
}
}
34 changes: 32 additions & 2 deletions packages/db/src/core/integration/vite-plugin-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { type VitePlugin, getDbDirectoryUrl, getRemoteDatabaseUrl, getAstroEnv }
import { createLocalDatabaseClient } from '../../runtime/db-client.js';
import { type SQL, sql } from 'drizzle-orm';
import type { LibSQLDatabase } from 'drizzle-orm/libsql';
import { executeSeedFile } from '../cli/commands/execute/index.js';
import { existsSync } from 'node:fs';
import { normalizeDatabaseUrl } from '../../runtime/index.js';
import { getResolvedFileUrl } from '../load-file.js';
import { bundleFile, getResolvedFileUrl, importBundledFile } from '../load-file.js';
import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core';
import { EXEC_DEFAULT_EXPORT_ERROR, EXEC_ERROR } from '../errors.js';
import { LibsqlError } from '@libsql/client';
import { AstroDbError } from '../../runtime/utils.js';

export const RESOLVED_VIRTUAL_MODULE_ID = '\0' + VIRTUAL_MODULE_ID;

Expand Down Expand Up @@ -190,3 +192,31 @@ async function recreateTables({ db, tables }: { db: LibSQLDatabase; tables: DBTa
...setupQueries.map((q) => db.run(q)),
]);
}

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') {
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;
}
}