@@ -18,6 +18,33 @@ module.exports = {
1818 destroyOneRecord : destroyOneRecord ,
1919 updateOneRecord : updateOneRecord ,
2020
21+ init : function ( ) {
22+
23+ for ( let name in sails . models ) {
24+
25+ let model = sails . models [ name ] ;
26+ let associations = { } ;
27+
28+ sails . log . verbose ( "Registering model " + name + " as " + pluralize ( model . identity ) ) ;
29+ model . associations . forEach ( ( association ) => {
30+
31+ let alias = association [ 'alias' ] ;
32+ let type = association [ association [ 'type' ] ] ;
33+
34+ sails . log . verbose ( name + " has a relation with " + type + " (as " + alias + ")" ) ;
35+ associations [ alias ] = {
36+ type : pluralize ( type )
37+ } ;
38+ } ) ;
39+
40+ Serializer . register ( pluralize ( model . identity ) , {
41+ id : model . primaryKey || 'id' ,
42+ convertCase : this . getAttributesSerializedCaseSetting ( ) ,
43+ relationships : associations
44+ } ) ;
45+ } ;
46+ } ,
47+
2148 getAttributesSerializedCaseSetting : function ( ) {
2249
2350 var caseSetting = undefined ;
@@ -101,20 +128,6 @@ module.exports = {
101128
102129 serialize : function ( modelName , data ) {
103130
104- let Model = this . _getModelObjectFromModelName ( modelName ) ;
105- let id = 'id' ;
106-
107- if ( Model ) {
108- id = Model . primaryKey ;
109- } else {
110- sails . log . warn ( 'Model ' + modelName + ' could not be found by sails-json-api-blueprints' ) ;
111- }
112-
113- Serializer . register ( modelName , {
114- convertCase : this . getAttributesSerializedCaseSetting ( ) ,
115- id : id
116- } ) ;
117-
118131 var returnedValue = Serializer . serialize ( modelName , data ) ;
119132 delete returnedValue . jsonapi ; // Let's ignore the version for now
120133
0 commit comments