@@ -4,170 +4,168 @@ import * as express from 'express';
4
4
import Person from './models/Person' ;
5
5
import Movie from './models/Movie' ;
6
6
7
- export default function ( app : express . Application ) {
7
+ export default function ( router : express . Router ) {
8
8
9
9
// Create a new Person.
10
- app . post ( '/persons' , function ( req , res , next ) {
11
- return Person
10
+ router . post ( '/persons' , async ( req , res ) => {
11
+ const person = await Person
12
12
. query ( )
13
- . insert ( req . body )
14
- . then ( person => res . send ( person ) )
15
- . catch ( next ) ;
13
+ . allowInsert ( '[pets, children.[pets, movies], movies, parent]' )
14
+ . insertGraph ( req . body ) ;
15
+
16
+ res . send ( person ) ;
16
17
} ) ;
17
18
18
19
// Patch a Person.
19
- app . patch ( '/persons/:id' , function ( req , res , next ) {
20
- return Person
20
+ router . patch ( '/persons/:id' , async ( req , res ) => {
21
+ const person = await Person
21
22
. query ( )
22
- . patchAndFetchById ( req . params . id , req . body )
23
- . then ( person => res . send ( person ) )
24
- . catch ( next ) ;
23
+ . patchAndFetchById ( req . params . id , req . body ) ;
24
+
25
+ res . send ( person ) ;
25
26
} ) ;
26
27
27
28
// Get all Persons. The result can be filtered using query parameters
28
29
// `minAge`, `maxAge` and `firstName`. Relations can be fetched eagerly
29
30
// by giving a relation expression as the `eager` query parameter.
30
- app . get ( '/persons' , function ( req , res , next ) {
31
+ router . get ( '/persons' , async ( req , res ) => {
31
32
// We don't need to check for the existence of the query parameters because
32
33
// we call the `skipUndefined` method. It causes the query builder methods
33
34
// to do nothing if one of the values is undefined.
34
- return Person
35
+ const persons = await Person
35
36
. query ( )
36
37
. allowEager ( '[pets, children.[pets, movies], movies]' )
37
38
. eager ( req . query . eager )
38
39
. skipUndefined ( )
39
40
. where ( 'age' , '>=' , req . query . minAge )
40
41
. where ( 'age' , '<' , req . query . maxAge )
41
- . where ( 'firstName' , 'like' , req . query . firstName )
42
- . then ( persons => res . send ( persons ) )
43
- . catch ( next ) ;
42
+ . where ( 'firstName' , 'like' , req . query . firstName ) ;
43
+
44
+ res . send ( persons ) ;
44
45
} ) ;
45
46
46
47
// Delete a person.
47
- app . delete ( '/persons/:id' , function ( req , res , next ) {
48
- return Person
48
+ router . delete ( '/persons/:id' , async ( req , res ) => {
49
+ await Person
49
50
. query ( )
50
51
. delete ( )
51
- . where ( 'id' , req . params . id )
52
- . then ( ( ) => res . send ( { } ) )
53
- . catch ( next ) ;
52
+ . where ( 'id' , req . params . id ) ;
53
+
54
+ res . send ( { } ) ;
54
55
} ) ;
55
56
56
57
// Add a child for a Person.
57
- app . post ( '/persons/:id/children' , async function ( req , res , next ) {
58
+ router . post ( '/persons/:id/children' , async ( req , res ) => {
58
59
const person = await Person
59
60
. query ( )
60
- . findById ( req . params . id )
61
- . catch ( next ) ;
61
+ . findById ( req . params . id ) ;
62
62
63
63
if ( ! person ) {
64
- res . sendStatus ( 404 ) ;
64
+ throwNotFound ( ) ;
65
65
} else {
66
- await person
66
+ const child = await person
67
67
. $relatedQuery ( 'children' )
68
- . insert ( req . body )
69
- . then ( child => res . send ( child ) )
70
- . catch ( next ) ;
68
+ . insert ( req . body ) ;
69
+
70
+ res . send ( child ) ;
71
71
}
72
72
} ) ;
73
73
74
74
// Add a pet for a Person.
75
- app . post ( '/persons/:id/pets' , async function ( req , res , next ) {
75
+ router . post ( '/persons/:id/pets' , async ( req , res ) => {
76
76
const person = await Person
77
77
. query ( )
78
- . findById ( req . params . id )
79
- . catch ( next ) ;
78
+ . findById ( req . params . id ) ;
80
79
81
80
if ( ! person ) {
82
- res . sendStatus ( 404 ) ;
81
+ throwNotFound ( ) ;
83
82
} else {
84
- await person
83
+ const pet = await person
85
84
. $relatedQuery ( 'pets' )
86
- . insert ( req . body )
87
- . then ( pet => res . send ( pet ) )
88
- . catch ( next ) ;
85
+ . insert ( req . body ) ;
86
+
87
+ res . send ( pet ) ;
89
88
}
90
89
} ) ;
91
90
92
91
// Get a Person's pets. The result can be filtered using query parameters
93
92
// `name` and `species`.
94
- app . get ( '/persons/:id/pets' , async function ( req , res , next ) {
93
+ router . get ( '/persons/:id/pets' , async ( req , res ) => {
95
94
const person = await Person
96
95
. query ( )
97
- . findById ( req . params . id )
98
- . catch ( next ) ;
96
+ . findById ( req . params . id ) ;
99
97
100
98
if ( ! person ) {
101
- res . sendStatus ( 404 ) ;
99
+ throwNotFound ( ) ;
102
100
} else {
103
-
104
101
// We don't need to check for the existence of the query parameters because
105
102
// we call the `skipUndefined` method. It causes the query builder methods
106
103
// to do nothing if one of the values is undefined.
107
- return person
104
+ const pets = await person
108
105
. $relatedQuery ( 'pets' )
109
106
. skipUndefined ( )
110
107
. where ( 'name' , 'like' , req . query . name )
111
- . where ( 'species' , req . query . species )
112
- . then ( pets => res . send ( pets ) )
113
- . catch ( next ) ;
108
+ . where ( 'species' , req . query . species ) ;
109
+
110
+ res . send ( pets ) ;
114
111
}
115
112
} ) ;
116
113
117
114
// Add a movie for a Person.
118
- app . post ( '/persons/:id/movies' , function ( req , res , next ) {
115
+ router . post ( '/persons/:id/movies' , async ( req , res ) => {
119
116
// Inserting a movie for a person creates two queries: the movie insert query
120
117
// and the join table row insert query. It is wise to use a transaction here.
121
- return objection . transaction ( Person , async ( Person ) => {
118
+ const movie = await objection . transaction ( Person , async ( Person ) => {
122
119
const person = await Person
123
120
. query ( )
124
- . findById ( req . params . id )
125
- . catch ( next ) ;
121
+ . findById ( req . params . id ) ;
126
122
127
123
if ( ! person ) {
128
- res . sendStatus ( 404 ) ;
124
+ return throwNotFound ( ) ;
129
125
} else {
130
- await person
126
+ return person
131
127
. $relatedQuery ( 'movies' )
132
- . insert ( req . body )
133
- . then ( movie => res . send ( movie ) )
134
- . catch ( next ) ;
128
+ . insert ( req . body ) ;
135
129
}
136
130
} ) ;
131
+
132
+ res . send ( movie ) ;
137
133
} ) ;
138
134
139
135
// Add existing Person as an actor to a movie.
140
- app . post ( '/movies/:id/actors' , async function ( req , res , next ) {
136
+ router . post ( '/movies/:id/actors' , async ( req , res ) => {
141
137
const movie = await Movie
142
138
. query ( )
143
- . findById ( req . params . id )
144
- . catch ( next ) ;
139
+ . findById ( req . params . id ) ;
145
140
146
141
if ( ! movie ) {
147
- res . sendStatus ( 404 ) ;
142
+ throwNotFound ( ) ;
148
143
} else {
149
144
await movie
150
145
. $relatedQuery ( 'actors' )
151
- . relate ( req . body . id )
152
- . then ( ( ) => res . send ( req . body ) )
153
- . catch ( next ) ;
146
+ . relate ( req . body . id ) ;
147
+
148
+ res . send ( req . body ) ;
154
149
}
155
150
} ) ;
156
151
157
152
// Get Movie's actors.
158
- app . get ( '/movies/:id/actors' , async function ( req , res , next ) {
153
+ router . get ( '/movies/:id/actors' , async ( req , res ) => {
159
154
const movie = await Movie
160
155
. query ( )
161
- . findById ( req . params . id )
162
- . catch ( next ) ;
156
+ . findById ( req . params . id ) ;
163
157
164
158
if ( ! movie ) {
165
- res . sendStatus ( 404 ) ;
159
+ throwNotFound ( ) ;
166
160
} else {
167
- await movie
168
- . $relatedQuery ( 'actors' )
169
- . then ( movie => res . send ( movie ) )
170
- . catch ( next ) ;
161
+ const actors = await movie . $relatedQuery ( 'actors' ) ;
162
+ res . send ( actors ) ;
171
163
}
172
164
} ) ;
173
165
} ;
166
+
167
+ function throwNotFound ( ) {
168
+ const err : any = new Error ( ) ;
169
+ err . statusCode = 404 ;
170
+ throw err ;
171
+ }
0 commit comments