19-Nodejs Course 2023: Database Collections
Hasan Zohdy
Posted on October 31, 2022
If you are struggling in any step of this course, please feel free to ask me in the comments section below or join our discord channel.
Database Collections
As we mentioned earlier, the data are stored in collections which is referred as well to Tables
, now we want to know how to create or get a collection using Nodejs, it's just as easy as you may think, just call the collection
method from the database :)
Users Collection
Open src/app/users/controllers/list-users.ts
and update it to the following code
// src/app/users/controllers/list-users.ts
import database from "core/database";
export default async function usersList(request: any) {
const usersCollection = database.database.collection("users");
const usersList = await usersCollection.find({}).toArray();
return {
users: usersList,
};
}
Don't forget to async the function
What we did here we called our database
class which internally holds the mongodb database driver
, then we called collection
method and passed to it our collection name which we'll call users
.
Now we have an instance of collection, we can use it to perform operations on it, in this case we called find
method which returns a cursor, then we called toArray
method which returns an array of documents.
You should see now an empty array in the response, because we don't have any users in our database, let's add some users.
Enhancing the collection call
Calling database.database
is not a good practice, so let's make it better, open src/core/database/database.ts
and update it to the following code
// src/core/database/database.ts
import { Db } from "mongodb";
export class Database {
/**
* Database instance
*/
public database!: Db;
/**
* Set database instance
*/
public setDatabase(database: Db) {
this.database = database;
return this;
}
/**
* Get database collection
*/
public collection(collectionName: string) {
return this.database.collection(collectionName);
}
}
const database = new Database();
export default database;
Now we can use the collection
method directly from the database
instance, open src/app/users/controllers/list-users.ts
and update it to the following code
// src/app/users/controllers/list-users.ts
export default async function usersList(request: any) {
const usersCollection = database.collection("users");
const usersList = await usersCollection.find({}).toArray();
return {
users: usersList,
};
}
Create New User
Now we want to create a new user, open src/app/users/controllers/create-user.ts
and update it to the following code
// src/app/users/controllers/create-user.ts
import database from "core/database";
export default async function createUser(request: any) {
// we'll use this for now because of the multiple part, but we'll fix it later
const name = request.body.name.value;
const email = request.body.email.value;
const users = database.collection("users");
const result = await users.insertOne({
name,
email,
});
return {
user: {
id: result.insertedId,
name,
email,
},
};
}
Don't forget to async the function
Now let's open postman and create a new user by just adding the following body
{
"name": "Hasan",
"email": "hassanzohdy@gmail.com"
}
And that's it! you should see now the same email and name with the a generated id.
Conclusion
In this lesson we learned how to create a collection and how to use it to perform operations on it, we also learned how to create a new user and how to get all users.
Next Lesson
In the next lesson we'll start working with Database Models
!
🎨 Project Repository
You can find the latest updates of this project on Github
😍 Join our community
Join our community on Discord to get help and support (Node Js 2023 Channel).
🎞️ Video Course (Arabic Voice)
If you want to learn this course in video format, you can find it on Youtube, the course is in Arabic language.
💰 Bonus Content 💰
You may have a look at these articles, it will definitely boost your knowledge and productivity.
General Topics
- Event Driven Architecture: A Practical Guide in Javascript
- Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript
- After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL
Packages & Libraries
- Collections: Your ultimate Javascript Arrays Manager
- Supportive Is: an elegant utility to check types of values in JavaScript
- Localization: An agnostic i18n package to manage localization in your project
React Js Packages
Courses (Articles)
Posted on October 31, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.