Skip to content

Package

plugin-kysely v0.9.0

Changelog

seedcordseedcord

The whole Discord bot, typed end to end

Website · Guide · Reference · Discord

npm node bun license

About

@seedcord/plugin-kysely connects a seedcord bot to a SQL database through Kysely. You pass it the Kysely dialect for your database, like SQLite, Postgres, or MySQL. It opens the connection when the bot starts, runs your migrations, loads your service classes, and closes the connection when the bot shuts down. Handlers can reach each service through this.core. You declare your database schema once, and Kysely types every query from it.

Install it when your bot stores data in a SQL database. It works with a gateway bot and with an http bot on your own server. On a Cloudflare Workers bot, attaching it is a compile error.

seedcord is in beta, so a minor version can break your bot. Each release's changelog says what changed and how to update.

Installation

Install the plugin, Kysely, and the driver for your database. For SQLite through better-sqlite3:

pnpm add @seedcord/plugin-kysely kysely better-sqlite3

To use Node's built-in node:sqlite without a driver, wrap a DatabaseSync with the plugin's nodeSqlite().

Usage

Attach the plugin in bot.ts. Read it from this.core in a handler, under the key you pass:

// bot.ts
import { Seedcord } from '@seedcord/gateway';
import { Kysely } from '@seedcord/plugin-kysely';
import Database from 'better-sqlite3';
import { GatewayIntentBits } from 'discord.js';
import { SqliteDialect } from 'kysely';

export default new Seedcord({
    bot: {
        clientOptions: { intents: [GatewayIntentBits.Guilds] },
        interactions: { path: './handlers' },
        commands: { path: './commands' },
        events: { path: null }
    },
    subscribers: { path: null }
}).attach('sql', Kysely, {
    path: './services',
    dialect: () => new SqliteDialect({ database: new Database('bot.db') }),
    migrations: { path: './migrations' }
});

dialect is a function because seedcord codegen and seedcord build import bot.ts without starting the bot. The plugin calls it when the bot starts, so neither command opens your database.

Each file in services/ holds a service for one table. AppDatabase is the interface you write that lists your tables:

// services/Users.ts
import { KyselyService, RegisterKyselyService } from '@seedcord/plugin-kysely';

import type { AppDatabase } from '../database';

@RegisterKyselyService('users')
export class Users extends KyselyService<'users'> {
    public async findByUserId(userId: string) {
        return this.db.selectFrom(this.table).selectAll().where('user_id', '=', userId).executeTakeFirst();
    }
}

declare module '@seedcord/plugin-kysely' {
    interface KyselyDatabase {
        schema: AppDatabase;
    }
}

Run seedcord codegen after you add or rename a service. It adds the service's key to KyselyServices, the type behind the decorator and core.sql.services. Then call await this.core.sql.services.users.findByUserId(this.user.id) from a handler. The Kysely guide covers the options, other databases, the schema, services, and migrations.