Skip to content

Commit

Permalink
Fix some types
Browse files Browse the repository at this point in the history
  • Loading branch information
jpalumickas committed Mar 27, 2023
1 parent f83b8e2 commit 1396ba7
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
dist
yarn-error.log
docs/out
packages/adapter-prisma/.env
10 changes: 8 additions & 2 deletions packages/adapter-prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
"publishConfig": {
"access": "public"
},
"files": [
"dist",
"src"
],
"license": "MIT",
"repository": "jpalumickas/uplo",
"homepage": "https://uplo.js.org",
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts"
"build": "tsup src/index.ts --format cjs,esm --dts",
"generate": "prisma generate"
},
"peerDependencies": {
"@prisma/client": ">=2.19.0",
Expand All @@ -30,7 +35,8 @@
"devDependencies": {
"@prisma/client": "^4.11.0",
"@uplo/node": "workspace:^0.17.8",
"dataloader": "^2.2.2"
"dataloader": "^2.2.2",
"prisma": "^4.11.0"
},
"engines": {
"node": ">=14"
Expand Down
40 changes: 40 additions & 0 deletions packages/adapter-prisma/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model FileAttachment {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
name String @db.VarChar
recordType String @db.VarChar
recordId String @db.Uuid
blobId String @db.Uuid
createdAt DateTime @default(now()) @db.Timestamptz(6)
blob FileBlob @relation(fields: [blobId], references: [id])
@@unique([recordType, recordId, name, blobId])
@@index([recordType, recordId, name])
@@index([blobId])
}

model FileBlob {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
serviceName String @db.VarChar
key String @unique @db.VarChar
fileName String @db.VarChar
contentType String? @db.VarChar
size BigInt
checksum String @db.VarChar
metadata Json @default("{}")
createdAt DateTime @default(now()) @db.Timestamptz(6)
attachments FileAttachment[]
}
16 changes: 9 additions & 7 deletions packages/adapter-prisma/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class PrismaAdapter implements Adapter {
async deleteAttachment(id: ID): Promise<AttachmentData | null> {
const result = await this.prisma.fileAttachment.delete({
where: {
id,
id: id as string,
},
});

Expand All @@ -57,13 +57,15 @@ class PrismaAdapter implements Adapter {
recordType: string;
name: string;
}): Promise<AttachmentData[]> {
return await this.prisma.fileAttachment.deleteMany({
const result = await this.prisma.fileAttachment.deleteMany({
where: {
recordId,
recordId: recordId as string,
recordType,
name,
},
});

return result as unknown as AttachmentData[]
}

async createBlob({ params }: CreateBlobOptions): Promise<BlobData> {
Expand All @@ -84,7 +86,7 @@ class PrismaAdapter implements Adapter {

async findBlob(id: string | number): Promise<BlobData | null> {
return (await this.prisma.fileBlob.findUnique({
where: { id },
where: { id: id as string },
})) as BlobData | null;
}

Expand Down Expand Up @@ -133,7 +135,7 @@ class PrismaAdapter implements Adapter {
where: {
name: attachmentName,
recordType,
recordId,
recordId: recordId as string,
},
});
}
Expand All @@ -142,8 +144,8 @@ class PrismaAdapter implements Adapter {
data: {
name: attachmentName,
recordType,
recordId,
blob: { connect: { id: blob.id } },
recordId: recordId as string,
blob: { connect: { id: blob.id as string } },
},
include: { blob: true },
});
Expand Down
53 changes: 26 additions & 27 deletions packages/adapter-prisma/src/loaders/findAttachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,45 @@ interface FindAttachmentsRecordData {
name: string;
}

const SEPARATOR = '##__--__##'
const SEPARATOR = '##__--__##';
type GroupReturn = {
[groupName: string]: FindAttachmentsRecordData[]
}
[groupName: string]: FindAttachmentsRecordData[];
};

const groupByTypeAndName = (data: Readonly<FindAttachmentsRecordData[]>) => {
return data.reduce<GroupReturn>((obj, item) => {
const groupName = [item.recordType, item.name].join(SEPARATOR);
if (!obj[groupName]) {
obj[groupName] = []
obj[groupName] = [];
}

obj[groupName].push(item);
return obj
}, {})

}
return obj;
}, {});
};

export const initFindAttachmentsLoader = (prisma: PrismaClient) =>
new DataLoader(async (recordData: Readonly<FindAttachmentsRecordData[]>) => {
const grouped = groupByTypeAndName(recordData);
const OR = Object.keys(grouped).map(groupName => {
const [recordType, name] = groupName.split(SEPARATOR)
return {
recordType,
name,
recordId: { in: grouped[groupName].map(item => item.recordId) },
}

});

const fileAttachments: AttachmentData[] =
await prisma.fileAttachment.findMany({
where: {
OR,
},
include: {
blob: true,
const grouped = groupByTypeAndName(recordData);
const OR = Object.keys(grouped).map((groupName) => {
const [recordType, name] = groupName.split(SEPARATOR);
return {
recordType,
name,
recordId: {
in: grouped[groupName].map((item) => item.recordId as string),
},
});
};
});

const fileAttachments = (await prisma.fileAttachment.findMany({
where: {
OR,
},
include: {
blob: true,
},
})) as AttachmentData[];

const result = recordData.map((recordItem) => {
return fileAttachments.filter(
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"publishConfig": {
"access": "public"
},
"files": [
"dist",
"src"
],
"license": "MIT",
"repository": "jpalumickas/uplo",
"homepage": "https://uplo.js.org",
Expand Down
21 changes: 19 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1396ba7

Please sign in to comment.