From 203dbe369f9e3247f54d62d519eaf5e0a9880da9 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 27 Feb 2024 12:13:48 -0500 Subject: [PATCH] refactor: ensure precedence of file name --- packages/db/src/core/consts.ts | 15 --------- .../db/src/core/integration/vite-plugin-db.ts | 23 +++++-------- packages/db/src/core/utils.ts | 4 +-- packages/db/src/runtime/queries.ts | 33 +++++++++++++++++-- 4 files changed, 41 insertions(+), 34 deletions(-) diff --git a/packages/db/src/core/consts.ts b/packages/db/src/core/consts.ts index b292e2e891..1f6771b04f 100644 --- a/packages/db/src/core/consts.ts +++ b/packages/db/src/core/consts.ts @@ -12,18 +12,3 @@ export const DB_TYPES_FILE = 'db-types.d.ts'; export const VIRTUAL_MODULE_ID = 'astro:db'; export const DB_PATH = '.astro/content.db'; - -export const SEED_DEV_FILE_NAMES = [ - 'seed.ts', - 'seed.js', - 'seed.mjs', - 'seed.mts', - 'seed.dev.ts', - 'seed.dev.js', - 'seed.dev.mjs', - 'seed.dev.mts', - 'seed.development.ts', - 'seed.development.js', - 'seed.development.mjs', - 'seed.development.mts', -]; diff --git a/packages/db/src/core/integration/vite-plugin-db.ts b/packages/db/src/core/integration/vite-plugin-db.ts index 26a79176b2..7567c5a711 100644 --- a/packages/db/src/core/integration/vite-plugin-db.ts +++ b/packages/db/src/core/integration/vite-plugin-db.ts @@ -1,14 +1,7 @@ -import { existsSync } from 'node:fs'; -import { - DB_PATH, - RUNTIME_DRIZZLE_IMPORT, - RUNTIME_IMPORT, - SEED_DEV_FILE_NAMES, - VIRTUAL_MODULE_ID, -} from '../consts.js'; +import { SEED_DEV_FILE_NAMES_SORTED } from '../../runtime/queries.js'; +import { DB_PATH, RUNTIME_DRIZZLE_IMPORT, RUNTIME_IMPORT, VIRTUAL_MODULE_ID } from '../consts.js'; import type { DBTables } from '../types.js'; import { getDbDirUrl, getRemoteDatabaseUrl, type VitePlugin } from '../utils.js'; -import { fileURLToPath } from 'node:url'; const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID; @@ -67,9 +60,11 @@ export function getLocalVirtualModContents({ isDev: boolean; }) { const dbUrl = new URL(DB_PATH, root); - const devSeedFile = SEED_DEV_FILE_NAMES.map((f) => - fileURLToPath(new URL(f, getDbDirUrl(root))) - ).find((f) => existsSync(f)); + const seedFilePaths = SEED_DEV_FILE_NAMES_SORTED.map( + // Format as /db/[name].ts + // for Vite import.meta.glob + (name) => new URL(name, getDbDirUrl('file:///')).pathname + ); return ` import { collectionToTable, createLocalDatabaseClient, seedDev } from ${RUNTIME_IMPORT}; @@ -83,11 +78,11 @@ ${getStringifiedCollectionExports(tables)} // TODO: test error logging to see if try / catch is needed ${ - isDev && devSeedFile + isDev ? `await seedDev({ db, tables: ${JSON.stringify(tables)}, - runSeed: () => import(${JSON.stringify(devSeedFile)}), + fileGlob: import.meta.glob(${JSON.stringify(seedFilePaths)}), });` : '' } diff --git a/packages/db/src/core/utils.ts b/packages/db/src/core/utils.ts index e8a6ef53ae..7f78a19fda 100644 --- a/packages/db/src/core/utils.ts +++ b/packages/db/src/core/utils.ts @@ -18,6 +18,6 @@ export function getAstroStudioUrl(): string { return env.ASTRO_STUDIO_URL || 'https://stardate.astro.build'; } -export function getDbDirUrl(root: URL) { - return new URL('db/', root); +export function getDbDirUrl(rootPath: string) { + return new URL('db/', rootPath); } diff --git a/packages/db/src/runtime/queries.ts b/packages/db/src/runtime/queries.ts index 5ce604beb5..e19c78ea38 100644 --- a/packages/db/src/runtime/queries.ts +++ b/packages/db/src/runtime/queries.ts @@ -22,17 +22,44 @@ import { SEED_EMPTY_ARRAY_ERROR } from '../core/errors.js'; const sqlite = new SQLiteAsyncDialect(); +/** + * Sorted by precedence. + * Ex. If both "seed.dev.ts" and "seed.ts" are present, + * "seed.dev.ts" will be used. + */ +export const SEED_DEV_FILE_NAMES_SORTED = [ + 'seed.development.ts', + 'seed.development.js', + 'seed.development.mjs', + 'seed.development.mts', + 'seed.dev.ts', + 'seed.dev.js', + 'seed.dev.mjs', + 'seed.dev.mts', + 'seed.ts', + 'seed.js', + 'seed.mjs', + 'seed.mts', +]; + export async function seedDev({ db, tables, - runSeed, + // Glob all potential seed files to catch renames and deletions. + fileGlob, }: { db: SqliteRemoteDatabase; tables: DBTables; - runSeed: () => Promise; + fileGlob: Record Promise>; }) { await recreateTables({ db, tables }); - await runSeed(); + for (const fileName of SEED_DEV_FILE_NAMES_SORTED) { + const key = Object.keys(fileGlob).find((f) => new RegExp(`${fileName}$`).test(f)); + if (key) { + await fileGlob[key](); + return; + } + } } export async function recreateTables({