Skip to content

Commit d339ba8

Browse files
authored
docs: improve drizzle integration example with drizzle-kit usage (#170)
1 parent fccca93 commit d339ba8

File tree

1 file changed

+79
-17
lines changed

1 file changed

+79
-17
lines changed

docs/3.integrations/drizzle.md

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,86 @@ icon: simple-icons:drizzle
1010

1111
## Example
1212

13-
```ts [index.ts]
13+
Define your database schema using Drizzle's schema system:
14+
15+
```ts [schema.ts]
16+
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";
17+
18+
export const users = sqliteTable('users', {
19+
id: integer('id').primaryKey({ autoIncrement: true }),
20+
fullName: text('full_name').notNull(),
21+
email: text('email').notNull().unique(),
22+
createdAt: integer('created_at', { mode: 'timestamp' }).$defaultFn(() => new Date()),
23+
});
24+
25+
export type User = typeof users.$inferSelect;
26+
export type NewUser = typeof users.$inferInsert;
27+
```
28+
29+
Initialize your database with Drizzle integration:
30+
31+
```ts [database.ts]
1432
import { createDatabase } from "db0";
1533
import sqlite from "db0/connectors/better-sqlite3";
1634
import { drizzle } from "db0/integrations/drizzle";
17-
import { sqliteTable, text, numeric } from "drizzle-orm/sqlite-core";
18-
19-
// Initialize DB instance
20-
// You can use any other available connector
21-
const db = createDatabase(sqlite({}));
22-
23-
// Use simple db0 API to make queries
24-
await db.sql`create table if not exists users (
25-
id integer primary key autoincrement,
26-
full_name text
27-
)`;
28-
await db.sql`insert into users (full_name) values ('John Doe')`;
29-
30-
// And then leverage drizzle typed API to make more advanced ones
31-
const drizzleDb = drizzle(db);
32-
await drizzleDb.select().from(users).all();
35+
import * as schema from "./schema";
36+
37+
// Initialize DB instance with SQLite connector
38+
const db0 = createDatabase(sqlite({ name: 'database.sqlite' }));
39+
40+
// Create Drizzle instance with schema
41+
export const db = drizzle(db0, { schema });
42+
```
43+
44+
Use Drizzle's migration system to create tables:
45+
46+
```bash
47+
# Generate migration from schema
48+
npx drizzle-kit generate
49+
50+
# Apply migrations
51+
npx drizzle-kit migrate
52+
53+
# Or push schema directly (development)
54+
npx drizzle-kit push
55+
```
56+
57+
Then use fully typed queries:
58+
59+
```ts [queries.ts]
60+
import { db, users, type NewUser } from "./database";
61+
62+
// Insert a new user with type safety
63+
const newUser: NewUser = {
64+
fullName: 'John Doe',
65+
66+
createdAt: new Date()
67+
};
68+
69+
const insertedUser = await db.insert(users).values(newUser).returning().get()
70+
71+
// Query users with full type safety
72+
const allUsers = await db.select().from(users);
73+
74+
// Query with conditions
75+
const johnDoe = await db.select()
76+
.from(users)
77+
.where(eq(users.email, '[email protected]'));
3378
```
79+
80+
## Configuration
81+
82+
Create a `drizzle.config.ts` file for migration management:
83+
84+
```ts [drizzle.config.ts]
85+
import { defineConfig } from 'drizzle-kit';
86+
87+
export default defineConfig({
88+
schema: './schema.ts',
89+
out: './migrations',
90+
dialect: 'sqlite',
91+
dbCredentials: {
92+
url: './database.sqlite'
93+
}
94+
});
95+
```

0 commit comments

Comments
 (0)