The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Chore/update rules in generate v3 (#31113)
Joshen Lim464f9aa5ddce
diff --git a/apps/studio/components/ui/AIAssistantPanel/Message.tsx b/apps/studio/components/ui/AIAssistantPanel/Message.tsxindex bcd29442f04..9cada1b99f5 100644--- a/apps/studio/components/ui/AIAssistantPanel/Message.tsx+++ b/apps/studio/components/ui/AIAssistantPanel/Message.tsx@@ -107,6 +107,18 @@ export const Message = function Message({code: (props: any) => {return <code className={cn('text-xs', props.className)}>{props.children}</code>},+ a: (props: any) => {+ return (+ <a+ target="_blank"+ rel="noopener noreferrer"+ href={props.href}+ className="underline transition underline-offset-2 decoration-foreground-lighter hover:decoration-foreground text-foreground"+ >+ {props.children}+ </a>+ )+ },}}>{content}diff --git a/apps/studio/pages/api/ai/sql/generate-v3.ts b/apps/studio/pages/api/ai/sql/generate-v3.tsindex 2ec951239eb..951cd36ea91 100644--- a/apps/studio/pages/api/ai/sql/generate-v3.ts+++ b/apps/studio/pages/api/ai/sql/generate-v3.ts@@ -74,12 +74,28 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) {- Use vector(384) data type for any embedding/vector related query- When debugging, retrieve sql schema details to ensure sql is correct- In Supabase, the auth schema already has a users table which is used to store users. It is common practice to create a profiles table in the public schema that links to auth.users to store user information instead. You don't need to create a new users table.+ - Never suggest creating a view to retrieve information from the users table of the auth schema. This is against our best practices.When generating tables, do the following:+ - Ensure that all tables always have a primary key+ - Ensure that all tables have RLS enabled. Inform the user that they will need to create RLS policies before being able to read or write to the table over Supabase APIs.- For primary keys, always use "id bigint primary key generated always as identity" (not serial)- Prefer creating foreign key references in the create statement- Prefer 'text' over 'varchar'- Prefer 'timestamp with time zone' over 'date'+ - If a foreign key is created, include a statement to create a database index on the columns involved in the foreign key. This is a best practice as it is common to use foreign key columns in join conditions when querying the database, hence the index will improve the performance of those joins and reduce database resource consumption.+ - If the table is a foreign table, create it in a new schema called private. If the private schema doesnt exist, create it.+ - Explain to the user that we create it outside of the public schema as it can present a security risk as foreign tables views cannot be configured to respect RLS policies. Provide this link for users to view more details about the security risk: https://supabase.com/docs/guides/database/database-advisors?queryGroups=lint&lint=0017_foreign_table_in_api++ When generating views, do the following:+ - All views should include 'with (security_invoker=on)' clause in the SQL statement for creating views.+ - Place the 'with (security_invoker=on)' immediately after the CREATE VIEW statement, before AS+ - If the view is a materialized view, create it in a new schema called private. If the private schema doesnt exist, create it.+ - Explain to the user that we create it outside of the public schema as it can present a security risk as materialized views cannot be configured to respect RLS policies of the underlying tables they are built upon, nor can they be secured with RLS directly. Provide this link for users to view more details about the security risk: https://supabase.com/docs/guides/database/database-advisors?queryGroups=lint&lint=0016_materialized_view_in_api++ When installing database extensions, do the following:+ - Never install extensions in the public schema+ - Extensions should be installed in the extensions schema, or a dedicated schemaFeel free to suggest corrections for suspected typos.@@ -90,6 +106,7 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) {- Then retrieve existing RLS policies and guidelines on how to write policies using the getRlsKnowledge tool .- Then write new policies or update existing policies based on the prompt- When asked to suggest policies, either alter existing policies or add new ones to the public schema.+ - When writing policies that use a function from the auth schema, ensure that the calls are wrapped with parentheses e.g select auth.uid() should be written as (select auth.uid()) instead# You write database functionsYour purpose is to generate a database function with the constraints given by the user. The output may also include a database trigger