Get table definition?

How can I get a SQL to create a table based on a table. For example:
const files = pgTable(
"files",
{
id: uuid("id").primaryKey().defaultRandom(),
createdAt: timestamp("created_at", { mode: "date" }).notNull().defaultNow(),
url: text("url").notNull(),
size: decimal("size"),
contentType: text("content_type"),
originalName: text("original_name"),
path: text("path").notNull().default(""),
},
(table) => ({
pathIdx: index().on(table.path),
})
)


const definition = getTableDefinition(files);
const files = pgTable(
"files",
{
id: uuid("id").primaryKey().defaultRandom(),
createdAt: timestamp("created_at", { mode: "date" }).notNull().defaultNow(),
url: text("url").notNull(),
size: decimal("size"),
contentType: text("content_type"),
originalName: text("original_name"),
path: text("path").notNull().default(""),
},
(table) => ({
pathIdx: index().on(table.path),
})
)


const definition = getTableDefinition(files);
Definition should be something like this:
CREATE TABLE IF NOT EXISTS "files" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"url" text NOT NULL,
"size" numeric,
"content_type" text,
"original_name" text,
"path" text DEFAULT '' NOT NULL
);
CREATE TABLE IF NOT EXISTS "files" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL,
"url" text NOT NULL,
"size" numeric,
"content_type" text,
"original_name" text,
"path" text DEFAULT '' NOT NULL
);
1 Reply
rphlmr ⚡
rphlmr ⚡4mo ago
👋 As far as I know, there is nothing builtin, that’s the job of Drizzle Kit to generate it. Now it is open source, maybe it’s possible to use some of its features or wait for the programmatic api
Want results from more Discord servers?
Add your server