summary refs log tree commit diff
path: root/src/db
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-05-29 22:48:16 +0200
committerRory& <root@rory.gay>2025-05-29 22:48:48 +0200
commit9d33b2fe6b3fbea60d981d9f4ed24cf82b05a7af (patch)
treeee44d3b3df88febdce52efe436f4582820fb6471 /src/db
parentGet database working (diff)
downloadnodejs-final-assignment-9d33b2fe6b3fbea60d981d9f4ed24cf82b05a7af.tar.xz
Prettier config
Diffstat (limited to 'src/db')
-rw-r--r--src/db/db.js30
-rw-r--r--src/db/index.js4
-rw-r--r--src/db/schemas/user.js32
3 files changed, 40 insertions, 26 deletions
diff --git a/src/db/db.js b/src/db/db.js

index 36f2105..9a7b50e 100644 --- a/src/db/db.js +++ b/src/db/db.js
@@ -1,19 +1,19 @@ -import { connect } from "mongoose"; -import { readSecret } from "#util/secretUtils.js"; +import { connect } from 'mongoose'; +import { readSecret } from '#util/secretUtils.js'; export async function initDb() { - const connectionString = await readSecret( - process.env["DATABASE_SECRET_PATH"], - ); - try { - const res = await connect(connectionString); - if (res.connection.readyState === 1) { - console.log("[MONGODB] Connected successfully!"); - } else { - console.error("[MONGODB] Failed to connect to ", connectionString); + const connectionString = await readSecret( + process.env['DATABASE_SECRET_PATH'] + ); + try { + const res = await connect(connectionString); + if (res.connection.readyState === 1) { + console.log('[MONGODB] Connected successfully!'); + } else { + console.error('[MONGODB] Failed to connect to ', connectionString); + } + } catch (e) { + console.error('[MONGODB] Error connecting to database!'); + throw e; } - } catch (e) { - console.error("[MONGODB] Error connecting to database!"); - throw e; - } } diff --git a/src/db/index.js b/src/db/index.js
index f55b773..cd306b7 100644 --- a/src/db/index.js +++ b/src/db/index.js
@@ -1,2 +1,2 @@ -export * from "./db.js"; -export * from "./schemas/index.js"; +export * from './db.js'; +export * from './schemas/index.js'; diff --git a/src/db/schemas/user.js b/src/db/schemas/user.js
index eaddb08..da2516c 100644 --- a/src/db/schemas/user.js +++ b/src/db/schemas/user.js
@@ -1,14 +1,28 @@ -import { model, Schema } from "mongoose"; +import { model, Schema } from 'mongoose'; export const userSchema = new Schema({ - username: { - type: String, - required: true, - unique: true, - trim: true, - }, + username: { + type: String, + required: true, + unique: true, + trim: true + }, + passwordHash: { + type: String, + required: true + }, + email: { + type: String, + required: true, + unique: true, + trim: true + }, + createdAt: { + type: Date, + default: Date.now + } }); -export const User = model("user", userSchema); +export const User = model('user', userSchema); -console.log("[MONGODB] User schema initialized successfully!"); +console.log('[MONGODB] User schema initialized successfully!');