98 lines
2.8 KiB
TypeScript
98 lines
2.8 KiB
TypeScript
import { PrismaClient } from '@prisma/client'
|
|
import { readdirSync, readFileSync } from 'fs'
|
|
import { join, resolve } from 'path'
|
|
import { Pool } from 'pg'
|
|
import { PrismaPg } from '@prisma/adapter-pg'
|
|
|
|
const connectionString = process.env.DATABASE_URL
|
|
const pool = new Pool({ connectionString })
|
|
const adapter = new PrismaPg(pool)
|
|
|
|
const prisma = new PrismaClient({ adapter })
|
|
|
|
async function main() {
|
|
console.log('Ensuring system user exists...')
|
|
let systemUser = await prisma.user.findUnique({ where: { id: 'system' } })
|
|
if (!systemUser) {
|
|
systemUser = await prisma.user.create({
|
|
data: {
|
|
id: 'system',
|
|
username: 'system',
|
|
email: 'system@localhost',
|
|
password: 'none',
|
|
role: 'admin'
|
|
}
|
|
})
|
|
}
|
|
|
|
console.log('Seeding initial maps from .canvas files...')
|
|
|
|
const rootDir = resolve(process.cwd())
|
|
const files = readdirSync(rootDir).filter(f => f.endsWith('.canvas'))
|
|
|
|
// We'll assign these to the first admin user
|
|
const adminUser = await prisma.user.findFirst({ where: { role: 'admin' } })
|
|
const ownerId = adminUser?.id || '1'
|
|
|
|
for (const file of files) {
|
|
try {
|
|
const filePath = join(rootDir, file)
|
|
const data = JSON.parse(readFileSync(filePath, 'utf-8'))
|
|
const id = file.replace('.canvas', '')
|
|
|
|
const existing = await prisma.canvas.findUnique({ where: { id } })
|
|
|
|
if (!existing) {
|
|
await prisma.canvas.create({
|
|
data: {
|
|
id,
|
|
name: data.name || id,
|
|
ownerId,
|
|
isPublic: true, // Make public so anyone can view/edit
|
|
nodes: data.nodes || [],
|
|
edges: data.edges || []
|
|
}
|
|
})
|
|
console.log(`✅ Seeded map: ${id}`)
|
|
} else {
|
|
console.log(`⏩ Map ${id} already exists, skipping.`)
|
|
}
|
|
} catch (e) {
|
|
console.error(`❌ Failed to read or seed ${file}:`, e)
|
|
}
|
|
}
|
|
|
|
// Also seed the xflow-project from db.ts manually if it's not in the files
|
|
try {
|
|
const { canvases } = await import('../app/server/utils/db.js')
|
|
const xflowProject = canvases.get('xflow-project')
|
|
if (xflowProject) {
|
|
const existing = await prisma.canvas.findUnique({ where: { id: 'xflow-project' } })
|
|
if (!existing) {
|
|
await prisma.canvas.create({
|
|
data: {
|
|
id: xflowProject.id,
|
|
name: xflowProject.name,
|
|
ownerId: ownerId,
|
|
isPublic: true,
|
|
nodes: xflowProject.nodes as any,
|
|
edges: xflowProject.edges as any
|
|
}
|
|
})
|
|
console.log(`✅ Seeded map: xflow-project (from db.ts)`)
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.error('Could not seed xflow-project from db.ts', e)
|
|
}
|
|
}
|
|
|
|
main()
|
|
.catch(e => {
|
|
console.error(e)
|
|
process.exit(1)
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect()
|
|
})
|