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

@ -1,61 +1,67 @@
{ {
"name": "thomaswilson-sveltekit", "name": "thomaswilson-sveltekit",
"license": "UNLICENSED", "license": "UNLICENSED",
"version": "1.0.0", "version": "1.0.0",
"scripts": { "scripts": {
"dev": "vite dev", "dev": "vite dev",
"build": "vite build", "build": "vite build",
"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/leaflet": "^1.9.15", "@types/better-sqlite3": "^7.6.13",
"@types/sanitize-html": "^2.13.0", "@types/leaflet": "^1.9.15",
"@typescript-eslint/eslint-plugin": "^8.55.0", "@types/node": "^25.3.2",
"@typescript-eslint/parser": "^8.55.0", "@types/sanitize-html": "^2.13.0",
"eslint": "^9.17.0", "@typescript-eslint/eslint-plugin": "^8.55.0",
"eslint-config-prettier": "^10.1.1", "@typescript-eslint/parser": "^8.55.0",
"eslint-plugin-svelte": "^3.15.0", "dotenv": "^17.3.1",
"prettier": "^3.4.2", "eslint": "^9.17.0",
"prettier-plugin-svelte": "^3.3.2", "eslint-config-prettier": "^10.1.1",
"svelte": "^5.50.3", "eslint-plugin-svelte": "^3.15.0",
"svelte-check": "^4.3.6", "prettier": "^3.4.2",
"svelte-preprocess": "^6.0.0", "prettier-plugin-svelte": "^3.3.2",
"tslib": "^2.8.1", "prisma": "^7.4.2",
"typescript": "^5.7.2", "svelte": "^5.50.3",
"vite": "^6.0.7", "svelte-check": "^4.3.6",
"vitest": "^3.0.8" "svelte-preprocess": "^6.0.0",
}, "tslib": "^2.8.1",
"type": "module", "typescript": "^5.7.2",
"dependencies": { "vite": "^6.0.7",
"@sveltejs/adapter-node": "^5.5.3", "vitest": "^3.0.8"
"@types/js-yaml": "^4.0.9", },
"date-fns": "^4.1.0", "type": "module",
"feed": "^4.2.2", "dependencies": {
"js-yaml": "^4.1.0", "@prisma/adapter-better-sqlite3": "^7.4.2",
"just-shuffle": "^4.2.0", "@prisma/client": "^7.4.2",
"leaflet": "^1.9.4", "@sveltejs/adapter-node": "^5.5.3",
"node-fetch": "^3.3.2", "@types/js-yaml": "^4.0.9",
"rehype-stringify": "^10.0.1", "date-fns": "^4.1.0",
"remark": "^15.0.1", "feed": "^4.2.2",
"remark-frontmatter": "^5.0.0", "js-yaml": "^4.1.0",
"remark-gfm": "^4.0.0", "just-shuffle": "^4.2.0",
"remark-parse": "^11.0.0", "leaflet": "^1.9.4",
"remark-rehype": "^11.1.1", "node-fetch": "^3.3.2",
"remark-stringify": "^11.0.0", "rehype-stringify": "^10.0.1",
"sanitize-html": "^2.14.0", "remark": "^15.0.1",
"sass": "^1.85.1", "remark-frontmatter": "^5.0.0",
"strip-markdown": "^6.0.0", "remark-gfm": "^4.0.0",
"unified": "^11.0.5", "remark-parse": "^11.0.0",
"zod": "^3.24.1" "remark-rehype": "^11.1.1",
}, "remark-stringify": "^11.0.0",
"engines": { "sanitize-html": "^2.14.0",
"node": ">=22.0.0" "sass": "^1.85.1",
} "strip-markdown": "^6.0.0",
"unified": "^11.0.5",
"zod": "^3.24.1"
},
"engines": {
"node": ">=22.0.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())
}

15
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 Platform {} interface Locals {
// interface Session {} prisma: PrismaClient;
// interface Stuff {} }
// interface Platform {}
// interface Session {}
// 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

@ -3,17 +3,17 @@ import preprocess from 'svelte-preprocess';
/** @type {import('@sveltejs/kit').Config} */ /** @type {import('@sveltejs/kit').Config} */
const config = { const config = {
extensions: ['.svelte', '.md'], extensions: ['.svelte', '.md'],
// Consult https://github.com/sveltejs/svelte-preprocess // Consult https://github.com/sveltejs/svelte-preprocess
// for more information about preprocessors // for more information about preprocessors
preprocess: [preprocess()], preprocess: [preprocess()],
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" ]