// 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") conn.run("PRAGMA synchronous = NORMAL") conn.run("PRAGMA cache_size = 1000") conn.run("PRAGMA temp_store = MEMORY") conn.run("PRAGMA mmap_size = 67108864") conn.run("PRAGMA foreign_keys = ON") done() // 只调用一次 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") conn.run("PRAGMA synchronous = NORMAL") conn.run("PRAGMA cache_size = 2000") // 生产环境更大缓存 conn.run("PRAGMA temp_store = MEMORY") conn.run("PRAGMA mmap_size = 134217728") // 128MB 内存映射 conn.run("PRAGMA foreign_keys = ON") conn.run("PRAGMA auto_vacuum = INCREMENTAL") // 增量清理 done() // 只调用一次 done() }, }, }, }