Skip to content

Commit

Permalink
fix: fix bun starter (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno authored Sep 15, 2024
1 parent 406dc09 commit 22be273
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 53 deletions.
Binary file modified ts-bun-starter/bun.lockb
100644 → 100755
Binary file not shown.
39 changes: 21 additions & 18 deletions ts-bun-starter/src/create.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { getDb, initDb } from "../utils/surreal";
import { getDb } from "../utils/surreal";
import { jsonify } from "surrealdb.js";

async function createUser() {
const db = await initDb();
if (!db) {
console.error("Database not initialized");
return;
}
try {
const user = await db.create('User', {
// User details
username: "newUser",
email: "[email protected]",
password: "securePassword", // Note: Store hashed passwords, not plain text
});
console.log("User created:", user);
} catch (err) {
console.error("Failed to create user:", err);
}
const db = await getDb();
if (!db) {
console.error("Database not initialized");
return;
}
try {
const user = await db.create("User", {
// User details
username: "newUser",
email: "[email protected]",
password: "securePassword", // Note: Store hashed passwords, not plain text
});
console.log("User created:", jsonify(user));
} catch (err) {
console.error("Failed to create user:", err);
} finally {
await db.close();
}
}

createUser();
createUser();
29 changes: 16 additions & 13 deletions ts-bun-starter/src/select.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { getDb, initDb } from "../utils/surreal";
import { getDb } from "../utils/surreal";
import { jsonify } from "surrealdb.js";

export async function getAllUsers() {
const db = await initDb();
if (!db) {
console.error("Database not initialized");
return;
}
try {
const users = await db.select('User');
console.log("All users:", users);
} catch (err) {
console.error("Failed to get users:", err);
}
const db = await getDb();
if (!db) {
console.error("Database not initialized");
return;
}
try {
const users = await db.select("User");
console.log("All users:", jsonify(users));
} catch (err) {
console.error("Failed to get users:", err);
} finally {
await db.close();
}
}

getAllUsers();
getAllUsers();
32 changes: 10 additions & 22 deletions ts-bun-starter/utils/surreal.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import { Surreal } from "surrealdb.js";

let db: Surreal | undefined;
export async function getDb(): Promise<Surreal | undefined> {
const db = new Surreal();

export async function initDb(): Promise<Surreal | undefined> {
if (db) return db;
const surreal = new Surreal();
try {
await surreal.connect("http://127.0.0.1:8000/rpc");
await surreal.use({ namespace: "test", database: "test" });
// db = surreal;
return db;
} catch (err) {
console.error("Failed to connect to SurrealDB:", err);
throw err;
}
}

export async function closeDb(): Promise<void> {
if (!db) return;
try {
await db.connect("http://127.0.0.1:8000/rpc");
await db.use({ namespace: "test", database: "test" });
return db;
} catch (err) {
console.error("Failed to connect to SurrealDB:", err);
await db.close();
db = undefined;
throw err;
}
}

export function getDb(): Surreal | undefined {
return db;
}

0 comments on commit 22be273

Please sign in to comment.