Skip to content

Commit

Permalink
feat: migrate to turso (#202)
Browse files Browse the repository at this point in the history
* feat(db): migrate to sqlite codebase

* build(deps-dev): update dependencies

* fix(migrations): update migrations

* fix(crud): ignore bogus type errors

* build: use new turso based config

* refactor: provide detailed error info
  • Loading branch information
arunanshub authored Mar 15, 2024
1 parent 5cabd11 commit 1809e9c
Show file tree
Hide file tree
Showing 20 changed files with 960 additions and 956 deletions.
13 changes: 11 additions & 2 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import 'dotenv/config'
import type { Config } from 'drizzle-kit'

const databaseUrl = process.env.DATABASE_URL
const authToken = process.env.TURSO_AUTH_TOKEN
if (!databaseUrl || !authToken) {
throw new Error(
'Either DATABASE_URL or TURSO_AUTH_TOKEN not set in the environment variables.',
)
}

export default {
out: 'src/server/database/migrations',
schema: 'src/server/database/schema/*',
driver: 'mysql2',
driver: 'turso',
dbCredentials: {
uri: process.env.DATABASE_URL as NonNullable<string>,
url: databaseUrl,
authToken,
},
} satisfies Config
14 changes: 10 additions & 4 deletions migrate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
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) {
throw Error('Database URL not configured in environment variable')
if (!databaseUrl || !authToken) {
throw Error(
'Either database URL (DATABASE_URL) or Turso Auth token (TURSO_AUTH_TOKEN) not set 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
43 changes: 23 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,47 @@
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare"
"postinstall": "nuxt prepare",
"db:migrate": "tsx migrate.ts",
"db:seed": "tsx src/server/database/seed.ts",
"db:generate": "drizzle-kit generate:sqlite --config ./drizzle.config.ts"
},
"browserslist": ["last 4 versions, >0.2%, not dead"],
"devDependencies": {
"@biomejs/biome": "1.6.1",
"@nuxt/devtools": "latest",
"@tailwindcss/typography": "^0.5.10",
"@types/better-sqlite3": "^7.6.9",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.11.8",
"drizzle-kit": "^0.20.13",
"@types/node": "^20.11.27",
"drizzle-kit": "^0.20.14",
"lodash-es": "^4.17.21",
"mysql2": "^3.9.1",
"nuxt": "^3.10.0",
"nuxt": "^3.10.3",
"nuxt-build-cache": "^0.1.1",
"prettier": "^3.2.4",
"prettier-plugin-tailwindcss": "^0.5.11",
"ts-node": "^10.9.2",
"type-fest": "^4.10.2",
"vue": "^3.4.15",
"vue-router": "^4.2.5",
"vue-tsc": "^2.0.0"
"prettier": "^3.2.5",
"prettier-plugin-tailwindcss": "^0.5.12",
"tsx": "^4.7.1",
"type-fest": "^4.12.0",
"vue": "^3.4.21",
"vue-router": "^4.3.0",
"vue-tsc": "^2.0.6"
},
"dependencies": {
"@libsql/client": "^0.5.6",
"@nuxt/content": "2.12.1",
"@nuxt/image": "^1.3.0",
"@nuxt/image": "^1.4.0",
"@nuxtjs/critters": "^0.6.0",
"@nuxtjs/fontaine": "^0.4.1",
"@nuxtjs/google-fonts": "^3.1.3",
"@nuxtjs/robots": "^3.0.0",
"@nuxtjs/tailwindcss": "^6.11.1",
"@planetscale/database": "^1.14.0",
"@vueuse/core": "^10.7.2",
"@vueuse/nuxt": "^10.7.2",
"@nuxtjs/tailwindcss": "^6.11.4",
"@vueuse/core": "^10.9.0",
"@vueuse/nuxt": "^10.9.0",
"browserslist-to-esbuild": "^2.1.1",
"drizzle-orm": "^0.30.0",
"drizzle-orm": "^0.30.1",
"nuxt-delay-hydration": "^1.3.3",
"nuxt-icon": "^0.6.8",
"postcss-preset-env": "^9.3.0",
"nuxt-icon": "^0.6.9",
"postcss-preset-env": "^9.5.1",
"valibot": "^0.30.0"
}
}
Loading

0 comments on commit 1809e9c

Please sign in to comment.