peterzainzinger
DTDrizzle Team
•Created by peterzainzinger on 12/28/2023 in #help
Relational Query - how to call mapRelationalRow
I am trying to integrate drizzle orm with electric sql. I got pretty much everything working (with some hacks), the last remaining thing is live queries with relational queries. So my approach is the following:
type RunLiveResult<T extends SQLiteRelationalQuery<any, any>> =
T extends SQLiteRelationalQuery<any, infer R> ? R : never;
function useDrizzleRelationalLive<
const T extends SQLiteRelationalQuery<any, any>
>(
db: {
liveRaw(sql: Statement): LiveResultContext<any>;
},
rawQuery: T
): RunLiveResult<T> | undefined {
const selectQuery = useMemo(() => rawQuery.toSQL(), [rawQuery]);
const { results } = useLiveQuery(
db.liveRaw({
sql: selectQuery.sql,
args: selectQuery.params as any,
})
);
const unwrapped = Array.isArray(results)
? results.map(unwrapJsonValue)
: results;
return unwrapped as any;
}
So basically electric-sql gives me a useLiveQuery hook where I pass the raw sql + params of the relational query. This works fine, but the return type is not correct. I did read the drizzle source code, there I found "mapRelationalRow" https://github.com/drizzle-team/drizzle-orm/blob/d535e0b667bec3aad6d238a71cebc23de30c455f/drizzle-orm/src/relations.ts#L659 - the first 3 arguments are clear what to provide, but what about "buildQueryResultSelection" and "mapColumnValue" - are these properties on anything (exposed or not)
1 replies