// knexfile.mjs (ESM格式) export default { development: { client: "sqlite3", connection: { filename: "./database/development.sqlite3", }, migrations: { directory: "./src/db/migrations", // 迁移文件目录 // 启用ES模块支持 extension: "mjs", loadExtensions: [".mjs", ".js"], }, seeds: { directory: "./src/db/seeds", // 种子数据目录, // 启用ES模块支持 extension: "mjs", loadExtensions: [".mjs", ".js"], timestampFilenamePrefix: true, }, useNullAsDefault: true, // SQLite需要这一选项 pool: { min: 1, max: 3, // 适当增加连接数以提高并发性能 acquireTimeoutMillis: 60000, // 获取连接的超时时间 createTimeoutMillis: 30000, // 创建连接的超时时间 destroyTimeoutMillis: 5000, // 销毁连接的超时时间 idleTimeoutMillis: 30000, // 连接空闲超时时间 reapIntervalMillis: 1000, // 检查和回收连接的间隔 createRetryIntervalMillis: 200, // 创建连接重试间隔 afterCreate: (conn, done) => { // SQLite 性能优化设置 conn.run("PRAGMA journal_mode = WAL", done) // 启用 WAL 模式提高并发 conn.run("PRAGMA synchronous = NORMAL", done) // 平衡性能和安全性 conn.run("PRAGMA cache_size = 1000", done) // 增加缓存大小 conn.run("PRAGMA temp_store = MEMORY", done) // 临时数据存储在内存中 conn.run("PRAGMA mmap_size = 67108864", done) // 启用内存映射,64MB conn.run("PRAGMA foreign_keys = ON", done) // 启用外键约束 }, }, }, // 生产环境、测试环境配置可按需添加 production: { client: "sqlite3", connection: { filename: "./database/db.sqlite3", }, migrations: { directory: "./src/db/migrations", // 迁移文件目录 // 启用ES模块支持 extension: "mjs", loadExtensions: [".mjs", ".js"], }, seeds: { directory: "./src/db/seeds", // 种子数据目录, // 启用ES模块支持 extension: "mjs", loadExtensions: [".mjs", ".js"], timestampFilenamePrefix: true, }, useNullAsDefault: true, // SQLite需要这一选项 pool: { min: 1, max: 5, // 生产环境适当增加连接数 acquireTimeoutMillis: 60000, createTimeoutMillis: 30000, destroyTimeoutMillis: 5000, idleTimeoutMillis: 30000, reapIntervalMillis: 1000, createRetryIntervalMillis: 200, afterCreate: (conn, done) => { // SQLite 性能优化设置 conn.run("PRAGMA journal_mode = WAL", done) conn.run("PRAGMA synchronous = NORMAL", done) conn.run("PRAGMA cache_size = 2000", done) // 生产环境更大缓存 conn.run("PRAGMA temp_store = MEMORY", done) conn.run("PRAGMA mmap_size = 134217728", done) // 128MB 内存映射 conn.run("PRAGMA foreign_keys = ON", done) conn.run("PRAGMA auto_vacuum = INCREMENTAL", done) // 增量清理 }, }, }, }