If you’re expecting values above 2^31 but below 2^53, you can utilise mode: 'number' and deal with javascript number as opposed to bigint.
import { bigint, mssqlTable } from "drizzle-orm/mssql-core";export const table = mssqlTable('table', { bigint: bigint({ mode: 'number' })});// will be inferred as `number`bigint: bigint({ mode: 'number' })// will be inferred as `bigint`bigint: bigint({ mode: 'bigint' })// will be inferred as `string`bigint: bigint({ mode: 'string' })
float [ (n) ] Where n is the number of bits that are used to store the mantissa of the float number in scientific notation and, therefore, dictates the precision and storage size. If n is specified, it must be a value between 1 and 53. The default value of n is 53.
For more info please refer to the official MSSQL docs.
You can specify either date or string infer modes:
// will infer as datedate: date({ mode: "date" }),// will infer as stringdate: date({ mode: "string" }),
datetime
datetime
Defines a date that is combined with a time of day with fractional seconds that is based on a 24-hour clock.
MSSQL docs
Avoid using datetime for new work. Instead, use the time, date, datetime2, and datetimeoffset data types. These types align with the SQL Standard, and are more portable. time, datetime2 and datetimeoffset provide more seconds precision. datetimeoffset provides time zone support for globally deployed applications.
For more info please refer to the official MSSQL docs.
You can specify either date or string infer modes:
// will infer as datedatetime: datetime({ mode: "date" }),// will infer as stringdatetime: datetime({ mode: "string" }),
datetime2
datetime2
Defines a date that is combined with a time of day that is based on 24-hour clock. datetime2 can be considered as an extension of the existing datetime type that has a larger date range, a larger default fractional precision, and optional user-specified precision.
For more info please refer to the official MSSQL docs.
You can specify either date or string infer modes:
// will infer as datedatetime2: datetime2({ mode: "date" }),// will infer as stringdatetime2: datetime2({ mode: "string" }),
datetimeoffset
datetimeoffset
Defines a date that is combined with a time of a day based on a 24-hour clock like datetime2, and adds time zone awareness based on Coordinated Universal Time (UTC).
For more info please refer to the official MSSQL docs.
You can specify either date or string infer modes:
// will infer as datedatetimeoffset: datetimeoffset({ mode: "date" }),// will infer as stringdatetimeoffset: datetimeoffset({ mode: "string" }),
---
Customizing data type
Every column builder has a .$type() method, which allows you to customize the data type of the column.
This is useful, for example, with unknown or branded types:
type UserId = number & { __brand: 'user_id' };export const users = mssqlTable('users', { id: int().$type<UserId>().primaryKey(),});
Default value
The DEFAULT clause specifies a default value to use for the column if no value
is explicitly provided by the user when doing an INSERT.
If there is no explicit DEFAULT clause attached to a column definition,
then the default value of the column is NULL.
An explicit DEFAULT clause may specify that the default value is NULL
import { int, mssqlTable, text } from "drizzle-orm/mssql-core";export const table = mssqlTable('table', { int: int().default(42), text: text().default('text'),});
CREATE TABLE [table] ( [int] int CONSTRAINT [table_int_default] DEFAULT ((42)), [text] text CONSTRAINT [table_text_default] DEFAULT ('text'));
When using $default() or $defaultFn(), which are simply different aliases for the same function,
you can generate defaults at runtime and use these values in all insert queries.
Note: This value does not affect the drizzle-kit behavior, it is only used at runtime in drizzle-orm
When using $onUpdate() or $onUpdateFn(), which are simply different aliases for the same function,
you can generate defaults at runtime and use these values in all update queries.
Adds a dynamic update value to the column. The function will be called when the row is updated,
and the returned value will be used as the column value if none is provided.
If no default (or $defaultFn) value is provided, the function will be called
when the row is inserted as well, and the returned value will be used as the column value.
Note: This value does not affect the drizzle-kit behavior, it is only used at runtime in drizzle-orm
A primary key constraint indicates that a column, or group of columns, can be used as a unique identifier for rows in the table.
This requires that the values be both unique and not null.