-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
47 additions
and
53 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |