The gist in the accepted answer has a bug in that it doesn't include milliseconds in the ULID. I've created a fork with the correction, and a few other changes: https://gist.github.com/mark2016/64b29b6b42032750a21956a0da1956aa