{"id":18488,"library":"koishi-plugin-database-leveldb","title":"Koishi Plugin Database LevelDB","description":"A database service plugin for the Koishi bot framework that uses LevelDB as the storage backend. Current stable version is 1.2.0, released as part of the koishi-shangxue-plugins collection. It provides a lightweight, embedded key-value store with no external database dependencies, making it suitable for small to medium-sized chatbots. Key differentiators: high read/write performance due to LevelDB, minimal configuration, and automatic data persistence in local files. It fully integrates with Koishi's database abstraction layer, supporting table definition, CRUD operations, and querying.","status":"active","version":"1.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/koishi-shangxue-plugins/service-more","tags":["javascript","bot","chatbot","koishi","plugin","database","client","storage","typescript"],"install":[{"cmd":"npm install koishi-plugin-database-leveldb","lang":"bash","label":"npm"},{"cmd":"yarn add koishi-plugin-database-leveldb","lang":"bash","label":"yarn"},{"cmd":"pnpm add koishi-plugin-database-leveldb","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency; required as the bot framework to use this plugin","package":"koishi","optional":false}],"imports":[{"note":"CommonJS require() may not work if the package or its dependencies use ESM; recommended to use ESM imports.","wrong":"const KoishiDatabaseLeveldb = require('koishi-plugin-database-leveldb')","symbol":"default export","correct":"import KoishiDatabaseLeveldb from 'koishi-plugin-database-leveldb'"},{"note":"Must use export for apply and inject with TypeScript module augmentation; ensure 'database' is in inject array.","wrong":"function apply(ctx) { ... }","symbol":"apply function","correct":"export const inject = ['database']; export async function apply(ctx: Context) { ... }"},{"note":"The declaration must be inside a module augmentation, not a regular declaration. Ensure the module name is 'koishi'.","wrong":"declare module 'koishi' { interface Tables { demo: DemoTable } }","symbol":"Tables type augmentation","correct":"declare module 'koishi' { interface Tables { demo: DemoTable } }"}],"quickstart":{"code":"import { Context } from 'koishi'\n\n// Install plugin in your Koishi app\n// app.plugin(require('koishi-plugin-database-leveldb'))\n// or with ESM: import KoishiDatabaseLeveldb from 'koishi-plugin-database-leveldb'; app.plugin(KoishiDatabaseLeveldb)\n\n// Then in a plugin:\ndeclare module 'koishi' {\n  interface Tables {\n    todos: Todo\n  }\n}\n\ninterface Todo {\n  id: number\n  content: string\n  completed: boolean\n}\n\nexport const inject = ['database']\n\nexport async function apply(ctx: Context) {\n  ctx.model.extend('todos', {\n    id: 'integer',\n    content: 'string',\n    completed: 'boolean',\n  }, { primary: 'id' })\n\n  // Create a todo\n  await ctx.database.create('todos', { id: 1, content: 'Buy milk', completed: false })\n\n  // Read todos\n  const todos = await ctx.database.get('todos', {})\n  console.log(todos)\n\n  // Update a todo\n  await ctx.database.set('todos', { id: 1 }, { completed: true })\n\n  // Delete a todo\n  await ctx.database.remove('todos', { id: 1 })\n}","lang":"typescript","description":"Shows how to define a table, inject database, and perform CRUD operations using Koishi's database interface with LevelDB backend."},"warnings":[{"fix":"Set 'database.leveldb.dir' in Koishi config to change the storage path.","message":"Data persistence directory: Database files are stored in 'data/database/leveldb' relative to the Koishi working directory. Ensure the directory is writable and backed up if needed.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Plan schema carefully or manually clear the database (delete the directory) when modifying table structures.","message":"No built-in migration: LevelDB schema is not enforced; changes to table definitions after data creation may lead to unexpected behavior or crashes.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Switch to a more scalable database plugin if performance becomes an issue.","message":"Performance with large datasets: LevelDB performance degrades with millions of records; consider using SQLite or MySQL for larger bots.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use a single Koishi process per data directory.","message":"Concurrent access: LevelDB supports multiple processes but not recommended; running multiple Koishi instances with the same data directory can corrupt data.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run 'npm install koishi-plugin-database-leveldb' or add to package.json.","cause":"Package not installed or not in node_modules.","error":"Error: Cannot find module 'koishi-plugin-database-leveldb'"},{"fix":"Ensure your plugin has 'export const inject = ['database']' and the database plugin is loaded before yours.","cause":"Missing 'database' injection; plugin did not request database service.","error":"TypeError: ctx.database.get is not a function"},{"fix":"Create the 'data/database' directory or change the storage path via config.","cause":"Data directory not writable or parent directory missing.","error":"LeveldbError: IO error: ... LOCK: No such file or directory"},{"fix":"Verify that the database plugin (koishi-plugin-database-leveldb) is installed and registered via app.plugin() before using ctx.model.","cause":"Database service not initialized; database plugin not properly loaded.","error":"TypeError: Cannot read property 'extend' of undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}