MCPcopy Index your code
hub / github.com/simstudioai/sim / resolveInsertOrderKey

Function resolveInsertOrderKey

apps/sim/lib/table/rows/ordering.ts:191–222  ·  view source on GitHub ↗
(
  trx: DbTransaction,
  tableId: string,
  requestedPosition?: number
)

Source from the content-addressed store, hash-verified

189 * Caller holds the row-order lock.
190 */
191export 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 —

Callers 3

upsertRowFunction · 0.90
insertOrderedRowFunction · 0.85

Calls 4

isFeatureEnabledFunction · 0.90
keyBetweenFunction · 0.90
maxOrderKeyFunction · 0.85
orderKeyAtSlotFunction · 0.85

Tested by

no test coverage detected