TIL: Back to Learning Mongo
I bought this Udemy course on mongodb nearly a year ago. Based on my completion history, evidently I got about a 3rd of the way through it before quitting and moving on to something else. Considering how much the course focuses on nodejs, I’d assume I probably went and started working on courses that covered node and javascript in general (even knowing what I do now, there’s still some advanced topics being covered).
As mentioned the other day, my coding ADD is in full effect, so I decided to bring this course back from the dead and see what else I could learn from it now that I feel much more comfortable working with node.
To my surprise, a lot of the early content in this course is great, new info for me. Despite the fact that I’m a tester, I’ve never actually tested any of my node apps that I’ve written (like with a test suite), it’s always just been manually playing around with stuff.
So today I got through quite a bit of coverage, which I’m going to barf back up down below from my notes:
Promises
- Used to help define asynchronous functionality
- You can create a new Promise by using the following syntax:
|
|
- Note in the above that a Promise has two different inputs, resolve and reject.
- When your Promise has completed running whatever it needs to do, and the call was successful, you’d call the
resolve()
method, passing in any variables you need to return. - If the promise has failed, such as erroring out, you can call the
reject()
method to return back the failure. - To handle the actual function in the code, you use the dot notation to chain on to the initial function call:
|
|
- Most libraries already have promises written, we’ll be written the consumers to handle them (at least in this course).
Basic MongoDB Concepts
- Mongo allows for multiple databases within a single mongo instance
- Each database is made up of n number of collections
- Collections represent a specific kind of data (you wouldn’t typically mix with another collection)
- We’re using Mongoose for Node in this course
- We’re using Mocha for testing against the Node app
User Models
- We create user models (using Mongoose in this case) to define how collections will be structured
- This is where we’re setting what the schema will be for that collection when accessing data within the collection.
- Going to be using
mongoose.Schema
to define the model. - Schema is created like follows:
|
|
- Note how the properties of the schema is assigned to a Javascript type of
String
. - In order to actually create the model (and in turn the collection), the following is required:
|
|
- If the defined collection does not exist in the connected database, it will create it on runtime.
User
above can now be referred to as theUser Class
.User
does NOT represent a single user, it represents the ENTIRE SET of data
- Best practice is to only export the Class name when creating models in projects (making usable in the rest of the application)
Inserting Records
- Start by creating a new instance of your model, complete with the parameters for that model:
|
|
- Once the instance has been created, you can use the
.save()
method to save it to mongo. - Save method will return a promise, which can then be used to move on to assertions in Mocha.
- After the new instance is created, Mongoose attaches an
isNew
property to the instance, defaulting to true - Once this has been saved to the database, this is flipped to false
- After the new instance is created, Mongoose attaches an
Mocha Tests
- Using mocha for testing automatically gives you access to
describe
function (test case) andit
method (test step). - Have to make assertions to do the evaluation
assert
package must be imported, not immediately accessible
- Execute tests by running
mocha <test_folder_name>
. - As an alternative, you can set up an npm start rule to kick it off (same as above).
test_helper
test_helper
file is created to handle things we want to be done related to the test- In this example we’re using it to establish connection over to mongo
- Inside of
test_helper
you can use abeforeEach()
function to define what needs to be done before each part of the test (such as wiping the db) - Need to use the
done
callback to pause tests to wait for long running tasks to finishdone
is provided by MochabeforeEach
andit
accept the done callback
- Use the
before
function to have the tests wait until the connection has been completed before starting
💚