-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(tables): typed predicate filter grammar, cursor pagination, and the v2 table surface #6067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
47492ab
feat(table): v2 table block — predicate grammar, unified predicate en…
TheodoreSpeaks 4c63dcf
Merge remote-tracking branch 'origin/staging' into feat/table-block-v2
TheodoreSpeaks d74c0c0
feat(table): switch v2 filter grammar to PostgREST strings + unbounde…
TheodoreSpeaks a048d02
Merge remote-tracking branch 'origin/staging' into feat/table-block-v2
TheodoreSpeaks da06034
feat(table): byte-bounded query drain (5MB) + PostgREST round-trip an…
TheodoreSpeaks 0696861
Merge remote-tracking branch 'origin/staging' into feat/table-block-v2
TheodoreSpeaks ded16c4
feat(table): predicate-object filter grammar + cursor pagination
TheodoreSpeaks 8f68622
Merge origin/staging into feat/table-block-v2
TheodoreSpeaks 5d11720
feat(table): gate the v2 tables API behind the tables-v2-api flag
TheodoreSpeaks 740ca2c
Merge origin/staging into feat/table-block-v2
TheodoreSpeaks 2106332
feat(table): ship table_v2 as a preview block, unhide v1 Table
TheodoreSpeaks 295d98f
fix(table): stop keyset paging from silently dropping unkeyed rows
TheodoreSpeaks de74b64
fix(table): close the filter paths that widen a bulk delete to the wh…
TheodoreSpeaks eaf4179
fix(table): reject ambiguous and mixed-grammar filter nodes
TheodoreSpeaks 9b5fe21
fix(table): system columns, rollout safety, and DoS bounds for v2
TheodoreSpeaks ead6391
fix(table): name the grammar mistake when a legacy $-filter reaches t…
TheodoreSpeaks 705ea6f
fix(table): reject hybrid predicate nodes at the wire instead of sile…
TheodoreSpeaks 0df4d58
fix(table): reject a v2 predicate at the legacy filter compiler
TheodoreSpeaks b3f3196
fix(table): treat a blank table_v2 filter/order editor field as absent
TheodoreSpeaks 1564fdd
Merge origin/staging into feat/table-block-v2
TheodoreSpeaks 4f9c104
fix(table): resolve select operands for contains/ncontains on multi-s…
TheodoreSpeaks e885854
fix(tools): stop normalizeToolId eating the _v2 version suffix
TheodoreSpeaks f2d964f
fix(table): make name→storage predicate translation one operation
TheodoreSpeaks 79d0dd0
Merge origin/staging into feat/table-block-v2
TheodoreSpeaks e268562
fix(merge): restore staging's env-flags exports dropped by the merge …
TheodoreSpeaks 1d50208
refactor(table): store saved views in the v2 predicate grammar
TheodoreSpeaks 2546a09
refactor(table): move the grid onto the v2 predicate grammar (Track C)
TheodoreSpeaks 3dfaaad
chore(tools): carry every Sim table op in the minimal dev registry
TheodoreSpeaks d62ad32
docs(tables): OpenAPI spec for the v2 tables read API
TheodoreSpeaks 9d8fa6c
fix(table): close the review findings on the dual-grammar boundaries
TheodoreSpeaks 3ebb639
fix(table): reject empty filter groups and bind cursors to their sort
TheodoreSpeaks c66ecef
fix(table): storage-validate async-route filters, reject dual-group n…
TheodoreSpeaks 808bc0d
fix(table): storage-validate read-path predicates on GET rows and find
TheodoreSpeaks 96d49b8
Merge remote-tracking branch 'origin/staging' into feat/table-block-v2
TheodoreSpeaks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| * | ||
| * v2 query route: predicate parsing, unconditional name→id translation | ||
| * (session auth included — the string grammar is name-keyed for every caller), | ||
| * cursor validation, and the response envelope. | ||
| */ | ||
| import { hybridAuthMockFns } from '@sim/testing' | ||
| import { NextRequest } from 'next/server' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import type { TableDefinition } from '@/lib/table/types' | ||
|
|
||
| const { mockCheckAccess, mockQueryRows, mockGate } = vi.hoisted(() => ({ | ||
| mockCheckAccess: vi.fn(), | ||
| mockQueryRows: vi.fn(), | ||
| mockGate: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/app/api/table/utils', async () => { | ||
| const { NextResponse } = await import('next/server') | ||
| return { | ||
| checkAccess: mockCheckAccess, | ||
| accessError: (result: { status: number }) => | ||
| NextResponse.json({ error: 'Access denied' }, { status: result.status }), | ||
| tablesV2GateError: mockGate, | ||
| } | ||
| }) | ||
|
|
||
| vi.mock('@/lib/table', async () => { | ||
| // row-wire pulls the column-keys helpers through this barrel. | ||
| const columnKeys = await import('@/lib/table/column-keys') | ||
| return { ...columnKeys } | ||
| }) | ||
|
|
||
| vi.mock('@/lib/table/rows/service', () => ({ | ||
| queryRows: mockQueryRows, | ||
| })) | ||
|
|
||
| import { encodeCursor } from '@/lib/table/rows/cursor' | ||
| import { POST } from '@/app/api/table/[tableId]/query/route' | ||
|
|
||
| function buildTable(): TableDefinition { | ||
| return { | ||
| id: 'tbl_1', | ||
| name: 'People', | ||
| description: null, | ||
| schema: { | ||
| columns: [ | ||
| { id: 'col_aaa', name: 'name', type: 'string' }, | ||
| { id: 'col_bbb', name: 'wins', type: 'number' }, | ||
| ], | ||
| }, | ||
| metadata: null, | ||
| rowCount: 0, | ||
| maxRows: 100, | ||
| workspaceId: 'workspace-1', | ||
| createdBy: 'user-1', | ||
| archivedAt: null, | ||
| createdAt: new Date('2024-01-01'), | ||
| updatedAt: new Date('2024-01-01'), | ||
| } | ||
| } | ||
|
|
||
| function authAs(authType: 'session' | 'internal_jwt') { | ||
| hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({ | ||
| success: true, | ||
| userId: 'user-1', | ||
| authType, | ||
| }) | ||
| } | ||
|
|
||
| function callQuery(body: Record<string, unknown>) { | ||
| const req = new NextRequest('http://localhost:3000/api/table/tbl_1/query', { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify(body), | ||
| }) | ||
| return POST(req, { params: Promise.resolve({ tableId: 'tbl_1' }) }) | ||
| } | ||
|
|
||
| const EMPTY_RESULT = { | ||
| rows: [], | ||
| rowCount: 0, | ||
| totalCount: 0, | ||
| limit: 0, | ||
| offset: 0, | ||
| nextCursor: null, | ||
| } | ||
|
|
||
| describe('POST /api/table/[tableId]/query', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockCheckAccess.mockResolvedValue({ ok: true, table: buildTable() }) | ||
| mockQueryRows.mockResolvedValue(EMPTY_RESULT) | ||
| mockGate.mockResolvedValue(null) | ||
| }) | ||
|
|
||
| it('returns 404 when the tables-v2-api flag is off', async () => { | ||
| const { NextResponse } = await import('next/server') | ||
| authAs('session') | ||
| mockGate.mockResolvedValue(NextResponse.json({ error: 'Not found' }, { status: 404 })) | ||
| const res = await callQuery({ workspaceId: 'workspace-1' }) | ||
| expect(res.status).toBe(404) | ||
| expect(mockQueryRows).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('runs the flag gate only after the access check, so it cannot leak a cohort oracle', async () => { | ||
| authAs('session') | ||
| mockCheckAccess.mockResolvedValue({ ok: false, status: 403 }) | ||
| const res = await callQuery({ workspaceId: 'workspace-1' }) | ||
| expect(res.status).toBe(403) | ||
| expect(mockGate).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('translates predicate/sort column names to storage ids for SESSION auth too', async () => { | ||
| authAs('session') | ||
| const res = await callQuery({ | ||
| workspaceId: 'workspace-1', | ||
| predicate: { | ||
| all: [ | ||
| { field: 'name', op: 'eq', value: 'John' }, | ||
| { field: 'wins', op: 'gte', value: 10 }, | ||
| ], | ||
| }, | ||
| sort: [{ field: 'wins', direction: 'desc' }], | ||
| }) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| const options = mockQueryRows.mock.calls[0][1] | ||
| expect(options.predicate).toEqual({ | ||
| all: [ | ||
| { field: 'col_aaa', op: 'eq', value: 'John' }, | ||
| { field: 'col_bbb', op: 'gte', value: 10 }, | ||
| ], | ||
| }) | ||
| expect(options.sort).toEqual({ col_bbb: 'desc' }) | ||
| expect(options.withExecutions).toBe(false) | ||
| }) | ||
|
|
||
| it('rejects a keyset cursor combined with a custom sort', async () => { | ||
| authAs('internal_jwt') | ||
| const cursor = encodeCursor({ | ||
| lastRow: { id: 'row_1', orderKey: 'a1' }, | ||
| keysetValid: true, | ||
| nextOffset: 1, | ||
| }) | ||
| const res = await callQuery({ | ||
| workspaceId: 'workspace-1', | ||
| sort: [{ field: 'wins', direction: 'desc' }], | ||
| cursor, | ||
| }) | ||
|
|
||
| expect(res.status).toBe(400) | ||
| const body = await res.json() | ||
| expect(body.error).toMatch(/not valid for a sorted query/) | ||
| expect(body.code).toBe('CURSOR_SORT_CONFLICT') | ||
| expect(mockQueryRows).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('returns 400 (not 500) for a cursor that decodes to a JSON primitive', async () => { | ||
| authAs('internal_jwt') | ||
| const res = await callQuery({ | ||
| workspaceId: 'workspace-1', | ||
| cursor: Buffer.from('42').toString('base64url'), | ||
| }) | ||
|
|
||
| expect(res.status).toBe(400) | ||
| const body = await res.json() | ||
| expect(body.error).toBe('Invalid cursor') | ||
| expect(body.code).toBe('INVALID_CURSOR') | ||
| }) | ||
|
|
||
| it('returns 400 for a predicate referencing an unknown column', async () => { | ||
| authAs('internal_jwt') | ||
| const res = await callQuery({ | ||
| workspaceId: 'workspace-1', | ||
| predicate: { all: [{ field: 'nope', op: 'eq', value: 1 }] }, | ||
| }) | ||
|
|
||
| expect(res.status).toBe(400) | ||
| expect((await res.json()).error).toMatch(/Unknown filter column/) | ||
| }) | ||
|
|
||
| it('passes nextCursor through the response envelope and skips the count on later pages', async () => { | ||
| authAs('internal_jwt') | ||
| mockQueryRows.mockResolvedValue({ ...EMPTY_RESULT, nextCursor: 'tok' }) | ||
| const cursor = encodeCursor({ | ||
| lastRow: { id: 'row_1', orderKey: 'a1' }, | ||
| keysetValid: true, | ||
| nextOffset: 1, | ||
| }) | ||
|
|
||
| const res = await callQuery({ workspaceId: 'workspace-1', cursor }) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| const body = await res.json() | ||
| expect(body.data.nextCursor).toBe('tok') | ||
| const options = mockQueryRows.mock.calls[0][1] | ||
| expect(options.includeTotal).toBe(false) | ||
| expect(options.after).toEqual({ orderKey: 'a1', id: 'row_1' }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.