(
workspaceId: string,
options?: { scope?: TableScope }
)
| 170 | * @returns Array of table definitions |
| 171 | */ |
| 172 | export async function listTables( |
| 173 | workspaceId: string, |
| 174 | options?: { scope?: TableScope } |
| 175 | ): Promise<TableDefinition[]> { |
| 176 | const { scope = 'active' } = options ?? {} |
| 177 | const tables = await db |
| 178 | .select({ |
| 179 | id: userTableDefinitions.id, |
| 180 | name: userTableDefinitions.name, |
| 181 | description: userTableDefinitions.description, |
| 182 | schema: userTableDefinitions.schema, |
| 183 | metadata: userTableDefinitions.metadata, |
| 184 | maxRows: userTableDefinitions.maxRows, |
| 185 | workspaceId: userTableDefinitions.workspaceId, |
| 186 | createdBy: userTableDefinitions.createdBy, |
| 187 | archivedAt: userTableDefinitions.archivedAt, |
| 188 | createdAt: userTableDefinitions.createdAt, |
| 189 | updatedAt: userTableDefinitions.updatedAt, |
| 190 | rowCount: userTableDefinitions.rowCount, |
| 191 | }) |
| 192 | .from(userTableDefinitions) |
| 193 | .where( |
| 194 | scope === 'all' |
| 195 | ? eq(userTableDefinitions.workspaceId, workspaceId) |
| 196 | : scope === 'archived' |
| 197 | ? and( |
| 198 | eq(userTableDefinitions.workspaceId, workspaceId), |
| 199 | sql`${userTableDefinitions.archivedAt} IS NOT NULL` |
| 200 | ) |
| 201 | : and( |
| 202 | eq(userTableDefinitions.workspaceId, workspaceId), |
| 203 | isNull(userTableDefinitions.archivedAt) |
| 204 | ) |
| 205 | ) |
| 206 | .orderBy(userTableDefinitions.createdAt) |
| 207 | |
| 208 | const jobsByTable = await latestJobsForTables(tables.map((t) => t.id)) |
| 209 | |
| 210 | return tables.map((t) => { |
| 211 | const metadata = (t.metadata as TableMetadata) ?? null |
| 212 | const { pendingDeleteRemaining, ...jobFields } = jobsByTable.get(t.id) ?? EMPTY_JOB_FIELDS |
| 213 | return { |
| 214 | id: t.id, |
| 215 | name: t.name, |
| 216 | description: t.description, |
| 217 | schema: applyColumnOrderToSchema(t.schema as TableSchema, metadata), |
| 218 | metadata, |
| 219 | rowCount: Math.max(0, t.rowCount - pendingDeleteRemaining), |
| 220 | maxRows: t.maxRows, |
| 221 | workspaceId: t.workspaceId, |
| 222 | createdBy: t.createdBy, |
| 223 | archivedAt: t.archivedAt, |
| 224 | createdAt: t.createdAt, |
| 225 | updatedAt: t.updatedAt, |
| 226 | ...jobFields, |
| 227 | } |
| 228 | }) |
| 229 | } |
no test coverage detected