Skip to content

Commit

Permalink
build: use new turso based config
Browse files Browse the repository at this point in the history
  • Loading branch information
arunanshub committed Mar 15, 2024
1 parent 46a5791 commit fd402f6
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
13 changes: 7 additions & 6 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import 'dotenv/config'
import type { Config } from 'drizzle-kit'

const dbName = process.env.DB_NAME
if (!dbName) {
throw new Error('DB_NAME not set')
const databaseUrl = process.env.DATABASE_URL
const authToken = process.env.TURSO_AUTH_TOKEN
if (!databaseUrl || !authToken) {
throw new Error('DATABASE_URL or AUTH_TOKEN not set')
}

export default {
out: 'src/server/database/migrations',
schema: 'src/server/database/schema/*',
driver: 'd1',
driver: 'turso',
dbCredentials: {
dbName: dbName,
wranglerConfigPath: 'wrangler.toml',
url: databaseUrl,
authToken,
},
} satisfies Config
10 changes: 7 additions & 3 deletions migrate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import 'dotenv/config'
import { migrate } from 'drizzle-orm/planetscale-serverless/migrator'
import { migrate } from 'drizzle-orm/libsql/migrator'
import { getDb } from '~/server/database/db'

const databaseUrl = process.env.DATABASE_URL
const authToken = process.env.TURSO_AUTH_TOKEN

if (!databaseUrl) {
if (!databaseUrl || !authToken) {
throw Error('Database URL not configured in environment variable')
}
// This will run migrations on the database, skipping the ones already applied
await migrate(getDb(databaseUrl), {
migrate(getDb({ url: databaseUrl, authToken }), {
migrationsFolder: './src/server/database/migrations',
}).catch((err) => {
console.error(err)
process.exit(1)
})
3 changes: 3 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ export default defineNuxtConfig({
},
runtimeConfig: {
databaseUrl: process.env.DATABASE_URL,
turso: {
authToken: process.env.TURSO_AUTH_TOKEN,
},
},

// Development
Expand Down
13 changes: 10 additions & 3 deletions src/server/database/db.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { drizzle } from 'drizzle-orm/d1'
import { createClient } from '@libsql/client'
import { drizzle } from 'drizzle-orm/libsql'
import * as answers from './schema/answers'
import * as distributions from './schema/distributions'
import * as questions from './schema/questions'
import * as results from './schema/results'
import * as sections from './schema/sections'

export function getDb(db: D1Database) {
return drizzle(db, {
interface Options {
url: string
authToken: string
}

export function getDb(opts: Options) {
const client = createClient({ ...opts })
return drizzle(client, {
schema: {
...questions,
...answers,
Expand Down
21 changes: 12 additions & 9 deletions src/server/database/seed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sql } from 'drizzle-orm'
import 'dotenv/config'
import { groupBy, mapValues } from 'lodash-es'
import * as answer from '~/server/crud/answer'
import * as question from '~/server/crud/question'
Expand All @@ -11,18 +11,16 @@ import sectionData from '~~/seed-data/sections.json'

async function main() {
const databaseUrl = process.env.DATABASE_URL
if (!databaseUrl) {
throw Error('database URL not found')
const authToken = process.env.TURSO_AUTH_TOKEN
if (!databaseUrl || !authToken) {
throw Error('database URL or Auth token not found')
}
const db = getDb(databaseUrl)
const db = getDb({ authToken, url: databaseUrl })

// create the distros
await Promise.all(
distros.map(async (distro) =>
db
.insert(distributions)
.values({ ...distro })
.onDuplicateKeyUpdate({ set: { id: sql`id` } }),
db.insert(distributions).values({ ...distro }),
),
)

Expand All @@ -36,12 +34,14 @@ async function main() {
questionData.map(async ({ question: q, answers: ans }, ix) => {
return db.transaction(async (tx) => {
const id = createdSections[ix]?.id
// @ts-expect-error
const createdQuestion = await question.createQuestion(tx, {
...q,
sectionId: id as NonNullable<typeof id>,
})
// create answers related to question `q`
await answer.createAnswers(
// @ts-expect-error
tx,
ans.map((a) => ({ questionId: createdQuestion?.id, ...a })),
)
Expand Down Expand Up @@ -75,4 +75,7 @@ async function main() {
)
}

await main()
main().catch((e) => {
console.error(e)
process.exit(1)
})
4 changes: 3 additions & 1 deletion src/server/middleware/0.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ declare module 'h3' {

export default defineEventHandler((event) => {
const config = useRuntimeConfig(event)
const databaseUrl = config.databaseUrl
const authToken = config.turso.authToken
if (!db) {
db = getDb(config.databaseUrl)
db = getDb({ url: databaseUrl, authToken })
}
event.context.db = db
})

0 comments on commit fd402f6

Please sign in to comment.