0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-10 22:38:53 -05:00

feat: create db on filesystem for build

This commit is contained in:
bholmesdev 2024-01-09 16:31:14 -05:00 committed by Nate Moore
parent aa1b66f5cf
commit b253e6f344
4 changed files with 37 additions and 6 deletions

View file

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

View file

@ -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({

View file

@ -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;
}

View file

@ -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)}