Files
speckle-server/packages/server/modules/blobstorage/helpers/db.ts
T
Kristaps Fabians Geikins 3ccf9c6aea chore(server): blobstorage IoC 4 - getBlobMetadataFactory (#2960)
* chore(server): blobstorage IoC 4 - getBlobMetadataFactory

* tests fix

* chore(server): blobstorage IoC 5 - getBlobMetadataCollectionFactory

---------

Co-authored-by: Alessandro Magionami <alessandro.magionami@gmail.com>
2024-09-12 13:02:18 +03:00

26 lines
732 B
TypeScript

import { BadRequestError } from '@/modules/shared/errors'
export const cursorFromRows = <Row, Target extends keyof Row>(
rows: Array<Row>,
cursorTarget: Target
) => {
if (rows?.length > 0) {
const lastRow = rows[rows.length - 1]
const cursor = lastRow[cursorTarget]
if (!(cursor instanceof Date))
throw new BadRequestError('The cursor target is not a date object')
return Buffer.from(cursor.toISOString()).toString('base64')
} else {
return null
}
}
export const decodeCursor = (cursor: string) => {
const decoded = Buffer.from(cursor, 'base64').toString()
if (isNaN(Date.parse(decoded)))
throw new BadRequestError('The cursor is not a base64 encoded date string')
return decoded
}