drizzle-team / drizzle-team/drizzle-orm

[BUG]: SQL.js driver is not mapping to the correct type when using a query

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

Description

### What version of `drizzle-orm` are you using?

0.30.9

### What version of `drizzle-kit` are you using?

0.20.17

### Describe the Bug

Here a repository to showcase the bug:

https://github.com/Neo-Ciber94/drizzle-sqljs-query-bug

> Clone, install deps and run `npm run start`

I'm using `drizzle` with `SQL.js`, the problem is that this query is returning a JSON not mapped to the actual schema:

```ts
const queryResult = await db.query.posts.findMany({
with: {
comments: true,
},
});
```

The result
```json
[
{
"post_id": 1,
"title": "Post 1",
"content": "This is the first post",
"created_at": "2024-04-22T02:46:59.085Z",
"comments": "[[1,1,\"Comment for the first post\",\"2024-04-22T02:46:59.094Z\"],[2,1,\"Other comment for the first post\",\"2024-04-22T02:46:59.094Z\"],[3,1,\"Yes, one comment more for the first post\",\"2024-04-22T02:46:59.094Z\"]]"
},
{
"post_id": 2,
"title": "Post 2",
"content": "This is our second post",
"created_at": "2024-04-22T02:46:59.086Z",
"comments": "[[4,2,\"This is for the second post\",\"2024-04-22T02:46:59.094Z\"]]"
}
]
```

This is the schema:

```ts

```ts
import { text, integer, sqliteTable } from "drizzle-orm/sqlite-core";
import * as columns from "./columns";
import { relations } from "drizzle-orm";

export const posts = sqliteTable("post", {
id: integer("post_id").primaryKey(),
title: text("title").notNull(),
content: text("content").notNull(),
createdAt: columns
.date("created_at")
.notNull()
.$defaultFn(() => new Date()),
});

export const comments = sqliteTable("comment", {
id: integer("comment_id").primaryKey(),
postId: integer("post_id")
.notNull()
.references(() => posts.id),
content: text("content").notNull(),
createdAt: columns
.date("created_at")
.notNull()
.$defaultFn(() => new Date()),
});

export const postRelations = relations(posts, ({ many }) => ({
comments: many(comments),
}));

export const commentRelations = relations(comments, ({ one }) => ({
post: one(posts, { fields: [comments.postId], references: [posts.id] }),
}));
```

### Expected behavior

The returned value should be mapped to the posts schema with comments.

```ts
type Post = {
id: number;
title: string;
content: string;
createdAt: Date;
comments: {
id: number;
content: string;
createdAt: Date;
postId: number;
}[]
}
```

### Environment & setup

_No response_

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.