Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update drizzle.md #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/3.integrations/drizzle.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,44 @@ await db.sql`insert into users (full_name) values ('John Doe')`;
const drizzleDb = drizzle(db);
await drizzleDb.select().from(users).all();
```

## Example with Nuxt 3 and Nitro

> [!IMPORTANT]
> Database support is currently experimental.
> Refer to the [db0 issues](https://github.com/unjs/db0/issues) for status and bug report.

```ts [server/utils/db.ts]
// ./server/utils/db.ts
import { drizzle } from "db0/integrations/drizzle";
export const db = useDatabase();
export const orm = drizzle(useDatabase());

db.sql`CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT,
email TEXT,
role TEXT,
bio TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)`;
```

```ts [server/api/index.ts]
// ./server/api/index.ts
export default defineEventHandler(async () => {
try {
const users = await orm.select().from(users).all();
return { ...users }
} catch (e: any) {
throw createError({
statusCode: 400,
statusMessage: e.message,
});
}
});
```

## References

- [Nuxt 3 Nightly Example Repo](https://github.com/justserdar/jsd-nuxt-turso-drizzle/tree/nightly)