Canvix — Free & open-source graphic design tool. A community-built Canva alternative with a drag-and-drop editor, templates, shapes, text, images & one-click export. Works on desktop, tablet & mobile.
TypeScript canva-alternativecanvasdesigndesign-tooldrag-and-dropeditorgraphic-designkonvanextjsopen-sourcereactself-hostedtemplatestypescript
feat(db): migrate Prisma from SQLite to PostgreSQL
Ashiq Hussain Mir00c61bc3fdfe
diff --git a/.env.example b/.env.examplenew file mode 100644index 0000000..3007eed--- /dev/null+++ b/.env.example@@ -0,0 +1,11 @@+# Canvix — environment variables+#+# PostgreSQL connection string. Works with any Postgres server:+# - local install: postgresql://user:password@localhost:5432/canvix+# - Docker: postgresql://user:password@localhost:5432/canvix+# - Neon (serverless): postgresql://user:[email protected]/canvix?sslmode=require+# - Supabase / RDS / etc: same format+#+# Local development quick-start (Docker):+# docker run --name canvix-pg -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=canvix -p 5432:5432 -d postgres:17+DATABASE_URL=postgresql://user:password@localhost:5432/canvixdiff --git a/.gitignore b/.gitignoreindex baf6d49..19315bb 100644--- a/.gitignore+++ b/.gitignore@@ -32,6 +32,7 @@ yarn-error.log*# env files (can opt-in for committing if needed).env*+!.env.example# vercel.vercel@@ -68,6 +69,7 @@ download/tests/examples/agent-ctx/+tools/*.pid.env.localmini-services/diff --git a/README.md b/README.mdindex 3f40162..c7058d9 100644--- a/README.md+++ b/README.md@@ -151,7 +151,7 @@ watermarks, no paywalls.## 🚀 Quick start-> **Prerequisites:** [Node.js 20+](https://nodejs.org) or [Bun](https://bun.sh), and a SQLite-capable machine (any machine).+> **Prerequisites:** [Node.js 20+](https://nodejs.org) or [Bun](https://bun.sh), and a [PostgreSQL](https://www.postgresql.org) database (local install, Docker, or a free serverless host like [Neon](https://neon.tech)).```bashgit clone https://github.com/mir-ashiq/canvix.git@@ -159,7 +159,7 @@ cd canvixbun install # or: npm install# set up the database-cp .env.example .env # then edit DATABASE_URL if you like+cp .env.example .env # then edit DATABASE_URL with your Postgres connection stringbun run db:push # create tablesbun run db:seed # load the built-in templates@@ -181,7 +181,7 @@ bun run start| Styling | Tailwind CSS 4 + shadcn/ui || Canvas engine | [Konva.js](https://konvajs.org) via react-konva || State | Zustand |-| Database | Prisma ORM + SQLite |+| Database | Prisma ORM + PostgreSQL (jsonb) || Fonts | 15 hand-picked Google Fonts |## 📁 Project structurediff --git a/package.json b/package.jsonindex c72312e..9a9140c 100644--- a/package.json+++ b/package.json@@ -8,7 +8,7 @@"build": "next build && cp -r .next/static .next/standalone/.next/ && cp -r public .next/standalone/","start": "NODE_ENV=production bun .next/standalone/server.js 2>&1 | tee server.log","lint": "eslint .",- "db:push": "prisma db push --accept-data-loss",+ "db:push": "prisma db push","db:generate": "prisma generate","db:seed": "bun prisma/seed.ts","db:migrate": "prisma migrate dev",diff --git a/prisma/schema.prisma b/prisma/schema.prismaindex 3ceed77..86d1dbe 100644--- a/prisma/schema.prisma+++ b/prisma/schema.prisma@@ -3,7 +3,7 @@ generator client {}datasource db {- provider = "sqlite"+ provider = "postgresql"url = env("DATABASE_URL")}@@ -13,8 +13,8 @@ model Design {name String @default("Untitled design")width Int @default(1080)height Int @default(1080)- // Serialized JSON: { pages: PageData[], ... }- pages String @default("[]")+ // Structured document: { pages: PageData[], ... } — stored as native jsonb+ pages Json @default("[]")thumbnail String? // small dataURL previewsource String @default("blank") // blank | template:<id> | duplicate:<id>createdAt DateTime @default(now())@@ -29,7 +29,7 @@ model Template {category String // social | presentation | print | logo | thumbnailwidth Intheight Int- pages String // serialized JSON — same shape as Design.pages+ pages Json // structured document — same shape as Design.pagesaccent String @default("#00C4CC") // card decorationcreatedAt DateTime @default(now())}diff --git a/prisma/seed.ts b/prisma/seed.tsindex cd461b9..d3e88f2 100644--- a/prisma/seed.ts+++ b/prisma/seed.ts@@ -1,10 +1,14 @@// Seed the Canvix database with built-in templates.// Run: bun run db:seed-import { PrismaClient } from '@prisma/client'+import { PrismaClient, Prisma } from '@prisma/client'import { TEMPLATES } from '../src/lib/templates'const db = new PrismaClient()+// Typed template list cast for the Postgres jsonb column.+const pagesOf = (t: (typeof TEMPLATES)[number]) =>+ t.pages as unknown as Prisma.InputJsonValue+async function main() {console.log(`Seeding ${TEMPLATES.length} templates…`)for (const t of TEMPLATES) {@@ -16,7 +20,7 @@ async function main() {width: t.width,height: t.height,accent: t.accent,- pages: JSON.stringify(t.pages),+ pages: pagesOf(t),},create: {slug: t.slug,@@ -25,7 +29,7 @@ async function main() {width: t.width,height: t.height,accent: t.accent,- pages: JSON.stringify(t.pages),+ pages: pagesOf(t),},})console.log(` ✓ ${t.slug}`)diff --git a/src/app/api/designs/[id]/route.ts b/src/app/api/designs/[id]/route.tsindex c5baf58..ff6c559 100644--- a/src/app/api/designs/[id]/route.ts+++ b/src/app/api/designs/[id]/route.ts@@ -1,13 +1,23 @@import { NextRequest, NextResponse } from 'next/server'+import { Prisma } from '@prisma/client'import { db } from '@/lib/db'+// Normalise incoming pages payload (accepts object or legacy JSON string)+// into a value suitable for the Postgres jsonb column.+function toPages(value: unknown): Prisma.InputJsonValue {+ if (typeof value === 'string') {+ try { return JSON.parse(value) as Prisma.InputJsonValue } catch { return [] }+ }+ return (value ?? []) as Prisma.InputJsonValue+}+// GET /api/designs/[id] — full design incl. pagesexport async function GET(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) {try {const { id } = await paramsconst design = await db.design.findUnique({ where: { id } })if (!design) return NextResponse.json({ error: 'Not found' }, { status: 404 })- return NextResponse.json({ ...design, pages: JSON.parse(design.pages || '[]') })+ return NextResponse.json({ ...design, pages: design.pages ?? [] })} catch (error) {console.error('GET /api/designs/[id] failed', error)return NextResponse.json({ error: 'Failed to get design' }, { status: 500 })@@ -21,7 +31,7 @@ export async function PUT(req: NextRequest, { params }: { params: Promise<{ id:const body = await req.json()const data: Record<string, unknown> = {}if (typeof body.name === 'string' && body.name.trim()) data.name = body.name.trim().slice(0, 120)- if (body.pages !== undefined) data.pages = typeof body.pages === 'string' ? body.pages : JSON.stringify(body.pages)+ if (body.pages !== undefined) data.pages = toPages(body.pages)if (body.thumbnail !== undefined) data.thumbnail = typeof body.thumbnail === 'string' ? body.thumbnail : nullconst design = await db.design.update({ where: { id }, data })return NextResponse.json({diff --git a/src/app/api/designs/route.ts b/src/app/api/designs/route.tsindex ef547d0..a0ed2bc 100644--- a/src/app/api/designs/route.ts+++ b/src/app/api/designs/route.ts@@ -1,6 +1,16 @@import { NextRequest, NextResponse } from 'next/server'+import { Prisma } from '@prisma/client'import { db } from '@/lib/db'+// Normalise incoming pages payload (accepts object or legacy JSON string)+// into a value suitable for the Postgres jsonb column.+function toPages(value: unknown): Prisma.InputJsonValue {+ if (typeof value === 'string') {+ try { return JSON.parse(value) as Prisma.InputJsonValue } catch { return [] }+ }+ return (value ?? []) as Prisma.InputJsonValue+}+// GET /api/designs — list all designs (metadata only, newest first)export async function GET() {try {@@ -33,7 +43,7 @@ export async function POST(req: NextRequest) {name: typeof body.name === 'string' && body.name.trim() ? body.name.trim() : 'Untitled design',width: Number.isFinite(body.width) ? Math.round(body.width) : 1080,height: Number.isFinite(body.height) ? Math.round(body.height) : 1080,- pages: typeof body.pages === 'string' ? body.pages : JSON.stringify(body.pages ?? []),+ pages: toPages(body.pages),source: typeof body.source === 'string' ? body.source.slice(0, 120) : 'blank',thumbnail: typeof body.thumbnail === 'string' ? body.thumbnail : null,},diff --git a/src/app/api/templates/route.ts b/src/app/api/templates/route.tsindex f2af98a..980ecde 100644--- a/src/app/api/templates/route.ts+++ b/src/app/api/templates/route.ts@@ -16,7 +16,7 @@ export async function GET() {width: t.width,height: t.height,accent: t.accent,- pages: JSON.parse(t.pages || '[]'),+ pages: t.pages ?? [],})))} catch (error) {