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:
parent
aa1b66f5cf
commit
b253e6f344
4 changed files with 37 additions and 6 deletions
|
@ -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 DB_TYPES_FILE = 'db-types.d.ts';
|
||||||
|
|
||||||
export const VIRTUAL_MODULE_ID = 'astro:db';
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -4,17 +4,26 @@ import { vitePluginInjectEnvTs } from './vite-plugin-inject-env-ts.js';
|
||||||
import { typegen } from './typegen.js';
|
import { typegen } from './typegen.js';
|
||||||
import { collectionsSchema } from './types.js';
|
import { collectionsSchema } from './types.js';
|
||||||
import { seed } from './seed.js';
|
import { seed } from './seed.js';
|
||||||
|
import { existsSync } from 'fs';
|
||||||
|
import { rm } from 'fs/promises';
|
||||||
|
import { getDbUrl } from './consts.js';
|
||||||
|
|
||||||
export function integration(): AstroIntegration {
|
export function integration(): AstroIntegration {
|
||||||
return {
|
return {
|
||||||
name: 'astro:db',
|
name: 'astro:db',
|
||||||
hooks: {
|
hooks: {
|
||||||
async 'astro:config:setup'({ updateConfig, config, command }) {
|
async 'astro:config:setup'({ updateConfig, config, command }) {
|
||||||
|
if (command === 'preview') return;
|
||||||
|
|
||||||
// TODO: refine where we load collections
|
// TODO: refine where we load collections
|
||||||
// @matthewp: may want to load collections by path at runtime
|
// @matthewp: may want to load collections by path at runtime
|
||||||
const collections = collectionsSchema.parse(config.db?.collections ?? {});
|
const collections = collectionsSchema.parse(config.db?.collections ?? {});
|
||||||
const isDev = command === 'dev';
|
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 });
|
await seed({ collections, root: config.root });
|
||||||
}
|
}
|
||||||
updateConfig({
|
updateConfig({
|
||||||
|
|
|
@ -38,11 +38,21 @@ export type {
|
||||||
|
|
||||||
const sqlite = new SQLiteAsyncDialect();
|
const sqlite = new SQLiteAsyncDialect();
|
||||||
|
|
||||||
export async function createDb(collections: DBCollections) {
|
export async function createDb({
|
||||||
const client = createClient({ url: ':memory:' });
|
collections,
|
||||||
|
dbUrl,
|
||||||
|
createTables = false,
|
||||||
|
}: {
|
||||||
|
collections: DBCollections;
|
||||||
|
dbUrl: string;
|
||||||
|
createTables?: boolean;
|
||||||
|
}) {
|
||||||
|
const client = createClient({ url: dbUrl });
|
||||||
const db = drizzle(client);
|
const db = drizzle(client);
|
||||||
|
|
||||||
await createDbTables(db, collections);
|
if (createTables) {
|
||||||
|
await createDbTables(db, collections);
|
||||||
|
}
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import {
|
||||||
INTERNAL_MOD_IMPORT,
|
INTERNAL_MOD_IMPORT,
|
||||||
SUPPORTED_SEED_FILES,
|
SUPPORTED_SEED_FILES,
|
||||||
VIRTUAL_MODULE_ID,
|
VIRTUAL_MODULE_ID,
|
||||||
|
getDbUrl,
|
||||||
} from './consts.js';
|
} from './consts.js';
|
||||||
import type { DBCollections } from './types.js';
|
import type { DBCollections } from './types.js';
|
||||||
import type { Plugin as VitePlugin } from 'vite';
|
import type { Plugin as VitePlugin } from 'vite';
|
||||||
|
@ -15,7 +16,7 @@ const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID;
|
||||||
export function vitePluginDb({
|
export function vitePluginDb({
|
||||||
collections,
|
collections,
|
||||||
root,
|
root,
|
||||||
isDev: isDev,
|
isDev,
|
||||||
}: {
|
}: {
|
||||||
collections: DBCollections;
|
collections: DBCollections;
|
||||||
root: URL;
|
root: URL;
|
||||||
|
@ -52,10 +53,16 @@ export function getVirtualModContents({
|
||||||
const seedFile = SUPPORTED_SEED_FILES.map((f) => fileURLToPath(new URL(f, root))).find((f) =>
|
const seedFile = SUPPORTED_SEED_FILES.map((f) => fileURLToPath(new URL(f, root))).find((f) =>
|
||||||
existsSync(f)
|
existsSync(f)
|
||||||
);
|
);
|
||||||
|
const dbUrl = isDev ? ':memory:' : getDbUrl(root).href;
|
||||||
|
const shouldSetUpDb = isDev || !existsSync(getDbUrl(root));
|
||||||
return `
|
return `
|
||||||
import { collectionToTable, createDb } from ${INTERNAL_MOD_IMPORT};
|
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};
|
export * from ${DRIZZLE_MOD_IMPORT};
|
||||||
|
|
||||||
${getStringifiedCollectionExports(collections)}
|
${getStringifiedCollectionExports(collections)}
|
||||||
|
|
Loading…
Add table
Reference in a new issue