@@ -8,11 +8,11 @@ module.exports = function (db, name) {
88 // Create router
99 var router = express . Router ( )
1010
11- // GET /:resource
12- // GET /:resource ?q=
13- // GET /:resource ?attr=&attr=
14- // GET /:resource ?_end=&*
15- // GET /:resource ?_start=&_end=&*
11+ // GET /name
12+ // GET /name ?q=
13+ // GET /name ?attr=&attr=
14+ // GET /name ?_end=&*
15+ // GET /name ?_start=&_end=&*
1616 function list ( req , res , next ) {
1717
1818 // Filters list
@@ -103,41 +103,59 @@ module.exports = function (db, name) {
103103 next ( )
104104 }
105105
106- // GET /:resource/:id
106+ // GET /name/:id
107+ // GET /name/:id?_embed=&_expand
107108 function show ( req , res , next ) {
108109 var _embed = req . query . _embed
110+ var _expand = req . query . _expand
109111 var id = utils . toNative ( req . params . id )
110- var resource = db ( name )
111- . getById ( id )
112+ var resource = db ( name ) . getById ( id )
113+
114+ // Filter empty params
115+ function filter ( p ) {
116+ return p && p . trim ( ) . length > 0
117+ }
112118
113119 if ( resource ) {
114120 // Clone resource to avoid making changes to the underlying object
115121 resource = _ . cloneDeep ( resource )
122+
116123 // Always use an array
117- _embed = _ . isArray ( _embed ) ? _embed : [ _embed ]
124+ _embed = [ ] . concat ( _embed )
125+ _expand = [ ] . concat ( _expand )
118126
119127 // Embed other resources based on resource id
120- _embed . forEach ( function ( otherResource ) {
121-
122- if ( otherResource
123- && otherResource . trim ( ) . length > 0
124- && db . object [ otherResource ] ) {
125-
126- var query = { }
127- var prop = pluralize . singular ( name ) + 'Id'
128- query [ prop ] = id
129- resource [ otherResource ] = db ( otherResource ) . where ( query )
130-
131- }
132- } )
128+ // /posts/1?_embed=comments
129+ _embed
130+ . filter ( filter )
131+ . forEach ( function ( otherResource ) {
132+ if ( db . object [ otherResource ] ) {
133+ var query = { }
134+ var singularResource = pluralize . singular ( name )
135+ query [ singularResource + 'Id' ] = id
136+ resource [ otherResource ] = db ( otherResource ) . where ( query )
137+ }
138+ } )
139+
140+ // Expand inner resources based on id
141+ // /posts/1?_expand=user
142+ _expand
143+ . filter ( filter )
144+ . forEach ( function ( innerResource ) {
145+ var plural = pluralize ( innerResource )
146+ if ( db . object [ plural ] ) {
147+ var prop = innerResource + 'Id'
148+ resource [ innerResource ] = db ( plural ) . getById ( resource [ prop ] )
149+ }
150+ } )
133151
134152 res . locals . data = resource
135153 }
136154
137155 next ( )
138156 }
139157
140- // POST /:resource
158+ // POST /name
141159 function create ( req , res , next ) {
142160 for ( var key in req . body ) {
143161 req . body [ key ] = utils . toNative ( req . body [ key ] )
@@ -151,8 +169,8 @@ module.exports = function (db, name) {
151169 next ( )
152170 }
153171
154- // PUT /:resource /:id
155- // PATCH /:resource /:id
172+ // PUT /name /:id
173+ // PATCH /name /:id
156174 function update ( req , res , next ) {
157175 for ( var key in req . body ) {
158176 req . body [ key ] = utils . toNative ( req . body [ key ] )
@@ -168,7 +186,7 @@ module.exports = function (db, name) {
168186 next ( )
169187 }
170188
171- // DELETE /:resource /:id
189+ // DELETE /name /:id
172190 function destroy ( req , res , next ) {
173191 db ( name ) . removeById ( utils . toNative ( req . params . id ) )
174192
0 commit comments