Giter Site home page Giter Site logo

deno_supabase's Introduction

DenoでSupabaseを操作するサンプル

概要

  • DBからデータを取得する
  • DBにデータを追加する

準備

  1. GitHubのアカウントを作成
  2. SupabaseにGitHubでログイン
  3. project test1 を作成し、SUPABASE_URL と SUPABASE_KEY を設定
export SUPABASE_URL=***
export SUPABASE_KEY=***
  1. table post を作成し、RLS(Row Level Security)はひとまずdisabledにする
  2. table postに、下記の項目を追加
username :text
title :text
date :timestamptz
description :text
participants :numeric
  1. サーバーを起動する
deno run server.js
  1. ブラウザで http://localhost:8000/ を開く

使い方

fetch

if (req.method === "GET" && pathname === "/fetch-posts") {
    const data = await fetchPosts();
    if (error) return handleError(error);

    console.log("成功したかも" + JSON.stringify(data));
    return new Response(JSON.stringify(data), { headers: { "content-type": "application/json" } });
}

insert

if (req.method === "POST" && pathname === "/register-post") {
    const requestData = await req.json();
    const postData = {
        username: requestData.username,
        title: requestData.title,
        date: requestData.date,
        description: requestData.description,
        participants: 0
    };

    const { error } = await registerPost(postData);
    if (error) return handleError(error);

    console.log("成功したかも" + requestData.date);
    return new Response(JSON.stringify(requestData), { headers: { "content-type": "application/json" } });
}

Update

if (req.method === "POST" && pathname === "/add-participants") {
    const requestData = await req.json();

    const { participants, error1 } = await getParticipants(requestData);
    if (error1) return handleError(error1);

    const newParticipantCount = participants[0].participants + 1;
    const { error } = await updateParticipants(requestData, newParticipantCount);
    if (error) return handleError(error);

    console.log("成功したかも" + requestData);
    return new Response(JSON.stringify(requestData), { headers: { "content-type": "application/json" } });
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.