Skip to content

Instantly share code, notes, and snippets.

View marcbachmann's full-sized avatar
🏳️‍🌈

Marc Bachmann marcbachmann

🏳️‍🌈
View GitHub Profile
@marcbachmann
marcbachmann / index.coffee
Last active August 29, 2015 14:03
Spellcheck test
# http://jsperf.com/in-vs-hasownproperty-vs-has/2
fs = require('fs')
_ = require('lodash')
async = require('async')
nodehun = require('nodehun')
redis = require("redis")
cache = redis.createClient()
affbuf = fs.readFileSync('/Users/marcbachmann/Desktop/de-NZZ.aff');
@marcbachmann
marcbachmann / db.coffee
Created October 2, 2014 08:37
mongodb-native Entity class
mongodb_url = require('../../config').get('mongodb:url')
MongoClient = require('mongodb').MongoClient
exports.db = null
exports.connect = (callback) ->
exports.client = new MongoClient()
MongoClient.connect mongodb_url, (err, db) ->
return callback(err) if err
exports.db = db
return callback(null, db)
@marcbachmann
marcbachmann / example.coffee
Last active August 29, 2015 14:27
Knex Migration Class
db = require('knex')(yourConfig)
Migration = require('../../lib/db').Migration
upgrade = new Migration(db)
upgrade.createTable 'documents', (t) ->
t.uuid('id')
t.text('title')
upgrade.createTable 'users', (t) ->
@marcbachmann
marcbachmann / Dockerfile
Created August 25, 2015 12:37
Docker elasticsearch with modified config file
FROM elasticsearch
RUN sed -i.bak -e "s|^varname:.*$|varname: \"value"|;" /data/elasticsearch.yml
RUN plugin -i elasticsearch/marvel/latest
@marcbachmann
marcbachmann / backup.sh
Created September 20, 2015 00:47
Database backup script
#!/bin/bash
# Based on https://gist.github.com/2206527
echo -e "Backup MySQL Databases to S3"
# Basic variables
host="localhost"
mysqlpass="rootpass"
bucket="s3://bucketname"
accesskey="awsaccesskey"
secretkey="awssecretkey"
@marcbachmann
marcbachmann / populate.coffee
Last active October 8, 2015 05:33
Small script to populate collections
_ = require('lodash')
module.exports =
populate = (mapName, fromCollection, toCollection, fromId, toId='id') ->
indexed = _.indexBy(toCollection, toId)
for doc in fromCollection || []
doc[mapName] = indexed[doc[revision_id]]
docs = [
@marcbachmann
marcbachmann / nodejs passport configuration
Last active December 15, 2015 22:09
This is a passport LocaStrategy with custom error messages from my app.
passport.use(new LocalStrategy(
function(username, password, done) {
if (username.indexOf('@') == -1) {
username = username + '@example.com';
}
User.findOne({ 'email': username.toLowerCase()}, function(err, user) {
if (err) { return done(err); }
if (!user) {
var error = new Error('User not found or wrong Credentials');
@marcbachmann
marcbachmann / nodejs passport authenticate
Last active December 15, 2015 22:09
Login function with express-validator
function postLogin(req, res, next){
req.assert('username', 'username is required').notEmpty();
req.assert('password', 'password is required').notEmpty();
var username = (req.body.username)? req.body.username: '';
var password = (req.body.password)? req.body.password: '';
var errors = req.validationErrors(true);
if (errors) {
res.render('login.jade', {locals: { title: 'Login Error', username: username, password: password, errors: errors}});
return;
@marcbachmann
marcbachmann / assert-npm-version.js
Created January 4, 2016 09:34
A npm version check that doesn't have any dependencies
#!/usr/bin/env node
// Embed semver module to make this script completely independent
// minified semver@5.1.0
// https://github.com/npm/node-semver
var semver={};!function(r){function e(r,e){if(r instanceof i)return r;if("string"!=typeof r)return null;if(r.length>L)return null;var t=e?F[pr]:F[ar];if(!t.test(r))return null;try{return new i(r,e)}catch(n){return null}}function t(r,t){var n=e(r,t);return n?n.version:null}function n(r,t){var n=e(r.trim().replace(/^[=v]+/,""),t);return n?n.version:null}function i(r,e){if(r instanceof i){if(r.loose===e)return r;r=r.version}else if("string"!=typeof r)throw new TypeError("Invalid Version: "+r);if(r.length>L)throw new TypeError("version is longer than "+L+" characters");if(!(this instanceof i))return new i(r,e);B("SemVer",r,e),this.loose=e;var t=r.trim().match(e?F[pr]:F[ar]);if(!t)throw new TypeError("Invalid Version: "+r);if(this.raw=r,this.major=+t[1],this.minor=+t[2],this.patch=+t[3],this.major>U||this.major<0)throw new TypeError("Invalid major version");if(this.m
@marcbachmann
marcbachmann / 0-backfill-changelog-on-github-releases.js
Last active February 4, 2016 10:20
Migrate CHANGELOG.md to github releases
var fs = require('fs')
var _ = require('lodash')
var async = require('async')
var GitHubApi = require('github')
// Token from your settings page on github
var token = 'YOURGITHUBTOKEN'
// Repository to migrate
var owner = 'upfrontIO'