SolidJSS
SolidJSโ€ข10mo agoโ€ข
9 replies
Jason.json

Nesting server side functions in namespaces or any kind of block of code.

Is there any way to make nested server functions for example I would like to have an namespace called Users and then I could use .all() or .query() server-side function. It would make my code much cleaner after all. I tried to implement the namespaces like so:

Library database code


src/lib/db.ts
export namespace Users {
  export async function query(username:string) {
    "use server";
     // Rest of the code...
  }

  export async function all() {
    "use server";
     // Rest of the code...
  }
}


Example usage


src/routes/index.tsx
import { createAsync } from "@solidjs/router";
import { Suspense, Show } from "solid-js";
import { Users } from "~/lib/db";

export default function Index() {
  const users = createAsync(() => Users.all()); 

  return <Suspense>
    <h1>Users of your app</h1> 
    <Show when={users()}>
     // rest of the code...
    </Show>
  </Suspense>
}

, but I get an error: ^ Server Functions cannot be nested in other blocks or functions. Is there aby other way to make it cleaner?
Was this page helpful?