@@ -199,6 +199,7 @@ function Model(opts) {
199199 model . allProperties = allProperties ;
200200 model . properties = opts . properties ;
201201 model . settings = opts . settings ;
202+ model . keys = opts . keys ;
202203
203204 model . drop = function ( cb ) {
204205 if ( arguments . length === 0 ) {
@@ -619,6 +620,68 @@ function Model(opts) {
619620 return this ;
620621 } ;
621622
623+ model . prependValidation = function ( key , validation ) {
624+ if ( opts . validations . hasOwnProperty ( key ) ) {
625+ opts . validations [ key ] . splice ( 0 , 0 , validation ) ;
626+ } else {
627+ opts . validations [ key ] = [ validation ] ;
628+ }
629+ } ;
630+
631+ var currFields = { } ;
632+
633+ model . addProperty = function ( propIn , options ) {
634+ var cType ;
635+ var prop = Property . normalize ( {
636+ prop : propIn , name : ( options && options . name || propIn . name ) ,
637+ customTypes : opts . db . customTypes , settings : opts . settings
638+ } ) ;
639+
640+ // Maintains backwards compatibility
641+ if ( opts . keys . indexOf ( k ) != - 1 ) {
642+ prop . key = true ;
643+ } else if ( prop . key ) {
644+ opts . keys . push ( k ) ;
645+ }
646+
647+ if ( options && options . klass ) {
648+ prop . klass = options . klass ;
649+ }
650+
651+ switch ( prop . klass ) {
652+ case 'primary' :
653+ opts . properties [ prop . name ] = prop ;
654+ break ;
655+ case 'hasOne' :
656+ association_properties . push ( prop . name )
657+ break ;
658+ }
659+
660+ allProperties [ prop . name ] = prop ;
661+ fieldToPropertyMap [ prop . mapsTo ] = prop ;
662+
663+ if ( prop . required ) {
664+ model . prependValidation ( prop . name , Validators . required ( ) ) ;
665+ }
666+
667+ if ( prop . key && prop . klass == 'primary' ) {
668+ keyProperties . push ( prop ) ;
669+ }
670+
671+ if ( prop . lazyload !== true && ! currFields [ prop . name ] ) {
672+ currFields [ prop . name ] = true ;
673+ if ( ( cType = opts . db . customTypes [ prop . type ] ) && cType . datastoreGet ) {
674+ model_fields . push ( {
675+ a : prop . mapsTo , sql : cType . datastoreGet ( prop , opts . db . driver . query )
676+ } ) ;
677+ } else {
678+ model_fields . push ( prop . mapsTo ) ;
679+ }
680+ }
681+
682+ return prop ;
683+ } ;
684+
622685 Object . defineProperty ( model , "table" , {
623686 value : opts . table ,
624687 enumerable : false
@@ -627,14 +690,6 @@ function Model(opts) {
627690 value : opts . keys ,
628691 enumerable : false
629692 } ) ;
630- Object . defineProperty ( model , "keys" , {
631- value : opts . keys ,
632- enumerable : false
633- } ) ;
634- Object . defineProperty ( model , "properties" , {
635- value : opts . properties ,
636- enumerable : false
637- } ) ;
638693 Object . defineProperty ( model , "uid" , {
639694 value : opts . driver . uid + "/" + opts . table + "/" + opts . keys . join ( "/" ) ,
640695 enumerable : false
@@ -647,55 +702,16 @@ function Model(opts) {
647702 }
648703 }
649704
650- var currFields = { } , cType , name ;
651-
652705 // If no keys are defined add the default one
653706 if ( opts . keys . length == 0 && ! _ . any ( opts . properties , { key : true } ) ) {
654- name = opts . settings . get ( "properties.primary_key" ) ;
655-
656- opts . properties [ name ] = Property . normalize ( {
657- prop : { type : 'serial' , key : true , required : false , klass : 'key' } ,
658- name : name , customTypes : opts . db . customTypes , settings : opts . settings
659- } ) ;
707+ opts . properties [ opts . settings . get ( "properties.primary_key" ) ] = {
708+ type : 'serial' , key : true , required : false , klass : 'primary'
709+ } ;
660710 }
661711
662712 // standardize properties
663713 for ( k in opts . properties ) {
664- var prop = opts . properties [ k ] = Property . normalize ( {
665- prop : opts . properties [ k ] , name : k ,
666- customTypes : opts . db . customTypes , settings : opts . settings
667- } ) ;
668- prop . klass = 'primary' ;
669- allProperties [ k ] = prop ;
670- fieldToPropertyMap [ prop . mapsTo ] = prop ;
671-
672- if ( opts . keys . indexOf ( k ) != - 1 ) {
673- prop . key = true ;
674- } else if ( prop . key ) {
675- opts . keys . push ( k ) ;
676- }
677- if ( prop . key ) {
678- keyProperties . push ( prop ) ;
679- }
680-
681- if ( prop . lazyload !== true && ! currFields [ k ] ) {
682- currFields [ k ] = true ;
683- if ( ( cType = opts . db . customTypes [ prop . type ] ) && cType . datastoreGet ) {
684- model_fields . push ( {
685- a : prop . mapsTo , sql : cType . datastoreGet ( prop , opts . db . driver . query )
686- } ) ;
687- } else {
688- model_fields . push ( prop . mapsTo ) ;
689- }
690- }
691- if ( prop . required ) {
692- // Prepend `required` validation
693- if ( opts . validations . hasOwnProperty ( k ) ) {
694- opts . validations [ k ] . splice ( 0 , 0 , Validators . required ( ) ) ;
695- } else {
696- opts . validations [ k ] = [ Validators . required ( ) ] ;
697- }
698- }
714+ model . addProperty ( opts . properties [ k ] , { name : k , klass : 'primary' } ) ;
699715 }
700716
701717 if ( keyProperties . length == 0 ) {
@@ -707,7 +723,7 @@ function Model(opts) {
707723 model [ AvailableHooks [ k ] ] = createHookHelper ( AvailableHooks [ k ] ) ;
708724 }
709725
710- OneAssociation . prepare ( model , one_associations , association_properties , model_fields ) ;
726+ OneAssociation . prepare ( model , one_associations ) ;
711727 ManyAssociation . prepare ( opts . db , model , many_associations ) ;
712728 ExtendAssociation . prepare ( opts . db , model , extend_associations ) ;
713729
0 commit comments