Add Prisma integration with SQLite, update dependencies, and configure Svelte

This commit is contained in:
Norbert Maciaszek
2025-11-24 21:40:52 +01:00
parent ca22858160
commit 0a6e627e77
11 changed files with 1725 additions and 9 deletions

4
.gitignore vendored
View File

@@ -21,3 +21,7 @@ Thumbs.db
# Vite
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
/../generated/prisma
data.db

1499
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -20,12 +20,19 @@
"@tailwindcss/forms": "^0.5.10",
"@tailwindcss/typography": "^0.5.19",
"@tailwindcss/vite": "^4.1.17",
"daisyui": "^5.5.5",
"prettier": "^3.6.2",
"prettier-plugin-svelte": "^3.4.0",
"prisma": "^7.0.0",
"svelte": "^5.43.8",
"svelte-check": "^4.3.4",
"tailwindcss": "^4.1.17",
"typescript": "^5.9.3",
"vite": "^7.2.2"
},
"dependencies": {
"@prisma/adapter-better-sqlite3": "^7.0.0",
"@prisma/client": "^7.0.0",
"valibot": "^1.1.0"
}
}

14
prisma.config.ts Normal file
View File

@@ -0,0 +1,14 @@
// This file was generated by Prisma and assumes you have installed the following:
// npm install --save-dev prisma dotenv
import "dotenv/config";
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: env("DATABASE_URL"),
},
});

View File

@@ -0,0 +1,24 @@
-- CreateTable
CREATE TABLE "Year" (
"year" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
);
-- CreateTable
CREATE TABLE "Person" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"yearId" INTEGER NOT NULL,
CONSTRAINT "Person_yearId_fkey" FOREIGN KEY ("yearId") REFERENCES "Year" ("year") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "Gift" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"url" TEXT,
"description" TEXT,
"price" REAL,
"status" TEXT NOT NULL DEFAULT 'PLANNED',
"personId" INTEGER NOT NULL,
CONSTRAINT "Gift_personId_fkey" FOREIGN KEY ("personId") REFERENCES "Person" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

View File

@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (e.g., Git)
provider = "sqlite"

46
prisma/schema.prisma Normal file
View File

@@ -0,0 +1,46 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
}
model Year {
year Int @id
people Person[]
}
model Person {
id Int @id @default(autoincrement())
name String
yearId Int
year Year @relation(fields: [yearId], references: [year])
gifts Gift[]
}
model Gift {
id Int @id @default(autoincrement())
name String
url String?
description String?
price Float?
status GiftStatus @default(PLANNED)
personId Int
person Person @relation(fields: [personId], references: [id])
}
enum GiftStatus {
PLANNED
BUY
BOUGHT
WRAPPED
GIVEN
}

View File

@@ -0,0 +1,15 @@
import { command, query } from '$app/server';
import { DB } from '$lib/server/db';
import * as v from 'valibot';
export const getYears = query(async () => {
return await DB.GET.years();
});
export const addYear = command(v.number(), async (year) => {
await DB.CREATE.year({
year: year
});
getYears().refresh();
});

106
src/lib/server/db.ts Normal file
View File

@@ -0,0 +1,106 @@
import { DATABASE_URL } from '$env/static/private';
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3';
import type { Prisma } from '@prisma/client';
import { PrismaClient } from '@prisma/client';
const adapter = new PrismaBetterSqlite3({
url: DATABASE_URL
});
const db = new PrismaClient({ adapter });
const GET = {
year: async (where: Prisma.YearWhereUniqueInput) => {
return await db.year.findUnique({
where
});
},
years: async () => {
return await db.year.findMany();
},
person: async (where: Prisma.PersonWhereUniqueInput) => {
return await db.person.findUnique({
where
});
},
people: async (where: Prisma.PersonWhereUniqueInput) => {
return await db.person.findMany({
where
});
},
gift: async (where: Prisma.GiftWhereUniqueInput) => {
return await db.gift.findUnique({
where
});
},
gifts: async (where: Prisma.GiftWhereUniqueInput) => {
return await db.gift.findMany({
where
});
}
};
const CREATE = {
year: async (data: Prisma.YearCreateInput) => {
return await db.year.create({
data
});
},
person: async (data: Prisma.PersonCreateInput) => {
return await db.person.create({
data
});
},
gift: async (data: Prisma.GiftCreateInput) => {
return await db.gift.create({
data
});
}
};
const UPDATE = {
year: async (where: Prisma.YearWhereUniqueInput, data: Prisma.YearUpdateInput) => {
return await db.year.update({
where,
data
});
},
person: async (where: Prisma.PersonWhereUniqueInput, data: Prisma.PersonUpdateInput) => {
return await db.person.update({
where,
data
});
},
gift: async (where: Prisma.GiftWhereUniqueInput, data: Prisma.GiftUpdateInput) => {
return await db.gift.update({
where,
data
});
}
};
const DELETE = {
year: async (where: Prisma.YearWhereUniqueInput) => {
return await db.year.delete({
where
});
},
person: async (where: Prisma.PersonWhereUniqueInput) => {
return await db.person.delete({
where
});
},
gift: async (where: Prisma.GiftWhereUniqueInput) => {
return await db.gift.delete({
where
});
}
};
export const DB = {
GET,
CREATE,
UPDATE,
DELETE
};

View File

@@ -1,3 +1,4 @@
@import 'tailwindcss';
@plugin '@tailwindcss/forms';
@plugin '@tailwindcss/typography';
@plugin "daisyui";

View File

@@ -11,7 +11,20 @@ const config = {
// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
// See https://svelte.dev/docs/kit/adapters for more information about adapters.
adapter: adapter()
adapter: adapter(),
experimental: {
remoteFunctions: true
}
},
compilerOptions: {
warningFilter: (warning) => {
return !warning.message.startsWith('a11y_');
},
experimental: {
async: true
}
}
};