change databases from surrealdb to pocketbase

This commit is contained in:
2025-06-26 01:17:45 +02:00
parent 18d7fe8caa
commit 274b49dd27
9 changed files with 89 additions and 112 deletions

View File

@@ -1,31 +1,27 @@
import Surreal from "surrealdb";
import type { AccessToken } from "@twurple/auth";
import PocketBase, { RecordService } from "pocketbase";
const surrealurl = process.env.SURREAL_URL ?? "";
if (surrealurl === "") { console.error("Please provide a SURREAL_URL in .env."); process.exit(1); };
const namespace = process.env.SURREAL_NAMESPACE ?? "";
if (namespace === "") { console.error("Please provide a SURREAL_NAMESPACE in .env."); process.exit(1); };
const database = process.env.SURREAL_DATABASE ?? "";
if (database === "") { console.error("Please provide a SURREAL_DATABASE in .env."); process.exit(1); };
const username = process.env.SURREAL_USERNAME ?? "";
if (username === "") { console.error("Please provide a SURREAL_USERNAME in .env."); process.exit(1); };
const password = process.env.SURREAL_PASSWORD ?? "";
if (password === "") { console.error("Please provide a SURREAL_PASSWORD in .env."); process.exit(1); };
const pocketbaseurl = process.env.POCKETBASE_URL ?? "localhost:8090";
if (pocketbaseurl === "") { console.error("Please provide a POCKETBASE_URL in .env."); process.exit(1); };
export default async function DB(): Promise<Surreal> {
const db = new Surreal();
try {
await db.connect(surrealurl, {
auth: {
username,
password
}
});
await db.use({ namespace, database });
return db;
}
catch (err) {
console.error("Failed to connect to SurrealDB:", err instanceof Error ? err.message : String(err));
await db.close();
throw err;
};
export type authRecord = {
id: string;
accesstoken: AccessToken;
};
export type userRecord = {
id: string;
username: string;
balance: number;
inventory: object;
lastlootbox: string;
};
type TypedPocketBase = {
collection(idOrName: 'auth'): RecordService<authRecord>;
collection(idOrName: 'users'): RecordService<userRecord>;
};
const pb = new PocketBase(pocketbaseurl) as TypedPocketBase;
export default pb;

View File

@@ -1,77 +1,38 @@
import type { AccessToken } from "@twurple/auth";
import DB from "./connection";
import type { RecordId } from "surrealdb";
type authRecord = {
accesstoken: AccessToken,
user: string,
};
export async function createAuthRecord(token: AccessToken, userId: string): Promise<void> {
const db = await DB();
if (!db) return;
const data: authRecord = {
accesstoken: token,
user: userId
};
import pocketbase, { type authRecord } from "./connection";
const pb = pocketbase.collection('auth');
export async function createAuthRecord(token: AccessToken, userId: string) {
try {
await db.create("auth", data);
} catch (err) {
console.error(err);
} finally {
await db.close();
};
};
type getAuthRecordQuery = authRecord & { id: RecordId };
type getAuthRecordResult = { accesstoken: AccessToken, id: RecordId };
export async function getAuthRecord(userId: string, requiredIntents: string[]): Promise<getAuthRecordResult | undefined> {
const db = await DB();
if (!db) return undefined;
try {
const data = await db.query<getAuthRecordQuery[][]>("SELECT * from auth WHERE user=$userId AND accesstoken.scope CONTAINSALL $intents;", { userId, intents: requiredIntents });
if (!data[0] || !data[0][0]) return undefined;
return { accesstoken: data[0][0].accesstoken, id: data[0][0].id };
} catch (err) {
console.error(err);
return undefined;
} finally {
await db.close();
};
};
export async function updateAuthRecord(userId: string, intents: string[], newtoken: AccessToken): Promise<boolean> {
const db = await DB();
if (!db) return false;
try {
const data = await getAuthRecord(userId, intents);
const newrecord: authRecord = {
accesstoken: newtoken,
user: userId
const data: authRecord = {
accesstoken: token,
id: userId
};
await db.update(data?.id!, newrecord);
return true;
await pb.create(data);
} catch (err) { };
};
export async function getAuthRecord(userId: string, requiredIntents: string[]) {
try {
const data = await pb.getOne(userId);
if (!requiredIntents.every(intent => data.accesstoken.scope.includes(intent))) return undefined;
return { accesstoken: data.accesstoken };
} catch (err) {
console.error(err);
return false;
} finally {
await db.close();
return undefined;
};
};
export async function updateAuthRecord(userId: string, newtoken: AccessToken) {
try {
const newrecord = {
accesstoken: newtoken,
};
await pb.update(userId, newrecord);
} catch (err) { };
};
export async function deleteAuthRecord(userId: string): Promise<void> {
const db = await DB();
if (!db) return;
try {
const data = await db.query<getAuthRecordQuery[][]>("SELECT * FROM auth WHERE user=$userId;", { userId });
if (!data[0] || !data[0][0]) return undefined;
for (const obj of data[0]) {
db.delete(obj.id);
};
} catch (err) {
console.error(err);
};
await pb.delete(userId);
} catch (err) { };
};