tabi-memo / backend

0 stars 0 forks source link

【Backend】Research Supabase #6

Open samuraikun opened 1 year ago

samuraikun commented 1 year ago

Research Supabase

Create Database

-- Supabase AI is experimental and may produce incorrect answers
-- Always verify the output before executing

-- Creating the 'users' table
create table
  users (
    id bigint primary key not null,
    user_uuid uuid not null unique,
    email text unique not null,
    name text not null,
    profile_picture_url varchar(255)
  );

-- Creating the 'trips' table
create table
  trips (
    id bigint primary key not null,
    trip_uuid uuid not null unique,
    user_id bigint references users (id),
    title text not null,
    date_from DATE,
    date_to DATE,
    cost numeric,
    description text,
    image_storage_object_id uuid -- FK to storage_objects, omitted as per your request
  );

-- Creating the 'schedules' table
create table
  schedules (
    id BIGSERIAL primary key not null,
    schedule_uuid uuid not null unique,
    trip_id bigint references trips (id),
    title text not null,
    time_from TIMESTAMPTZ,
    time_to TIMESTAMPTZ,
    address varchar(255),
    url varchar(255),
    memo text,
    cost numeric,
    image_storage_object_id uuid -- FK to storage_objects, omitted as per your request
  );

-- Creating the 'tags' table
create table
  tags (
    id BIGSERIAL primary key not null,
    tag_uuid uuid not null unique,
    name varchar(255) not null unique
  );

-- Creating the 'trip_tags' table
create table
  trip_tags (
    trip_id bigint references trips (id),
    tag_id bigint references tags (id),
    primary key (trip_id, tag_id)
  );

-- Creating the 'invitations' table
create table
  invitations (
    id BIGSERIAL primary key not null,
    invitation_uuid uuid not null unique,
    trip_id bigint references trips (id),
    invited_by_user_id bigint references users (id),
    invitee_user_id bigint references users (id),
    email varchar(255) not null,
    invitation_url varchar(255) not null,
    permission_level int not null
  );

GraphQL Playground

How to use from Frontend?

It's like this 👇

<html>
  <head>
    <title>GraphiQL</title>
    <link
      href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/2.4.7/graphiql.css"
      rel="stylesheet"
    />
  </head>
  <body style="margin: 0">
    <div id="graphiql" style="height: 100vh"></div>
    <script
      crossorigin
      src="https://unpkg.com/react@18/umd/react.production.min.js"
    ></script>
    <script
      crossorigin
      src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"
    ></script>
    <script
      crossorigin
      src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/2.4.7/graphiql.js"
    ></script>
    <script>
      const fetcher = GraphiQL.createFetcher({
        url: "https://ndmxdrvtdsakizkewsan.supabase.co/graphql/v1",
        headers: { apiKey: "API_KEY" },
      });
      ReactDOM.render(
        React.createElement(GraphiQL, { fetcher: fetcher }),
        document.getElementById("graphiql")
      );
    </script>
  </body>
</html>

Samples

Query

query {
  tripCollection(filter: { user_id: { eq: 1 } }) {
    edges {
      node {
        id
        uuid
        title
        date_from
        date_to
        user {
          id
          name
        }
      }
    }
  }
}

Mutation

mutation {
  insertIntotripCollection(
    objects: [
      {
        id: 4
        uuid: "1b87f517-4224-4fab-a814-744f13e995f8"
        user_id: 2
        title: "Osaka"
        date_from: "2023-01-01"
        date_to: "2023-01-05"
      }
    ]
  ) {
    affectedCount
    records {
      id
      title
    }
  }
}

Storage (WIP)