Skip to content

2color/prisma-uuid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple TypeScript Script Example with UUIDs

This example shows how to use Prisma Client in a simple TypeScript script to read and write data in a PostgreSQL database.

This example uses UUIDs as the primary keys.

Getting started

  1. Start the PostgreSQL development database with Docker Compose:
docker compose up -d
  1. Run the migration
npm run migrate:dev
  1. Run the script
npm run dev

Using the native uuid type in PostgreSQL with Prisma

model User {
  id        String   @id @default(uuid()) @db.Uuid
  createdAt DateTime @default(now())
  email     String   @unique
  name      String?
  posts     Post[]
}

Note the first line which adds the @db.Uuid native type attribute. This tells Prisma Migrate to generate a migration with the uuid type for the id column.

To verify, open the migration:

CREATE TABLE "User" (
    "id" UUID NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "email" TEXT NOT NULL,
    "name" TEXT,
    PRIMARY KEY ("id")
);

Verifying the size of the PostgreSQL UUID type

To check the size of the type uuid in PostgreSQL, run the following query:

SELECT typlen FROM pg_type WHERE oid = 'uuid'::regtype::oid

Which returns:

+----------+
| typlen   |
|----------|
| 16       |
+----------+

Indicating that it's 16 bytes long (128 bits).

The size of the uuid type is also documented in the PostgreSQL docs:

A UUID is written as a sequence of lower-case hexadecimal digits, in several groups separated by hyphens, specifically a group of 8 digits followed by three groups of 4 digits followed by a group of 12 digits, for a total of 32 digits representing the 128 bits.

About

How to use Prisma with UUIDs in PostgreSQL

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published