Unity Supabase Insert

Roh·2023년 11월 21일
0

Supabase

목록 보기
4/4

이번에는 supabase database에 Insert를 해보자.

저번 포스트에서 연동을 해줬으니 이번엔 편하게 작업해보자.
Docs설명을 보면 아래와 같다.

Docs를 바탕으로 아래와 같이 코드를 작성해주었다.

using System;
using Supabase;
using UnityEngine;
using Client = Supabase.Client;
using System.Collections.Generic;
using Postgrest.Models; // Postgrest.Models.BaseModels

namespace com.example
{
	public class product : BaseModel  //
    {
        public int id { get; set; }
        public string name { get; set; }
        public string model_code{ get; set; }
        public int stock{ get; set; }
        public int brand_id { get; set; }
        public int price { get; set; } 
        public string description { get; set; }
        public string image_url { get; set; }



        public override bool Equals(object obj)
        {
            return obj is product productInstance &&
                    id == productInstance.id;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(id);
        }

    }

    public class SupabasePost : MonoBehaviour  //insert data
    {
        public SupabaseSettings SupabaseSettings = null!;
        private Client client;

        private async void Start()
        {
            var options = new SupabaseOptions
            {
                AutoConnectRealtime = true
            };

            var supabase = new Supabase.Client(SupabaseSettings.SupabaseURL, SupabaseSettings.SupabaseAnonKey, options);
            await supabase.InitializeAsync();

            var model = new product(){
                id = 7,
                name = "1",
                model_code = "1",
                stock = 1,
                brand_id = 1,
                price = 1000,
                description = "hello post",
                image_url = "http://hello"
            };

            var result = await supabase.From<product>().Insert(model);
            List<product> products = result.Models;

        }
    }
}

SupaManager에 Supabase Post Script를 넣고 실행시켜 보자.

정상적으로 내 database에 항목이 추가됐다.!!! post를 할때는 primary key를 꼭 생각하고 post를 해주자.

profile
Better than doing nothing

0개의 댓글

관련 채용 정보