Posted in Uncategorized

Setup eslint for code style enforcement

Setup Eslint in VS code

Click on the extension tab and search for ESlint. click on the application install button to install in VS Code.

Refresh VS Code for ESlint to load.

Setup Eslint in the development folder

In terminal application, go to your development folder and run the following commands.

$ npm install eslint.  // to install eslint
$ npx eslint --init  // to initialise eslint
# follow the prompts. 

Posted in Git, reference

Setup Github for team development

Only one member of the team requires to complete the following steps.

  1. Go to GitHub page in the browser and log in.
  2. Click on ‘+’ in the upper right corner next to user’s avatar icon.
  • In the dropdown list, choose a new repository.
  • Fill in the Repository name.
  • For my team, we are developing Nodejs application so I chose Node in “Add .gitignore” list. And following the prompts.
  • Once a repo is created, click on settings.
  • Click on “Collaborators” in the left menu and add your team members with their permission settings.
  • Then, click on “Branches” in the left menu to add protection to master branch. (If you cannot find it, you have yet to create master branch. So, I suggest to do git clone and git push). Type “master” in “Branch name pattern” and check the following options:
    1. Require pull request reviews before merging
    2. Require status checks to pass before merging
    3. Require branches to be up to date before merging
    4. Include Administrators
  • Clone the repo onto your computer by running git clone command.
Posted in deployment, Heroku, reference

Adding build script for Heroku

At this stage, I would assume you already have Heroku app setup. So I will drive straight into adding the build script.

In this example, I want to generate css files from scss files. So, I will be using that too.

Open your package.json file in your editor.

Under script object: add the following

"scripts": {
  "build": "sass public/assets/scss:public/assets/css",

Save the file and commit to Heroku.

Reference:

Posted in deployment, Heroku, MySQL

Deploy node.js app with sequelizejs to Heroku

This post is more focus on MySQL database with Sequelize.js ORM. Last time, I have already talked about how to deploy node.js app with MySQL database to Heroku. So, I will run through those similar steps. If you need detail instruction, please visit the previous post.

First of all, open your command line application. You will find me referring Command-line application as terminal since I feel more comfortable with this term.

In your terminal, run the following command to setup a Heroku app

# go to your development folder. Here,  I am assuming you already have # a git repo with your application code. 

$ heroku login
# follow the prompts to login to Heroku

$ heroku create <<your_app_name>>
# if success, Creating <your_app_name>.. done message will be shown

After your have created your app, in your web browser, go to your Heroku Dashboard.

In your Heroku Dashboard, add JAWSDB under resources tab

Open your model/index.js file in your code editor and change your database connection statement to the following.

// DB setup
const db = {};

let sequelize;
if (process.env.JAWSDB_URL) {
  // for Heroku
  sequelize = new Sequelize(process.env.JAWSDB_URL, {});
} else {
  const env = process.env.NODE_ENV || "development";
  const config = path.resolve(__dirname, "..", "config", "config.json")[env];
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

Normally, you can just added Heroku’s database credentials to “production” object in config.json file. But I do not like to commit such information to GitHub, Heroku or any Open Environment for security purposes. Hence, normally config.json file is in my .gitignore file and never commit to git at all.

If you have sequelize migration files like I do, modify “production” object your config.json file with JAWSDB settings.

In your terminal,

# Go to your project folder
# run the following commands
$ npx sequelize db:migrate --env=production
$ npx sequelize-cli db:seed:all --env=production

References:

  1. https://sequelize.readthedocs.io/en/1.7.0/articles/heroku/#running-migrations
  2. https://medium.com/@ng.eric314/node-js-setting-up-sequelized-for-herokus-jawsdb-while-using-environmental-variables-3f4a0535c0fa
Posted in deployment, Heroku, MySQL

Deploying code with MySQL database to Heroku

If you are reading this, we’ll assume that you have created a node.js application with MySQL database and commit your code to GitHub but not to Heroku. So, let’s start from creating an app for your application in Heroku.

1. Create a named application in Heroku (commandLine)
  • Go to your command line application in your system. For Mac/Linux, it would be terminal application and in windows, command prompt.
  • In the terminal, run the following command. Remember to replace <<app_name>> with your desired application name.
$ heroku create <<app_name>>
  • When you run the above command, you should get response whether the app is created or have some error like app_name exists and the like. To be safe side, you can also run the following command to check. If app is created, you should see the Heroku in the list
$ git remote -v
2. create database on server
  • In your web browser, go to Heroku site and log in with your credentials.
  • In your Heroku account, find the app_name in application list and click on it.
  • Under resources > Add-ons, search for “JawsDB MySQL”. Press Enter and follow the prompts.
  • Click on the “JawsDB MySQL” to get database connection information.
3. Create and setup datababse on server
  • Open your MySQL workbench or other MySQL application which allows the connection and database manipulation
  • In MySQL workbench, add new connection by inserting the JawsDB connection information.
  • Open the newly created connection.
  • Insert your sql statements to create tables and insert information if you have any.
  • Once all the sql queries is run successfully, quit the workbench since we have successfully setup the database for our app in Heroku.
4. Modify your code to get Jawsdb connection information from the server environment variable
  • In VS code, go to the file where connection is made and add the following code.
let connectionInfo;

if(process.env.JAWSDB_URL) {
    connectionInfo = process.env.JAWSDB_URL;
} else {
    connectionInfo = {
        host:process.env.DB_HOST,
        port: process.env.DB_PORT,
        user: process.env.DB_USER,
        password:process.env.DB_PASSWORD,
        database: process.env.DB_NAME
    };
}

const connection = mysql.createConnection(connectionInfo);
  • Save the file and commit your code to GitHub.
5. Commit code to Heroku

In terminal window, push the code to Heroku server by the following command.

$ git push heroku master

Or if you are committing from a branch

$ git push heroku <<my_branch>>:master

References :

  1. Deploying code to Heroku.
  2. JawsDB MySQL
  3. Creating a named app
  4. Manually setting up the built packs
Posted in Uncategorized

Happy 2020

Happy New Year, everyone. Hope year 2020 is a great year.

Sadly, Australia has lost so many during this new year season. All my condolences to all who lost lives, loved ones, houses or livelihoods to those fires and my respects to all those heroes out there fighting those fires and protecting the communities.

Posted in Git, reference

Add node modules to .gitignore

I like to setup .gitignore file at the very start (when I set up my repos and first clone it). So, most of the time, I get away with git caching the file problem. But most of my friends had that problem and another friend of ours shared this post by Imcneel. I would like to make a note here so that I can comeback to it later if I need it. Who knows right?

NOTE: if you are using VS Code editor, your VS Code may prompt that you have too many action in your repository. Mine does but it can be because I am using VS Code extensions for the git.

If not, you can follow Imcneel‘s instruction as following:

The following steps are command line commands so you will need your command prompt, terminal or similar command line application. Here are the steps.

1. Navigate to git repository folder.

2. Create a .gitnore file if not exists by the following command:

$ touch .gitignore

3. Add node_modules in .gitignore by opening and adding “node_modules” in the .gitignore file. Or you can run the following command.

$ echo node_modules >> .gitignore

4. remove cached node_modules from the repo by running this command:

$ git rm -r --cached node_modules

5. run git add and git commit commands for the git to make notes of the node_modules folder removal.

$ git add .   // This will add both .gitignore file and node_module removal (and any other change you made to the repo). 
$ git commit -m "Remove node_modules folder from repo"

Reference:

  1. Imcneel’s remove node modules

Posted in Coding, reference

Generate random number within a range

This topic is not hard and there are a lot answers already out there.

The logic is not hard but I kept forgetting and messing up the formula. Thus, I am posting this post so that I can make quick reference to the formula instead of skimming all the information all over again. I put my reference sites at the end of this page so please feel free to visit the original sites for detail explanations.

Random number within range (upper boundary exclusive)

Math.floor(Math.random() * (max - min)) + min

e.g.

Math.floor(Math.random() * 11) // for random integer from 0 to 10

Random number within range (both boundary inclusive)

Math.floor(Math.random() * (max - min + 1)) + min;

References:

Posted in Uncategorized

Branching a Git

Branching a git is needed to collaborate with others using Git system. Here, I will talk about how to branch off a git repository.

1. Clone the project repository onto your computer if you haven’t done so.

2. Go to project repository folder in your command line terminal.

3. Run the following command to create and checkout a new branch. Remember to replace [branch_name] suitable to your task such as “create-html-skeleton” or “add-create-new-function”

$ git checkout -b [brance_name]

4. Verify if branching works by:

$ git branch

You should see at least 2 branches: master and your new branch.

5. You can do the git add and git commit commands as usual.

6. The only difference will be git push. In git push command, instead of origin master, use origin your branch name like the following command

$ git push origin my_branch

7. Once you are done with the command line, go to your GitHub account in your browser.

8. In the repository page, you should be able to see “Compare & pull request”. Click on it and follow the prompts to create a pull request to your team.

Posted in Git

Removing .DS_Store files from Git

I am now using Git on MacBook. Whenever I make changes to my Git folder, MacBook will add .DS_Store. Normally, it is fine since it is the way it was configured. BUT, it is super annoying sometimes to remove accidentally committed .DS_Store files from Git. So, as usual, I did a google search on this issue.

I found the solution on Stackoverflow. I found I can actually set up gitignore setting for .DS_Store files. Thank you, Benzado and Chris Redford, for the solution. Here are the steps.

  1. Go to your command line terminal.
  2. Go to your git folder.
  3. Type the following to remove .DS_Store files
$ find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

$ echo .DS_Store >> .gitignore

$ git add .gitignore

After that, commit your git as usual

$ git commit -m "remove .DS_Store files from git"

$ git push

References