Skip to content

Commit

Permalink
something is broken somewhere with prisma.. investigating
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Einars <contact@dle.dev>
  • Loading branch information
polaroidkidd committed Oct 29, 2023
1 parent 60746c7 commit cb18b75
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
5 changes: 2 additions & 3 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ REDIS_URL="redis://default:redispw@localhost:6379"
REDIS_TOKEN=""

# Thumbor
PUBLIC_THUMBOR_URL="http://localhost:6501"
THUMBOR_UPLOAD_URL="http://localhost:6501/image"
# Images: Cloudflare
CLOUDFLARE_IMAGE_API_TOKEN=""
CLOUDFLARE_IMAGE_API=""
PUBLIC_CLOUDFLARE_IMAGE_DELIVERY=""
CLOUDFLARE_IMAGE_API="http://localhost:6501/image"
PUBLIC_CLOUDFLARE_IMAGE_DELIVERY="https://img.cloudkit.dle.dev"

IS_CI=false
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"private": true,
"type": "module",
"scripts": {
"prep": "docker-compose up -d && pnpm prisma:push:dev && pnpm psql:seed",
"dev": "pnpm prisma:gen:dev && vite dev",
"dev:prod": "pnpm prisma:gen:prod && vite dev",
"clean": "rimraf playwright-report && rimraf ./build && rimraf ./.svelte-kit",
"build:prod": "pnpm clean && pnpm prisma:gen:prod && vite build --mode production",
"build:ci": "pnpm clean && pnpm prisma:gen:ci && vite build --mode ci",
Expand All @@ -25,7 +27,8 @@
"prisma:gen:prod": "dotenv -e .env.production -- prisma generate --no-engine --schema ./prisma/prod.schema.prisma",
"prisma:push:dev": "dotenv -e .env.development -- prisma db push --schema ./prisma/dev.schema.prisma",
"prisma:push:prod": "dotenv -e .env.production -- prisma db push --schema ./prisma/prod.schema.prisma",
"prisma:studio": "dotenv -e .env.development -- prisma studio --schema ./prisma/schema.prisma",
"prisma:studio": "dotenv -e .env.development -- prisma studio --schema ./prisma/dev.schema.prisma",
"prisma:studio:prod": "dotenv -e .env.production -- prisma studio --schema ./prisma/prod.schema.prisma",
"psql:dump": "docker exec -i sk-db /bin/bash -c \"PGPASSWORD=pass123 pg_dump --username admin sk-db\" > ./sk-db-dump.sql",
"psql:restore": "docker exec -i sk-db /bin/bash -c \"PGPASSWORD=pass123 psql --username admin sk-db\" < /sk-db-dump.sql",
"psql:seed": "pnpm prisma:gen:dev && dotenv -e .env.development -- node prisma/seed.js",
Expand Down Expand Up @@ -95,4 +98,4 @@
"vitest": "0.34.6",
"wrangler": "^3.15.0"
}
}
}
36 changes: 24 additions & 12 deletions prisma/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,42 @@ const auth = lucia({
}
});

async function createImageSet(type) {
async function createImageInService(type) {
const id = uuid();
const image = await fetch('https://picsum.photos/736')
.then((response) => response.arrayBuffer())
.then((buffer) => sharp(buffer).webp({ quality: 80 }).toBuffer());

const path = `${type}/${id}`;
await fetch(`${process.env.THUMBOR_UPLOAD_URL}/${path}`, {
method: 'PUT',

const form = new FormData();
form.append('file', new Blob([image]), id);
form.append('id', path);

await fetch(process.env.CLOUDFLARE_IMAGE_API, {
method: 'POST',
headers: {
'Content-Type': 'image/webp',
Slug: `${id}.webp`
Authorization: `Bearer ${process.env.CLOUDFLARE_IMAGE_API_TOKEN}`
},
body: image
body: form
});
// await fetch(`${process.env.THUMBOR_UPLOAD_URL}/${path}`, {
// method: 'PUT',
// headers: {
// 'Content-Type': 'image/webp',
// Slug: `${id}.webp`
// },
// body: image
// });

return { path: `${process.env.THUMBOR_UPLOAD_URL}/${type}/${id}`, id: id };
return { path, id };
}

async function freshInit() {
await db.user.deleteMany({});
await db.image.deleteMany({});

const { path, id } = await createImageSet('avatars');
const { path, id } = await createImageInService('avatars');
const image = await db.image.create({
data: {
id: id,
Expand All @@ -68,11 +80,11 @@ async function freshInit() {
key: {
providerId: 'username',
providerUserId: 'admin@dle.dev',
password: 'adminadmin'
password: 'admin@dle.dev'
},
attributes: {
email: 'admin@dle.dev',
firstName: 'Svelte',
firstName: 'Daniel',
lastName: 'Kit',
bio: "I'm a full-stack web developer.",
avatarId: image.id,
Expand All @@ -84,8 +96,8 @@ async function freshInit() {
});

// Create 10 users
for (let i = 0; i < 2; i++) {
const { path, id } = await createImageSet('avatars');
for (let i = 0; i < 10; i++) {
const { path, id } = await createImageInService('avatars');
const image = await db.image.create({
data: {
id: id,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/atoms/media/avatar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
export let initials = '';
export let border = '';
export let isBase64 = false;
$: url = isBase64
let url = isBase64
? (src as string)
: `${PUBLIC_CLOUDFLARE_IMAGE_DELIVERY}/${src}/${isLarge ? 'public' : 'public'}`;
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/server/repository/prismaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { dev } from '$app/environment';
const prismaConfiguration = {
datasources: {
db: {
url: dev ? DATABASE_URL : DATA_PROXY
url: !dev ? DATABASE_URL : DATA_PROXY
}
}
};
Expand Down

0 comments on commit cb18b75

Please sign in to comment.