UNPKG

surrealql

Version:

small SurrealDB JS/TS library to help facilitate the use of SurrealQL

1 lines 1.93 kB
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAiBnC,MAAM,UAAU,gBAAgB,CAAC,OAAgB,EAAE;IAClD,MAAM,OAAO,GAA2B,EAAE,CAAC;IAE3C,IAAI,IAAI,CAAC,IAAI;QAAE,OAAO,CAAC,eAAe,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACxH,IAAI,IAAI,CAAC,SAAS;QAAE,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC;IACnD,IAAI,IAAI,CAAC,QAAQ;QAAE,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC;IAEjD,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC;QAC3B,OAAO,EAAE,IAAI,CAAC,GAAG,IAAI,2BAA2B;QAChD,MAAM,EAAE,MAAM;QACd,OAAO;QACP,YAAY,EAAE,MAAM;KACpB,CAAC,CAAC;IAEH,OAAO,KAAK,WAAuB,KAAa;QAC/C,OAAO,MAAM,KAAK,CAAI,EAAE,EAAE;YACzB,IAAI,EAAE,KAAK;SACX,CAAC,CAAC;IACJ,CAAC,CAAC;AACH,CAAC","sourcesContent":["import { $fetch } from \"ohmyfetch\";\n\ntype Options = Partial<{\n\t/**\n\t * This is the path that will be POSTed to. Include the full path to sql.\n\t * For example, supply `http://localhost:8000/sql` or `https://surrealdb.example.com/sql`.\n\t * This allows custom paths that don't end in `/sql` to be provided too\n\t */\n\turl: string;\n\tauth: {\n\t\tusername: string;\n\t\tpassword: string;\n\t};\n\tnamespace: string;\n\tdatabase: string;\n}>;\n\nexport function create_surrealql(opts: Options = {}) {\n\tconst headers: Record<string, string> = {};\n\n\tif (opts.auth) headers[\"Authorisation\"] = Buffer.from(`${opts.auth.username}:${opts.auth.password}`).toString(\"base64\");\n\tif (opts.namespace) headers[\"NS\"] = opts.namespace;\n\tif (opts.database) headers[\"DB\"] = opts.database;\n\n\tconst fetch = $fetch.create({\n\t\tbaseURL: opts.url ?? \"http://localhost:8000/sql\",\n\t\tmethod: \"POST\",\n\t\theaders,\n\t\tresponseType: \"json\"\n\t});\n\n\treturn async function<T = unknown>(query: string) {\n\t\treturn await fetch<T>(\"\", {\n\t\t\tbody: query\n\t\t});\n\t};\n}\n"]}