You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.3 KiB
51 lines
1.3 KiB
import buildKnex from "knex"
|
|
import knexConfig from "../../knexfile.mjs"
|
|
|
|
// const cache = {}
|
|
|
|
// buildKnex.QueryBuilder.extend("cache", async function () {
|
|
// try {
|
|
// const cacheKey = this.toString()
|
|
// if (cache[cacheKey]) {
|
|
// return cache[cacheKey]
|
|
// }
|
|
// const data = await this
|
|
// cache[cacheKey] = data
|
|
// return data
|
|
// } catch (e) {
|
|
// throw new Error(e)
|
|
// }
|
|
// })
|
|
|
|
const environment = process.env.NODE_ENV || "development"
|
|
const db = buildKnex(knexConfig[environment])
|
|
|
|
export default db
|
|
|
|
// async function createDatabase() {
|
|
// try {
|
|
// // SQLite会自动创建数据库文件,只需验证连接
|
|
// await db.raw("SELECT 1")
|
|
// console.log("SQLite数据库连接成功")
|
|
|
|
// // 检查users表是否存在(示例)
|
|
// const [tableExists] = await db.raw(`
|
|
// SELECT name
|
|
// FROM sqlite_master
|
|
// WHERE type='table' AND name='users'
|
|
// `)
|
|
|
|
// if (tableExists) {
|
|
// console.log("表 users 已存在")
|
|
// } else {
|
|
// console.log("表 users 不存在,需要创建(通过迁移)")
|
|
// }
|
|
|
|
// await db.destroy()
|
|
// } catch (error) {
|
|
// console.error("数据库操作失败:", error)
|
|
// process.exit(1)
|
|
// }
|
|
// }
|
|
|
|
// createDatabase()
|
|
|