11'use strict' ;
22
3+ var util = require ( 'util' ) ;
4+
35var Query = require ( __dirname + '/node/query' ) ;
46var Column = require ( __dirname + '/column' ) ;
57var TableNode = require ( __dirname + '/node/table' ) ;
68var JoinNode = require ( __dirname + '/node/join' ) ;
9+ var Joiner = require ( __dirname + '/joiner' ) ;
710
811var Table = function ( config ) {
912 this . _schema = config . schema ;
@@ -15,6 +18,18 @@ var Table = function(config) {
1518
1619Table . define = function ( config ) {
1720 var table = new Table ( config ) ;
21+
22+ //allow hash of columns as well as array
23+ if ( config . columns && ! util . isArray ( config . columns ) ) {
24+ var cols = [ ]
25+ for ( var key in config . columns ) {
26+ var col = config . columns [ key ] ;
27+ col . name = key ;
28+ cols . push ( col ) ;
29+ }
30+ config . columns = cols ;
31+ }
32+
1833 for ( var i = 0 ; i < config . columns . length ; i ++ ) {
1934 table . addColumn ( config . columns [ i ] ) ;
2035 }
@@ -156,6 +171,11 @@ Table.prototype.leftJoin = function(other) {
156171 return new JoinNode ( 'LEFT' , this . toNode ( ) , other . toNode ( ) ) ;
157172} ;
158173
174+ //auto-join tables based on column intropsection
175+ Table . prototype . joinTo = function ( other ) {
176+ return Joiner . leftJoin ( this , other ) ;
177+ } ;
178+
159179Table . prototype . as = function ( alias ) {
160180 //TODO could this be cleaner?
161181 var t = Table . define ( this . _initialConfig ) ;
0 commit comments