drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.56k stars 646 forks source link

[FEATURE]: Full namespace import similar to Zod, `import { d } from 'drizzle-orm/**'` #3389

Closed johurul-haque closed 2 weeks ago

johurul-haque commented 2 weeks ago

Describe what you want

Current behavior:

import { uuid, varchar } from 'drizzle-orm/pg-core';

export const usersTable = d.pgTable(
  'users',
  {
    id: uuid('id').primaryKey().defaultRandom(),
    password: varchar('password').notNull(),
  }
);

Requested behavior:

import { d } from 'drizzle-orm/pg-core';

export const usersTable = d.pgTable(
  'users',
  {
    id: d.uuid('id').primaryKey().defaultRandom(),
    password: d.varchar('password').notNull(),
  }
);

At the moment, I can do * as d to mimic this, but I won't get intellisense on my editor for that. I have to manually write the import statement every time.

nicholasdly commented 2 weeks ago

Just curious, why would you want a full namespace import? Personally, I prefer being able to import exactly what I need, where I need it.

If you're just trying to avoid a long list of imports when defining your database schema, you can use the new table callback feature introduced in drizzle-orm@0.34.0.

Instead of this:

import { boolean, pgTable, text, uuid } from "drizzle-orm/pg-core";

export const ingredients = pgTable("ingredients", {
  id: uuid("id").defaultRandom().primaryKey(),
  name: text("name").notNull(),
  description: text("description"),
  inStock: boolean("in_stock").default(true),
});

You can now do this:

import { pgTable } from "drizzle-orm/pg-core";

export const ingredients = pgTable("ingredients", (t) => ({
  id: t.uuid().defaultRandom().primaryKey(),
  name: t.text().notNull(),
  description: t.text(),
  inStock: t.boolean("in_stock").default(true),
}));

This also ensures you're using the correct column types for that specific dialect.

Source: https://github.com/drizzle-team/drizzle-orm/releases/tag/0.34.0