Skip to content
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
15 changes: 14 additions & 1 deletion src/generator/datasource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('Datasource Generator', () => {
expect(result.content).toContain('country LowCardinality(String)');
});

it('formats LowCardinality(Nullable) correctly', () => {
it('formats LowCardinality(Nullable) correctly with .lowCardinality().nullable()', () => {
const ds = defineDatasource('test_ds', {
schema: {
country: t.string().lowCardinality().nullable(),
Expand All @@ -151,6 +151,19 @@ describe('Datasource Generator', () => {

const result = generateDatasource(ds);
expect(result.content).toContain('country LowCardinality(Nullable(String))');
expect(result.content).not.toContain('Nullable(LowCardinality');
});

it('formats LowCardinality(Nullable) correctly with .nullable().lowCardinality()', () => {
const ds = defineDatasource('test_ds', {
schema: {
country: t.string().nullable().lowCardinality(),
},
});

const result = generateDatasource(ds);
expect(result.content).toContain('country LowCardinality(Nullable(String))');
expect(result.content).not.toContain('Nullable(LowCardinality');
});

it('includes default values', () => {
Expand Down
12 changes: 10 additions & 2 deletions src/schema/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,18 @@ describe("Type Validators (t.*)", () => {
expect(type._tinybirdType).toBe("LowCardinality(Nullable(String))");
});

it("preserves both modifiers when chained", () => {
it("preserves lowCardinality modifier and omits nullable when combined (nullable is in the type string)", () => {
const type = t.string().lowCardinality().nullable();
expect(type._modifiers.lowCardinality).toBe(true);
expect(type._modifiers.nullable).toBe(true);
expect(type._modifiers.nullable).toBeUndefined();
expect(type._tinybirdType).toBe("LowCardinality(Nullable(String))");
});

it("omits nullable modifier when nullable().lowCardinality() is chained", () => {
const type = t.string().nullable().lowCardinality();
expect(type._modifiers.lowCardinality).toBe(true);
expect(type._modifiers.nullable).toBeUndefined();
expect(type._tinybirdType).toBe("LowCardinality(Nullable(String))");
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/schema/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ function createValidator<TType, TTinybirdType extends string>(
`LowCardinality(Nullable(${string}))`
>(newType as `LowCardinality(Nullable(${string}))`, {
...modifiers,
nullable: true,
}) as unknown as TypeValidator<
TType | null,
`Nullable(${TTinybirdType})`,
Expand All @@ -129,9 +128,10 @@ function createValidator<TType, TTinybirdType extends string>(
// Extract base type from Nullable(X) and wrap as LowCardinality(Nullable(X))
const baseType = tinybirdType.replace(/^Nullable\((.+)\)$/, "$1");
const newType = `LowCardinality(Nullable(${baseType}))`;
const { nullable: _, ...rest } = modifiers;
return createValidator<TType, `LowCardinality(Nullable(${string}))`>(
newType as `LowCardinality(Nullable(${string}))`,
{ ...modifiers, lowCardinality: true },
{ ...rest, lowCardinality: true },
) as unknown as TypeValidator<
TType,
`LowCardinality(${TTinybirdType})`,
Expand Down
Loading