0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-13 22:11:20 -05:00

Add unsafeWritable option

This commit is contained in:
Matthew Phillips 2024-02-15 10:13:31 -05:00
parent eea1264571
commit 49f8fab4bf
3 changed files with 15 additions and 3 deletions

View file

@ -11,12 +11,16 @@ export const MISSING_PROJECT_ID_ERROR = `${red('▶ Directory not linked.')}
${cyan('astro db link')}\n`;
export const STUDIO_CONFIG_MISSING_WRITABLE_COLLECTIONS_ERROR = (collectionName: string) => `${red(
`▶ Writable collection ${bold(collectionName)} requires Astro Studio.`
`▶ Writable collection ${bold(collectionName)} requires Astro Studio or the ${yellow('unsafeWritable')} option.`
)}
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 UNSAFE_WRITABLE_WARNING = `${yellow('unsafeWritable')} option is enabled and you are using writable collections.
Redeploying your app may result in wiping away your database.
I hope you know what you are doing.\n`
export const STUDIO_CONFIG_MISSING_CLI_ERROR = `${red('▶ This command requires Astro Studio.')}
Visit ${cyan('https://astro.build/studio')} to create your account

View file

@ -8,7 +8,7 @@ import { DB_PATH } from '../consts.js';
import { createLocalDatabaseClient } from '../../runtime/db-client.js';
import { astroConfigWithDbSchema } from '../types.js';
import { type VitePlugin } from '../utils.js';
import { STUDIO_CONFIG_MISSING_WRITABLE_COLLECTIONS_ERROR } from '../errors.js';
import { STUDIO_CONFIG_MISSING_WRITABLE_COLLECTIONS_ERROR, UNSAFE_WRITABLE_WARNING } from '../errors.js';
import { errorMap } from './error-map.js';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
@ -32,13 +32,20 @@ function astroDBIntegration(): AstroIntegration {
const collections = configWithDb.db?.collections ?? {};
const studio = configWithDb.db?.studio ?? false;
const unsafeWritable = Boolean(configWithDb.db?.unsafeWritable);
const foundWritableCollection = Object.entries(collections).find(([, c]) => c.writable);
if (!studio && foundWritableCollection) {
const writableAllowed = studio || unsafeWritable;
if (!writableAllowed && foundWritableCollection) {
logger.error(
STUDIO_CONFIG_MISSING_WRITABLE_COLLECTIONS_ERROR(foundWritableCollection[0])
);
process.exit(1);
}
// Using writable collections with the opt-in flag. Warn them to let them
// know the risk.
else if(unsafeWritable && foundWritableCollection) {
logger.warn(UNSAFE_WRITABLE_WARNING);
}
let dbPlugin: VitePlugin;
if (studio && command === 'build' && process.env.ASTRO_DB_TEST_ENV !== '1') {

View file

@ -289,6 +289,7 @@ export const dbConfigSchema = z.object({
.function()
.returns(z.union([z.void(), z.promise(z.void())]))
.optional(),
unsafeWritable: z.boolean().optional().default(false),
});
export type DBUserConfig = Omit<z.input<typeof dbConfigSchema>, 'data'> & {