Edits history of script submission #22565 for ' Sign in (surrealdb)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { Surreal } from "surrealdb@1";
    
    /**
     * @returns The authenication token.
     */
    export async function main(
      url: string,
      user: string,
      password: string,
      namespace?: string,
      database?: string,
      scope?: string,
    ) {
      const client = new Surreal();
      await client.connect(url);
      // surrealdb infers the auth level (root / namespace / database / scope) from
      // which keys are present, so only include the ones actually provided —
      // passing `scope: undefined` etc. makes it attempt scope auth and fail.
      const credentials: Record<string, string> = { username: user, password };
      if (namespace !== undefined) credentials.namespace = namespace;
      if (database !== undefined) credentials.database = database;
      if (scope !== undefined) credentials.scope = scope;
      return await client.signin(credentials as Parameters<typeof client.signin>[0]);
    }
    

    Submitted by hugo989 14 days ago