Skip to content

Instantly share code, notes, and snippets.

@avimar
avimar / knex-functions.js
Created August 28, 2016 07:15
knex mysql update on duplicate and replace
function upsert(table, data, update){
if(!update) update=data;
var insert = knex(table).insert(data).toString();
var update = knex(table).update(update).toString().replace(/^update .* set /i, '');
return knex.raw(insert + ' on duplicate key update ' + update);
}
function replace(table, data){
var insert = knex(table).insert(data).toString().replace(/^INSERT/i, 'REPLACE');
return knex.raw(insert);
var _putItem = function (item, shouldCreateNewItem, shouldReturnInformationFromJoinedTable) {
return knex(...).update()
.then(function(affectedRows) {
if (affectedRows == 0 && shouldCreateNewItem) return knex.insert(...);
else if (shouldReturnInformationFromJoinedTable) return knex(...). ....leftJoin(..).select().first()
else return true;
})
}
@avimar
avimar / file.js
Created July 13, 2016 17:55
append a value to the query string
function checkAndSetAccount(){
var q = riot.route.query();
if (Cookies.get('account')==q.account) return;//already done.
if (q.account) {
Cookies.set('account',q.account);
auth.trigger('switchAccount');
}
if (!q.account && Cookies.get('account')!==Cookies.get('accountOriginal')) {//nothing in query string: need to set it
//console.log(window.location)
var newLocation=window.location.hash;
//using a function-scope `data` variable to save the data
function getData(respondentId) {
var data={};
return Respondent.findById(respondentId)
.then(respondent => {
data.respondent = respondent;
return Box.findById(respondent.box_id)
})
.then(box => {
data.box = box
var myURL="mydomain.com";
var restifyOptions={}
restifyOptions.certificate = fs.readFileSync('PathTo:fullchain.pem');
restifyOptions.key = fs.readFileSync('PathTo:privkey.pem');
var server = restify.createServer(restifyOptions);
var nonSecure = restify.createServer({name: 'redirectToSSL'});
nonSecure.get(/.*/,function (req, res, next) {
res.redirect(301, 'https://' + myURL + req._url.href, next);
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
@avimar
avimar / index.js
Last active December 17, 2015 08:49
Restify with optional HTTPS redirect
//This allows you to specify a certificate and a key and will create a server that just handles redirects, if you want it.
/*Using let's encrypt to create a free certificate:
cd /root/
git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt
./letsencrypt-auto certonly --webroot -w /my/web/root --email my@email.com -d myDomain.com
//add ssl:certificate location of 'fullchain.pem' and ssl:key location of 'privkey.pem'
*/
it('should round up to the next billing interval', function(){
var cases = [//number, expect, increment
{ n: 4000, e: 6, i:6 },
{ n: 6001, e: 12, i:6 },
{ n: 17999, e: 18, i:6 },
{ n: 1, e: 6, i:6 },
{ n: 62000, e: 66, i:6 },
{ n: 1, e: 1, i:1 },
{ n: 62000, e: 120, i:60 },
];
@avimar
avimar / auth.tag
Created July 16, 2015 10:41
Example of riotj's router
<auth>
<login if={'login'==opts.action || 'logout' == opts.action} action={opts.action}></login>
<register if={'register' == opts.action}></register>
<change-pass if={'verifyEmail' == opts.action || 'changePass' == opts.action} action={opts.action} uid={opts.uid} data={opts.data}></changePass>
<script>
console.log(opts.action);
if(opts.action=='login' && Cookies.get('apiKey')) riot.route('myAccount');
if(opts.action=='register' && Cookies.get('apiKey')) riot.route('myAccount');
else if(opts.action=="" && Cookies.get('apiKey')) riot.route('myAccount');
@avimar
avimar / gist:5968480
Created July 10, 2013 17:47
Install newrelic with salt-stack on your debian/ubuntu system
#install instructions at https://newrelic.com/docs/server/server-monitor-installation-ubuntu-and-debian
base:
pkgrepo.managed:
- humanname: Newrelic PPA
- name: deb http://apt.newrelic.com/debian/ newrelic non-free
# - dist: precise
- file: /etc/apt/sources.list.d/newrelic.list
- keyid: 548C16BF
- keyserver: subkeys.pgp.net