drizzle-team / drizzle-team/drizzle-orm

[FEATURE]:Better types

Open
#2,480 2 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe what you want

Hi,

I wish we could have better types in Drizzle ORM. Here’s an example of what I'm currently doing and what I want to do:

**What I can do:**

```ts
const doSomething = (table: SQLiteTable) => {
// Do something with table
}
```

**What I want to do:**

```ts
const doSomething = (table: SQLiteTable<{ id: string, createdAt: string }>) => {
// Do something with table
}
```

I want to specify that the tables should have `id` and `createdAt` columns.

**Why do I need this?**

I'm creating a repository pattern-based project and need these specific types. Here’s a basic example:

```ts
class Instance {
public data: T;

public constructor(data: T) {
this.data = data;
}
}

abstract class Repo> {
public table: T;

public constructor(table: T) {
this.table = table;
}

public abstract notFound(): NotFoundException;
public abstract revalidatePath(): void;
protected abstract revalidate(): void;

public async findById(id: string) {
const [item] = await db
.select()
.from(this.table)
.where(eq(this.table.id, id));

if (!item) {
return null;
}

return new Instance(item);
}

public async findByIdOrThrow(id: string) {
const item = await this.findById(id);

if (!item) {
throw this.notFound();
}

return item;
}

public async create(input: InferInsertModel) {
const [item] = await db.insert(this.table).values(input).returning();
this.revalidate();
return new Instance(item);
}
}

class UserRepo extends Repo {
public notFound() {
return new NotFoundException("User");
}

public revalidatePath(): void {
revalidatePath("/users");
}

protected revalidate(): void {
this.revalidatePath();
}

public constructor() {
super(users);
}
}

const userService = new UserRepo();
userService.findById("id");
userService.findByIdOrThrow("id");
userService.create({
id: "",
email: "",
roleId: "",
});
```

By defining the type of the table, I can ensure the presence of specific columns like `id` and `createdAt`, making my TypeScript code more robust and less prone to errors.

I would also love to have a type for where orderBy,...

Thank you!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.