Created
November 19, 2021 10:47
-
-
Save mikoscz/da0a5ef93f8dde29137e365c61d9ce38 to your computer and use it in GitHub Desktop.
ember_example_supabase_adapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Adapter from '@ember-data/adapter'; | |
import { createClient } from '@supabase/supabase-js'; | |
const DB_URL = ''; | |
const ANON_KEY = ''; | |
export default class TodoAdapter extends Adapter { | |
supabase = createClient(DB_URL, ANON_KEY); | |
async findRecord(_store, _type, id) { | |
const { data, error } = await this.supabase.from('todos').match({ id }); | |
if (error) { | |
throw new Error(`Supabase::findRecord - ${error}`); | |
} | |
return data[0]; | |
} | |
async createRecord(_store, _type, snapshot) { | |
const record = this.serialize(snapshot, { includeId: true }); | |
const { data, error } = await this.supabase.from('todos').insert([record]); | |
if (error) { | |
throw new Error(`Supabase::createRecord - ${error}`); | |
} | |
return data[0]; | |
} | |
async updateRecord(_store, _type, snapshot) { | |
const record = this.serialize(snapshot, { includeId: true }); | |
const id = snapshot.id; | |
const { data, error } = await this.supabase | |
.from('todos') | |
.update(record) | |
.match({ id }); | |
if (error) { | |
throw new Error(`Supabase::updateRecord - ${error}`); | |
} | |
return data[0]; | |
} | |
async deleteRecord(_store, _type, snapshot) { | |
const id = snapshot.id; | |
const { data, error } = await this.supabase | |
.from('todos') | |
.delete() | |
.match({ id }); | |
if (error) { | |
throw new Error(`Supabase::deleteRecord - ${error}`); | |
} | |
return data[0]; | |
} | |
async findAll() { | |
const { data, error } = await this.supabase.from('todos').select(); | |
if (error) { | |
throw new Error(`Supabase::findAll - ${error}`); | |
} | |
return data; | |
} | |
query() { | |
// TODO: implement if we would have time | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment