Add prisma to blog, as data soruce

This commit is contained in:
wilson 2026-03-01 08:52:30 +00:00
parent 9730ba356b
commit c9feb54cd4
15 changed files with 6312 additions and 3712 deletions

3
.gitignore vendored
View file

@ -9,3 +9,6 @@ node_modules
.env .env
.env.* .env.*
!.env.example !.env.example
generated/prisma
dev.db

View file

@ -8,22 +8,26 @@
"preview": "vite preview", "preview": "vite preview",
"check": "svelte-check --tsconfig ./tsconfig.json", "check": "svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-check --tsconfig ./tsconfig.json --watch", "check:watch": "svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --ignore-path .gitignore --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .", "lint": "prettier --check --plugin-search-dir=. . && eslint --ignore-path .gitignore .",
"format": "prettier --ignore-path .gitignore --write --plugin-search-dir=. .", "format": "prettier --config ./prettierrc --write --plugin-search-dir=. .",
"test": "vitest" "test": "vitest"
}, },
"devDependencies": { "devDependencies": {
"@sveltejs/kit": "^2.51.0", "@sveltejs/kit": "^2.51.0",
"@sveltejs/vite-plugin-svelte": "^5.0.3", "@sveltejs/vite-plugin-svelte": "^5.0.3",
"@types/better-sqlite3": "^7.6.13",
"@types/leaflet": "^1.9.15", "@types/leaflet": "^1.9.15",
"@types/node": "^25.3.2",
"@types/sanitize-html": "^2.13.0", "@types/sanitize-html": "^2.13.0",
"@typescript-eslint/eslint-plugin": "^8.55.0", "@typescript-eslint/eslint-plugin": "^8.55.0",
"@typescript-eslint/parser": "^8.55.0", "@typescript-eslint/parser": "^8.55.0",
"dotenv": "^17.3.1",
"eslint": "^9.17.0", "eslint": "^9.17.0",
"eslint-config-prettier": "^10.1.1", "eslint-config-prettier": "^10.1.1",
"eslint-plugin-svelte": "^3.15.0", "eslint-plugin-svelte": "^3.15.0",
"prettier": "^3.4.2", "prettier": "^3.4.2",
"prettier-plugin-svelte": "^3.3.2", "prettier-plugin-svelte": "^3.3.2",
"prisma": "^7.4.2",
"svelte": "^5.50.3", "svelte": "^5.50.3",
"svelte-check": "^4.3.6", "svelte-check": "^4.3.6",
"svelte-preprocess": "^6.0.0", "svelte-preprocess": "^6.0.0",
@ -34,6 +38,8 @@
}, },
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@prisma/adapter-better-sqlite3": "^7.4.2",
"@prisma/client": "^7.4.2",
"@sveltejs/adapter-node": "^5.5.3", "@sveltejs/adapter-node": "^5.5.3",
"@types/js-yaml": "^4.0.9", "@types/js-yaml": "^4.0.9",
"date-fns": "^4.1.0", "date-fns": "^4.1.0",

File diff suppressed because it is too large Load diff

4
pnpm-workspace.yaml Normal file
View file

@ -0,0 +1,4 @@
onlyBuiltDependencies:
- '@prisma/engines'
- better-sqlite3
- prisma

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 } from "prisma/config";
export default defineConfig({
schema: "./prisma/schema.prisma",
migrations: {
path: "prisma/migrations",
},
datasource: {
url: process.env["DATABASE_URL"],
},
});

13
prisma/schema.prisma Normal file
View file

@ -0,0 +1,13 @@
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "sqlite"
}
model PhotoPost {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
}

9
src/app.d.ts vendored
View file

@ -1,10 +1,15 @@
/// <reference types="@sveltejs/kit" /> /// <reference types="@sveltejs/kit" />
import type { PrismaClient } from '../generated/prisma/client.ts';
// See https://kit.svelte.dev/docs/types#app // See https://kit.svelte.dev/docs/types#app
// for information about these interfaces // for information about these interfaces
declare namespace App { declare global {
// interface Locals {} namespace App {
interface Locals {
prisma: PrismaClient;
}
// interface Platform {} // interface Platform {}
// interface Session {} // interface Session {}
// interface Stuff {} // interface Stuff {}
} }
}

9
src/hooks.server.ts Normal file
View file

@ -0,0 +1,9 @@
import type { Handle } from '@sveltejs/kit';
import { PrismaClientFactory } from './prisma/PrismaClientFactory.js';
export const handle: Handle = async ({ event, resolve }) => {
const prismaClient = PrismaClientFactory.fromEnv().createClient();
event.locals.prisma = prismaClient;
const response = await resolve(event);
return response;
};

View file

@ -0,0 +1,20 @@
import { notStrictEqual } from 'node:assert';
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3';
import { PrismaClient } from '../../generated/prisma/client.js';
import { DATABASE_URL } from '$env/static/private';
export class PrismaClientFactory {
private constructor(private readonly databaseUrl: string) {}
public static fromEnv(): PrismaClientFactory {
const value = DATABASE_URL ?? '';
notStrictEqual(value, '', `"env.DATABASE_URL" must be defined`);
return new PrismaClientFactory(value);
}
createClient(): PrismaClient {
const adapter = new PrismaBetterSqlite3({ url: this.databaseUrl });
const prisma = new PrismaClient({ adapter });
return prisma;
}
}

View file

@ -0,0 +1,10 @@
import type { PageServerLoad } from './$types.js';
export const load: PageServerLoad = async ({ locals }) => {
const allPosts = await locals.prisma.photoPost.findMany({
orderBy: { createdAt: 'asc' },
});
return {
allPosts,
};
};

View file

@ -0,0 +1,8 @@
<script lang="ts">
import type { PageProps } from "./$types.js";
const { data }: PageProps = $props();
</script>
<h1>Photo Posts</h1>
<p>You have {data.allPosts.length} Photo Posts</p>

View file

@ -11,9 +11,9 @@ const config = {
kit: { kit: {
adapter: adapter({ split: false }), adapter: adapter({ split: false }),
env: { env: {
publicPrefix: 'PUBLIC_' publicPrefix: 'PUBLIC_',
} },
} },
}; };
export default config; export default config;

View file

@ -6,8 +6,9 @@ const config = {
resolve: { resolve: {
alias: { alias: {
$lib: '/src/lib', $lib: '/src/lib',
} $srcPrisma: '/src/prisma',
} },
},
}; };
export default config; export default config;

View file

@ -3,13 +3,12 @@ export default {
resolve: { resolve: {
alias: { alias: {
$lib: '/src/lib', $lib: '/src/lib',
} $srcPrisma: '/src/prisma',
},
}, },
test: { test: {
deps: { deps: {
inline: [ inline: ['date-fns'],
"date-fns" },
] },
} };
}
}

View file

@ -19,4 +19,5 @@ COPY --from=builder /app/node_modules node_modules/
COPY package.json . COPY package.json .
EXPOSE 3000 EXPOSE 3000
ENV NODE_ENV=production ENV NODE_ENV=production
VOLUME /data
CMD [ "node", "build" ] CMD [ "node", "build" ]