Daily coding lessons

I’ve always wanted to write more about coding. The problem with programming is there is so much builds on top of prior knowledge and whenever I begin writing an article I start teaching the prior knowledge required. Then I teach the prior knowledge required for that knowledge and so on until I get bored of writing and never hit publish (seriously I have about 20 articles like this in my drafts right now).

So I’m going to do an experiment: Raw daily coding lessons. Each day after I’ve worked for a few hours I’m going to write a post about what I learnt that day to hopefully help others who get stuck in similar situations. It may lack context and prior information. It may be raw and somewhat useless for most people. But i’m hoping over time I can get better at explaining things and teaching so my mistakes can help others, not just myself.

If you want more information on anything I write about just let me know and I’ll try and help you out 🙂

This experiment begins today, wish me luck.

 

tim

 
  • Sékine Coulibaly

    Hi Tim, nice writeup ! I’m in the process of write a model for Cassandra. I’m somewhat new to OO Javascript, and it’s still unclear to me why User.prototype.changeName() is not defined within the User constructor ? Besides, what is the purpose of User.prototype.data = {} since the constructor gets a ‘data’ input argument ?

    • Hi Sékine,

      You can define both changeName and data in the constructor and it will work in exactly the same way. I just prefer doing it this way as I find it neater as when I have a huge model in the future with many methods and variables it’s easier for someone new to come along and see them all defined in the one place rather than inside the constructor / functions etc. It’s also the way I’ve seen most other Javascript coders define their classes and I like to keep things as ‘standard’ as possible.

      You also get slightly more performance when defining what variables / functions an object will have ahead of time. I don’t think there’s any speed difference between defining them as prototype. or in the constructor but try not to add any new member variables inside any other functions. You can learn more about this here: https://www.youtube.com/watch?v=UJPdhx5zTaw

      Cheers, Tim

  • 2Buku4U

    How would you unit test code that uses this model without hitting the database? I am not a big fan of putting data access code inside the model. With the data access code encapsulated in it’s own layer it can be mocked so that unit tests do not actually hit the db. Having models that are just data containers can be hydrated with data from the test framework and used within the unit test without the need of a db. A data access layer can be mocked as well so that it returns know data sets as models so that unit tests are not pulling from the actual db.

    • Hi,

      You can simply replace the save function with a sinon stub to use the model without hitting the database. Eg:

      var sinon = require(“sinon”);
      var User = require(“./models/user”);

      var user = new User();
      user.save = sinon.stub().callsArgWith(0, null, user);

      Then do what you like with that user object and whenever save is called it will call the callback immediately with the user instead of writing out to the DB.

  • Diogo Alves

    Tim thank you for share. Could you create more one example that show models N:N, N:1 and 1:1? Eg. how can i update this code to use 1 user have a lot of posts, just add this attribute in schemas.js? Tnks.

  • This is just what I was looking for, thanks for the informative article

  • Dung Phan

    It usefull,
    So i want to user collect data from rest api. so cant i do it. thank

  • Dung Phan

    So how can i struct model lile:
    model User {

    function getAll(){
    }
    function aaa(){
    }
    ..}

    How can i do i with node js! thank.

  • Chaim Eliyah

    –first question already answered above

    • Chaim Eliyah

      Oh crud. I think you already answered that. var self = this.

    • Chaim Eliyah

      another question, though — in schemas.js, would the practice be to have schemas.js match the database schemas, and then have the tables be elements within each schema?
      e.g.,
      schemas = {
      user : [ internalUser: { key: value }, externalUser: { key: value } ]
      }

      etc.?

  • Dave Morris

    Thanks Tim, really useful!

  • Emanuell Paredes

    which change are necesary for implement this practice in javascript 6.
    Thank

  • You realize you essentially just recreated the models from Backbone, right? Except you don’t have events and you use a back end DB instead of making an AJAX call to a REST service. Also you moved the “findbyid” function into the single User object rather than using a collection object.

    You should try Ampersand for something similar to this that works nicer with Node. It was based off Backbone.

    Oh and thanks for linking to my site for the ES6 classes tutorial. 🙂