numericdecimaldec
The DECIMAL data type stores exact, fixed-point numbers. This type is used when it is important to preserve exact precision, for example, with monetary data.
For more info please refer to the official CockroachDB docs.
The JSONB data type stores JSON (JavaScript Object Notation) data as a binary representation of the JSONB value, which eliminates whitespace, duplicate keys, and key ordering
For more info please refer to the official CockroachDB docs.
You can specify .$type<..>() for json object inference, it won’t check runtime values.
It provides compile time protection for default values, insert and select schemas.
// will be inferred as { foo: string }jsonb: jsonb().$type<{ foo: string }>();// will be inferred as string[]jsonb: jsonb().$type<string[]>();// won't compilejsonb: jsonb().$type<string[]>().default({});
---
bit
bit
The BIT data types store bit arrays. With BIT, the length is fixed.
Size
The number of bits in a BIT value is determined as follows:
Type declaration
Logical size
BIT
1 bit
BIT(N)
N bits
For more info please refer to the official CockroachDB docs.
CREATE TABLE "table" ( "timestamp1" timestamp, "timestamp2" timestamp (6) with time zone, "timestamp3" timestamp default now(), "timestamp4" timestamp default now());
You can specify either date or string infer modes:
// will infer as datetimestamp: timestamp({ mode: "date" }),// will infer as stringtimestamp: timestamp({ mode: "string" }),
The string mode does not perform any mappings for you. This mode was added to Drizzle ORM to provide developers
with the possibility to handle dates and date mappings themselves, depending on their needs.
Drizzle will pass raw dates as strings to and from the database, so the behavior should be as predictable as possible
and aligned 100% with the database behavior
The date mode is the regular way to work with dates. Drizzle will take care of all mappings between the database and the JS Date object
date
date
The DATE data type stores a year, month, and day.
For more info please refer to the official CockroachDB docs.
enumenumerated types
Enumerated (enum) types are data types that comprise a static, ordered set of values.
They are equivalent to the enum types supported in a number of programming languages.
An example of an enum type might be the days of the week, or a set of status values for a piece of data.
For more info please refer to the official CockroachDB docs.
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' };type Data = { foo: string; bar: number;};const users = cockroachTable('users', { id: int4().$type<UserId>().primaryKey(), jsonbField: jsonb().$type<Data>(),});
Identity Columns
To use this feature you would need to have drizzle-orm@0.32.0 or higher and drizzle-kit@0.23.0 or higher
PostgreSQL and CockroachDB supports identity columns as a way to automatically generate unique int4 values for a column. These values are generated using sequences and can be defined using the GENERATED AS IDENTITY clause.
Types of Identity Columns
GENERATED ALWAYS AS IDENTITY: The database always generates a value for the column. Manual insertion or updates to this column are not allowed unless the OVERRIDING SYSTEM VALUE clause is used.
GENERATED BY DEFAULT AS IDENTITY: The database generates a value by default, but manual values can also be inserted or updated. If a manual value is provided, it will be used instead of the system-generated value.
You can specify all properties available for sequences in the .generatedAlwaysAsIdentity() function. Additionally, you can specify custom names for these sequences
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,
a string constant, a blob constant, a signed-number, or any constant expression enclosed in parentheses.
import { sql } from "drizzle-orm";import { int4, cockroachTable, uuid } from "drizzle-orm/cockroach-core";import { sql } from 'drizzle-orm';export const table = cockroachTable('table', { int: int4().default(42), uuid1: uuid().defaultRandom(), uuid2: uuid().default(sql`gen_random_uuid()`),});
CREATE TABLE IF NOT EXISTS "table" ( "int" int4 DEFAULT 42, "uuid1" uuid DEFAULT gen_random_uuid(), "uuid2" uuid DEFAULT gen_random_uuid());
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.
These functions can assist you in utilizing various implementations such as uuid, cuid, cuid2, and many more.
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
CREATE TABLE IF NOT EXISTS "table" ( "int4" int4 NOT NULL);
Primary key
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.