Logo

dev-resources.site

for different kinds of informations.

Custom schema specific Supabase Server Component clients in Grida Form workspace

Published at
12/30/2024
Categories
opensource
typescript
schema
supabase
Author
ramunarasinga-11
Author
16 person written this
ramunarasinga-11
open
Custom schema specific Supabase Server Component clients in Grida Form workspace

In this article, we review the code in server.ts in Grida. Well, what’s so special about this file? let’s find out.

Image description

Server Component Client

Before we look at Grida Forms server.ts, Let’s first understand what a Server Component Client means.

In the Supabase Auth guides, at step 3, you will find the information about Server Component Client and it has the below code.

import { createServerClient } from '@supabase/ssr'
import { cookies } from 'next/headers'

export async function createClient() {
  const cookieStore = await cookies()

  return createServerClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL!,
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
    {
      cookies: {
        getAll() {
          return cookieStore.getAll()
        },
        setAll(cookiesToSet) {
          try {
            cookiesToSet.forEach(({ name, value, options }) =>
              cookieStore.set(name, value, options)
            )
          } catch {
            // The `setAll` method was called from a Server Component.
            // This can be ignored if you have middleware refreshing
            // user sessions.
          }
        },
      },
    }
  )
}
Enter fullscreen mode Exit fullscreen mode

Server Component client — To access Supabase from Server Components, Server Actions, and Route Handlers, which run only on the server.

So far, I have reviewed Supabase Server Component Client in Nextjs-Subscription-Payments example and Midday.ai but I have never seen multiple Server components clients defined in the same file like in Grida Form server.ts

Grida Form server.ts

Grida Form server.ts has multiple Server Component clients defined.

workspace client

At line 7, you will find the below code:

export const workspaceclient = createClient<Database, "public">(
  process.env.NEXT_PUBLIC_SUPABASE_URL!,
  process.env.SUPABASE_SERVICE_KEY!,
  {
    db: {
      schema: "public",
    },
  }
);
Enter fullscreen mode Exit fullscreen mode

Grida Forms Client

At line 17, you will find the below code:

export const grida_forms_client = createClient<Database, "grida_forms">(
  process.env.NEXT_PUBLIC_SUPABASE_URL!,
  process.env.SUPABASE_SERVICE_KEY!,
  {
    db: {
      schema: "grida_forms",
    },
  }
);
Enter fullscreen mode Exit fullscreen mode

Grida Sites Client

At line 27, you will find this below code:

export const grida_sites_client = createClient<Database, "grida_sites">(
  process.env.NEXT_PUBLIC_SUPABASE_URL!,
  process.env.SUPABASE_SERVICE_KEY!,
  {
    db: {
      schema: "grida_sites",
    },
  }
);
Enter fullscreen mode Exit fullscreen mode

Grida Commerce Client

At line 37, you will find the below code:

export const grida_commerce_client = createClient<Database, "grida_commerce">(
  process.env.NEXT_PUBLIC_SUPABASE_URL!,
  process.env.SUPABASE_SERVICE_KEY!,
  {
    db: {
      schema: "grida_commerce",
    },
  }
);
Enter fullscreen mode Exit fullscreen mode

Grida XSupabase Client

At line 47, you will find the below code:

export const grida_xsupabase_client = createClient<
  Database,
  "grida_x_supabase"
>(process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.SUPABASE_SERVICE_KEY!, {
  db: {
    schema: "grida_x_supabase",
  },
});
Enter fullscreen mode Exit fullscreen mode

All these clients are located in a single file — grida/apps/forms/lib/supabase/server.ts

createClient imported

At line 4 in server.ts, you will find the below import

import { createClient } from "@supabase/supabase-js";
Enter fullscreen mode Exit fullscreen mode

But is this same as what is provided in the Supabase Auth Documentation? Nope. You will find the below import in Supabase Auth Docs.

import { createServerClient } from '@supabase/ssr'
Enter fullscreen mode Exit fullscreen mode

Wait, so Supabase Auth Docs tells us to use @supabase/ssr and Grida Forms workspace uses @supabase/supabase-js. Do you know why?

In order to configure your Supabase client with a custom schema, you will have to use @supabase/supabase-js. Using Custom Schema Guide has the following example code snippet provided:

// Initialize the JS client
import { createClient } from '@supabase/supabase-js'
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, { db: { schema: 'myschema' } })
// Make a request
const { data: todos, error } = await supabase.from('todos').select('*')
// You can also change the target schema on a per-query basis
const { data: todos, error } = await supabase.schema('myschema').from('todos').select('*')
Enter fullscreen mode Exit fullscreen mode

You will find the clients in Grida Form server.ts have the same syntax for defining the db schema.

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

I am open to work on interesting projects. Send me an email at [email protected]

My Github — https://github.com/ramu-narasinga

My website — https://ramunarasinga.com

My Youtube channel — https://www.youtube.com/@thinkthroo

Learning platform — https://thinkthroo.com

Codebase Architecture — https://app.thinkthroo.com/architecture

Best practices — https://app.thinkthroo.com/best-practices

Production-grade projects — https://app.thinkthroo.com/production-grade-projects

References:

  1. https://github.com/gridaco/grida/blob/main/apps/forms/lib/supabase/server.ts#L17

  2. https://supabase.com/docs/guides/auth/server-side/nextjs?queryGroups=router&router=app

  3. https://supabase.com/docs/guides/api/using-custom-schemas

schema Article's
30 articles in total
Favicon
Schema Markup can boost your click-through rates by up to 30%?
Favicon
Custom schema specific Supabase Server Component clients in Grida Form workspace
Favicon
Zod for TypeScript Schema Validation: A Comprehensive Guide
Favicon
Database schema design of Splitwise application
Favicon
Validating JSON Schema with Fixed and User-Defined Keys in Python
Favicon
Using Zod's z.union: An Important Pitfall to Avoid
Favicon
Desvendando o Atlas: Gerencie Seus Esquemas de Banco de Dados com Estilo🚀
Favicon
Customize Schema with @extend_schema_view
Favicon
Hotel reservation Schema design (PostgreSQL)
Favicon
How to Insert Data into Specific Table of PostgreSQL Schemas with Node.js
Favicon
Typesafe Supabase Flutter Queries
Favicon
Using yup to build schema with value parsing and validation.
Favicon
Designing an Optimal Database Schema for a Followers-Following System in a Blog-Post App
Favicon
JobPosting from Schema.org
Favicon
Unlocking the Power of Mongoose Schemas: Enhancements for Better Data Handling
Favicon
Unraveling the Power of Schema Markup: Elevating Your Website's Visibility and Performance
Favicon
How to handle complex json schema
Favicon
Navigating Django Schemas with Ease: How Django-schema-viewer Helps Developers
Favicon
🐣Your First Database Schema Change in 5 Minutes with Bytebase
Favicon
Multiplos schemas no mesmo tópico Kafka na linha de comando
Favicon
How pgroll works under the hood
Favicon
Automating API Documentation: A Journey from TypeScript to OpenAPI and Schema Governence with Optic
Favicon
Master schema validation in TypeScript with Zod
Favicon
🚛 Deploy Database Schema Migrations with Bytebase
Favicon
Random Data Generator Website
Favicon
Xata's JSON Column Type
Favicon
Introducing pgroll: Zero-downtime, Reversible, Schema Migrations for Postgres
Favicon
Designing Your Database Schema
Favicon
WHAT ARE THE TYPES OF SCHEMA MARKEUP?
Favicon
Iceberg Schema Evolution in Trino

Featured ones: