| 1 | import { type SQL, sql } from 'drizzle-orm'; |
| 2 | import { toSnakeCase } |
| 3 | import type { AnyPgColumn |
| 4 | import { customAlphabet } |
| 5 | import slugify from 'slugify |
| 6 | |
| 7 | const slugAlphabet = customAlphabet |
| 8 | '_-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', |
| 9 | 10, |
| 10 | ); |
| 11 | |
| 12 | export function titleToSlug(title |
| 13 | const slugified = |
| 14 | const slugifiedWithSuffix = |
| 15 | return { |
| 16 | slugified, |
| 17 | slugifiedWithSuffix, |
| 18 | }; |
| 19 | } |
| 20 | |
| 21 | // Opaque keyset cursor: a (timestamp, id) pair, base64url encoded so callers treat it as a token instead of something they can hand-build. |
| 22 | export function encodeCursor({ |
| 23 | return Buffer.from |
| 24 | } |
| 25 | |
| 26 | export function decodeCursor(cursor |
| 27 | const [timestamp, |
| 28 | .toString('utf8') |
| 29 | .split('|') |
| 30 | if (!timestamp || |
| 31 | const date = |
| 32 | if (Number.isNaN |
| 33 | return { date, |
| 34 | } |
| 35 | |
| 36 | // Timestamps leave the database already in the shape responses promise, so nothing downstream has to convert them. The format matches `Date.prototype.toISOString` byte for byte. |
| 37 | export function isoTimestamp(column |
| 38 | return sql<string |
| 39 | } |
| 40 | |
| 41 | /** The value an upsert tried to insert, for `onConflictDoUpdate`. The database names columns in snake_case, however the schema spells them. */ |
| 42 | export function excluded(column |
| 43 | return sql.raw |
| 44 | } |
| 45 | |
| 46 | // For `ilike`: a search for "50%" or "a_b" matches those characters, not any. |
| 47 | export function escapeLike(value |
| 48 | return value.replace |
| 49 | } |