Skip to content

Commit 3902bc9

Browse files
committed
Turso + drizzle + sqlite
1 parent 655f946 commit 3902bc9

File tree

17 files changed

+622
-12240
lines changed

17 files changed

+622
-12240
lines changed

.env.example

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Turso
2-
TURSO_DATABASE_URL=someurl
3-
TURSO_DATABASE_AUTH_TOKEN=sometoken
2+
TURSO_DATABASE_URL=some_url
3+
TURSO_DATABASE_AUTH_TOKEN=some_token
4+
CLERK_USER_ID=your_clerk_user_id # useful for seeding the database with your clerk user id
45

56
# Clerk
67
CLERK_SECRET_KEY=skey

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ node_modules
88
.env*
99
.env*.local
1010
!.env.example
11+
12+
# sqlite db tables
13+
*.db*
14+
15+
# drizzle migrations
16+
/drizzle/migrations/meta
17+
!/drizzle/migrations/*.sql

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,21 @@ dependencies:
4444
bun install
4545
```
4646

47+
Next thing, you'll need to run the db migrations and push the schema to
48+
Turso:
49+
50+
```bash
51+
bun db:generate && bun db:migrate && bun db:push
52+
```
53+
54+
If you want to have some data right off the bat, you can run the seed command:
55+
56+
```bash
57+
bun db:seed
58+
```
59+
4760
Then you can run the app:
4861

4962
```bash
50-
bun run dev
63+
bun dev
5164
```

app/db/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {createClient} from '@libsql/client'
2+
import {drizzle} from 'drizzle-orm/libsql'
3+
4+
const client = createClient({
5+
url: process.env.TURSO_DATABASE_URL,
6+
authToken: process.env.TURSO_DATABASE_AUTH_TOKEN,
7+
})
8+
9+
export const db = drizzle(client)

app/db/schema.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {relations} from 'drizzle-orm'
2+
import {integer, sqliteTable, text} from 'drizzle-orm/sqlite-core'
3+
4+
export const tagColors = [
5+
'red',
6+
'green',
7+
'blue',
8+
'yellow',
9+
'gray',
10+
'orange',
11+
'purple',
12+
'pink',
13+
] as const
14+
15+
export const tweet = sqliteTable('tweet', {
16+
id: integer('id', {mode: 'number'})
17+
.primaryKey({autoIncrement: true})
18+
.notNull(),
19+
description: text('description'),
20+
url: text('url', {length: 2083}).notNull(),
21+
createdAt: integer('created_at', {mode: 'timestamp'}).notNull(),
22+
userId: text('user_id', {length: 100}).notNull(),
23+
})
24+
25+
export const tweetRelations = relations(tweet, ({many}) => ({
26+
tweetsToTags: many(tweetsToTags),
27+
}))
28+
29+
export const tag = sqliteTable('tag', {
30+
id: integer('id', {mode: 'number'})
31+
.primaryKey({autoIncrement: true})
32+
.notNull(),
33+
name: text('name', {length: 255}).notNull(),
34+
color: text('color', {enum: tagColors}).notNull(),
35+
userId: text('user_id', {length: 100}).notNull(),
36+
})
37+
38+
export const tagsRelations = relations(tag, ({many}) => ({
39+
tweetsToTags: many(tweetsToTags),
40+
}))
41+
42+
export const tweetsToTags = sqliteTable('tweet_tag', {
43+
tweetId: integer('tweet_id', {mode: 'number'})
44+
.notNull()
45+
.references(() => tweet.id),
46+
tagId: integer('tag_id', {mode: 'number'})
47+
.notNull()
48+
.references(() => tag.id),
49+
})
50+
51+
export const tweetWithTagsRelations = relations(tweetsToTags, ({one}) => ({
52+
tweet: one(tweet, {
53+
fields: [tweetsToTags.tweetId],
54+
references: [tweet.id],
55+
}),
56+
tag: one(tag, {
57+
fields: [tweetsToTags.tagId],
58+
references: [tag.id],
59+
}),
60+
}))

0 commit comments

Comments
 (0)