( trx: DbTransaction, tableId: string, requestedPosition?: number )
| 189 | * Caller holds the row-order lock. |
| 190 | */ |
| 191 | export async function resolveInsertOrderKey( |
| 192 | trx: DbTransaction, |
| 193 | tableId: string, |
| 194 | requestedPosition?: number |
| 195 | ): Promise<string> { |
| 196 | const fractionalOrdering = await isFeatureEnabled('tables-fractional-ordering') |
| 197 | const orderKeyAtSlot = async (slot: number): Promise<string | null> => { |
| 198 | if (slot < 0) return null |
| 199 | if (fractionalOrdering) { |
| 200 | const [r] = await trx |
| 201 | .select({ orderKey: userTableRows.orderKey }) |
| 202 | .from(userTableRows) |
| 203 | .where(eq(userTableRows.tableId, tableId)) |
| 204 | .orderBy(asc(userTableRows.orderKey), asc(userTableRows.id)) |
| 205 | .limit(1) |
| 206 | .offset(slot) |
| 207 | return r?.orderKey ?? null |
| 208 | } |
| 209 | const [r] = await trx |
| 210 | .select({ orderKey: userTableRows.orderKey }) |
| 211 | .from(userTableRows) |
| 212 | .where(and(eq(userTableRows.tableId, tableId), eq(userTableRows.position, slot))) |
| 213 | .limit(1) |
| 214 | return r?.orderKey ?? null |
| 215 | } |
| 216 | if (requestedPosition === undefined) { |
| 217 | return keyBetween(await maxOrderKey(trx, tableId), null) |
| 218 | } |
| 219 | const lo = await orderKeyAtSlot(requestedPosition - 1) |
| 220 | const hi = await orderKeyAtSlot(requestedPosition) |
| 221 | return keyBetween(lo, hi) |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Resolves the `order_key` for an insert expressed by an anchor row id — |
no test coverage detected