RobertS
RobertS
DTDrizzle Team
Created by RobertS on 5/8/2024 in #help
Pagination proper TypeScript return types generics
Otherwise it will be recursive calls to itself. This one works but I would like to be able to specify explicit return types to have controll over it
export class MyRepository extends BaseRepository<typeof users> {
constructor(@Inject(PG_CONNECTION) private db: NodePgDatabase<typeof schema>) {
super(users, db)
}
async findAll() {
const query = super.findAll()
const result = await query
return result
}
}

@Injectable()
export class BaseRepository<T extends PgTable> {
constructor(
protected readonly schema: T,
protected readonly db: NodePgDatabase,
) {}

findAll() {
const query = this.db.select().from(this.schema).where(eq(this.schema.id, 1))
const dynamicQuery = query.$dynamic()
return this.withPagination(dynamicQuery)
// return await this.withPagination(dynamicQuery, 1)
}

async withPagination<M extends PgSelect>(qb: M, 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 class MyRepository extends BaseRepository<typeof users> {
constructor(@Inject(PG_CONNECTION) private db: NodePgDatabase<typeof schema>) {
super(users, db)
}
async findAll() {
const query = super.findAll()
const result = await query
return result
}
}

@Injectable()
export class BaseRepository<T extends PgTable> {
constructor(
protected readonly schema: T,
protected readonly db: NodePgDatabase,
) {}

findAll() {
const query = this.db.select().from(this.schema).where(eq(this.schema.id, 1))
const dynamicQuery = query.$dynamic()
return this.withPagination(dynamicQuery)
// return await this.withPagination(dynamicQuery, 1)
}

async withPagination<M extends PgSelect>(qb: M, 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,
}
}
}
3 replies