diff --git a/packages/db/src/consts.ts b/packages/db/src/consts.ts index db0f2f6725..ace2d6c389 100644 --- a/packages/db/src/consts.ts +++ b/packages/db/src/consts.ts @@ -12,3 +12,8 @@ export const SUPPORTED_SEED_FILES = ['db.seed.js', 'db.seed.mjs', 'db.seed.mts', export const DB_TYPES_FILE = 'db-types.d.ts'; export const VIRTUAL_MODULE_ID = 'astro:db'; + +// TODO: copy DB to build for serverless +export function getDbUrl(root: URL) { + return new URL('.astro/content.db', root); +} diff --git a/packages/db/src/integration.ts b/packages/db/src/integration.ts index f17cee9ead..eeaacc82f9 100644 --- a/packages/db/src/integration.ts +++ b/packages/db/src/integration.ts @@ -4,17 +4,26 @@ import { vitePluginInjectEnvTs } from './vite-plugin-inject-env-ts.js'; import { typegen } from './typegen.js'; import { collectionsSchema } from './types.js'; import { seed } from './seed.js'; +import { existsSync } from 'fs'; +import { rm } from 'fs/promises'; +import { getDbUrl } from './consts.js'; export function integration(): AstroIntegration { return { name: 'astro:db', hooks: { async 'astro:config:setup'({ updateConfig, config, command }) { + if (command === 'preview') return; + // TODO: refine where we load collections // @matthewp: may want to load collections by path at runtime const collections = collectionsSchema.parse(config.db?.collections ?? {}); const isDev = command === 'dev'; - if (!isDev) { + if (command === 'build') { + const dbUrl = getDbUrl(config.root); + if (existsSync(dbUrl)) { + await rm(dbUrl); + } await seed({ collections, root: config.root }); } updateConfig({ diff --git a/packages/db/src/internal.ts b/packages/db/src/internal.ts index 5d1dcebae9..654847e7e9 100644 --- a/packages/db/src/internal.ts +++ b/packages/db/src/internal.ts @@ -38,11 +38,21 @@ export type { const sqlite = new SQLiteAsyncDialect(); -export async function createDb(collections: DBCollections) { - const client = createClient({ url: ':memory:' }); +export async function createDb({ + collections, + dbUrl, + createTables = false, +}: { + collections: DBCollections; + dbUrl: string; + createTables?: boolean; +}) { + const client = createClient({ url: dbUrl }); const db = drizzle(client); - await createDbTables(db, collections); + if (createTables) { + await createDbTables(db, collections); + } return db; } diff --git a/packages/db/src/vite-plugin-db.ts b/packages/db/src/vite-plugin-db.ts index 348fd188e9..e251701478 100644 --- a/packages/db/src/vite-plugin-db.ts +++ b/packages/db/src/vite-plugin-db.ts @@ -6,6 +6,7 @@ import { INTERNAL_MOD_IMPORT, SUPPORTED_SEED_FILES, VIRTUAL_MODULE_ID, + getDbUrl, } from './consts.js'; import type { DBCollections } from './types.js'; import type { Plugin as VitePlugin } from 'vite'; @@ -15,7 +16,7 @@ const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID; export function vitePluginDb({ collections, root, - isDev: isDev, + isDev, }: { collections: DBCollections; root: URL; @@ -52,10 +53,16 @@ export function getVirtualModContents({ const seedFile = SUPPORTED_SEED_FILES.map((f) => fileURLToPath(new URL(f, root))).find((f) => existsSync(f) ); + const dbUrl = isDev ? ':memory:' : getDbUrl(root).href; + const shouldSetUpDb = isDev || !existsSync(getDbUrl(root)); return ` import { collectionToTable, createDb } from ${INTERNAL_MOD_IMPORT}; -export const db = await createDb(${JSON.stringify(collections)}); +export const db = await createDb(${JSON.stringify({ + collections, + dbUrl, + createTables: shouldSetUpDb, + })}); export * from ${DRIZZLE_MOD_IMPORT}; ${getStringifiedCollectionExports(collections)}