https://kotlinlang.org logo
Title
m

Matheus Finatti

04/28/2023, 2:32 PM
Hi :android-wave: I’m trying to do something on SQLDelight (using latest version on KMM project) but it’s not working, I’m not sure if this isn’t supported or I’m doing something wrong. I have something like this, just 2 tables to store
userId <-> groupId
relationship and one to store members, which has a
groupId
column. I’m trying to
flatMap
one query into the other, but if I insert a new group under that
userId
it doesn’t trigger the chain. small example:
suspend fun getGroups(userId: String): Flow<List<Groups>> =
    userGroupQueries.getGroups(userId)
        .asFlow()
        .mapToList(coroutineContext)
        .flatMapLatest { groupIds: List<String> -> 
            memberQueries.getAllGroupsMembers(groupIds)
                .asFlow()
                .mapToList(coroutineContext)
                .mapLatest { members -> members.groupBy { it.groupId }}
        }

UserGroup.sq
CREATE TABLE UserGroup(
    userid TEXT NOT NULL,
    groupid TEXT NOT NULL,
    FOREIGN KEY (groupid) REFERENCES EGroup(id),
    FOREIGN KEY (userid) REFERENCES User(id)
);

getGroups:
SELECT groupid FROM UserGroup WHERE userid = ?;

Member.sq
getAllFromGroupIds:
SELECT * FROM Member WHERE groupid IN ?;
I can try to provide a minimum reproducible example if necessary. Didn’t find anything like this on SO or on github so I wonder if I’m trying to do something silly
g

glureau

04/28/2023, 2:42 PM
Why getting a Flow if you just want the latest? I'm worried you're waiting for a Flow to complete here, and it probably won't since it listen the DB changes. (Also I'm presume it could be a sql join for performance)
m

Matheus Finatti

04/28/2023, 2:46 PM
I don’t know 🤔, I kinda expected to be notified if somewhere along the chain a query emits, like if the user has a new group or a group new members.
In the end all I do is map it to a domain model like
Group(id: String, members: List<Member>)
so maybe this whole ordeal is indeed unnecessary
g

glureau

04/28/2023, 3:01 PM
You're listening 2 differents tables, but not at the same time. If the 1st request emit something, you'll start listening the second one. Let's say the 1st request emit a 2nd time, you'll start another listener on the 2nd request.
If you do an inner join in sql, you'll be able to listen for what you want.
m

Matheus Finatti

04/28/2023, 3:07 PM
Thanks, that makes sense.