updates and fixes

* seo friendly urls
* custom client serve-static module
* database fixes
* fix recent pages
* other fixes
This commit is contained in:
Philipinho
2024-05-18 03:19:42 +01:00
parent eefe63d1cd
commit 9c7c2f1163
102 changed files with 921 additions and 536 deletions
@@ -0,0 +1,69 @@
// adapted from https://github.com/charlie-hadden/kysely-paginate/blob/main/src/offset.ts - MIT
import { SelectQueryBuilder, StringReference, sql } from 'kysely';
export type PaginationMeta = {
limit: number;
page: number;
hasNextPage: boolean;
hasPrevPage: boolean;
};
export type PaginationResult<T> = {
items: T[];
meta: PaginationMeta;
};
export async function executeWithPagination<O, DB, TB extends keyof DB>(
qb: SelectQueryBuilder<DB, TB, O>,
opts: {
perPage: number;
page: number;
experimental_deferredJoinPrimaryKey?: StringReference<DB, TB>;
},
): Promise<PaginationResult<O>> {
if (opts.page < 1) {
opts.page = 1;
}
qb = qb.limit(opts.perPage + 1).offset((opts.page - 1) * opts.perPage);
const deferredJoinPrimaryKey = opts.experimental_deferredJoinPrimaryKey;
if (deferredJoinPrimaryKey) {
const primaryKeys = await qb
.clearSelect()
.select((eb) => eb.ref(deferredJoinPrimaryKey).as('primaryKey'))
.execute()
// @ts-expect-error TODO: Fix the type here later
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
.then((rows) => rows.map((row) => row.primaryKey));
qb = qb
.where((eb) =>
primaryKeys.length > 0
? // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
eb(deferredJoinPrimaryKey, 'in', primaryKeys as any)
: eb(sql`1`, '=', 0),
)
.clearOffset()
.clearLimit();
}
const rows = await qb.execute();
const hasNextPage = rows.length > 0 ? rows.length > opts.perPage : false;
const hasPrevPage = rows.length > 0 ? opts.page > 1 : false;
// If we fetched an extra row to determine if we have a next page, that
// shouldn't be in the returned results
if (rows.length > opts.perPage) {
rows.pop();
}
return {
items: rows,
meta: {
limit: opts.perPage,
page: opts.page,
hasNextPage,
hasPrevPage,
},
};
}