Skip to content

Generated Types

Each column in a schema has a TypeScript type for type-safe select, insert, and update queries with Kysely.

Types for a schema can be retrieved using the infer method on the schema.

Consider the following table definition:

ts
const users = table({
  columns: {
    id: integer().generatedAlwaysAsIdentity(),
    name: text(),
    createdAt: timestampWithTimeZone().default(sql`now()`).notNull(),
  },
  constraints: {
    primaryKey: primaryKey(["id"]),
  },
});

When we infer the types with:

ts
type DB = typeof dbSchema.infer;

The DB type will be:

ts
type DB = {
  users: {
    id: {
      readonly __select__: number;
      readonly __insert__: never;
      readonly __update__: never;
    };
    name: {
      readonly __select__: string;
      readonly __insert__: string | null | undefined;
      readonly __update__: string | null;
    },
    createdAt: {
      readonly __select__: Date;
      readonly __insert__: Date | undefined;
      readonly __update__: Date;
    },
  };
};

The types are inferred according to the column data types, constraints, default values and if the column is generated.

Column data types

Here's a table for select, insert, and update types for each column type:

ColumnSelectInsertUpdate
bigintstringbigint | number | stringbigint | number | string
bigserialstringbigint | number | stringbigint | number | string
bitstringstringstring
bitVaryingstringstringstring
booleanbooleanboolean | Boolish*boolean | Boolish*
byteaBufferBuffer | stringBuffer | string
characterVaryingstringstringstring
characterstringstringstring
cidrstringstringstring
dateDateDate | stringDate | string
doublePrecisionstringbigint | number | stringbigint | number | string
enumeratedenum valuesenum valuesenum values
inetstringstringstring
integernumbernumber | stringnumber | string
jsonJsonValue*JsonValue*JsonValue*
jsonbJsonValue*JsonValue*JsonValue*
macaddrstringstringstring
macaddr8stringstringstring
numericstringbigint | number | stringbigint | number | string
realnumberbigint | number | stringbigint | number | string
serialnumbernumber | stringnumber | string
smallintnumbernumber | stringnumber | string
timestringstringstring
timeWithTimeZonestringstringstring
timestampDateDate | stringDate | string
timestampWithTimeZoneDateDate | stringDate | string
tsquerystringstringstring
tsvectorstringstringstring
uuidstringstringstring
xmlstringstringstring

(*) Boolish and JsonValue are defined as follows:

ts
type Boolish = "true" | "false" | "yes" | "no" | 1 | 0 | "1" | "0" | "on" | "off";
type JsonArray = JsonValue[];
type JsonValue = boolean | number | string | Record<string, unknown> | JsonArray;

Optionality and nullability

By default, columns have:

  • Nullable selects, inserts and updates.
  • Optional inserts and updates.

Columns will have their optionality and nullability changed accordingly to the constraints they have, their default data value, and if they are generated:

ColumnOptionalNullable
with default data valueyesyes
with NOT NULL constraintnono
with NOT NULL constraint with default data valueyesno
serialyesno
bigserialyesno
generated by default as identityyesno
primary keynono
serial, bigserial generated by default as identity primary keyyesno

Examples

Type of an integer column named id:

ts
type id = {
  readonly __select__: number | null;
  readonly __insert__: number | string | null | undefined;
  readonly __update__: number | string | null;
};

Generated always as identity will not accept inserts or updates:

ts
type id = {
  readonly __select__: number;
  readonly __insert__: never;
  readonly __update__: never;
};

Type of an integer column with a NOT NULL constraint:

ts
type id = {
  readonly __select__: number;
  readonly __insert__: number | string;
  readonly __update__: number | string;
};

Type of an integer column with a default data value and a NOT NULL constraint:

ts
type id = {
  readonly __select__: number;
  readonly __insert__: number | string | undefined;
  readonly __update__: number | string;
};

Type of an bigint column generated by default as identity:

ts
type id = {
  readonly __select__: string;
  readonly __insert__: bigint | number | string | undefined;
  readonly __update__: bigint | number | string;
};