feat(db): extraction des embeddings + mode WAL + config DB provider-agnostic

- Ajout de la table de relation 1-1 NoteEmbedding pour alléger Model Note
- Refactor complet des actions IA sémantique et Memory Echo pour utiliser la jointure
- Migration propre des 85 embeddings locaux existants
- Ajout PRAGMA journal_mode=WAL pour la concurrence au sein de lib/prisma
- Ajout npm run db:switch pour configuration auto SQLite / PostgreSQL
- Fix du compilateur Turbopack et Next-PWA
This commit is contained in:
Sepehr Ramezani
2026-04-17 22:05:19 +02:00
parent a57c277168
commit 3ef5915062
10 changed files with 180 additions and 36 deletions

View File

@@ -0,0 +1,61 @@
// scripts/migrate-embeddings.ts
const { PrismaClient } = require('../prisma/client-generated')
const prisma = new PrismaClient({
datasources: {
db: {
url: process.env.DATABASE_URL || "file:../prisma/dev.db"
}
}
})
async function main() {
console.log("Fetching notes with embeddings...")
const notes = await prisma.note.findMany({
where: {
embedding: { not: null }
},
select: {
id: true,
embedding: true
}
})
console.log(`Found ${notes.length} notes with an embedding.`)
if (notes.length === 0) {
console.log("Nothing to migrate.")
return
}
let count = 0
for (const note of notes) {
if (!note.embedding) continue
await prisma.noteEmbedding.upsert({
where: { noteId: note.id },
create: {
noteId: note.id,
embedding: note.embedding
},
update: {
embedding: note.embedding
}
})
count++
if (count % 10 === 0) {
console.log(`Migrated ${count}/${notes.length}...`)
}
}
console.log(`✅ Successfully migrated ${count} note embeddings to the NoteEmbedding table.`)
}
main()
.catch((e) => {
console.error("Migration failed:", e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})

View File

@@ -0,0 +1,48 @@
// scripts/switch-db.js
const fs = require('fs')
const path = require('path')
const envPath = path.join(__dirname, '..', '.env')
const schemaPath = path.join(__dirname, '..', 'prisma', 'schema.prisma')
const target = process.argv[2]
if (!['sqlite', 'postgresql'].includes(target)) {
console.error("Usage: node scripts/switch-db.js [sqlite|postgresql]")
process.exit(1)
}
// 1. Update schema.prisma
let schemaContent = fs.readFileSync(schemaPath, 'utf8')
// Find the datasource db block and replace the provider
schemaContent = schemaContent.replace(
/datasource db \{\s*provider\s*=\s*"[^"]+"/g,
`datasource db {\n provider = "${target}"`
)
fs.writeFileSync(schemaPath, schemaContent)
// 2. Update .env
let envContent = fs.existsSync(envPath) ? fs.readFileSync(envPath, 'utf8') : ''
const sqliteUrl = 'file:./dev.db'
const pgUrl = 'postgresql://postgres:postgres@localhost:5432/keep_notes?schema=public'
// Update or append DATABASE_URL
if (target === 'sqlite') {
if (envContent.match(/^DATABASE_URL=.*$/m)) {
envContent = envContent.replace(/^DATABASE_URL=.*$/m, `DATABASE_URL="${sqliteUrl}"`)
} else {
envContent += `\nDATABASE_URL="${sqliteUrl}"`
}
} else {
if (envContent.match(/^DATABASE_URL=.*$/m)) {
envContent = envContent.replace(/^DATABASE_URL=.*$/m, `DATABASE_URL="${pgUrl}"`)
} else {
envContent += `\nDATABASE_URL="${pgUrl}"`
}
}
fs.writeFileSync(envPath, envContent)
console.log(`✅ Successfully switched database provider to ${target}`)
console.log('You should now run:')
console.log(' npx prisma generate')
console.log(' npx prisma db push')