Introduction

When dealing with hierarchical data, we usually use recursion for constructing the tree structure. It would be a performance problem if you query the tree node data from the database every time the recursion happens. Also, if you work with languages which are heavily based on callback like Javascript, you will find it is very difficult to deal with the recursion because you have to determine when the callback has finished execution. Luckily, PostgreSQL supports recursive query using the WITH RECURSIVE keyword. All the data structure can be returns within a single query.

WITH queries in PostgreSQL

According to PostgreSQL document, WITH queries provide a method for dealing with large queries by creating temporary tables that exist only for that query. This is a very simple example from Postgres document to illustrate the usage of WITH queries.

Read more

Installation & Configuration

There are several ways to install PostgreSQL on Mac OS. Take a look at this page Mac OS X packages choose one method that best suits for you. In this post I will use the Graphic installer as the demo but you can also use other way, there will be not many differences.

After you have downloaded and installed PostgreSQL using the graphic installer, there will be a new account named postgres and the server will automatically start. Now you need to add the postgres executable files into your shell’s PATH. Luckily, the graphic installer has generated the configuration for you already. Just add this line to the end of your shell rc file (.bashrc, .zshrc)

source /Library/PostgreSQL/9.3/pg_env.sh

Restart your shell and now those postgres binary files (postgres, pg_ctl, psql,… ) is ready for use. But there is still one problem, it is the postgres user in your system. You need to update its password so later you can log into that account. Open System Preferences > Users & Groups and then choose Reset Password of the postgres account.

To start the server, use this command and then input the password of the postgres account.

$ su postgres -c "pg_ctl start"

Password:
server starting
2014-02-26 10:09:20 ICT LOG:  redirecting log output to logging collector process
2014-02-26 10:09:20 ICT HINT:  Future log output will appear in directory "pg_log".
Read more

Sequelize is an Object-Relational-Mapper, which is written entirely in Javascript and can be used in the Node.JS environment. However, setting it up might seem a little messy since the document does not cover everything that it can do. In this post, I only cover some basic information as well as some tips that you cannot find in the Sequelize Docs.

1. Setting up Sequelize

1.1 Installing Sequelize

In order to use Sequelize, you need to install it along with the database engine of your choice. For example

$ npm install --save sequelize
$ npm install --save pg       # for postgres
$ npm install --save mysql    # for mysql
$ npm install --save sqlite3  # for sqlite
$ npm install --save mariasql # for mariasql
Read more

Installing python and all its stuff maybe a bit confusing if you’re installing it using Macports (MacPorts - The MacOS package manager). By default, Macports comes with many python ports, e.g., python24, python27, python34. Each of them is a separate version of python (2.4, 2.7, 3.4,…). It provides the users the ability to install and maintain multiple version of python at the same time.

To browse all versions of python provided by Macports, use this command

$ port search python
python26 @2.6.9 (lang)
    An interpreted, object-oriented programming language
python27 @2.7.6 (lang)
    An interpreted, object-oriented programming language

To install a specific version of python

$ port install python24 python27

When you install python through Macports, it will auto install the python_select port. This is a tool for switching among python versions. To view all the the installed python versions, execute this one

Read more

The built-in js-mode in Emacs does not provide many features for working with js framework beside js editing and syntax highlighting. The tips in this post will help you transform your Emacs into a powerful Javascript IDE.

Associate files with js mode

By default, open a file with .js extension will automatically activate js-mode. For some other file types, if you want to link them to js-mode, add this to your .emacs

(add-to-list 'auto-mode-alist '("\\.json$" . js-mode))

js2-mode - A better js mode for Emacs

js2-mode by SteveYegge is arguably the best JavaScript mode available for emacs. It has very accurate syntax highlighting, supports newer JavaScript extensions implemented in SpiderMonkey, and highlights syntax errors as you type.

The easiest way to install js2-mode is via package.el (Emacs Package Manager). Also, you need to install ac-js2 for using js2-mode with auto-complete suggestion. Once installed, add this to your .emacs to activate js2 and its auto-complete.

(add-hook 'js-mode-hook 'js2-minor-mode)
(add-hook 'js2-mode-hook 'ac-js2-mode)
Read more

Paredit is a minor mode for keeping parenthese balanced. It is extremely useful for working with lisp-based programming languages. Also, it is activated automatically when you open any lisp-based languages file. Usually in other languages, we have to work with parentheses, too. For example the { and } for code block, [ and ] for array… To use paredit with non-lisp mode, add the following function into your .emacs file

(defun my-paredit-nonlisp ()
  "Turn on paredit mode for non-lisps."
  (interactive)
  (set (make-local-variable 'paredit-space-for-delimiter-predicates)
       '((lambda (endp delimiter) nil)))
  (paredit-mode 1))

Actually, this is taken from Emacs Starter Kit so if you are using Emacs Starter Kit, the above function is added already for you as esk-paredit-nonlisp. Emacs Starter Kit also activates it by default for you in some modes (javascript for example). Now, if you want to activate paredit for a certain mode, add this to your .emacs

Read more

JSHint command line tool

JSHint is a tool that helps to detect errors and potential problems in your JavaScript code. You can try the online demo version at JSHint home page at http://www.jshint.com/. This post demonstrates how to setup jshint and integrate it into Emacs for javascript development on Nodejs.

Firstly, make sure you have installed nodejs and its package manager (npm). Next, install jshint globally using npm with this command

$ npm install -g jshint

You can try linting your js file with jshint

$ jshint myfile.js

JSHint with Flycheck

Before that, I used jshint with flymake. However, now I found that flycheck (an improvement of flymake) has built-in support for jshint so you don’t need to install jshint-mode anymore. Just install flycheck using package.el (Emacs Packages Manager). Add this to your .emacs to activate flycheck when you visit any .js file

Read more

The problem

Usually, in Conkeror, when you want to bind a keystroke to a command or function in a key map, you would use the define_key function (see more at Conkeror Key Bindings). However, when you have too many key bindings, it will lead to the problem of repetition in your config code. For example

define_key(default_global_keymap, "A-z", previous_buffer);
define_key(default_global_keymap, "C-j", previous_buffer);
define_key(default_global_keymap, "A-x", next_buffer);
define_key(default_global_keymap, "C-l", next_buffer);

This is extremely annoying since I have to rebind too many keys in Conkeror (the Emacs style keys is inefficient). Luckily, the flexibility of Javascript allows us dynamically define the number of arguments that passed to the function. By using that, you can define your own function for binding the keys to avoid repetition in your init code.

Read more

On OSX, when I use wget for downloading file from https source, I got the Certificate issue error.

Resolving fbcdn-profile-a.akamaihd.net (fbcdn-profile-a.akamaihd.net)... 23.2.16.8, 23.2.16.41, 23.2.16.32, ...
Connecting to fbcdn-profile-a.akamaihd.net (fbcdn-profile-a.akamaihd.net)|23.2.16.8|:443... connected.
ERROR: The certificate of ‘fbcdn-profile-a.akamaihd.net’ is not trusted.
ERROR: The certificate of ‘fbcdn-profile-a.akamaihd.net’ hasn't got a known issuer.

The reason for this problem is that the default certificate directory is hard-coded in wget as /etc/ssl/certs, which corresponds to the Linux directory layout and doesn’t exist on Mac OS.

To fix this, you can either tell wget to skip certificate check or fix the certificate error in Mac OS. For the first solution, just add --no-check-certificate to the wget command when you start it. For example

$ wget --no-check-certificate http://example.com

If you want to fix the problem, first, install curl-ca-bundle from Macports

$ sudo port install curl-ca-bundle

Next, edit your ~/.wgetrc file (create a new one if it’s not exist yet) and add this line to the end of the file

CA_CERTIFICATE=/opt/local/share/curl/curl-ca-bundle.crt

If you have followed my previous post on How to install and run Macports form home directory, then the link to curl-ca-bundle should be

CA_CERTIFICATE=~/macports/share/curl/curl-ca-bundle.crt
Read more