(
key: string,
context?: StorageContext,
options?: { includeDeleted?: boolean }
)
| 154 | * Get file metadata by key with optional context filter |
| 155 | */ |
| 156 | export async function getFileMetadataByKey( |
| 157 | key: string, |
| 158 | context?: StorageContext, |
| 159 | options?: { includeDeleted?: boolean } |
| 160 | ): Promise<FileMetadataRecord | null> { |
| 161 | const { includeDeleted = false } = options ?? {} |
| 162 | const conditions = [eq(workspaceFiles.key, key)] |
| 163 | |
| 164 | if (context) { |
| 165 | conditions.push(eq(workspaceFiles.context, context)) |
| 166 | } |
| 167 | |
| 168 | if (!includeDeleted) { |
| 169 | conditions.push(isNull(workspaceFiles.deletedAt)) |
| 170 | } |
| 171 | |
| 172 | const [record] = await db |
| 173 | .select() |
| 174 | .from(workspaceFiles) |
| 175 | .where(conditions.length > 1 ? and(...conditions) : conditions[0]) |
| 176 | // Prefer the active row when includeDeleted lets both an active and a |
| 177 | // soft-deleted row for the same key match. |
| 178 | .orderBy(sql`${workspaceFiles.deletedAt} IS NULL DESC`) |
| 179 | .limit(1) |
| 180 | |
| 181 | return record ?? null |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Get active (non-deleted) file metadata for multiple keys in a single query. |
no test coverage detected