export type PaginationResult<T> = {
data: T
page: number
pageSize: number
}
export class MyRepository extends BaseRepository<typeof users> {
constructor() {
super(users)
}
async findAll() {
const users = await super.findAll()
users.data[0] // no proper suggestion here
return users
}
}
@Injectable()
export class BaseRepository<T extends PgTable> {
constructor(protected readonly schema: T) {}
async findAll() {
const query = db.select().from(this.schema).where(eq(this.schema.id, 1))
const dynamicQuery = query.$dynamic()
return await this.withPagination(dynamicQuery, 1)
}
async withPagination<T extends PgSelect>(
qb: T,
orderByColumn: PgColumn | SQL | SQL.Aliased,
page = 1,
pageSize = 3,
) {
const data = await qb
.orderBy(orderByColumn)
.limit(pageSize)
.offset((page - 1) * pageSize)
return {
data,
page,
pageSize,
totalCount: 1000,
}
}
}
export type PaginationResult<T> = {
data: T
page: number
pageSize: number
}
export class MyRepository extends BaseRepository<typeof users> {
constructor() {
super(users)
}
async findAll() {
const users = await super.findAll()
users.data[0] // no proper suggestion here
return users
}
}
@Injectable()
export class BaseRepository<T extends PgTable> {
constructor(protected readonly schema: T) {}
async findAll() {
const query = db.select().from(this.schema).where(eq(this.schema.id, 1))
const dynamicQuery = query.$dynamic()
return await this.withPagination(dynamicQuery, 1)
}
async withPagination<T extends PgSelect>(
qb: T,
orderByColumn: PgColumn | SQL | SQL.Aliased,
page = 1,
pageSize = 3,
) {
const data = await qb
.orderBy(orderByColumn)
.limit(pageSize)
.offset((page - 1) * pageSize)
return {
data,
page,
pageSize,
totalCount: 1000,
}
}
}