-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
6fdf578
commit 5d6f36b
Showing
9 changed files
with
578 additions
and
2 deletions.
There are no files selected for viewing
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 +1,2 @@ | ||
node_modules | ||
node_modules | ||
.env |
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
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import UserAccessor from "../db_accessor/user.accessor.js"; | ||
|
||
export default class UserController { | ||
static async getAllUsers(req, res) { | ||
const users = await UserAccessor.getAllUsers(); | ||
console.log(users); | ||
res.json({ users }); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { config as dotenvConfig } from 'dotenv'; | ||
import mongoose from 'mongoose'; | ||
|
||
export default class Connection { | ||
static async open(db) { | ||
dotenvConfig(); | ||
|
||
const { MONGO_USERNAME, MONGO_PASSWORD, MONGO_CLUSTER } = process.env; | ||
const DATABASE_URL = `mongodb+srv://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_CLUSTER}.xtdufxk.mongodb.net/?retryWrites=true&w=majority`; | ||
|
||
mongoose.connect( | ||
DATABASE_URL, | ||
{ | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
maxPoolSize: 50, | ||
socketTimeoutMS: 2500, | ||
dbName: db | ||
} | ||
); | ||
|
||
return mongoose.connection; | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Connection from "../db/connection.js"; | ||
import User from "../models/user.js"; | ||
|
||
export default class UserAccessor { | ||
static async getAllUsers() { | ||
try { | ||
await Connection.open("users"); | ||
const users = []; | ||
for await (const doc of User.find()) { | ||
users.push(doc); | ||
} | ||
return users; | ||
} catch (e) { | ||
throw e; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import mongoose from 'mongoose'; | ||
|
||
const Schema = mongoose.Schema; | ||
|
||
const UserSchema = new Schema({ | ||
username: {type: String, unique: true, required: true}, | ||
password: {type: String, required: true}, | ||
email: {type: String, required: true}, | ||
bio: {type: String, required: false} | ||
}, { | ||
collection: 'users' | ||
}); | ||
|
||
const db = mongoose.connection.useDb("users"); | ||
const User = db.model("User", UserSchema); | ||
|
||
export default User; |
Oops, something went wrong.