Creating composite types in PostgreSQL

Hi, I'm curious how I could recreate the following code using drizzle.
-- composite type
CREATE TYPE author AS (
name text,
role text
);

CREATE TABLE books (
book_id serial PRIMARY KEY,
title text,
authors author[]
);
-- composite type
CREATE TYPE author AS (
name text,
role text
);

CREATE TABLE books (
book_id serial PRIMARY KEY,
title text,
authors author[]
);
Is it simply
type Author = {
name: string;
role: string;
};

const books = pgTable('books', {
bookId: serial('book_id').primaryKey(),
title: text('title'),
authors: text('authors').array().$type<Author>(),
});
type Author = {
name: string;
role: string;
};

const books = pgTable('books', {
bookId: serial('book_id').primaryKey(),
title: text('title'),
authors: text('authors').array().$type<Author>(),
});
Because I ran \dT and it didn't show a custom type for the Author in postgres
6 Replies
bluesky
blueskyOP15mo ago
the generated sql simply makes it a text array
bluesky
blueskyOP15mo ago
@Dan Kochetov
Angelelz
Angelelz15mo ago
I don't think there is support for this in drizzle As a workaround you can create a empty migration and write the sql there before running it the $type<>() method, just helps you populate the type in typescript
bluesky
blueskyOP15mo ago
I see, thank you. I ended up just creating another table
Want results from more Discord servers?
Add your server