(
tableId: string,
options?: { includeArchived?: boolean; tx?: DbOrTx }
)
| 113 | * @returns Table definition or null if not found |
| 114 | */ |
| 115 | export async function getTableById( |
| 116 | tableId: string, |
| 117 | options?: { includeArchived?: boolean; tx?: DbOrTx } |
| 118 | ): Promise<TableDefinition | null> { |
| 119 | const { includeArchived = false, tx } = options ?? {} |
| 120 | const executor = tx ?? db |
| 121 | const results = await executor |
| 122 | .select({ |
| 123 | id: userTableDefinitions.id, |
| 124 | name: userTableDefinitions.name, |
| 125 | description: userTableDefinitions.description, |
| 126 | schema: userTableDefinitions.schema, |
| 127 | metadata: userTableDefinitions.metadata, |
| 128 | maxRows: userTableDefinitions.maxRows, |
| 129 | workspaceId: userTableDefinitions.workspaceId, |
| 130 | createdBy: userTableDefinitions.createdBy, |
| 131 | archivedAt: userTableDefinitions.archivedAt, |
| 132 | createdAt: userTableDefinitions.createdAt, |
| 133 | updatedAt: userTableDefinitions.updatedAt, |
| 134 | rowCount: userTableDefinitions.rowCount, |
| 135 | }) |
| 136 | .from(userTableDefinitions) |
| 137 | .where( |
| 138 | includeArchived |
| 139 | ? eq(userTableDefinitions.id, tableId) |
| 140 | : and(eq(userTableDefinitions.id, tableId), isNull(userTableDefinitions.archivedAt)) |
| 141 | ) |
| 142 | .limit(1) |
| 143 | |
| 144 | if (results.length === 0) return null |
| 145 | |
| 146 | const table = results[0] |
| 147 | const metadata = (table.metadata as TableMetadata) ?? null |
| 148 | const { pendingDeleteRemaining, ...jobFields } = await latestJobForTable(tableId, executor) |
| 149 | return { |
| 150 | id: table.id, |
| 151 | name: table.name, |
| 152 | description: table.description, |
| 153 | schema: applyColumnOrderToSchema(table.schema as TableSchema, metadata), |
| 154 | metadata, |
| 155 | rowCount: Math.max(0, table.rowCount - pendingDeleteRemaining), |
| 156 | maxRows: table.maxRows, |
| 157 | workspaceId: table.workspaceId, |
| 158 | createdBy: table.createdBy, |
| 159 | archivedAt: table.archivedAt, |
| 160 | createdAt: table.createdAt, |
| 161 | updatedAt: table.updatedAt, |
| 162 | ...jobFields, |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Lists all tables in a workspace. |
no test coverage detected