1- import { EDatabaseQueryFilterOperator , EDatabaseTypes , IDatabaseField , IDatabaseQueryFilter , IDatabaseQueryFilterExpression } from '../interfaces' ;
1+ import { EDatabaseQueryFilterOperator , EDatabaseTypes , IDatabaseField , IDatabaseOrderBy , IDatabaseQueryFilter , IDatabaseQueryFilterExpression } from '../interfaces' ;
22import { DatabaseConnection } from '../connection' ;
33import { DatabaseException } from '../errors' ;
44
@@ -157,10 +157,11 @@ export class BaseModel {
157157 * @returns The data found
158158 * @throws [{@link DatabaseException}]
159159 */
160- public async find ( query ?: Record < string , any > , limit ?: number ) : Promise < Record < string , unknown > [ ] > {
160+ public async find ( params : { query ?: Record < string , any > , limit ?: number , orderBy ?: IDatabaseOrderBy } ) : Promise < Record < string , unknown > [ ] > {
161+ const { query, limit, orderBy } = params || { } ;
161162 this . checkIsReady ( ) ;
162- return this . connection ! . read ( '*' , this . name ! ,
163- query
163+ return this . connection ! . read ( { keys : '*' , database : this . name ! ,
164+ filter : ( query
164165 ? {
165166 type : 'AND' ,
166167 filters : Object . keys ( query ) . map ( ( fieldKey ) => ( {
@@ -169,9 +170,10 @@ export class BaseModel {
169170 value : query [ fieldKey ] ,
170171 } ) ) ,
171172 }
172- : undefined ,
173+ : undefined ) ,
173174 limit,
174- ) ;
175+ orderBy,
176+ } ) ;
175177 }
176178
177179 /**
@@ -181,7 +183,7 @@ export class BaseModel {
181183 * @throws [{@link DatabaseException}]
182184 */
183185 public async findOne ( query ?: Record < string , any > ) : Promise < Record < string , unknown > | undefined > {
184- return Promise . resolve ( ( await this . find ( query , 1 ) ) [ 0 ] ) ;
186+ return Promise . resolve ( ( await this . find ( { query, limit : 1 } ) ) [ 0 ] ) ;
185187 }
186188
187189 /**
@@ -203,11 +205,14 @@ export class BaseModel {
203205 * @returns The data found
204206 * @throws [{@link DatabaseException}]
205207 */
206- public async select ( fields : string [ ] , filter ?: IDatabaseQueryFilterExpression , limit ?: number ) : Promise < Record < string , unknown > [ ] > {
208+ public async select ( params : { fields : string [ ] , query ?: Record < string , any > , filter ?: IDatabaseQueryFilterExpression , limit ?: number , orderBy ?: IDatabaseOrderBy } ,
209+ ) : Promise < Record < string , unknown > [ ] > {
207210 this . checkIsReady ( ) ;
211+ if ( ! params ) throw new DatabaseException ( 'Missing params for \'select\' method call, if you want to retrieve all data on this table call \'find\' instead.' ) ;
212+ const { fields, filter, limit, orderBy } = params ;
208213 this . fieldsCheck ( fields ) ;
209214 this . filterCheck ( filter ) ;
210- return this . connection ! . read ( fields , this . name ! , filter ?? undefined , limit ) ;
215+ return this . connection ! . read ( { keys : fields , database : this . name ! , filter, limit, orderBy } ) ;
211216 }
212217
213218 /**
@@ -218,7 +223,7 @@ export class BaseModel {
218223 * @throws [{@link DatabaseException}]
219224 */
220225 public async selectOne ( fields : string [ ] , filter ?: IDatabaseQueryFilterExpression ) : Promise < Record < string , unknown > | undefined > {
221- return ( await this . select ( fields , filter , 1 ) ) [ 0 ] ;
226+ return ( await this . select ( { fields, filter, limit : 1 } ) ) [ 0 ] ;
222227 }
223228
224229 /**
0 commit comments