My goal is to see if a row exists with the user's email. If it exists, do nothing. If it does not exist, create it. I've tried a few variations of the function below, but it fails every time when I use OAuth
, printing an error that a row with that column value does not exist, which is to be expected. Instead of making the document since it doesn't exist, the whole function just stops after that error.
I understand that this is because there is no matching row to select()
but there is also no way to just check if a row exists. With Firebase there was a simple function to check if a document existed, so I'm surprised to see that Supabase does not have this. Here is what I have now:
try {
Map<String, dynamic>? currentUserFromDB = await Supabase.db
.from('profiles')
.select()
.eq('email', user!.email.toString())
.maybeSingle();
bool userExistsInDB = currentUserFromDB != null;
if (!userExistsInDB) {
await Supabase.db.from('profiles').insert({
'uid': user.id,
'email': user.email,
}).catchError((error) {
print('Error: $error');
});
} else {
print('User exists in db');
}
} catch (e) {
print(e);
}
Profiles table:
- uid (Primary key, text)
- email (text)
Row Level Security:
- Enable delete for users based on uid
- Enable read access for all users
- Enable update for users based on uid