misc: adapter for Database interactions
This commit is contained in:
parent
da068dcc12
commit
d68be16688
7 changed files with 34 additions and 26 deletions
8
src/app.d.ts
vendored
8
src/app.d.ts
vendored
|
@ -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 { 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 {
|
declare global {
|
||||||
namespace App {
|
namespace App {
|
||||||
// interface Error {}
|
// interface Error {}
|
||||||
interface Locals {
|
interface Locals {
|
||||||
database: PrismaClient;
|
database: Database;
|
||||||
auth: Auth;
|
auth: Auth;
|
||||||
objectStorage: ObjectStorage;
|
objectStorage: ObjectStorage;
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,14 @@ import { S3_STORAGE_URL } from '$env/static/private';
|
||||||
|
|
||||||
import { auth } from '$lib/server/lucia';
|
import { auth } from '$lib/server/lucia';
|
||||||
import { ObjectStorageS3 } from '$lib/server/storage/s3';
|
import { ObjectStorageS3 } from '$lib/server/storage/s3';
|
||||||
|
import { DatabasePrisma } from '$lib/server/db/prisma';
|
||||||
|
|
||||||
const s3Client = new ObjectStorageS3(S3_STORAGE_URL);
|
const s3Client = new ObjectStorageS3(S3_STORAGE_URL);
|
||||||
|
const prismaClient = new DatabasePrisma();
|
||||||
|
|
||||||
export const handle: Handle = async ({ event, resolve }) => {
|
export const handle: Handle = async ({ event, resolve }) => {
|
||||||
event.locals.auth = auth;
|
event.locals.auth = auth;
|
||||||
|
event.locals.database = prismaClient;
|
||||||
event.locals.objectStorage = s3Client;
|
event.locals.objectStorage = s3Client;
|
||||||
|
|
||||||
event.locals.authReq = auth.handleRequest(event);
|
event.locals.authReq = auth.handleRequest(event);
|
||||||
|
|
3
src/lib/server/db/index.ts
Normal file
3
src/lib/server/db/index.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export interface Database {
|
||||||
|
listTracksWithProducer: (producerId: string) => Promise<{ title: string }[]>;
|
||||||
|
}
|
23
src/lib/server/db/prisma.ts
Normal file
23
src/lib/server/db/prisma.ts
Normal 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;
|
||||||
|
};
|
||||||
|
}
|
|
@ -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 };
|
|
|
@ -1,3 +1,3 @@
|
||||||
interface ObjectStorage {
|
export interface ObjectStorage {
|
||||||
putObject: (obj: Buffer) => Promise<Error | null>;
|
putObject: (obj: Buffer) => Promise<Error | null>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import type { ObjectStorage } from '.';
|
||||||
import { PutObjectCommand, S3Client, S3ServiceException } from '@aws-sdk/client-s3';
|
import { PutObjectCommand, S3Client, S3ServiceException } from '@aws-sdk/client-s3';
|
||||||
|
|
||||||
class ObjectStorageS3 implements ObjectStorage {
|
class ObjectStorageS3 implements ObjectStorage {
|
||||||
|
|
Loading…
Reference in a new issue