From 5ab3cbaa54642ada07085d1e0aa2e984dec1849a Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 29 May 2025 06:17:12 +0200 Subject: Allow unfree in nix flake, add mongodb-compass to devShell closure, basic mongo attempt, add test register route --- src/db/db.js | 16 ++++++++++++++++ src/db/index.js | 2 ++ src/db/schemas/index.js | 0 src/db/schemas/userSchema.js | 14 ++++++++++++++ 4 files changed, 32 insertions(+) create mode 100644 src/db/db.js create mode 100644 src/db/schemas/index.js create mode 100644 src/db/schemas/userSchema.js (limited to 'src/db') diff --git a/src/db/db.js b/src/db/db.js new file mode 100644 index 0000000..bf7ccfa --- /dev/null +++ b/src/db/db.js @@ -0,0 +1,16 @@ +import { connect } from "mongoose"; + +export async function initDb() { + const connStr = "mongodb://root:Foxy1987@localhost/myproject"; + try { + const res = await connect(connStr); + if (res.connection.readyState === 1) { + console.log("[MONGODB] Connected successfully!"); + } else { + console.error("[MONGODB] Failed to connect to ", connStr); + } + } catch (e) { + console.error("[MONGODB] Error connecting to database!"); + throw e; + } +} diff --git a/src/db/index.js b/src/db/index.js index e69de29..f55b773 100644 --- a/src/db/index.js +++ b/src/db/index.js @@ -0,0 +1,2 @@ +export * from "./db.js"; +export * from "./schemas/index.js"; diff --git a/src/db/schemas/index.js b/src/db/schemas/index.js new file mode 100644 index 0000000..e69de29 diff --git a/src/db/schemas/userSchema.js b/src/db/schemas/userSchema.js new file mode 100644 index 0000000..eaddb08 --- /dev/null +++ b/src/db/schemas/userSchema.js @@ -0,0 +1,14 @@ +import { model, Schema } from "mongoose"; + +export const userSchema = new Schema({ + username: { + type: String, + required: true, + unique: true, + trim: true, + }, +}); + +export const User = model("user", userSchema); + +console.log("[MONGODB] User schema initialized successfully!"); -- cgit 1.5.1