Koishi CSV Database Plugin

1.2.0 · active · verified Wed Apr 22

koishi-plugin-database-csvdb provides a lightweight, file-based database service for the Koishi chatbot framework, leveraging CSV files for data storage. Currently at version 1.2.0, this plugin offers a simple, no-additional-dependency solution for persistent storage within Koishi applications. Its primary differentiators are its ease of use, the universal compatibility of the CSV format (allowing direct editing with spreadsheet software like Excel), and its built-in concurrency safety achieved through file locking mechanisms. This makes it suitable for smaller to medium-scale Koishi bots where a full-fledged relational or NoSQL database might be overkill. Release cadence is tied to bug fixes and feature enhancements, typically following the Koishi core framework's evolution. It's an ideal choice for developers seeking minimal overhead and direct data accessibility.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize a Koishi application and register the `koishi-plugin-database-csvdb` plugin with a custom data path, then interact with the database using a simple command.

import { Context, Schema } from 'koishi'
import { apply, Config } from 'koishi-plugin-database-csvdb'

// A minimal Koishi application setup
const app = new Context()

// Register the CSVDB plugin
// The configuration 'path' specifies where the CSV files will be stored.
// Ensure this path is writable by the Koishi process.
app.plugin(apply, {
  path: process.env.CSVDB_PATH || './data/csv_database'
})

// Example: Define a simple service that uses the database
app.model.extend('user', {
  id: 'string',
  name: 'string',
  karma: { type: 'integer', initial: 0 }
})

app.command('mydata <key:string> [value:string]')
  .action(async ({ session }, key, value) => {
    if (!session?.user) return 'User data not found.'

    const userId = session.user.id
    const user = await app.database.getUser(session.platform, userId, [key])

    if (value) {
      // Set data
      await app.database.setUser(session.platform, userId, { [key]: value })
      return `Set user ${key} to ${value}`
    } else {
      // Get data
      return `Current user ${key} is: ${user?.[key] || 'not set'}`
    }
  })

// Koishi application start (example for local development)
// app.start()
// console.log('Koishi app with CSVDB started.')

view raw JSON →