0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-17 23:11:29 -05:00

refactor: throw when seeding writable in prod

This commit is contained in:
bholmesdev 2024-02-14 17:51:57 -05:00
parent e3ba7de572
commit eea1264571
2 changed files with 29 additions and 17 deletions

View file

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

View file

@ -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<any, any, any, any> = db
.insert(table)
.values(values as any)