Extended native types support
Extended native database type support is a Preview feature in 2.11.0+, and is supported by Prisma Migrate in 2.15.0+. This Preview includes the following features:
- Introspection adds type attributes to each field that enrich your schema with information about the underlying native database type
- Prisma Migrate uses type attributes to infer which specific native type to create in the database - for example,
@VarChar(90)
or@VarChar(200)
- New Prisma types
In the following example, the String
fields map to a varchar
with different character limits in the underlying database:
model Post {authorId Int? @db.Intcontent String? @db.VarChar(1000)id Int @id @default(autoincrement()) @db.Intpublished Boolean @default(false) @db.TinyInttitle String @db.VarChar(191)}
See also:
Native database type attributes
When you enable the nativeTypes
Preview feature, introspection adds a native database type attribute that describes the underlying database type:
model Post {id Int @idtitle String @db.VarChar(200)tagline String @db.Char(50)}
Each Prisma type can map to multiple native database types. Native database type attributes are:
- Specific to the underlying provider - for example, PostgreSQL uses
@db.Boolean
forBoolean
whereas MySQL uses@db.TinyInt
- Written in PascalCase (for example,
VarChar
orText
) - Prefixed by
@db
, wheredb
is the name of thedatasource
block in your schema
Type attributes give you:
Enable native database type attributes
To enable native database type attributes:
Update the
generator
block in your schema as shown:prisma/schema.prisma1generator client {2 provider = "prisma-client-js"3 previewFeatures = ["nativeTypes"]4}Introspect your database to populate the schema with type attributes:
$npx prisma introspectGenerate Prisma Client:
$npx prisma generate
Additional Prisma types
The type attribute Preview feature includes the following new Prisma types:
Working with new types in Prisma Client
BigInt
usesBigInt
type (Node.js 10.4.0+ required)Decimal
uses theDecimal.js
libraryBytes
usesBuffer
The following model uses the three new Prisma types:
model sample {bigInt BigInt? @db.BigIntbyteA Bytes? @db.ByteAdecimal Decimal? @db.Numericid Int @id @default(autoincrement()) @db.Integer}
The following example demonstrates how to create and filter on sample
records with Prisma Client:
// Create recordconst newTypes = await prisma.sample.create({data:{bigInt: BigInt(534543543534),decimal: new Decimal(24.454545),byteA: Buffer.from(["Hello", "this is a string", 1, 9, 4])}});// Filter recordsconst newTypesFilter = await prisma.dates.findMany({where: {bigInt: {gt: BigInt(4594375943753)},byteA: {not: Buffer.from(["Hey!"])}}})