☄️ vth

Backend

Database

Database guide

Database

The database is stored in SQLite.

Why SQLite?

SQLite is a small, fast and easy to use database engine, no need for a complex setup.

Query builder

The query builder is made with Kysely allowing you to write queries in a type-safe way.

How to use it in vth?

First define your database schema in lib/types.ts with types:

lib/types.ts
import type { Generated } from 'kysely';

export interface Database {
    example: {
        id: Generated<number>;
        message: string;
    }
}

Then create the database in lib/database.ts:

lib/database.ts
import SQlite from 'better-sqlite3';
import { Kysely, SqliteDialect } from 'kysely';
import type { Database } from '@/lib/types';

const dialect = new SqliteDialect({
    database: new SQlite('storage.db'),
});

export const db = new Kysely<Database>({
    dialect,
});

// generate the table
await db.schema
    .createTable('example')
    .ifNotExists()
    .addColumn('id', 'integer', (col) => col.autoIncrement().primaryKey())
    .addColumn('message', 'text')
    .execute();

Querying data

To create your first query use the created db instance:

import { db } from '@/lib/database';

const todos = await db
    .selectFrom('example')
    .selectAll()
    .where('id', '=', 1)
    .executeTakeFirst();

You can find more examples in the Kysely documentation.

Alternatives

You can use multiple database engines without much effort, such as:

  • PostgreSQL
  • MySQL
  • MS SQL

All of these engines are supported by Kysely and better-auth.

If you want to use a different database engine like MongoDB, you'll need to make additional changes like using a different query builder like Mongoose.