@@ -10,14 +10,15 @@ import {
1010 InstanceDestroyOptions ,
1111 InstanceRestoreOptions ,
1212 BulkCreateOptions ,
13+ FindAndCountOptions ,
1314} from 'sequelize' ;
1415import { Model , ModelCtor } from 'sequelize-typescript' ;
15- import { v7 as uuidv7 } from 'uuid ' ;
16+ import { randomUUID } from 'crypto ' ;
1617
1718export interface IRepositoryOptions < T extends Model > {
1819 autoGenerateId ?: boolean ;
1920 idField ?: Extract < keyof Attributes < T > , string > ;
20- idGenerator ?: ( ) => string ;
21+ idGenerator ?: ( ) => string | number ;
2122 logger ?: Logger ;
2223}
2324
@@ -46,6 +47,15 @@ export interface IRepository<TModel extends Model> {
4647 query ?: WhereOptions < Attributes < TModel > > ,
4748 options ?: Omit < FindOptions < Attributes < TModel > > , 'where' > ,
4849 ) : Promise < TModel [ ] > ;
50+ findAllPaginated (
51+ limit : number ,
52+ offset ?: number ,
53+ query ?: WhereOptions < Attributes < TModel > > ,
54+ options ?: Omit <
55+ FindAndCountOptions < Attributes < TModel > > ,
56+ 'where' | 'offset' | 'limit'
57+ > ,
58+ ) : Promise < { rows : TModel [ ] ; count : number } > ;
4959 updateByPk (
5060 primaryKey : string | number ,
5161 dto : Partial < Attributes < TModel > > ,
@@ -70,7 +80,7 @@ export class AbstractRepository<TModel extends Model>
7080 protected readonly logger : Logger ;
7181 protected readonly autoGenerateId : boolean ;
7282 protected readonly idField : string ;
73- protected readonly idGenerator : ( ) => string ;
83+ protected readonly idGenerator : ( ) => string | number ;
7484
7585 constructor (
7686 protected readonly model : ModelCtor < TModel > ,
@@ -80,7 +90,7 @@ export class AbstractRepository<TModel extends Model>
8090 autoGenerateId = false ,
8191 idField = 'id' ,
8292 logger = new Logger ( this . constructor . name ) ,
83- idGenerator = uuidv7 ,
93+ idGenerator = randomUUID ,
8494 } = options ;
8595
8696 if ( new . target === AbstractRepository ) {
@@ -185,6 +195,28 @@ export class AbstractRepository<TModel extends Model>
185195 }
186196 }
187197
198+ public async findAllPaginated (
199+ limit : number ,
200+ offset : number = 0 ,
201+ query ?: WhereOptions < Attributes < TModel > > ,
202+ options ?: Omit <
203+ FindAndCountOptions < Attributes < TModel > > ,
204+ 'where' | 'offset' | 'limit'
205+ > ,
206+ ) : Promise < { rows : TModel [ ] ; count : number } > {
207+ try {
208+ return await this . model . findAndCountAll ( {
209+ where : query ,
210+ limit,
211+ offset,
212+ ...options ,
213+ } ) ;
214+ } catch ( error ) {
215+ this . logger . error ( `findAllPaginated: ${ error } ` ) ;
216+ throw new InternalServerErrorException ( ) ;
217+ }
218+ }
219+
188220 public async updateByPk (
189221 primaryKey : string | number ,
190222 dto : Partial < Attributes < TModel > > ,
0 commit comments