misc: adapter for Database interactions

This commit is contained in:
Roman Godmaire 2023-11-19 12:37:09 -05:00
parent da068dcc12
commit d68be16688
7 changed files with 34 additions and 26 deletions

8
src/app.d.ts vendored
View file

@ -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;

View file

@ -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);

View file

@ -0,0 +1,3 @@
export interface Database {
listTracksWithProducer: (producerId: string) => Promise<{ title: string }[]>;
}

View file

@ -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;
};
}

View file

@ -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 };

View file

@ -1,3 +1,3 @@
interface ObjectStorage {
export interface ObjectStorage {
putObject: (obj: Buffer) => Promise<Error | null>;
}

View file

@ -1,3 +1,4 @@
import type { ObjectStorage } from '.';
import { PutObjectCommand, S3Client, S3ServiceException } from '@aws-sdk/client-s3';
class ObjectStorageS3 implements ObjectStorage {