Koishi INI Database Plugin
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
-
EACCES: permission denied, open '...'
cause The Koishi application process does not have sufficient read/write permissions for the directory configured in `path`.fixEnsure the directory specified in the `path` configuration (default: `data/database/inidb`) exists and the user running Koishi has full read/write access to it. You might need to adjust file permissions (`chmod`) or move the directory. -
[koishi] plugin load failed: koishi-plugin-database-inidb
cause This generic error can indicate several issues, including missing peer dependencies (Koishi itself), invalid plugin configuration, or a syntax error within the database INI files.fixVerify that `koishi` is installed and meets the peer dependency version (`npm install koishi`). Check the plugin configuration passed to `app.plugin(plugin, config)` for any typos or invalid values. Inspect Koishi's console output for more specific error messages after this generic failure. -
INI parsing error: Invalid section or key/value pair in file '...'
cause One of the INI database files has been corrupted or manually edited in a way that violates the INI file format specification.fixExamine the specified INI file for syntax errors (e.g., missing `[section]`, malformed `key=value` pairs). If the data is not critical or backed up, deleting the corrupted file might allow the plugin to regenerate it (though data will be lost for that table).
Warnings
- gotcha Performance may degrade significantly with very large datasets or high concurrent query rates due to the inherent overhead of file I/O operations and locking mechanisms for INI files. This plugin is best suited for small to medium-sized datasets.
- breaking Manually editing the generated INI database files without proper care can lead to data corruption, schema inconsistencies, or parsing errors, potentially rendering parts or all of your data unusable.
- gotcha The `path` configuration determines where your database files are stored. Misconfiguring this path (e.g., to a publicly accessible directory or one with insufficient permissions) can expose sensitive bot data or lead to file access errors.
- gotcha The INI file format is not designed for complex data structures (e.g., nested objects, arrays) or advanced querying capabilities (e.g., JOINs, complex filters). Storing such data might lead to flattened or serialized representations that are less efficient to query.
Install
-
npm install koishi-plugin-database-inidb -
yarn add koishi-plugin-database-inidb -
pnpm add koishi-plugin-database-inidb
Imports
- plugin
const { plugin } = require('koishi-plugin-database-inidb')import { plugin } from 'koishi-plugin-database-inidb' - Config
import { Config } from 'koishi-plugin-database-inidb'import { type Config } from 'koishi-plugin-database-inidb' - inidb
const inidb = require('koishi-plugin-database-inidb').defaultimport inidb from 'koishi-plugin-database-inidb'
Quickstart
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.');