Supabase query all posts from followed users that are not liked - database

I am trying to get all posts from followed users such that the posts have not been liked by the following user (aka me).
how can i create a schema and query such a thing in supabase?
My current tables are like this:
User Table {
id
username
}
Follows table {
followerId
followingId
}
Post table {
id
title
}
liked_by table {
user_id,
post_id,
}

Assuming youru post table also has user_id column where the user_id represents user_id of the user who created the post, you can create a postgres function that looks something like this:
create or replace function not_liked_posts()
returns table(post_id uuid, title text)
as
$func$
select
post.id as post_id,
post.title
from post
inner join users on post.user_id = users.id
where post.user_id in (select followerId from follows where auth.uid() = followingId)
where post.id not in (select post_id from liked_by where user_id = auth.uid());
$func$
language sql security invoker;
Note that auth.uid() is a function that Supabase provides out of the box to get the currently signed in user's user id.
Once you create the above function, you should be able to call it using the rpc() method of Supabase like this:
const { data, error } = await supabase
.rpc('not_liked_posts')

Related

Combine two Salesforce SOQL Query

I am using two SOQL query in Salesforce.
First Query: Select Id, FirstName, LastName from User where Id='00000ADFEDSFSRTGDR'
Second Query: Select IsFrozen from UserLogin where UserId='00000ADFEDSFSRTGDR'
Can we combine these two query into a single query. Please help me on this.
No. If you use "describe" calls on User or UserLogin you'll see they are linked but there's no "relationshipName" field in the describe's result. That's what's used to link, bit like table alias in normal database.
// No going "up"
System.debug(UserLogin.UserId.getDescribe().getRelationshipName());
// And no going "down" either
for(Schema.ChildRelationship cr : User.SObjectType.getDescribe().getChildRelationships()){
if(cr.getChildSObject() == UserLogin.sObjectType && cr.getField() == UserLogin.UserId){
System.debug(cr);
System.debug(cr.getRelationshipName());
}
}
So you can do
SELECT Id, Name,
(SELECT PermissionSet.Name FROM PermissionSetAssignments)
FROM User
because PermissionSetAssignment.AssigneeId has relationshipName. But not
SELECT Id, Name,
(SELECT IsFrozen FROM UserLogin)
FROM User
Going "up" doesn't work either. SELECT Account.Name FROM Contact works OK but SELECT IsFrozen, User.Name FROM UserLogin doesn't. Again - because there's no relationshipName in the describe results.
You'll have to query separately and link them them in code as Map<Id, User> for example.

Get Contact Emails of Currently Active Account as List

Given: A Salesforce user is viewing an account page.
Desired Output: All Emails of Contacts related to the Account currently viewed as a List object.
My code:
SELECT Email FROM Contact WHERE Id IN (SELECT ContactId FROM AccountContactRelation WHERE AccountId = ApexPages.CurrentPage.getParameters().get('id'))
This does not retrieve any results. When using a fixed number instead of ApexPages.CurrentPage.getParameters().get('id'), the Emails are correctly returned.
I'm sort of new to Apex. Could anyone point out what I am doing wrong?
To achieve the desired output you need to use SOQL variable injection.
You can do this by creating a variable first and then referencing the variable inside the SOQL using : and the variable name:
String theAccountId = ApexPages.CurrentPage.getParameters().get('id');
List<Contact> theContacts = [SELECT Email FROM Contact WHERE Id IN (SELECT ContactId FROM AccountContactRelation WHERE AccountId = :theAccountId)];
You can use a static query with a bind variable to retrieve the correct results.
Additionally, the Contact object contains an AccountId field of its own. Therefore, depending on your setup, you may be able to eliminate your subquery. You may also want to filter out Email fields that are empty, since Email is not a required field.
The complete result could look something like this:
String accountId = ApexPages.CurrentPage.getParameters().get('id');
List<Contact> accountContactsEmailList = [
SELECT
Email
FROM
Contact
WHERE
Email != ''
AND AccountId = :accountId
];
for (Contact contact : accountContactsEmailList) {
System.debug(contact.Email);
}

How to extract both type of users count

I have a action table. It contain a user type admin or consumer, some actions like login, change password etc..
userID actiontype usertype
1 login admin
2 login consumer
3 Change Password admin
4 Change password admin
5 Change Password consumer
...................
........
Now i have to create a result table in which i have all the count of actions by their both type of user as following table.
Result table
actiontype admin customer
login 1 1
change Password 2 1
How i can do this.??
sorry for this, actually i need this .that was because of some misunderstanding.
First you need to create a result table using a SQL statement like below :
Create Table :
CREATE TABLE ResultsTable
(
ActionType varchar(50),
Count int,
UserType varchar(50)
);
Then insert data using the following query.
Insert Data :
INSERT INTO ResultsTable (ActionType, Count, UserType)
SELECT ActionType, UserType, COUNT(*) as Count
FROM ActionTable
GROUP BY ActionType, UserType;
Assuming that the difference between Change Password and change password is just a typo and not actually in your database, you can solve that problem with a GROUP BY clause:
SELECT actiontype, usertype, COUNT(*) as count
FROM your_table
GROUP BY actiontype, usertype
Look at this doc to find more details on the clause.

Salesforce SOQL Filter by child relationship

I have the following simple query which shows I can access the field I want to filter by:
SELECT Id, Name, (SELECT HC4__IsSearchableExternally__c FROM Contacts)
FROM Account
However, what I really want to do is return only the Id and Name properties for Accounts that have at least one Contact where HC4__IsSearchableExternally__c is true. Is this possible to do with a Salesforce query?
Basically, I want to do something like the following (nonfunctional query):
SELECT Id, Name
FROM Account
WHERE (SELECT COUNT(Id) FROM Contacts WHERE HC4__IsSearchableExternally__c = true) > 0
Thanks for any help you can provide!
You can do this with a semi-join, e.g:
select id, name from account
where id in (select accountId from contact where HC4__IsSearchableExternally__c = true)

SalesForce SOQL MALFORMED_QUERY

Why is this SOQL query returning MALFORMED_QUERY: unexpected token: on
Select id FROM account
where id = '0012000000I7MkRAAV' or id = '0012000000I7MkRAAV'
and id = '0012000000I7MkRAAV'
Changing "and" to "or" returns the result just fine:
Select id FROM account
where id = '0012000000I7MkRAAV' or id = '0012000000I7MkRAAV'
or id = '0012000000I7MkRAAV'
I am executing the query in Force explorer.
You need to group your and/or's so that its not ambiguous, e.g.
Select id FROM account where id = '0012000000I7MkRAAV' or (id = '0012000000I7MkRAAV' and id = '0012000000I7MkRAAV')
The problem is that one account record can't has two ids in the same time. One object record has just one Id. In this query you can use only OR statement

Resources