Skip to content

Instantly share code, notes, and snippets.

@markselby
markselby / controllers.js
Created December 26, 2014 16:51
Node.js models and controllers
var grunt = require('grunt');
var controllers = [];
grunt.file.expand({ cwd: '' }, 'app/controllers/**/*.js').forEach(function(filename) {
// The routes in this file
var controller = require(process.cwd() + '/' + filename);
controller.filename = filename;
controller.priority = controller.priority || 1;
controllers.push(controller)
@markselby
markselby / es.json
Created August 21, 2014 10:11
Sucky ES
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "couchbaseDocument.doc.type:keyword_slice"
}
},
{
@markselby
markselby / proxy.js
Created August 19, 2014 17:10
JSON API proxy debugger
// Don't forget to npm install hoxy
var hoxy = require('hoxy');
var proxy = new hoxy.Proxy().listen(8080);
proxy.intercept({
phase: 'request',
}, function(req, resp) {
console.log('\n\n==============================================');
console.log('Request: ' + req.fullUrl());
Object.keys(req.headers).forEach(function(header) {
@markselby
markselby / command-line
Created March 20, 2014 03:05
Simplified Postgres database pool access for Node.js
NODE_ENV=development node sample-usage.js
@markselby
markselby / after-jquery.js
Last active August 29, 2015 13:57
Running Javascript after render.
// Somewhere in your javascript assets included at the end of the document
$(document).ready(function() {
loaded = true;
doInit();
});
@markselby
markselby / plv8-base.sql
Last active January 2, 2016 01:59
Simplify everything with Postgres and Javascript - PL/v8
-- eg SELECT sort_order('-relevance', ['title', 'updated_at', 'relevance'])
-- Optionally specify +/- prefix for sort field. Invalid sort field uses first in array
CREATE OR REPLACE FUNCTION sort_order(_field text, _valid text[]) RETURNS json AS
$$
_field = decodeURIComponent(_field);
var directions = { ' ': 'ASC', '-': 'DESC' }; // + becomes space
var direction = directions[_field[0]];
if(direction) { _field = _field.slice(1) } else { direction = directions['-']; };
if(_valid.indexOf(_field) < 0) _field = _valid[0];
return { field: _field, direction: direction };
@markselby
markselby / create-sitemap.sh
Last active January 1, 2016 09:09
Fast sitemap.xml generation using Postgres.
#/bin/sh
(
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"
psql -c "COPY (SELECT xmlelement(name url, xmlforest('http://writebox.co.uk/news/' || slug AS loc, to_char(updated_at, 'YYYY-MM-DDThh:mm:ss+07:00') AS lastmod)) FROM news) TO STDOUT" writebox_$2
echo "</urlset>"
) > $1/public/sitemap.xml
cat $1/public/sitemap.xml | gzip -9 > $1/public/sitemap.xml.gz
@markselby
markselby / tables-accessed.sql
Last active December 27, 2015 20:49
Handy Postgres statistics queries for web development.
/*
* Find which tables were involved in a number of queries.
* This is obviously a contrived example. A practical application would be to wrap all
* the queries for a web page in a transaction (perhaps using before/after filters in
* the case of Rails) and then use the stats for cache invalidation.
*/
BEGIN TRANSACTION;
/* Run the queries */
SELECT * from news LIMIT 5;
@markselby
markselby / async-gists.js
Last active February 19, 2020 10:26
Load GitHub Gists asynchronously and optionally specify which file to show. This allows you to keep related files in a single gist but show them individually on your pages. The async loading prevents your page rendering from stalling.
function loadGists() {
var els = $('code[gist]'), gists = {}, code = [], stylesheets = [];
// Get elements referencing a gist and build a gists hash referencing the elements that use it
els.each(function(idx, rawEl) {
var el = $(rawEl), gist = el.attr('gist');
rawEl.gist = gist;
rawEl.file = el.attr('file');
gists[gist] = gists[gist] || { targets: [] };
gists[gist].targets.push(el);
});
@markselby
markselby / nginx.conf
Created October 28, 2013 02:58
Nginx config for using LUA / Redis cache.
location / {
try_files $uri @redis_cache;
add_header Source Files; # Handy for development
}
location @redis_cache {
# Make sure this path is correct
# Run "nginx -V" from the command prompt and look for --prefix=/path/to/somewhere
# and place redis.lua there, or use an absolute path as shown below
content_by_lua_file /path/to/your/config/redis.lua;