Slides of my presentation about “Modular Development in Node.js”

Yesterday I had a talk about Modular Development in Node.js. Following topics were covered during my talk:

  1. Common problems which can be fixed by applying Modular Development
  2. Some of most important things that Modular Development offers
  3. NPM & Git
  4. Readme Driven Development & L.I.F.T

Herewith the slides.

Slides of my talk about MongoDB Database Replication

Last Tuesday I had a presentation at Sydney’s MongoDB User Group about replication of MongoDB databases. I started from very basic concepts and continued with more intermediate and advanced topics about replication. Following topics covered:

  1. Running Replica Set in Test environment
  2. Running Replica Set in Production environment
  3. Configuring Replica Set
  4. How election works
  5. How members sync data
  6. Arbiter
  7. Good to knows about different aspects of Replica Set
  8. Durability and Replica Set
  9. Rollbacks
  10. Common Replica Set design mistakes
  11. Introduction to Good Replica Set Design Patterns

Unfortunately this talk is not recorded but slides give you insight about each topic.

Auto Delete MongoDB Documents using Expiry Date

If you’re user of Redis, you probably know about setting expiry date for the data you store in Redis. This feature of Redis allows you to store information without being worried about implementation of a logic to remove them after some time. A good example of why we need to auto delete some data from data source is Reset Password functionality of a service. In this case you set some expiry date for the token in Redis and if user doesn’t use it during expected period of time, data will be removed from Redis automatically. Luckily MongoDB out of the box supports same feature.

Introduction to TTL Indexes in MongoDB
There is a special type of index in MongoDB called Time To Live or TTL indexes. This type of index can be used to define some expiry date for documents. You need to create this index for a field which contains a date object or an array of date objects. Let’s say you have a collection in your college database called ‘passwordRecoveryTokens’ which contains tokens that is part of your password recovery process. Schema of a document in this collection is as:

_id: ObjectId,
token: String,
accountNumber: String,
expires: Date

To create a TTL index for `expires` field simply run following command:

db.sampleCollection.createIndex({expires:1}, {expireAfterSeconds: 5 * 60})

ATTENTION: createIndex helper function is added to version 3.0 of MongoDB. For older version of MongoDB you can use ensureIndex. As you can see, creating a TTL index is exactly same as normal indexes, only different is about passing a number (expireAfterSeconds) as second parameter to ensureIndex function. MongoDB will do heavy lifting for you.
In background, MongoDB runs a thread which removes documents from different collections based on TTL indexes. MongoDB runs this job every 60 seconds so you shouldn’t rely on deletion on exact date and time because MongoDB eventually deletes expired documents which varies based on current workload of your instance.

It worth to consider following limitations before using TTL indexes in your project:

  1. You can’t use TTL indexes for a capped collection.
  2. You can’t create a TTL index for a field which is already indexed.
  3. By creating TTL index on background, you allow MongoDB to start deleting old documents meanwhile creating index in the same time.
  4. MongoDB doesn’t start deleting old documents meanwhile a foreground TTL index creation job is running
  5. If you’re using TTL indexes against an array field, MongoDB will use earlier date that it founds in the array to check if it’s time to delete document or not.

Creating TTL indexes using Mongoose
If you’re using MongoDB alongside with Node.js and Mongoose, good news is that Mongoose supports creation of TTL indexes. Simply add ‘expires’ to schema of the field you want:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var schema = new Schema({
token: string,
accountNumber: string,
expiresAt: {
expires: 5 * 60,
type: string

Mongoose supports `ms` formats as well so values such as `1d` or `2h` are valid values for expires property.

Naming CSS Classes Using Prefixes (Namespaces)

Component-based approach, this is what we can see in source code of different CSS frameworks: Bootstrap, Foundation, Ionic, just to name a few. Component-based is a great approach to develop a CSS framework.  In this approach, as developer we concentrate on the component (Button, Table, Form, Navigation) itself to name our classes and at the end  our web pages are mix of different components.

Problem with using industry-standard CSS classes
Bootstrap has great impact in front-end development. As front-end developer we all used it in our projects and we followed lots of patterns that is used in developement of Bootstrap for our own CSS frameworks. One of these many patterns is short and convenient names for the classes: E.g. “btn”, “btn-default”. If you’re developing some CSS code and you can guarantee that your code will be used in an isolated environment and not in conjunction with  something such as Bootstrap go and use same classes but don’t forget! As soon as you start bringing something such as Bootstrap you bring lots of bugs because of conflicts between what you defined as “Button” and what Bootstrap defines as “Button” and result is something strange.

Solution for this problem and many similar problems is really easy. Just use a short prefix for your classes. For example, instead of using “btn” use something such as “sl-btn” which “sl-” is your prefix and in this way you can easily bring any kind of framework to your project and you don’t need to be worried about these sort of conflicts.

It’s not just about CSS, JavaScript is source of this issue as well. Let me describe this issue with an example. Couple of days ago I was developing a Sitecore Package. This package was loading some JS code and a DIV tag which had couple of data attributes such as data-tags, data-user-id, data-session. My package was working properly without “data-tags” and as soon as I added “data-tags” it stopped working! Reason was very simple, Sitecore had a JavaScript framework which was monitoring all elements with data-tags attribute and doing some actions based on the availability of this attribute which was reason of error in my code. I simply added a namespace to all my data attributes and escaped this conflict.

Don’t forget to think about your code in conjunction with others and in this way you’ll handle lots of potential headaches!

Slides of my presentation about CoffeeScript

Couple of weeks ago I had a presentation in Sydney’s NodeNinjas group about CoffeeScript for Node.js developers. During that presentation I tried to describe why CoffeeScript is good enough to give a try to it and how a Node.js programmer can benefit from it. You can take a look at the presentation here. I’m planning to publish a series of posts about using CoffeeScript with a focus in Node.js development. Stay tuned …