From d68be16688d7ae68300c33bba15b78d871828021 Mon Sep 17 00:00:00 2001 From: Roman Godmaire Date: Sun, 19 Nov 2023 12:37:09 -0500 Subject: [PATCH] misc: adapter for Database interactions --- src/app.d.ts | 8 +++----- src/hooks.server.ts | 3 +++ src/lib/server/db/index.ts | 3 +++ src/lib/server/db/prisma.ts | 23 +++++++++++++++++++++++ src/lib/server/s3.ts | 20 -------------------- src/lib/server/storage/index.ts | 2 +- src/lib/server/storage/s3.ts | 1 + 7 files changed, 34 insertions(+), 26 deletions(-) create mode 100644 src/lib/server/db/index.ts create mode 100644 src/lib/server/db/prisma.ts delete mode 100644 src/lib/server/s3.ts diff --git a/src/app.d.ts b/src/app.d.ts index c075c65..bba5ac7 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -1,14 +1,12 @@ -// See https://kit.svelte.dev/docs/types#app - -import type { PrismaClient } from '@prisma/client'; import type { Auth, AuthRequest } from 'lucia'; +import type { Database } from '$lib/server/db'; +import type { ObjectStorage } from '$lib/server/storage'; -// for information about these interfaces declare global { namespace App { // interface Error {} interface Locals { - database: PrismaClient; + database: Database; auth: Auth; objectStorage: ObjectStorage; diff --git a/src/hooks.server.ts b/src/hooks.server.ts index abfb510..17ef5ec 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -4,11 +4,14 @@ import { S3_STORAGE_URL } from '$env/static/private'; import { auth } from '$lib/server/lucia'; import { ObjectStorageS3 } from '$lib/server/storage/s3'; +import { DatabasePrisma } from '$lib/server/db/prisma'; const s3Client = new ObjectStorageS3(S3_STORAGE_URL); +const prismaClient = new DatabasePrisma(); export const handle: Handle = async ({ event, resolve }) => { event.locals.auth = auth; + event.locals.database = prismaClient; event.locals.objectStorage = s3Client; event.locals.authReq = auth.handleRequest(event); diff --git a/src/lib/server/db/index.ts b/src/lib/server/db/index.ts new file mode 100644 index 0000000..8de4b3a --- /dev/null +++ b/src/lib/server/db/index.ts @@ -0,0 +1,3 @@ +export interface Database { + listTracksWithProducer: (producerId: string) => Promise<{ title: string }[]>; +} diff --git a/src/lib/server/db/prisma.ts b/src/lib/server/db/prisma.ts new file mode 100644 index 0000000..599cf58 --- /dev/null +++ b/src/lib/server/db/prisma.ts @@ -0,0 +1,23 @@ +import type { Database } from '.'; +import { PrismaClient } from '@prisma/client'; + +export class DatabasePrisma implements Database { + client: PrismaClient; + + constructor() { + this.client = new PrismaClient(); + } + + listTracksWithProducer = async (producerId: string) => { + const tracks = await this.client.track.findMany({ + select: { + title: true + }, + where: { + producerId + } + }); + + return tracks; + }; +} diff --git a/src/lib/server/s3.ts b/src/lib/server/s3.ts deleted file mode 100644 index 92e9a1f..0000000 --- a/src/lib/server/s3.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3'; - -const client = new S3Client(); - -const uploadObject = async (obj: Buffer) => { - const command = new PutObjectCommand({ - Bucket: 'test-bucket', - Key: 'hello-s3.txt', - Body: obj - }); - - try { - await client.send(command); - return null; - } catch (err) { - return err; - } -}; - -export { uploadObject }; diff --git a/src/lib/server/storage/index.ts b/src/lib/server/storage/index.ts index 6e5e732..0a584bc 100644 --- a/src/lib/server/storage/index.ts +++ b/src/lib/server/storage/index.ts @@ -1,3 +1,3 @@ -interface ObjectStorage { +export interface ObjectStorage { putObject: (obj: Buffer) => Promise; } diff --git a/src/lib/server/storage/s3.ts b/src/lib/server/storage/s3.ts index dc33902..389f909 100644 --- a/src/lib/server/storage/s3.ts +++ b/src/lib/server/storage/s3.ts @@ -1,3 +1,4 @@ +import type { ObjectStorage } from '.'; import { PutObjectCommand, S3Client, S3ServiceException } from '@aws-sdk/client-s3'; class ObjectStorageS3 implements ObjectStorage {