Skip to content

Commit 6c3edef

Browse files
feat(tables): add order_key column, fractional-indexing util, and ordering flag (off)
1 parent ddab1aa commit 6c3edef

9 files changed

Lines changed: 18155 additions & 1 deletion

File tree

apps/sim/lib/core/config/env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const env = createEnv({
6868
ENTERPRISE_TIER_COST_LIMIT: z.number().optional(), // Cost limit for enterprise tier users
6969
ENTERPRISE_STORAGE_LIMIT_GB: z.number().optional().default(500), // Default storage limit in GB for enterprise tier (can be overridden per org)
7070
BILLING_ENABLED: z.boolean().optional(), // Enable billing enforcement and usage tracking
71+
TABLES_FRACTIONAL_ORDERING: z.boolean().optional(), // Order table rows by fractional order_key (O(1) insert/delete) instead of integer position
7172

7273
// Table feature limits (per plan). Apply when billing is disabled (free tier defaults) or for billed plans.
7374
FREE_TABLES_LIMIT: z.number().optional(), // Max user tables per workspace on free tier (default: 3)

apps/sim/lib/core/config/feature-flags.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ export const isHosted = appHostname === 'sim.ai' || appHostname.endsWith('.sim.a
3636
*/
3737
export const isBillingEnabled = isTruthy(env.BILLING_ENABLED)
3838

39+
/**
40+
* Order table rows by fractional `order_key` (O(1) insert/delete) instead of the
41+
* legacy integer `position`. When off, behavior is unchanged. Keys are written
42+
* regardless of this flag; it only controls which column is authoritative for
43+
* reads/ordering and whether inserts/deletes reshift positions.
44+
*/
45+
export const isTablesFractionalOrderingEnabled = isTruthy(env.TABLES_FRACTIONAL_ORDERING)
46+
3947
/**
4048
* Is email verification enabled
4149
*/

apps/sim/lib/table/order-key.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Fractional order keys for table-row ordering.
3+
*
4+
* A row's order is a base-62 string key, not an integer position. Inserting
5+
* between two rows mints a key strictly between their keys, so no other row's
6+
* key changes — insert and delete become O(1) (no position reshift / recompact).
7+
*
8+
* Thin wrapper over `fractional-indexing` (Figma/rocicorp algorithm) so the
9+
* implementation is swappable. Keys never run out (variable-length strings);
10+
* the only cost is gradual length growth under repeated same-spot inserts.
11+
*/
12+
13+
import { generateKeyBetween, generateNKeysBetween } from 'fractional-indexing'
14+
15+
/**
16+
* Returns a key that sorts strictly between `a` and `b`. Pass `null` for an open
17+
* end: `keyBetween(null, first)` prepends, `keyBetween(last, null)` appends,
18+
* `keyBetween(null, null)` is the first key in an empty table.
19+
*
20+
* @throws if `a >= b` (callers must pass ordered, distinct bounds)
21+
*/
22+
export function keyBetween(a: string | null, b: string | null): string {
23+
return generateKeyBetween(a, b)
24+
}
25+
26+
/**
27+
* Returns `n` keys evenly spaced strictly between `a` and `b` (same open-end
28+
* semantics as {@link keyBetween}). Used for batch inserts and the backfill
29+
* (`nKeysBetween(null, null, count)` mints an ordered run for an empty range).
30+
*/
31+
export function nKeysBetween(a: string | null, b: string | null, n: number): string[] {
32+
return generateNKeysBetween(a, b, n)
33+
}

apps/sim/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
"es-toolkit": "1.45.1",
133133
"ffmpeg-static": "5.3.0",
134134
"fluent-ffmpeg": "2.1.3",
135+
"fractional-indexing": "3.2.0",
135136
"framer-motion": "^12.5.0",
136137
"free-email-domains": "1.2.25",
137138
"google-auth-library": "10.5.0",

bun.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE "user_table_rows" ADD COLUMN "order_key" text;--> statement-breakpoint
2+
CREATE INDEX "user_table_rows_table_order_key_idx" ON "user_table_rows" USING btree ("table_id","order_key","id");

0 commit comments

Comments
 (0)