@@ -71,151 +71,148 @@ function makePaginationQuery<T>(q: SelectQueryBuilder<T>): SelectQueryBuilder<T>
7171}
7272
7373AppDataSource . initialize ( ) . then ( async ( ) => {
74- // NOT OK
75- const BadInput = "1=1"
76-
7774 // Active record
78- await UserActiveRecord . findByName ( BadInput , "Saw " )
79-
75+ await UserActiveRecord . findByName ( "FirstNameToFind" , "LastNameToFind " )
76+
8077 // data mapper
8178 const selectQueryBuilder = makePaginationQuery < User > ( AppDataSource
8279 . createQueryBuilder ( User , "User" ) . select ( ) ) ;
83- selectQueryBuilder . where ( BadInput ) . getMany ( ) . then ( result => {
80+ selectQueryBuilder . where ( 'id > 5' ) . getMany ( ) . then ( result => {
8481 console . log ( result )
8582 } ) ;
86-
83+
8784 const selectQueryBuilder2 = makePaginationQuery < User > ( AppDataSource
8885 . createQueryBuilder ( User , "User" ) ) ;
89- selectQueryBuilder2 . where ( BadInput ) . getMany ( ) . then ( result => {
86+ selectQueryBuilder2 . where ( 'id > 5' ) . getMany ( ) . then ( result => {
9087 console . log ( result )
9188 } ) ;
92-
89+
9390 const insertQueryBuilder : InsertQueryBuilder < User2 > = AppDataSource
9491 . createQueryBuilder ( User2 , "User2" ) . insert ( ) ;
9592 insertQueryBuilder . into ( User2 )
9693 . values ( {
9794 firstName : "Timber" ,
98- lastName : ( ) => BadInput ,
95+ lastName : ( ) => "LastNameToFind" ,
9996 age : 33 ,
10097 } ) . execute ( ) . then ( result => {
101- console . log ( result )
102-
103-
104- } )
105-
98+ console . log ( result )
99+
100+
101+ } )
102+
106103 AppDataSource
107104 . createQueryBuilder ( User2 , "User" )
108105 . insert ( )
109106 . into ( User2 )
110107 . values ( {
111108 firstName : "Timber" ,
112- lastName : ( ) => BadInput ,
109+ lastName : ( ) => "LastNameToFind" ,
113110 age : 33 ,
114111 } )
115112 . orUpdate (
116- [ BadInput , BadInput ] ,
117- [ BadInput ] ,
113+ [ "firstName" , "lastName" ] ,
114+ [ "externalId" ] ,
118115 )
119116 . getQueryAndParameters ( )
120-
117+
121118 await AppDataSource . getRepository ( User2 ) . createQueryBuilder ( "user2" )
122119 . update ( User2 )
123- . set ( { firstName : ( ) => BadInput , lastName : "Saw2" , age : 12 } )
124- . where ( BadInput , )
120+ . set ( { firstName : ( ) => "firstname" , lastName : "Saw2" , age : 12 } )
121+ . where ( 'id > 5' )
125122 . execute ( )
126-
123+
127124 await AppDataSource . getRepository ( User2 ) . createQueryBuilder ( 'user2' )
128125 . delete ( )
129126 . from ( User2 )
130- . where ( BadInput )
127+ . where ( 'id > 5' )
131128 . execute ( )
132-
133-
129+
130+
134131 const queryRunner = AppDataSource . createQueryRunner ( )
135- await queryRunner . query ( BadInput )
136-
132+ await queryRunner . query ( 'SELECT name,id FROM table1 WHERE id > 5' )
133+
137134 await queryRunner . manager
138135 . createQueryBuilder ( User2 , "User" )
139- . select ( BadInput )
140- . where ( BadInput ) . execute ( )
141-
136+ . select ( "name,id" )
137+ . where ( "id > 5" ) . execute ( )
138+
142139 await AppDataSource
143140 . createQueryBuilder ( User , "User" )
144- . innerJoin ( "User.profile" , "profile" , BadInput , {
141+ . innerJoin ( "User.profile" , "profile" , 'id > 5' , {
145142 id : 2 ,
146143 } ) . getMany ( ) . then ( res => console . log ( res ) )
147-
144+
148145 await AppDataSource
149146 . createQueryBuilder ( User , "User" )
150- . leftJoinAndMapOne ( "User.profile" , "profile" , "profile" , BadInput , {
147+ . leftJoinAndMapOne ( "User.profile" , "profile" , "profile" , 'id > 5' , {
151148 id : 2 ,
152149 } ) . getMany ( ) . then ( res => console . log ( res ) )
153-
154-
150+
151+
155152 await AppDataSource
156153 . createQueryBuilder ( User2 , "User2" )
157154 . where ( ( qb ) => {
158155 const subQuery = qb
159156 . subQuery ( )
160- . select ( BadInput )
157+ . select ( "name,id" )
161158 . from ( User2 , "user2" )
162- . where ( BadInput )
159+ . where ( 'id > 5' )
163160 . getQuery ( )
164161 return "User2.id IN " + subQuery
165162 } )
166163 . setParameter ( "registered" , true )
167164 . getMany ( )
168-
169-
165+
166+
170167 // Using repository
171- let users = await AppDataSource . getRepository ( User2 ) . createQueryBuilder ( "User2" ) . where ( "User2.id =:kind" + BadInput , { kind : 1 } ) . getMany ( )
172-
168+ let users = await AppDataSource . getRepository ( User2 ) . createQueryBuilder ( "User2" ) . where ( "User2.id =:kind" , { kind : 1 } ) . getMany ( )
169+
173170 // Using DataSource
174171 users = await AppDataSource
175172 . createQueryBuilder ( )
176173 . select ( "User2" )
177174 . from ( User2 , "User2" )
178- . where ( BadInput , { id : 1 } )
175+ . where ( 'id > 5' , { id : 1 } )
179176 . getMany ( )
180-
177+
181178 // Using entity manager
182179 await AppDataSource . manager
183- . createQueryBuilder ( User2 , "User2" ) . where ( "User2.id =:kind" + BadInput , { kind : '1' } ) . getMany ( )
180+ . createQueryBuilder ( User2 , "User2" ) . where ( "User2.id =:kind and id > 5" , { kind : '1' } ) . getMany ( )
184181 await AppDataSource
185182 . createQueryBuilder ( User2 , "User2" )
186- . leftJoinAndSelect ( "user.photos" , "photo" , BadInput ) . getMany ( )
183+ . leftJoinAndSelect ( "user.photos" , "photo" , 'id > 5' ) . getMany ( )
187184 await AppDataSource
188- . createQueryBuilder ( User2 , "User2" ) . groupBy ( "User2.id" ) . having ( BadInput ) . getMany ( )
185+ . createQueryBuilder ( User2 , "User2" ) . groupBy ( "User2.id" ) . having ( 'id > 5' ) . getMany ( )
189186 // orderBy
190187 // it is a little bit restrictive, e.g. sqlite don't support it at all
191188 await AppDataSource
192- . createQueryBuilder ( User2 , "User2" ) . where ( BadInput , {
189+ . createQueryBuilder ( User2 , "User2" ) . where ( 'id > 5' , {
193190 firstName : "Timber" ,
194191 } )
195192 . where (
196193 new Brackets ( ( qb ) => {
197- qb . where ( BadInput ) . orWhere ( BadInput ) ;
194+ qb . where ( 'id > 5' ) . orWhere ( 'id > 5' ) ;
198195 } )
199196 )
200- . orderBy ( BadInput ) . orWhere ( BadInput ) . getMany ( )
201-
197+ . orderBy ( "name" ) . orWhere ( 'id > 5' ) . getMany ( )
198+
202199 // relation
203200 AppDataSource . createQueryBuilder ( ) . relation ( User , "name" )
204201 . of ( User )
205- . select ( ) . where ( BadInput ) . getMany ( ) . then ( results => {
206- console . log ( results )
207- } )
208-
202+ . select ( ) . where ( 'id > 5' ) . getMany ( ) . then ( results => {
203+ console . log ( results )
204+ } )
205+
209206 // Brackets
210207 await AppDataSource . createQueryBuilder ( User2 , "User2" )
211- . where ( BadInput )
208+ . where ( 'id > 5' )
212209 . andWhere (
213210 new Brackets ( ( qb ) => {
214- qb . where ( BadInput ) . orWhere ( BadInput ) ;
211+ qb . where ( 'id > 5' ) . orWhere ( 'id > 5' ) ;
215212 } )
216213 ) . andWhere (
217214 new NotBrackets ( ( qb ) => {
218- qb . where ( BadInput ) . orWhere ( BadInput )
215+ qb . where ( 'id > 5' ) . orWhere ( 'id > 5' )
219216 } ) ,
220217 ) . getMany ( )
221218} ) . catch ( error => console . log ( error ) )
0 commit comments