Testing Angular $q Promises

This is a quick reference to testing Angular’s $q promises.

Values of promise.$$state.status
Value State
0 Outstanding
1 Resolved
2 Rejected

Using Jupyter for Exploring a Heroku Database

If you want to establish a connection to a Heroku database and import data into Jupyter for some analysis, here’s some boilerplate to help:

# Python 3
from sqlalchemy import create_engine
import pandas as pd
import subprocess

DATABASE_URL = subprocess.check_output("heroku config:get DATABASE_URL --app <your heroku app name here>", shell=True).decode('utf-8')
engine = create_engine(DATABASE_URL)

result = pd.read_sql_query(
    '''
SELECT * FROM my_table;
    ''',
    con=engine)

Now you have result, which is a Pandas DataFrame. Happy data exploration!

Note: It probably goes without saying this is a bad idea to do on a production system. You should use a read-only replica for running queries in production, or use a dump of the database loaded onto another server.

Draft.js Data Model Demo

Facebook released a very interesting React-based rich text editor called Draft.js. The advantage of something like is that you have much more control over the data model for structured documents.

The documentation is a little sparse on examples of what the underlying data model looks like, so I build this quick demo that renders the data model in JSON.

See it live here.

Getting Jetbrains DataGrip to work with Heroku Postgres

Heroku Postgres forces you to use SSL, but the connection isn’t signed with a well-known CA. To get this to work in DataGrip, you have to mess around with some JDBC settings. Here’s how to do it:

In the add connections dialog, enter the username, password, hostname, port, and database name. You’ll have to enter these manually, because DataGrip’s URL support only works with JDBC database URLs, not the more common Postgres URLs used by Heroku.

Next, click on the Advanced tab. Then find the ssl property and set it to true. Then find the sslfactory property and set it to org.postgresql.ssl.NonValidatingFactory.

You should be good to go!

Abstract Classes with Static Methods in ES2015

ES2105 classes have much better support for static method patterns than traditional ways of doing it in ES5.

In ES2015, there are actually two prototype chains in classes: one for instance methods and one for static methods. This means you can use super and this in a static method, and it works as expected.

This makes using an abstract class pattern much easier. Here’s an example:

'use strict';

class AbstractShape {
  static get SHAPE_NAME() {
    throw new Error(
      'must specify a SHAPE_NAME in your subclass of ' +
      'AbstractShape'
    );
  }

  static get englishName() {
    return this.SHAPE_NAME['en-US'];
  }

  static get spanishName() {
    return this.SHAPE_NAME['en-MX'];
  }
}

class Square extends AbstractShape {
  static get SHAPE_NAME() {
    return {
      'en-US': 'square',
      'es-MX': 'cuadrado'
    }
  }
}

console.log(Square.englishName); // -> 'square'
console.log(Square.spanishName); // -> 'cuadrado'
console.log(AbstractShape.englishName); // Throws an Error

A few things that are interesting with ES2015 static methods:

  • You can’t have static properties, but you can make static getters to get the same effect (with some syntactic noise).
  • In a static method, this refers to the class itself. Effectively, you can just access other static methods with this.
  • You cannot refer to the class itself in the class body. This is not legal:
static get englishName() { return AbstractShape.SHAPE_NAME['en-US']; }

I’m guessing is the reason for this is that classes are not hoisted in ES2015.

More class posts coming soon!