Skip to content
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

feat(zql): fix IS to take null, fix Row to take Query #3459

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions apps/zbugs/src/pages/issue/comment.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {Row} from '@rocicorp/zero';
import classNames from 'classnames';
import {memo, useState} from 'react';
import type {Schema} from '../../../schema.js';
import {makePermalink} from '../../comment-permalink.js';
import {Button} from '../../components/button.js';
import {CanEdit} from '../../components/can-edit.js';
Expand All @@ -10,21 +9,17 @@ import {EmojiPanel} from '../../components/emoji-panel.js';
import {Link} from '../../components/link.js';
import {Markdown} from '../../components/markdown.js';
import {RelativeTime} from '../../components/relative-time.js';
import {type Emoji} from '../../emoji-utils.js';
import {useHash} from '../../hooks/use-hash.js';
import {useLogin} from '../../hooks/use-login.js';
import {useZero} from '../../hooks/use-zero.js';
import {CommentComposer} from './comment-composer.js';
import style from './comment.module.css';
import type {commentQuery} from './issue-page.js';

type Props = {
id: string;
issueID: string;
comment: Row<Schema['tables']['comment']> & {
readonly creator: Row<Schema['tables']['user']> | undefined;
} & {
readonly emoji: readonly Emoji[];
};
comment: Row<ReturnType<typeof commentQuery>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe export the type instead?

/**
* Height of the comment. Used to keep the layout stable when comments are
* being "loaded".
Expand Down
20 changes: 12 additions & 8 deletions apps/zbugs/src/pages/issue/issue-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@
// to load.
export const INITIAL_COMMENT_LIMIT = 101;

export function commentQuery(z: Zero<Schema>, displayed: IssueRow | undefined) {

Check warning on line 64 in apps/zbugs/src/pages/issue/issue-page.tsx

View workflow job for this annotation

GitHub Actions / Lint

Fast refresh only works when a file only exports components. Use a new file to share constants or functions between components
return z.query.comment
.where('issueID', 'IS', displayed?.id ?? null)
.related('creator', creator => creator.one())
.related('emoji', emoji =>
emoji.related('creator', creator => creator.one()),
)
.orderBy('created', 'asc')
.orderBy('id', 'asc');
}

export function IssuePage({onReady}: {onReady: () => void}) {
const z = useZero();
const params = useParams();
Expand Down Expand Up @@ -232,14 +243,7 @@
const [displayAllComments, setDisplayAllComments] = useState(false);

const [allComments, allCommentsResult] = useQuery(
z.query.comment
.where('issueID', displayed?.id ?? '')
.related('creator', creator => creator.one())
.related('emoji', emoji =>
emoji.related('creator', creator => creator.one()),
)
.orderBy('created', 'asc')
.orderBy('id', 'asc'),
commentQuery(z, displayed),
displayAllComments && displayed !== undefined,
);

Expand Down
3 changes: 3 additions & 0 deletions packages/zql/src/query/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,9 @@ describe('types', () => {
query.where('s', '=', null);
// @ts-expect-error - cannot compare with undefined
query.where('s', '=', undefined);

// IS can compare to null
query.where('s', 'IS', null);
});

test('start', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/zql/src/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,18 @@ export type GetFieldTypeNoUndefined<
SchemaValueToTSType<TSchema['columns'][TColumn]>,
null | undefined
>[]
: TOperator extends 'IS' | 'IS NOT'
? Exclude<SchemaValueToTSType<TSchema['columns'][TColumn]>, undefined> | null
: Exclude<SchemaValueToTSType<TSchema['columns'][TColumn]>, undefined>;

export type Row<T extends TableSchema | Query<TableSchema>> =
T extends TableSchema
? {
[K in keyof T['columns']]: SchemaValueToTSType<T['columns'][K]>;
}
: QueryRowType<T & Query<TableSchema>>;
: T extends Query<TableSchema>
? QueryRowType<T>
: never;

export type Rows<T extends TableSchema | Query<TableSchema>> =
T extends TableSchema ? Row<T>[] : QueryReturnType<T & Query<TableSchema>>;
Expand Down
21 changes: 20 additions & 1 deletion packages/zqlite/src/query.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {beforeEach, expect, test} from 'vitest';
import {beforeEach, expect, expectTypeOf, test} from 'vitest';
import {createSilentLogContext} from '../../shared/src/logging-test-utils.js';
import {must} from '../../shared/src/must.js';
import {normalizeTableSchema} from '../../zero-schema/src/normalize-table-schema.js';
Expand All @@ -8,6 +8,7 @@ import {newQuery, type QueryDelegate} from '../../zql/src/query/query-impl.js';
import {schemas} from '../../zql/src/query/test/testSchemas.js';
import {Database} from './db.js';
import {TableSource, toSQLiteTypeName} from './table-source.js';
import type {Row} from '../../zql/src/query/query.js';

let queryDelegate: QueryDelegate;
beforeEach(() => {
Expand Down Expand Up @@ -125,6 +126,24 @@ beforeEach(() => {
});
});

test('row type', () => {
const query = newQuery(queryDelegate, schemas.issue)
.whereExists('labels', q => q.where('name', '=', 'bug'))
.related('labels');
type RT = Row<typeof query>;
expectTypeOf<RT>().toEqualTypeOf<{
readonly id: string;
readonly title: string;
readonly description: string;
readonly closed: boolean;
readonly ownerId: string | null;
readonly labels: readonly {
readonly id: string;
readonly name: string;
}[];
}>();
});

test('basic query', () => {
const query = newQuery(queryDelegate, schemas.issue);
const data = query.run();
Expand Down
Loading