0

Run Arbitrary Query

by
Published Apr 10, 2023
Script surrealdb Verified

The script

Submitted by hugo989 Bun
Verified 14 days ago
1
import { Surreal } from "surrealdb@1";
2

3
/**
4
 * @param query The query string.
5
 * @param variables The variables used in the query.
6
 */
7
type Surrealdb = {
8
  url: string;
9
  token: string;
10
};
11
export async function main(
12
  auth: Surrealdb,
13
  namespace: string,
14
  database: string,
15
  query: string,
16
  variables?: Record<string, unknown>,
17
) {
18
  const client = new Surreal();
19
  await client.connect(auth.url);
20
  await client.authenticate(auth.token);
21
  await client.use({ namespace, database });
22
  const result = await client.query(query, variables);
23
  await client.close();
24
  return result;
25
}
26

Other submissions
  • Submitted by adam186 Deno
    Created 406 days ago
    1
    import Surreal from "https://deno.land/x/[email protected]/mod.ts";
    2
    
    
    3
    /**
    4
     * @param query The query string.
    5
     * @param variables The variables used in the query.
    6
     */
    7
    type Surrealdb = {
    8
      url: string;
    9
      token: string;
    10
    };
    11
    export async function main(
    12
      auth: Surrealdb,
    13
      namespace: string,
    14
      database: string,
    15
      query: string,
    16
      variables?: Record<string, unknown>,
    17
    ) {
    18
      const client = new Surreal(auth.url, auth.token);
    19
      await client.use(namespace, database);
    20
      const result = await client.query(query, variables);
    21
      client.close();
    22
      return result;
    23
    }
    24