Koishi INI Database Plugin

1.2.0 · active · verified Wed Apr 22

koishi-plugin-database-inidb provides a lightweight database service for the Koishi chatbot framework. It stores all data in INI-formatted files, making the database easily inspectable and manually editable outside the application. Currently at stable version 1.2.0, this plugin aims for simplicity and ease of setup by having no external dependencies beyond Koishi itself. Its release cadence is typically event-driven, responding to Koishi framework updates or feature requests. A key differentiator is its file-based approach, which ensures portability and human readability, contrasted with traditional SQL or NoSQL databases. It also features file-locking mechanisms to maintain data consistency during concurrent access, which is crucial for a bot environment. This makes it suitable for smaller Koishi instances where a full-fledged database server is an overkill and resource usage is a primary concern.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting up a Koishi application and integrating `koishi-plugin-database-inidb`. It includes an example of using the database service to store and retrieve user data via simple commands.

import { Context, Schema, h } from 'koishi'
import { plugin } from 'koishi-plugin-database-inidb'

// Create a basic Koishi instance (replace with your actual setup)
const app = new Context({
  prefix: ['.'],
  port: 8080
});

// Apply the INI database plugin
app.plugin(plugin, {
  path: process.env.KOISHI_INI_DB_PATH ?? 'data/inidb'
});

// Example usage: register a command that interacts with the database
// (assuming database services are set up by the plugin)
app.command('set <key:text> <value:text>')
  .action(async ({ session }, key, value) => {
    if (!key || !value) return 'Usage: .set <key> <value>';
    await session.database.set('user', { id: session.userId }, { [key]: value });
    return `Set ${key} for user ${session.userId} to ${value}`;
  });

app.command('get <key:text>')
  .action(async ({ session }, key) => {
    if (!key) return 'Usage: .get <key>';
    const user = await session.database.get('user', { id: session.userId }, [key]);
    if (user && user[key]) {
      return `Value of ${key} for user ${session.userId} is ${user[key]}`;
    } else {
      return `Key ${key} not found for user ${session.userId}`;
    }
  });

app.start();
console.log('Koishi app with INI DB started.');

view raw JSON →