I have been trying to initialise a database client and from what I can tell, it has to be done either in the root index or a layout.tsx that wraps where the database will be accessed. I've chosen to do so in layout.tsx.
// layout.tsx
export const getDBClient = routeLoader$(async ({ platform }) => {
let instance: DBClient | undefined = undefined;
if (instance) return instance;
instance = noSerialize(
await new DBClient({
apiKey: platform?.env?.DB_API_KEY
})
);
});
export default component$(() => <><Slot/></>);
and then in a component that will be within layout.tsx:
export default component$(async () => {
const res = await getDBClient().value.getSomeValue();
return <p>{res}</p>
})
From what I can tell, this seems to work, but TypeScript isn't happy about it, giving the following error:
Argument of type '() => Promise<JSX.Element>' is not assignable to parameter of type 'OnRenderFn<{}>'.
Type 'Promise<Element>' is missing the following properties from type 'JSXNode<any>': type, props, immutableProps, children, and 2 more.ts(2345)
Is there a way to fix this error? I'm looking at this page in the docs (https://qwik.builder.io/docs/advanced/eslint/) and there are multiple examples showing component$ wrapping an async function, so I'm not sure what I'm doing wrong.