nuxt-surrealdb
Version:
A Nuxt module aimed to simplify the use of SurrealDB
379 lines (378 loc) • 12.7 kB
JavaScript
import { hash } from "ohash";
import {
surrealFetchOptionsOverride
} from "#surrealdb/utils/overrides";
import {
computed,
createError,
toValue,
useNuxtApp,
useSurrealFetch,
useSurrealPreset,
useSurrealRPC
} from "#imports";
export function useSurrealDB(overrides) {
const { $surrealFetch, $surrealRPC } = useNuxtApp();
async function $authenticate(token, options) {
return $surrealRPC({
method: "authenticate",
params: [toValue(token)]
}, {
database: options?.database || overrides?.database,
token: options?.token || overrides?.token
});
}
async function authenticate(token, options) {
const { database, key, immediate, token: tokenOvr, watch, ...opts } = options || {};
const params = computed(() => [toValue(token)]);
const _key = key ?? "Sur_" + hash(["surreal", "authenticate", toValue(params)]);
return useSurrealRPC({ method: "authenticate", params }, {
...opts,
database: database || overrides?.database,
token: tokenOvr || overrides?.token,
immediate: immediate === void 0 ? false : immediate,
key: _key,
watch: watch === void 0 ? false : watch
});
}
async function $create(thing, data, options) {
return $surrealRPC({
method: "create",
params: [toValue(thing), toValue(data)]
}, {
database: options?.database || overrides?.database,
token: options?.token || overrides?.token
});
}
async function create(thing, data, options) {
const { database, immediate, key, token, watch, ...opts } = options || {};
const params = computed(() => [toValue(thing), toValue(data)]);
const _key = key ?? "Sur_" + hash(["surreal", "create", toValue(params)]);
return useSurrealRPC({ method: "create", params }, {
...opts,
database: database || overrides?.database,
token: token || overrides?.token,
immediate: immediate === void 0 ? false : immediate,
key: _key,
watch: watch === void 0 ? false : watch
});
}
async function $info(options) {
return $surrealRPC({
method: "info"
}, {
database: options?.database || overrides?.database,
token: options?.token || overrides?.token
});
}
async function info(options) {
const { database, key, token, ...opts } = options || {};
const _key = key ?? "Sur_" + hash(["surreal", "info"]);
return useSurrealRPC({ method: "info" }, {
...opts,
database: database || overrides?.database,
token: token || overrides?.token,
key: _key
});
}
async function $insert(thing, data, options) {
return $surrealRPC({
method: "insert",
params: [toValue(thing), toValue(data)]
}, {
database: options?.database || overrides?.database,
token: options?.token || overrides?.token
});
}
async function insert(thing, data, options) {
const { database, immediate, key, token, watch, ...opts } = options || {};
const params = computed(() => [toValue(thing), toValue(data)]);
const _key = key ?? "Sur_" + hash(["surreal", "insert", toValue(params)]);
return useSurrealRPC({ method: "insert", params }, {
...opts,
database: database || overrides?.database,
token: token || overrides?.token,
immediate: immediate === void 0 ? false : immediate,
key: _key,
watch: watch === void 0 ? false : watch
});
}
async function $invalidate(options) {
return $surrealRPC({
method: "invalidate"
}, {
database: options?.database || overrides?.database,
token: options?.token || overrides?.token
});
}
async function invalidate(options) {
const { database, immediate, key, token, watch, ...opts } = options || {};
const _key = key ?? "Sur_" + hash(["surreal", "invalidate"]);
return useSurrealRPC({ method: "invalidate" }, {
...opts,
database: database || overrides?.database,
token: token || overrides?.token,
immediate: immediate === void 0 ? false : immediate,
key: _key,
watch: watch === void 0 ? false : watch
});
}
async function $merge(thing, data, options) {
return $surrealRPC({
method: "merge",
params: [toValue(thing), toValue(data)]
}, {
database: options?.database || overrides?.database,
token: options?.token || overrides?.token
});
}
async function merge(thing, data, options) {
const { database, immediate, key, token, watch, ...opts } = options || {};
const params = computed(() => [toValue(thing), toValue(data)]);
const _key = key ?? "Sur_" + hash(["surreal", "merge", toValue(params)]);
return useSurrealRPC({ method: "merge", params }, {
...opts,
database: database || overrides?.database,
token: token || overrides?.token,
immediate: immediate === void 0 ? false : immediate,
key: _key,
watch: watch === void 0 ? false : watch
});
}
async function $patch(thing, patches, diff, options) {
return $surrealRPC({
method: "patch",
params: [toValue(thing), toValue(patches), toValue(diff)]
}, {
database: options?.database || overrides?.database,
token: options?.token || overrides?.token
});
}
async function patch(thing, patches, diff, options) {
const { database, immediate, key, token, watch, ...opts } = options || {};
const params = computed(() => [toValue(thing), toValue(patches), toValue(diff)]);
const _key = key ?? "Sur_" + hash(["surreal", "patch", toValue(params)]);
return useSurrealRPC({ method: "patch", params }, {
...opts,
database: database || overrides?.database,
token: token || overrides?.token,
immediate: immediate === void 0 ? false : immediate,
key: _key,
watch: watch === void 0 ? false : watch
});
}
async function $query(sql, vars, options) {
return $surrealRPC({
method: "query",
params: [toValue(sql), toValue(vars)]
}, {
database: options?.database || overrides?.database,
token: options?.token || overrides?.token
});
}
async function query(sql, vars, options) {
const { database, key, token, watch, ...opts } = options || {};
const params = computed(() => [toValue(sql), toValue(vars)]);
const _key = key ?? "Sur_" + hash(["surreal", "query", toValue(params)]);
return useSurrealRPC({ method: "query", params }, {
...opts,
database: database || overrides?.database,
token: token || overrides?.token,
key: _key,
watch: watch === false ? [] : [params, ...watch || []]
});
}
async function $remove(thing, options) {
return $surrealRPC({
method: "delete",
params: [toValue(thing)]
}, {
database: options?.database || overrides?.database,
token: options?.token || overrides?.token
});
}
async function remove(thing, options) {
const { database, key, immediate, token, watch, ...opts } = options || {};
const params = computed(() => [toValue(thing)]);
const _key = key ?? "Sur_" + hash(["surreal", "delete", toValue(params)]);
return useSurrealRPC({ method: "delete", params }, {
...opts,
database: database || overrides?.database,
token: token || overrides?.token,
key: _key,
immediate: immediate === void 0 ? false : immediate,
watch: watch === void 0 ? false : watch
});
}
async function $select(thing, options) {
return $surrealRPC({
method: "select",
params: [toValue(thing)]
}, {
database: options?.database || overrides?.database,
token: options?.token || overrides?.token
});
}
async function select(thing, options) {
const { database, key, token, watch, ...opts } = options || {};
const params = computed(() => [toValue(thing)]);
const _key = key ?? "Sur_" + hash(["surreal", "query", toValue(params)]);
return useSurrealRPC({ method: "select", params }, {
...opts,
database: database || overrides?.database,
token: token || overrides?.token,
key: _key,
watch: watch === false ? [] : [params, ...watch || []]
});
}
async function $signin(auth, options) {
const { NS, DB, SC, AC } = toValue(auth);
if (!(SC || AC) && (!toValue(auth).user || !toValue(auth).pass)) throw createError({ statusCode: 400, message: "Wrong admin credentials" });
const { host } = useSurrealPreset(options || overrides);
return $surrealRPC({
method: "signin",
params: [toValue(auth)]
}, {
database: {
host,
NS,
DB,
SC,
AC
}
});
}
async function signin(auth, options) {
const { NS, DB, SC, AC } = toValue(auth);
if (!(SC || AC) && (!toValue(auth).user || !toValue(auth).pass)) throw createError({ statusCode: 400, message: "Wrong admin credentials" });
const { database, immediate, key, token, watch, ...opts } = options || {};
const { host } = useSurrealPreset({ database: database || overrides?.database });
const params = computed(() => [toValue(auth)]);
const _key = key ?? "Sur_" + hash(["surreal", "signin", toValue(params)]);
return useSurrealRPC({ method: "signin", params }, {
...opts,
database: {
host,
NS,
DB,
SC,
AC
},
immediate: immediate === void 0 ? false : immediate,
key: _key,
watch: watch === void 0 ? false : watch
});
}
async function $signup(auth, options) {
const { NS, DB, SC, AC } = toValue(auth);
if (!NS) throw createError({ statusCode: 400, message: "Missing NS param" });
if (!DB) throw createError({ statusCode: 400, message: "Missing DB param" });
if (!SC || !AC) throw createError({ statusCode: 400, message: "Missing SC/AC param" });
const { host } = useSurrealPreset(options || overrides);
return $surrealRPC({
method: "signup",
params: [toValue(auth)]
}, {
database: {
host,
NS,
DB,
SC,
AC
}
});
}
async function signup(auth, options) {
const { NS, DB, SC, AC } = toValue(auth);
if (!NS) throw createError({ statusCode: 400, message: "Missing NS param" });
if (!DB) throw createError({ statusCode: 400, message: "Missing DB param" });
if (!SC || !AC) throw createError({ statusCode: 400, message: "Missing SC/AC param" });
const { database, immediate, key, token, watch, ...opts } = options || {};
const { host } = useSurrealPreset({ database: database || overrides?.database });
const params = computed(() => [toValue(auth)]);
const _key = key ?? "Sur_" + hash(["surreal", "signup", toValue(params)]);
return useSurrealRPC({ method: "signup", params }, {
...opts,
database: {
host,
NS,
DB,
SC,
AC
},
immediate: immediate === void 0 ? false : immediate,
key: _key,
watch: watch === void 0 ? false : watch
});
}
async function $update(thing, data, options) {
return $surrealRPC({
method: "update",
params: [toValue(thing), toValue(data)]
}, {
database: options?.database || overrides?.database,
token: options?.token || overrides?.token
});
}
async function update(thing, data, options) {
const { database, immediate, key, token, watch, ...opts } = options || {};
const params = computed(() => [toValue(thing), toValue(data)]);
const _key = key ?? "Sur_" + hash(["surreal", "update", toValue(params)]);
return useSurrealRPC({ method: "update", params }, {
...opts,
database: database || overrides?.database,
token: token || overrides?.token,
immediate: immediate === void 0 ? false : immediate,
key: _key,
watch: watch === void 0 ? false : watch
});
}
async function $version(options) {
const database = useSurrealPreset(options || overrides);
return $surrealFetch("version", {
...surrealFetchOptionsOverride(database)
});
}
async function version(options) {
const database = useSurrealPreset(options || overrides);
return useSurrealFetch("version", {
...surrealFetchOptionsOverride(database)
});
}
return {
$authenticate,
authenticate,
$create,
create,
$delete: $remove,
delete: remove,
$info,
info,
$insert,
insert,
$invalidate,
invalidate,
$merge,
merge,
$patch,
patch,
$query,
query,
$remove,
remove,
$select,
select,
$signin,
signin,
$signup,
signup,
$sql: $query,
sql: query,
$update,
update,
$version,
version,
$surrealFetch,
$surrealRPC
};
}