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

Fix smallserial type #1836

Merged
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
30 changes: 20 additions & 10 deletions drizzle-orm/src/pg-core/columns/smallserial.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';
import type {
ColumnBuilderBaseConfig,
ColumnBuilderRuntimeConfig,
HasDefault,
MakeColumnConfig,
NotNull,
} from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import type { AnyPgTable } from '~/pg-core/table.ts';
import { PgColumn, PgColumnBuilder } from './common.ts';

export type PgSmallSerialBuilderInitial<TName extends string> = PgSmallSerialBuilder<{
name: TName;
dataType: 'number';
columnType: 'PgSmallSerial';
data: number;
driverParam: number;
enumValues: undefined;
}>;
export type PgSmallSerialBuilderInitial<TName extends string> = NotNull<
HasDefault<
PgSmallSerialBuilder<{
name: TName;
dataType: 'number';
columnType: 'PgSmallSerial';
data: number;
driverParam: number;
enumValues: undefined;
}>
>
>;

export class PgSmallSerialBuilder<T extends ColumnBuilderBaseConfig<'number', 'PgSmallSerial'>>
extends PgColumnBuilder<T>
Expand Down Expand Up @@ -44,5 +54,5 @@ export class PgSmallSerial<T extends ColumnBaseConfig<'number', 'PgSmallSerial'>
}

export function smallserial<TName extends string>(name: TName): PgSmallSerialBuilderInitial<TName> {
return new PgSmallSerialBuilder(name);
return new PgSmallSerialBuilder(name) as PgSmallSerialBuilderInitial<TName>;
}
17 changes: 17 additions & 0 deletions drizzle-orm/type-tests/pg/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
real,
serial,
smallint,
smallserial,
text,
time,
timestamp,
Expand Down Expand Up @@ -105,6 +106,22 @@ export const cities = pgTable('cities_table', {
citiesNameIdx: index().on(cities.id),
}));

export const smallSerialTest = pgTable('cities_table', {
id: smallserial('id').primaryKey(),
name: text('name').notNull(),
population: integer('population').default(0),
}, (cities) => ({
citiesNameIdx: index().on(cities.id),
}));

Expect<
Equal<{
id?: number;
name: string;
population?: number | null;
}, typeof smallSerialTest.$inferInsert>
>;

export const classes = pgTable('classes_table', {
id: serial('id').primaryKey(),
class: text('class', { enum: ['A', 'C'] }),
Expand Down