From eea12645716ba1448a79bc0efa547e251e57ac38 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 14 Feb 2024 17:51:57 -0500 Subject: [PATCH] refactor: throw when seeding writable in prod --- packages/db/src/core/errors.ts | 29 ++++++++++++++++------------- packages/db/src/core/queries.ts | 17 +++++++++++++---- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/packages/db/src/core/errors.ts b/packages/db/src/core/errors.ts index ee0fdf1e85..22efac5525 100644 --- a/packages/db/src/core/errors.ts +++ b/packages/db/src/core/errors.ts @@ -1,32 +1,35 @@ import { cyan, bold, red, green, yellow } from 'kleur/colors'; -export const MISSING_SESSION_ID_ERROR = `${red( - '▶ Login required!' -)} +export const MISSING_SESSION_ID_ERROR = `${red('▶ Login required!')} To authenticate with Astro Studio, run ${cyan('astro db login')}\n`; -export const MISSING_PROJECT_ID_ERROR = `${red( - '▶ Directory not linked.' -)} +export const MISSING_PROJECT_ID_ERROR = `${red('▶ Directory not linked.')} To link this directory to an Astro Studio project, run ${cyan('astro db link')}\n`; -export const STUDIO_CONFIG_MISSING_WRITABLE_COLLECTIONS_ERROR = (collectionName: string) => `${ - red(`▶ Writable collection ${bold(collectionName)} requires Astro Studio.`) -} +export const STUDIO_CONFIG_MISSING_WRITABLE_COLLECTIONS_ERROR = (collectionName: string) => `${red( + `▶ Writable collection ${bold(collectionName)} requires Astro Studio.` +)} Visit ${cyan('https://astro.build/studio')} to create your account and set ${green('studio: true')} in your astro.config.mjs file to enable Studio.\n`; -export const STUDIO_CONFIG_MISSING_CLI_ERROR = `${ - red('▶ This command requires Astro Studio.') -} +export const STUDIO_CONFIG_MISSING_CLI_ERROR = `${red('▶ This command requires Astro Studio.')} Visit ${cyan('https://astro.build/studio')} to create your account and set ${green('studio: true')} in your astro.config.mjs file to enable Studio.\n`; +export const MIGRATIONS_NOT_INITIALIZED = `${yellow( + '▶ No migrations found!' +)}\n\n To scaffold your migrations folder, run\n ${cyan('astro db sync')}\n`; -export const MIGRATIONS_NOT_INITIALIZED = `${yellow('▶ No migrations found!')}\n\n To scaffold your migrations folder, run\n ${cyan('astro db sync')}\n` +export const SEED_WRITABLE_IN_PROD_ERROR = (collectionName: string) => { + return `${red( + `Writable collections should not be seeded in production with data().` + )} You can seed ${bold( + collectionName + )} in development mode only using the "mode" flag. See the docs for more: https://www.notion.so/astroinc/astrojs-db-README-dcf6fa10de9a4f528be56cee96e8c054?pvs=4#278aed3fc37e4cec80240d1552ff6ac5`; +}; diff --git a/packages/db/src/core/queries.ts b/packages/db/src/core/queries.ts index e05031fe19..cc4afa5627 100644 --- a/packages/db/src/core/queries.ts +++ b/packages/db/src/core/queries.ts @@ -10,13 +10,14 @@ import { type NumberField, type TextField, } from '../core/types.js'; -import { bold } from 'kleur/colors'; -import { type SQL, sql } from 'drizzle-orm'; +import { bold, red } from 'kleur/colors'; +import { type SQL, sql, getTableName } from 'drizzle-orm'; import { SQLiteAsyncDialect, type SQLiteInsert } from 'drizzle-orm/sqlite-core'; import type { AstroIntegrationLogger } from 'astro'; import type { DBUserConfig } from '../core/types.js'; import { hasPrimaryKey } from '../runtime/index.js'; import { isSerializedSQL } from '../runtime/types.js'; +import { SEED_WRITABLE_IN_PROD_ERROR } from './errors.js'; const sqlite = new SQLiteAsyncDialect(); @@ -52,10 +53,18 @@ export async function seedData({ }) { try { await data({ - seed: async ({ table }, values) => { + seed: async ({ table, writable }, values) => { + if (writable && mode === 'build') { + (logger ?? console).error(SEED_WRITABLE_IN_PROD_ERROR(getTableName(table))); + process.exit(1); + } await db.insert(table).values(values as any); }, - seedReturning: async ({ table }, values) => { + seedReturning: async ({ table, writable }, values) => { + if (writable && mode === 'build') { + (logger ?? console).error(SEED_WRITABLE_IN_PROD_ERROR(getTableName(table))); + process.exit(1); + } let result: SQLiteInsert = db .insert(table) .values(values as any)