Skip to content

Instantly share code, notes, and snippets.

View prestonp's full-sized avatar
😍
you had me at hello world

Preston Pham prestonp

😍
you had me at hello world
View GitHub Profile
@prestonp
prestonp / doc.md
Last active August 29, 2015 14:03
transaction example

Transactions API

To perform transactions use the dirac.tx api. Dirac defaults to using client pooling per request. So a single client must be used over the lifespan of a transaction. The tx will fetch one for you and release it upon .commit().

A dirac tx object has access to the same DALs in addition to transactional commands.

@prestonp
prestonp / readme.md
Last active August 29, 2015 14:03
dirac tx

Dirac.js - Postgres ORM Thing

Paul Dirac was a theoretical physicist who made fundamental contributions to the early development of both quantum mechanics and quantum electrodynamics. Dirac.js is a flexible and extendable database layer for Node Postgres.

Database as a Data Structure

Dirac.js is built on top of MoSQL, whose primary goal is to provide SQL query construction, but maintain value consistently throughout. This library extends that goal allowing you to reflect on the overall state of your database and retrieve your table structure in semantic JSON.

Dirac provides you with a decent foundation to start a postgres project with. It allows you to easily group all of your table logic and schema into one file and keep things generally dry and well-namespaced.

@prestonp
prestonp / search.sql
Last active August 29, 2015 14:05
simple postgres full text search
-- Setup
alter table orders add column search_vector tsvector;
update orders as o
set search_vector = to_tsvector( 'english',
o.id || ' ' ||
coalesce(o.name, '') || ' ' ||
coalesce(r.name, '') || ' ' ||
coalesce(u.name, '') || ' ' ||
coalesce(u.email, '')|| ' ' ||
mosql.registerConditionalHelper( '$matches', function( column, set, values, collection ) {
return column + ' @@ plainto_tsquery(' + set + ')';
});
mosql.registerConditionalHelper( '$partialMatches', function( column, set, values, collection ) {
// This breaks down a query for prefix matching.
// The limitation is it does not support spaces
// but rather & (AND) and | (OR) and ! (NOT).
// Ex to_tsquery('cat & hat & !ham') -- works
@prestonp
prestonp / demethodize.js
Last active August 29, 2015 14:07
demethodizing
// http://tech.pro/blog/2097/clever-way-to-demethodize-native-js-methods
var demethodize = Function.prototype.bind.bind(Function.prototype.call);
// demethodize lets you generalize functions over nodelists, jquery objects, and strings
var map = demethodize([].map);
map('Hello world', function(c) { return c.toUpperCase(); });
// What's more interesting is you can do this despite strings not having a .prototype.map
[].map.call('Hello world', function(c) { return c.toUppercase(); });
@prestonp
prestonp / query.sql
Created October 16, 2014 17:21
postgres update return new values and old values
-- returning updated values and old values by using a sub-expression
update orders
set type = 'delivery'
where id = 3767
returning id, type, (
select type from orders where id = 3767
) as old_type;
@prestonp
prestonp / dirac-middleware.js
Created October 16, 2014 18:40
dirac middleware success hook
m.update = function(collection, options){
options = options || {};
return function(req, res){
collection = collection || req.collection;
collection.update(req.queryObj, req.body, req.queryOptions, function(error, results){
if (error) return console.log(error), res.status(400).send();
@prestonp
prestonp / pg-change-timezone.md
Last active October 21, 2022 16:26
Change timezone in pg

Changing timezone in postgres

To change your server’s timezone per session you can use the following query:

set timezone TO 'GMT';

If you need to permanently update the timezone, find your postgresql.conf file in psql:

@prestonp
prestonp / gist:9ed46564b95180c8837d
Last active August 29, 2015 14:08
check if a row exists

Postgres exists(EXPRESSION) so useful

select *, exists(select 1 from order_amenities where order_id = 4275) as checked from amenities where restaurant_id = 187;