Skip to content

Instantly share code, notes, and snippets.

// api.js
export const getTasks() {
return get('/api/tasks')
.then(resp => new Promise(resolve => {
const { headers } = resp;
return resp.json()
.then(body => resolve({ body, headers }));
}));
};
@samokoder
samokoder / active_record_extension.rb
Created February 15, 2019 11:55 — forked from ozydingo/active_record_extension.rb
ActiveRecord left join
module ActiveRecordExtension
extend ActiveSupport::Concern
module ClassMethods
# Simple left join taking advantage of existing Rails & Arel code
def left_joins(*args)
inner_joins = self.joins(*args).arel.join_sources
left_joins = inner_joins.map do |join|
Arel::Nodes::OuterJoin.new(join.left, join.right)
end
@samokoder
samokoder / left_join_arel_example.rb
Created February 15, 2019 11:15 — forked from mildmojo/left_join_arel_example.rb
LEFT JOIN in ARel for ActiveRecord in Ruby on Rails
# Here's a contrived example of a LEFT JOIN using ARel. This is an example of
# the mechanics, not a real-world use case.
# NOTE: In the gist comments, @ozydingo linked their general-purpose ActiveRecord
# extension that works for any named association. That's what I really wanted!
# Go use that! Go: https://gist.github.com/ozydingo/70de96ad57ab69003446
# == DEFINITIONS
# - A Taxi is a car for hire. A taxi has_many :passengers.
# - A Passenger records one person riding in one taxi one time. It belongs_to :taxi.
@samokoder
samokoder / mysql-docker.sh
Created February 10, 2019 17:55 — forked from spalladino/mysql-docker.sh
Backup and restore a mysql database from a running Docker mysql container
# Backup
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
# Restore
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE
@samokoder
samokoder / serial-async.js
Created February 10, 2019 13:43
Execute multiple promises one after another (not in parallel)
// see https://stackoverflow.com/a/24985483
const fetchList = list => {
return list.reduce((promise, item) => {
return promise.then(() => {
// or any other promise
return new Promise((resolve) => {
console.log(`delay for ${item} secs`);
setTimeout(() => {
resolve();
@samokoder
samokoder / mongoDump.md
Created February 4, 2019 17:01 — forked from JaniAnttonen/mongoDump.md
Export Docker MongoDB collection as a JSON file

First, find the container that runs your MongoDB and ssh into it.

Then, find the collection you want to export:

mongo
show dbs
use <database>
show collections
exit
@samokoder
samokoder / rtlcss.js
Created August 8, 2018 09:45
Snippet to append rtl prefix
const fs = require('fs');
const rtlcss = require('rtlcss');
const options = {
'autoRename': false,
'autoRenameStrict': false,
'blacklist':{},
'clean': true,
'greedy': false,
'processUrls': false,
@samokoder
samokoder / env.sh
Created August 7, 2018 19:09
GoLang env
#!/bin/bash
export GOPATH=$(pwd)
@samokoder
samokoder / my.cnf.old
Created April 19, 2018 09:53 — forked from wood-goblin/my.cnf.old
Percona mysql sample config: /etc/mysql/my.cnf
# Generated by Percona Configuration Wizard (http://tools.percona.com/) version REL5-20120208
[mysql]
# CLIENT #
port = 3306
socket = /var/lib/mysql/data/mysql.sock
[mysqld]
@samokoder
samokoder / my-perf.js
Last active September 17, 2017 20:52
function myPerf() {
var i = window.performance.timing,
n = i.navigationStart;
return {
dns: (i.domainLookupEnd - i.domainLookupStart),
tcp: (i.connectEnd - i.connectStart),
srt: (i.responseStart - i.requestStart),
pdt: (i.responseEnd - i.responseStart),
rrt: (i.fetchStart - n),