diff --git a/.prettierignore b/.prettierignore index af84e34338..0703752ee5 100644 --- a/.prettierignore +++ b/.prettierignore @@ -10,6 +10,9 @@ benchmark/results/ **/vendor **/.vercel +# Short-term need to format +!packages/db/test/fixtures + # Directories .github .changeset diff --git a/packages/db/src/cli/commands/shell/index.ts b/packages/db/src/cli/commands/shell/index.ts index 43c3d7ed47..059ad47249 100644 --- a/packages/db/src/cli/commands/shell/index.ts +++ b/packages/db/src/cli/commands/shell/index.ts @@ -5,7 +5,7 @@ import { appTokenError } from '../../../errors.js'; import { createRemoteDatabaseClient, getAstroStudioEnv } from '../../../utils.js'; export async function cmd({ config, flags }: { config: AstroConfig; flags: Arguments }) { - const query = flags.query; + const query = flags.query; const appToken = flags.token ?? getAstroStudioEnv().ASTRO_STUDIO_APP_TOKEN; if (!appToken) { // eslint-disable-next-line no-console diff --git a/packages/db/src/cli/queries.ts b/packages/db/src/cli/queries.ts index e8a83c2ac2..d5cb8cd6db 100644 --- a/packages/db/src/cli/queries.ts +++ b/packages/db/src/cli/queries.ts @@ -15,7 +15,13 @@ import type { import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core'; import { customAlphabet } from 'nanoid'; import prompts from 'prompts'; -import { getCreateTableQuery, getModifiers, hasDefault, hasPrimaryKey, schemaTypeToSqlType } from '../internal.js'; +import { + getCreateTableQuery, + getModifiers, + hasDefault, + hasPrimaryKey, + schemaTypeToSqlType, +} from '../internal.js'; const sqlite = new SQLiteAsyncDialect(); const genTempTableName = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10); @@ -157,15 +163,10 @@ export async function getCollectionChangeQueries({ } } - const addedPrimaryKey = Object.entries(added).find( - ([, field]) => hasPrimaryKey(field) - ); - const droppedPrimaryKey = Object.entries(dropped).find( - ([, field]) => hasPrimaryKey(field) - ); + const addedPrimaryKey = Object.entries(added).find(([, field]) => hasPrimaryKey(field)); + const droppedPrimaryKey = Object.entries(dropped).find(([, field]) => hasPrimaryKey(field)); const updatedPrimaryKey = Object.entries(updated).find( - ([, field]) => - (hasPrimaryKey(field.old) || hasPrimaryKey(field.new)) + ([, field]) => hasPrimaryKey(field.old) || hasPrimaryKey(field.new) ); const recreateTableQueries = getRecreateTableQueries({ unescapedCollectionName: collectionName, @@ -425,10 +426,7 @@ function canAlterTableDropColumn(field: DBField) { return true; } -type DataLossReason = - | 'added-required' - | 'added-unique' - | 'updated-type'; +type DataLossReason = 'added-required' | 'added-unique' | 'updated-type'; type DataLossResponse = | { dataLoss: false } | { dataLoss: true; fieldName: string; reason: DataLossReason }; diff --git a/packages/db/src/integration.ts b/packages/db/src/integration.ts index f94b4fc2a4..72230192f6 100644 --- a/packages/db/src/integration.ts +++ b/packages/db/src/integration.ts @@ -44,7 +44,7 @@ export function integration(): AstroIntegration { dbPlugin = vitePluginDb({ connectToStudio: true, collections, - appToken + appToken, }); } else { const dbUrl = getLocalDbUrl(config.root); diff --git a/packages/db/src/utils-runtime.ts b/packages/db/src/utils-runtime.ts index ed635130e7..59d2fca1d4 100644 --- a/packages/db/src/utils-runtime.ts +++ b/packages/db/src/utils-runtime.ts @@ -5,7 +5,7 @@ import { DB_PATH } from './consts.js'; export function findLocalDatabase(localDbURL: string): string { let dbURL: URL | undefined = undefined; - if(process.env.VERCEL) { + if (process.env.VERCEL) { dbURL = new URL(DB_PATH, 'file://' + process.cwd() + '/vercel/path0/'); } else { dbURL = new URL(localDbURL); diff --git a/packages/db/src/utils.ts b/packages/db/src/utils.ts index c7c7f6a3ee..4aa9ce1fca 100644 --- a/packages/db/src/utils.ts +++ b/packages/db/src/utils.ts @@ -2,7 +2,7 @@ import type { AstroConfig } from 'astro'; import { sqliteTable, text } from 'drizzle-orm/sqlite-core'; import { loadEnv } from 'vite'; import { createRemoteDatabaseClient as runtimeCreateRemoteDatabaseClient } from './utils-runtime.js'; - + export type VitePlugin = Required['plugins'][number]; export const STUDIO_ADMIN_TABLE = 'ReservedAstroStudioAdmin'; diff --git a/packages/db/src/vite-plugin-db.ts b/packages/db/src/vite-plugin-db.ts index 8cead4b95e..17a000dfbd 100644 --- a/packages/db/src/vite-plugin-db.ts +++ b/packages/db/src/vite-plugin-db.ts @@ -36,15 +36,15 @@ export function vitePluginDb( }, async buildEnd() { // For local use, emit the database into the output - if('dbUrl' in params) { + if ('dbUrl' in params) { const data = await fs.promises.readFile(new URL(params.dbUrl)); this.emitFile({ fileName: 'content.db', source: data, - type: 'asset' + type: 'asset', }); } - } + }, }; } @@ -61,9 +61,9 @@ import { collectionToTable, createLocalDatabaseClient, findLocalDatabase } from export const dbUrl = findLocalDatabase(${JSON.stringify(dbUrl)}); const params = ${JSON.stringify({ - collections, - seeding: false, -})}; + collections, + seeding: false, + })}; params.dbUrl = dbUrl; export const db = await createLocalDatabaseClient(params); @@ -84,7 +84,9 @@ export function getStudioVirtualModContents({ return ` import {collectionToTable, createRemoteDatabaseClient} from ${INTERNAL_MOD_IMPORT}; -export const db = await createRemoteDatabaseClient(${JSON.stringify(appToken)}, import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL); +export const db = await createRemoteDatabaseClient(${JSON.stringify( + appToken + )}, import.meta.env.ASTRO_STUDIO_REMOTE_DB_URL); export * from ${DRIZZLE_MOD_IMPORT}; ${getStringifiedCollectionExports(collections)} diff --git a/packages/db/test/fixtures/basics/astro.config.ts b/packages/db/test/fixtures/basics/astro.config.ts index 21fad4078b..43405d66d5 100644 --- a/packages/db/test/fixtures/basics/astro.config.ts +++ b/packages/db/test/fixtures/basics/astro.config.ts @@ -20,21 +20,11 @@ export default defineConfig({ collections: { Author, Themes }, data({ seed }) { seed(Author, [ - { - name: 'Ben', - }, - { - name: 'Nate', - }, - { - name: 'Erika', - }, - { - name: 'Bjorn', - }, - { - name: 'Sarah', - }, + { name: 'Ben' }, + { name: 'Nate' }, + { name: 'Erika' }, + { name: 'Bjorn' }, + { name: 'Sarah' }, ]); }, }, diff --git a/packages/db/test/fixtures/basics/src/pages/insert-into-readonly.astro b/packages/db/test/fixtures/basics/src/pages/insert-into-readonly.astro index ef4bc3a693..7a6bb37bc3 100644 --- a/packages/db/test/fixtures/basics/src/pages/insert-into-readonly.astro +++ b/packages/db/test/fixtures/basics/src/pages/insert-into-readonly.astro @@ -5,8 +5,8 @@ const authors = await db.select().from(Author); let error: any = {}; try { - db.insert(Author).values({ name: 'Person A' }) -} catch(err) { + db.insert(Author).values({ name: 'Person A' }); +} catch (err) { error = err; } --- diff --git a/packages/db/test/fixtures/basics/src/pages/insert-into-writable.astro b/packages/db/test/fixtures/basics/src/pages/insert-into-writable.astro index e2ebcfacfa..3e3ad2f31a 100644 --- a/packages/db/test/fixtures/basics/src/pages/insert-into-writable.astro +++ b/packages/db/test/fixtures/basics/src/pages/insert-into-writable.astro @@ -4,7 +4,7 @@ import { Themes, db } from 'astro:db'; let error: any = {}; try { db.insert(Themes).values({ name: 'Person A' }); -} catch(err) { +} catch (err) { error = err; } --- diff --git a/packages/db/test/fixtures/ticketing-example/src/components/Form.tsx b/packages/db/test/fixtures/ticketing-example/src/components/Form.tsx index 89f2d2ea0f..c7d4dcd79e 100644 --- a/packages/db/test/fixtures/ticketing-example/src/components/Form.tsx +++ b/packages/db/test/fixtures/ticketing-example/src/components/Form.tsx @@ -1,43 +1,40 @@ // Generated by simple:form -import { type ComponentProps, createContext } from "preact"; -import { useContext, useState } from "preact/hooks"; -import { navigate } from "astro:transitions/client"; +import { type ComponentProps, createContext } from 'preact'; +import { useContext, useState } from 'preact/hooks'; +import { navigate } from 'astro:transitions/client'; import { - type FieldErrors, - type FormState, - type FormValidator, - formNameInputProps, - getInitialFormState, - toSetValidationErrors, - toTrackAstroSubmitStatus, - toValidateField, - validateForm, -} from "simple:form"; + type FieldErrors, + type FormState, + type FormValidator, + formNameInputProps, + getInitialFormState, + toSetValidationErrors, + toTrackAstroSubmitStatus, + toValidateField, + validateForm, +} from 'simple:form'; -export function useCreateFormContext( - validator: FormValidator, - fieldErrors?: FieldErrors -) { - const initial = getInitialFormState({ validator, fieldErrors }); - const [formState, setFormState] = useState(initial); - return { - value: formState, - set: setFormState, - setValidationErrors: toSetValidationErrors(setFormState), - validateField: toValidateField(setFormState), - trackAstroSubmitStatus: toTrackAstroSubmitStatus(setFormState), - }; +export function useCreateFormContext(validator: FormValidator, fieldErrors?: FieldErrors) { + const initial = getInitialFormState({ validator, fieldErrors }); + const [formState, setFormState] = useState(initial); + return { + value: formState, + set: setFormState, + setValidationErrors: toSetValidationErrors(setFormState), + validateField: toValidateField(setFormState), + trackAstroSubmitStatus: toTrackAstroSubmitStatus(setFormState), + }; } export function useFormContext() { - const formContext = useContext(FormContext); - if (!formContext) { - throw new Error( - "Form context not found. `useFormContext()` should only be called from children of a
component." - ); - } - return formContext; + const formContext = useContext(FormContext); + if (!formContext) { + throw new Error( + 'Form context not found. `useFormContext()` should only be called from children of a component.' + ); + } + return formContext; } type FormContextType = ReturnType; @@ -45,87 +42,82 @@ type FormContextType = ReturnType; const FormContext = createContext(undefined); export function Form({ - children, - validator, - context, - fieldErrors, - name, - ...formProps + children, + validator, + context, + fieldErrors, + name, + ...formProps }: { - validator: FormValidator; - context?: FormContextType; - fieldErrors?: FieldErrors; -} & Omit, "method" | "onSubmit">) { - const formContext = context ?? useCreateFormContext(validator, fieldErrors); + validator: FormValidator; + context?: FormContextType; + fieldErrors?: FieldErrors; +} & Omit, 'method' | 'onSubmit'>) { + const formContext = context ?? useCreateFormContext(validator, fieldErrors); - return ( - - { - e.preventDefault(); - e.stopPropagation(); - const formData = new FormData(e.currentTarget); - formContext.set((formState) => ({ - ...formState, - isSubmitPending: true, - submitStatus: "validating", - })); - const parsed = await validateForm({ formData, validator }); - if (parsed.data) { - const action = - typeof formProps.action === "string" - ? formProps.action - : // Check for Preact signals - formProps.action?.value ?? ""; - navigate(action, { formData }); - return formContext.trackAstroSubmitStatus(); - } + return ( + + { + e.preventDefault(); + e.stopPropagation(); + const formData = new FormData(e.currentTarget); + formContext.set((formState) => ({ + ...formState, + isSubmitPending: true, + submitStatus: 'validating', + })); + const parsed = await validateForm({ formData, validator }); + if (parsed.data) { + const action = + typeof formProps.action === 'string' + ? formProps.action + : // Check for Preact signals + formProps.action?.value ?? ''; + navigate(action, { formData }); + return formContext.trackAstroSubmitStatus(); + } - formContext.setValidationErrors(parsed.fieldErrors); - }} - > - {name ? : null} - {children} - - - ); + formContext.setValidationErrors(parsed.fieldErrors); + }} + > + {name ? : null} + {children} + + + ); } -export function Input({ - onInput, - ...inputProps -}: ComponentProps<"input"> & { name: string }) { - const formContext = useFormContext(); - const fieldState = formContext.value.fields[inputProps.name]; - if (!fieldState) { - throw new Error( - `Input "${inputProps.name}" not found in form. Did you use the
component?` - ); - } +export function Input({ onInput, ...inputProps }: ComponentProps<'input'> & { name: string }) { + const formContext = useFormContext(); + const fieldState = formContext.value.fields[inputProps.name]; + if (!fieldState) { + throw new Error( + `Input "${inputProps.name}" not found in form. Did you use the component?` + ); + } - const { hasErroredOnce, validationErrors, validator } = fieldState; - return ( - <> - { - const value = e.currentTarget.value; - if (value === "") return; - formContext.validateField(inputProps.name, value, validator); - }} - onInput={async (e) => { - onInput?.(e); - - if (!hasErroredOnce) return; - const value = e.currentTarget.value; - formContext.validateField(inputProps.name, value, validator); - }} - {...inputProps} - /> - {validationErrors?.map((e) => ( -

{e}

- ))} - - ); + const { hasErroredOnce, validationErrors, validator } = fieldState; + return ( + <> + { + const value = e.currentTarget.value; + if (value === '') return; + formContext.validateField(inputProps.name, value, validator); + }} + onInput={async (e) => { + onInput?.(e); + + if (!hasErroredOnce) return; + const value = e.currentTarget.value; + formContext.validateField(inputProps.name, value, validator); + }} + {...inputProps} + /> + {validationErrors?.map((e) =>

{e}

)} + + ); } diff --git a/packages/db/test/fixtures/ticketing-example/src/layouts/Layout.astro b/packages/db/test/fixtures/ticketing-example/src/layouts/Layout.astro index 6a6c31723f..114216824d 100644 --- a/packages/db/test/fixtures/ticketing-example/src/layouts/Layout.astro +++ b/packages/db/test/fixtures/ticketing-example/src/layouts/Layout.astro @@ -1,10 +1,10 @@ --- -import { ViewTransitions } from "astro:transitions"; -import "open-props/normalize"; -import "open-props/style"; +import { ViewTransitions } from 'astro:transitions'; +import 'open-props/normalize'; +import 'open-props/style'; interface Props { - title: string; + title: string; } const { title } = Astro.props; @@ -12,69 +12,69 @@ const { title } = Astro.props; - - - - - - - {title} - - - - - - + .newsletter { + display: flex; + align-items: center; + gap: var(--size-2); + } + + diff --git a/packages/db/test/fixtures/ticketing-example/src/pages/[event]/_Ticket.tsx b/packages/db/test/fixtures/ticketing-example/src/pages/[event]/_Ticket.tsx index bdff5a8674..ff37fba672 100644 --- a/packages/db/test/fixtures/ticketing-example/src/pages/[event]/_Ticket.tsx +++ b/packages/db/test/fixtures/ticketing-example/src/pages/[event]/_Ticket.tsx @@ -1,40 +1,40 @@ -import { createForm } from "simple:form"; -import { Form, Input } from "../../components/Form"; -import { z } from "zod"; -import { useState } from "preact/hooks"; +import { createForm } from 'simple:form'; +import { Form, Input } from '../../components/Form'; +import { z } from 'zod'; +import { useState } from 'preact/hooks'; export const ticketForm = createForm({ - email: z.string().email(), - quantity: z.number().max(10), - newsletter: z.boolean(), + email: z.string().email(), + quantity: z.number().max(10), + newsletter: z.boolean(), }); export function TicketForm({ price }: { price: number }) { - const [quantity, setQuantity] = useState(1); - return ( - <> - -

${(quantity * price) / 100}

+ const [quantity, setQuantity] = useState(1); + return ( + <> + +

${(quantity * price) / 100}

- - { - const value = Number(e.currentTarget.value); - setQuantity(value); - }} - /> + + { + const value = Number(e.currentTarget.value); + setQuantity(value); + }} + /> - - + + - - - - - ); + + + + + ); } diff --git a/packages/db/test/fixtures/ticketing-example/src/pages/[event]/index.astro b/packages/db/test/fixtures/ticketing-example/src/pages/[event]/index.astro index 01eab26496..b3554e8328 100644 --- a/packages/db/test/fixtures/ticketing-example/src/pages/[event]/index.astro +++ b/packages/db/test/fixtures/ticketing-example/src/pages/[event]/index.astro @@ -1,56 +1,48 @@ --- -import { Event, Ticket, db, eq } from "astro:db"; -import Layout from "../../layouts/Layout.astro"; -import { TicketForm, ticketForm } from "./_Ticket"; +import { Event, Ticket, db, eq } from 'astro:db'; +import Layout from '../../layouts/Layout.astro'; +import { TicketForm, ticketForm } from './_Ticket'; -if (!Astro.params.event) return Astro.redirect("/404"); +if (!Astro.params.event) return Astro.redirect('/404'); -const event = await db - .select() - .from(Event) - .where(eq(Event.id, Astro.params.event)) - .get(); +const event = await db.select().from(Event).where(eq(Event.id, Astro.params.event)).get(); -if (!event) return Astro.redirect("/404"); +if (!event) return Astro.redirect('/404'); const res = await Astro.locals.form.getData(ticketForm); if (res?.data) { - await db.insert(Ticket).values({ - eventId: Astro.params.event, - email: res.data.email, - quantity: res.data.quantity, - newsletter: res.data.newsletter, - }); + await db.insert(Ticket).values({ + eventId: Astro.params.event, + email: res.data.email, + quantity: res.data.quantity, + newsletter: res.data.newsletter, + }); } -const ticket = await db - .select() - .from(Ticket) - .where(eq(Ticket.eventId, Astro.params.event)) - .get(); +const ticket = await db.select().from(Ticket).where(eq(Ticket.eventId, Astro.params.event)).get(); --- -
-

{event.name}

-

- {event.description} -

+
+

{event.name}

+

+ {event.description} +

- - { - ticket && ( -
-

You're going 🙌

-

- You have purchased {ticket.quantity} tickets for {event.name}! -

-

- Check {ticket.email} for your tickets. -

-
- ) - } -
+ + { + ticket && ( +
+

You're going 🙌

+

+ You have purchased {ticket.quantity} tickets for {event.name}! +

+

+ Check {ticket.email} for your tickets. +

+
+ ) + } +
diff --git a/packages/db/test/fixtures/ticketing-example/src/pages/index.astro b/packages/db/test/fixtures/ticketing-example/src/pages/index.astro index b26acd474d..c8bbcbc701 100644 --- a/packages/db/test/fixtures/ticketing-example/src/pages/index.astro +++ b/packages/db/test/fixtures/ticketing-example/src/pages/index.astro @@ -1,17 +1,17 @@ --- -import { Event, db } from "astro:db"; +import { Event, db } from 'astro:db'; const firstEvent = await db.select().from(Event).get(); --- - - - - Eventbrite - - - - + + + + Eventbrite + + + + diff --git a/packages/db/test/fixtures/ticketing-example/tsconfig.json b/packages/db/test/fixtures/ticketing-example/tsconfig.json index e90c686cd3..e43d02f7c2 100644 --- a/packages/db/test/fixtures/ticketing-example/tsconfig.json +++ b/packages/db/test/fixtures/ticketing-example/tsconfig.json @@ -4,4 +4,4 @@ "jsx": "react-jsx", "jsxImportSource": "preact" } -} \ No newline at end of file +}