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.
- Start the PostgreSQL development database with Docker Compose:
docker compose up -d
- Run the migration
npm run migrate:dev
- Run the script
npm run dev
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")
);
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.