This website uses cookies to enhance the user experience

Upgrading Sails.js Applications

Share:

Sails.js upgrading typically involves transitioning the existing application to a newer version of Sails.js. The process is often straightforward, but occasionally includes changes that could potentially break the application if not handled correctly. To avoid such issues, it's imperative to ensure all Sails.js policies, models, and controllers are properly managed during the upgrade.

This chapter takes you on a journey through the Titanic, a Sails.js application, while introducing you to the various stages involved in upgrading Sails.js applications using the best practices.

Firstly, it's crucial to back up your code. This measure simultaneously serves to keep your original code intact and prevent accidental deletion of any files. After this crucial preliminary step, move on to the second stage which entails running application tests. You must ensure that all your application tests are passing. Assuming you've created an application based on the famous Titanic movie, your test could look like this:

describe('Passenger', function() {
  describe('#find()', function() {
    it('should check find function', function(done) {
      Passenger.find()
        .then(function(results) {
          // some assertions
          done();
        })
        .catch(done);
    });
  });
});

Note that using done and catch(done) ensures your test will fail if there’s an error. If any tests do not pass, deal with the issues before you continue with the upgrade.

The third stage involves updating the package.json and .sailsrc files. The package.json file stores the list and versions of the npm packages your project depends on. You can find the desired update version for Sails.js on its npm page. In the package.json file, locate "sails": "^0.12.3", and replace the old version 0.12.3 with the new one.

In the Titanic application, for example, you might have:

"dependencies": {
  "async": "2.0.1",
  "sails": "^1.4.1", //updated version
}

While the .sailsrc file is used to regulate the default behavior of Sails.js commands, it may also need to be adjusted during the upgrade process. The upgrade strategies in the .sailsrc file provide defining new defaults that override your environment-specific settings:

{
  "generators": {
    "modules": {}
  }
}

The fourth stage involves updating the dependencies. This could be done using the npm command:

npm install --save sails@1.4.1

Replace the 1.4.1 with the appropriate version you are upgrading to. The placeholder indicates the exact version of Sails the application depends on.

Next, begin the upgrading process by reading the changelog for potential changes that might affect your application. A changelog is documentation that includes a chronologically ordered list of changes for each version of a project. This way, you can anticipate possible hiccups with the upgrade.

The sixth stage involves gradually adapting your code to comply with the updated Sails.js version. This could mean updating the model connection settings, integrating a more advanced query engine, or enhancing customer controllers to ensure a smooth transition into the new Sails.js universe.

For instance, in Sails.js version 1.0, the Waterline ORM for managing data was given a new API syntax. Thus, in the Titanic Application, you must change default model settings as such:

module.exports.models = {
  connection: 'default',
  migrate: 'alter'
};

Additionally, queries must be upgraded to work with the new syntax:

//Lets get list of Jack's friends
Friend.find({ name: 'Jack' }).exec(function(err, jacksFriends) { 
    if(err) 
        console.log(err); 
    else 
        console.log(jacksFriends);
});

Finally, it's important to test after the modifications are made. The test ensures that your application remains functional after the upgrade. Execute your tests repeatedly to confirm that your application has been successfully upgraded.

Nonetheless, there are times when one's application may fail after an upgrade. Missing API methods, changed functions, broken middleware, and failed connections are likely problems. However, the nature of these possible mistakes makes debugging quite straightforward.

Upgrading Sails.js is a fairly routine task. Remember to read the upgrade guide and changelog relevant to your version, test your changes at each stage, and take necessary precautions to secure your code. Upgrading Sails.js is just like releasing a movie sequel, ensure it’s better than the previous release.

In conclusion, mastering the art of Sails.js upgrading marks a milestone in the journey to becoming a Sails.js proficient. Not only does it provide more advanced features and improved performances, but it also prepares you for future upgrades.

0 Comment


Sign up or Log in to leave a comment


Recent job openings