Skip to content

Instantly share code, notes, and snippets.

@troyk
Created April 3, 2012 19:05
Show Gist options
  • Save troyk/2294773 to your computer and use it in GitHub Desktop.
Save troyk/2294773 to your computer and use it in GitHub Desktop.
I had a dream; Make an ORM for plv8; got this far; need to pay the bills == use ruby
CREATE OR REPLACE FUNCTION dboBlit(account_id integer, user_id integer) RETURNS void AS $$
Blit = {};
Blit.DBO = (function(){
// load schemas
var schema = {};
plv8.elog(NOTICE, 'loading schema');
plv8.execute("\
SELECT c.relname as table, \
a.attname as col, \
format_type(a.atttypid, a.atttypmod) as type, \
a.atttypid as type_oid, \
d.adsrc as default, \
a.attnotnull as notnull \
FROM pg_attribute a \
LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum \
LEFT JOIN pg_class c ON a.attrelid = c.oid AND c.relkind = 'r' \
LEFT JOIN pg_namespace ns ON c.relnamespace = ns.oid \
WHERE ns.nspname IN ('public','crm1') \
AND a.attnum > 0 AND NOT a.attisdropped \
ORDER BY c.oid, a.attnum;").forEach(function(row){
var table = schema[row.table] || ( schema[row.table] = {} );
table[row.col] = {type:row.type, default:row.default, nullable: row.notnull };
});
//plv8.elog(NOTICE,JSON.stringify(schema));
function DBO() {
}
})();
$$ LANGUAGE plv8;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment