Difficulty Integrating Drizzle Database with Nuxt

Hi, I’m a comp sci 3rd year student. I am creating a visualisation tool for volleyball match statistics. I am at the stage where I wanted to integrate the database with drizzle (because i heard it’s good) but I’m struggling to integrate it. I have the sql script set up but it isn’t fully working and im not sure why (I’m sorry i don’t have more detailed info) Every tutorial I’ve found seems to be slightly outdated and as a beginner I don’t fully know how things should work. I need to be able to input player information (Name, Position, Team). Also I want to input match statistics per set and per point to be able to create a timeline overview of the game. Would anyone be able to assist me with setting up the database and show me some basics with using it with my code? Again I’m sorry for not providing much detail on what my issue specifically is, also if you know a better way of integrating an sql database I am super down to try that instead. Here is a link to my github repo https://github.com/zu169/Visualisation-Tool-for-Volleyball-Match-Statistics-
GitHub
GitHub - zu169/Visualisation-Tool-for-Volleyball-Match-Statistics-:...
The Volleyball Visualisation Tool is a comprehensive web-based platform designed to manage, display, and analyse match and player data for both men's and women's volleyball teams. I...
3 Replies
kapa.ai
kapa.ai3w ago
Hi @ズ Zu169. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the knowledge sources provided, I can offer some guidance on integrating a SQL database with your Nuxt application for your volleyball statistics visualization tool. Database Integration Options with Nuxt From the knowledge sources, there are several approaches to integrating SQL databases with Nuxt: 1. Nuxt Content's Database System: Nuxt Content v3 uses SQL-based storage and supports various database adapters including: - SQLite (default) - PostgreSQL - LibSQL (Turso) - D1 (Cloudflare) This is mentioned in the database configuration documentation. 2. Drizzle with Nuxt Auth: The knowledge sources mention that Drizzle has been used with Nuxt for authentication purposes, as noted in a pull request for session and authentication recipes. 3. Nitro SQL Database or NuxtHub SQL Database: These are mentioned as options for storing users in the sessions and authentication documentation. Setting Up a Database with Nuxt For your volleyball statistics application, you'll need to store player information and match statistics. Here's how you might approach it: 1. Choose a Database Adapter: - For local development, SQLite might be easiest to set up - For production, PostgreSQL might be more robust 2. Install Required Packages: - For PostgreSQL: npm i pg - For LibSQL: npm i @libsql/client 3. Configure in nuxt.config.ts:
export default defineNuxtConfig({
// If using Nuxt Content with PostgreSQL
content: {
database: {
type: 'postgres',
url: process.env.POSTGRES_URL,
// Other options for `pg`
}
}
})

export default defineNuxtConfig({
// If using Nuxt Content with PostgreSQL
content: {
database: {
type: 'postgres',
url: process.env.POSTGRES_URL,
// Other options for `pg`
}
}
})

Example Resources For a complete example of a Nuxt app with database integration and CRUD operations, you might want to check out the "atidone repository" mentioned in the sessions and authentication documentation. Debugging Database Issues If you encounter issues with your database: 1. For SQLite databases, you can use the SQLite VS Code extension to inspect the database as mentioned in the debugging tools documentation. 2. You can reset your database during development with:
rm -rf .data/content/contents.sqlite
npx nuxi cleanup
npx nuxi dev

rm -rf .data/content/contents.sqlite
npx nuxi cleanup
npx nuxi dev

Getting Additional Help If you're still struggling, the Nuxt community is very helpful. You can: 1. Open a GitHub Discussion as suggested in the getting help documentation. 2. When asking for help, it's recommended to: - Explain your objective clearly - Share what you've already tried - Share your code, preferably in a minimal reproduction For professional support, there's also the Nuxt Experts service mentioned in the documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@426802777092521999> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
AGill
AGill3w ago
You can init a db instance in a file like server/utils/db.ts
import { drizzle } from 'drizzle-orm/better-sqlite3';
import type { H3Event } from 'h3';

export const useDb = (event: H3Event) => {
const { url, authToken } = useRuntimeConfig(event);

const db = drizzle({
connection: {
url,
authToken,
},
logger: true,
});

return db;
};
import { drizzle } from 'drizzle-orm/better-sqlite3';
import type { H3Event } from 'h3';

export const useDb = (event: H3Event) => {
const { url, authToken } = useRuntimeConfig(event);

const db = drizzle({
connection: {
url,
authToken,
},
logger: true,
});

return db;
};
The import depends on what db driver you are using. The url and authToken env variables can be set in nuxt.config.ts Then use it in your routes like this
export defineEventHandler(async (event) => {
const db = useDb(event);

db.insert(...)...
});
export defineEventHandler(async (event) => {
const db = useDb(event);

db.insert(...)...
});
Plus drizzle docs are pretty good: https://orm.drizzle.team/docs/data-querying
Drizzle ORM - Query data
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.

Did you find this page helpful?