Skip to content

Instantly share code, notes, and snippets.

View HallM's full-sized avatar

Matthew Hall HallM

View GitHub Profile
import Bluebird from 'bluebird';
// assume express, promise-ready ORM (Sequelize, Bookshelf, Mongoose)
function wrap(genFn) {
var cr = Bluebird.coroutine(genFn);
return function (req, res, next) {
cr(req, res, next).catch(next);
}
}
@HallM
HallM / mvc-evolvution.txt
Created January 4, 2016 22:48
Evolving the MVC pattern for the real world
universe: everything that isn't this software
can only interface with software through the shell
- user
- monitor
- browser
- database
- file system
- external API
- API on the same machine but different piece of software
- sensors
function privateHandlePut(req, res) {
// do things
}
function publicHandleGet(req, res) {
// do things
}
export.controller = {
publicHandleGet
@HallM
HallM / pageorder-atomically-update.sql
Created January 20, 2016 23:46
Can update the order of a page and all pages in between old and new order indices safely
UPDATE page, (SELECT pageorder as oldorder FROM page WHERE id = :pgid) as t2 SET page.pageorder = CASE
WHEN id = :pgid THEN :neworder
WHEN page.pageorder <= :neworder AND page.pageorder > t2.oldorder THEN page.pageorder-1
WHEN page.pageorder >= :neworder AND page.pageorder < t2.oldorder THEN page.pageorder+1
ELSE page.pageorder
END
WHERE page.pageorder >= LEAST(:neworder, t2.oldorder) AND page.pageorder <= GREATEST(:neworder, t2.oldorder);
@HallM
HallM / sortable.sql
Created January 25, 2016 17:27
SQL to be able to have a sorting column with category
; removal
UPDATE page
, (SELECT category_id as oldcategory, page_order as oldorder FROM page WHERE id = :pgid) as t2
SET page.page_order = CASE
WHEN id = :pgid THEN -1
WHEN page.category_id = t2.oldcategory
AND page.page_order > t2.oldorder
THEN page.page_order-1
ELSE page.page_order
END
@HallM
HallM / short-prime.c
Last active January 25, 2016 23:39
N primes in 98 bytes of C code
// trivial division
h,i=2;g(j){return j*j>i||i%j++&&g(j);}main(){scanf("%i",&h);for(;h;i++)g(2)&&printf("%i ",i,--h);}
// Wilson's Theory
h,i;m(n){return n?n*m(n-1)%i:1;}main(){scanf("%i",&h);for(;h;)m(i++)+1-i||printf("%i ",i,--h);}
/*
how dynamic paths work
while this design sounds good, may wish to consider allowing something like
{param|/regex/}
would require the existance of "DynNode" to store the regex validator
but also, the parent Node would need to store an array of DynNodes
don't need to store the param names though, since it can be stored in the dynnodes themselves
when adding, must verify the names match up
'use strict'
var TrieRouter = require('./experimental-router');
TrieRouter.add(['get'], '/', function(req, res) {
//console.log('index');
}, {});
TrieRouter.add(['get'], '/aboutus', function(req, res) {
console.log('aboutus');
// Methods could be any of the CRUD actions
// they may return data like newest info, result data, or just a status
// they could set flash vars (universally) using Flash.post('error', 'something went wrong')
// POST->redirect flow without JS
// the result is probably ok to ignore when you think about the workflow.
// the redirect-to should load a page that loads data anyway.
// we expose Flash system on both sides anyway, so call that in here.
// would just need a way to know where to redirect following a non-XHR
@HallM
HallM / todo-ogre-concept.js
Last active February 12, 2016 20:35
just a concept of how the framework would handle the Todo MVC demo
// types/todo.js
export default Type({
owner: User, // user is assumed defined for this example
message: {
type: String,
validators: /\\w+/ // maybe a function, maybe an array, maybe an object with a custom error message
}
});
// db/todo.js