drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: useLiveQuery skip param

Open
#2,606 2 comments 1 reaction 1 assignee Claimed by @RomanNabukhotnyi View on GitHub
driver/expo-sqlite improvement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

I'm running into an issue where I have to create an unnecessary child component to handle cases where a dependent variable has yet to be defined.

I think it could look something like this:
```ts
export const useLiveQuery = | SQLiteRelationalQuery<'sync', unknown>>(
query: T,
{skip}: {skip: boolean} // <---- Take in skip boolean
) => {
const [data, setData] = useState>(
(is(query, SQLiteRelationalQuery) && query.mode === 'first' ? undefined : []) as Awaited,
);
const [error, setError] = useState();
const [updatedAt, setUpdatedAt] = useState();

useEffect(() => {
const entity = is(query, SQLiteRelationalQuery) ? query.table : (query as AnySQLiteSelect).config.table;

if (is(entity, Subquery) || is(entity, SQL)) {
setError(new Error('Selecting from subqueries and SQL are not supported in useLiveQuery'));
return;
}

let listener: ReturnType | undefined;

const handleData = (data: any) => {
setData(data);
setUpdatedAt(new Date());
};
// Adding the skip check here allows for validation above but prevents the query from firing and listener from registering
if (skip) return;
query.then(handleData).catch(setError);

if (is(entity, SQLiteTable) || is(entity, SQLiteView)) {
const config = is(entity, SQLiteTable) ? getTableConfig(entity) : getViewConfig(entity);
listener = addDatabaseChangeListener(({ tableName }) => {
if (config.name === tableName) {
query.then(handleData).catch(setError);
}
});
}

return () => {
listener?.remove();
};
}, [skip]);

return {
data,
error,
updatedAt,
} as const;
};
```

A developer may want to know if something has been skipped, so we may need to handle that case as well, but I think keeping it simple to start would be best.

I could open a PR and check all of this out if the above seems acceptable?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.