( conditions: SQL<unknown>[], limit: number, cursor?: string )
| 145 | } |
| 146 | |
| 147 | export async function queryAuditLogs( |
| 148 | conditions: SQL<unknown>[], |
| 149 | limit: number, |
| 150 | cursor?: string |
| 151 | ): Promise<CursorPaginatedResult> { |
| 152 | const allConditions = [...conditions] |
| 153 | |
| 154 | if (cursor) { |
| 155 | const cursorCondition = buildCursorCondition(cursor) |
| 156 | if (cursorCondition) allConditions.push(cursorCondition) |
| 157 | } |
| 158 | |
| 159 | const rows = await dbReplica |
| 160 | .select() |
| 161 | .from(auditLog) |
| 162 | .where(allConditions.length > 0 ? and(...allConditions) : undefined) |
| 163 | .orderBy(desc(auditLog.createdAt), desc(auditLog.id)) |
| 164 | .limit(limit + 1) |
| 165 | |
| 166 | const hasMore = rows.length > limit |
| 167 | const data = rows.slice(0, limit) |
| 168 | |
| 169 | let nextCursor: string | undefined |
| 170 | if (hasMore && data.length > 0) { |
| 171 | const last = data[data.length - 1] |
| 172 | nextCursor = encodeCursor({ |
| 173 | createdAt: last.createdAt.toISOString(), |
| 174 | id: last.id, |
| 175 | }) |
| 176 | } |
| 177 | |
| 178 | return { data, nextCursor } |
| 179 | } |
no test coverage detected