@@ -33,6 +33,7 @@ function Model(opts) {
3333 var extend_associations = [ ] ;
3434 var association_properties = [ ] ;
3535 var model_fields = [ ] ;
36+ var fieldToPropertyMap = { } ;
3637 var allProperties = { } ;
3738
3839 var createHookHelper = function ( hook ) {
@@ -142,6 +143,18 @@ function Model(opts) {
142143 return instance ;
143144 } ;
144145
146+ var mapDatastoreFieldsToProperties = function ( dataIn ) {
147+ var k , prop ;
148+ var dataOut = { } ;
149+
150+ for ( k in dataIn ) {
151+ prop = fieldToPropertyMap [ k ] ;
152+ if ( ! prop ) dataOut [ k ] = dataIn [ k ] ;
153+ else dataOut [ prop . name ] = dataIn [ k ] ;
154+ }
155+ return dataOut ;
156+ }
157+
145158 var model = function ( ) {
146159 var instance , i ;
147160
@@ -283,16 +296,18 @@ function Model(opts) {
283296 return cb ( new ORMError ( err . message , 'QUERY_ERROR' , { originalCode : err . code } ) ) ;
284297 }
285298 if ( data . length === 0 ) {
286- return cb ( new ORMError ( "Not found" , 'NOT_FOUND' , { model : opts . table } ) ) ;
299+ return cb ( new ORMError ( "Not found" , 'NOT_FOUND' , { model : opts . table } ) ) ;
287300 }
288301
302+ data = mapDatastoreFieldsToProperties ( data [ 0 ] ) ;
303+
289304 var uid = opts . driver . uid + "/" + opts . table + "/" + ids . join ( "/" ) ;
290305
291306 Singleton . get ( uid , {
292307 cache : ( options . hasOwnProperty ( "cache" ) ? options . cache : opts . cache ) ,
293308 save_check : opts . settings . get ( "instance.cacheSaveCheck" )
294309 } , function ( cb ) {
295- return createInstance ( data [ 0 ] , {
310+ return createInstance ( data , {
296311 uid : uid ,
297312 autoSave : options . autoSave ,
298313 autoFetch : ( options . autoFetchLimit === 0 ? false : options . autoFetch ) ,
@@ -630,22 +645,32 @@ function Model(opts) {
630645 }
631646 }
632647
633- var cur_fields = { } ;
648+ var currFields = { } , cType ;
649+
634650 // standardize properties
635651 for ( k in opts . properties ) {
636- opts . properties [ k ] = Property . normalize ( opts . properties [ k ] , opts . db . customTypes , opts . settings ) ;
637- opts . properties [ k ] . klass = 'primary' ;
638- allProperties [ k ] = opts . properties [ k ] ;
652+ var prop = opts . properties [ k ] = Property . normalize ( {
653+ prop : opts . properties [ k ] , name : k ,
654+ customTypes : opts . db . customTypes , settings : opts . settings
655+ } ) ;
656+ prop . klass = 'primary' ;
657+ allProperties [ k ] = prop ;
658+ fieldToPropertyMap [ prop . mapsTo ] = prop ;
639659
640660 if ( opts . id . indexOf ( k ) != - 1 ) {
641- opts . properties [ k ] . key = true ;
661+ prop . key = true ;
642662 }
643-
644- if ( opts . properties [ k ] . lazyload !== true && ! cur_fields [ k ] ) {
645- cur_fields [ k ] = true ;
646- model_fields . push ( opts . properties [ k ] . select || k ) ;
663+ if ( prop . lazyload !== true && ! currFields [ k ] ) {
664+ currFields [ k ] = true ;
665+ if ( ( cType = opts . db . customTypes [ prop . type ] ) && cType . datastoreGet ) {
666+ model_fields . push ( {
667+ a : prop . mapsTo , sql : cType . datastoreGet ( prop , opts . db . driver . query )
668+ } ) ;
669+ } else {
670+ model_fields . push ( prop . mapsTo ) ;
671+ }
647672 }
648- if ( opts . properties [ k ] . required ) {
673+ if ( prop . required ) {
649674 // Prepend `required` validation
650675 if ( opts . validations . hasOwnProperty ( k ) ) {
651676 opts . validations [ k ] . splice ( 0 , 0 , Validators . required ( ) ) ;
@@ -657,9 +682,11 @@ function Model(opts) {
657682
658683 for ( var i = 0 ; i < opts . id . length ; i ++ ) {
659684 k = opts . id [ i ] ;
660- allProperties [ k ] = opts . properties [ k ] || {
661- type : 'serial' , key : true , klass : 'key'
662- } ;
685+ allProperties [ k ] = opts . properties [ k ] || Property . normalize ( {
686+ prop : { type : 'serial' , key : true , klass : 'key' } ,
687+ name : k , customTypes : opts . db . customTypes , settings : opts . settings
688+ } ) ;
689+ fieldToPropertyMap [ allProperties [ k ] . mapsTo ] = allProperties [ k ] ;
663690 }
664691 model_fields = opts . id . concat ( model_fields ) ;
665692
0 commit comments