Skip to content

Commit

Permalink
refactor: 💡 schema.prisma
Browse files Browse the repository at this point in the history
  • Loading branch information
meetqy committed Jun 30, 2023
1 parent d586d1e commit b07c34f
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 105 deletions.
3 changes: 3 additions & 0 deletions packages/curd/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Config, ConfigInput } from "./src/config";
import { Folder, FolderInput } from "./src/folder";
import { Image, ImageInput } from "./src/image";
import { Library, LibraryInput } from "./src/library";
import { Tag, TagInput } from "./src/tag";

Expand All @@ -8,6 +9,7 @@ export const ZodInput = {
tag: TagInput,
config: ConfigInput,
library: LibraryInput,
image: ImageInput,
};

// library id => libraryId
Expand All @@ -31,6 +33,7 @@ const curd = {
tag: Tag,
config: Config,
library: Library,
image: Image,
};

export default curd;
178 changes: 90 additions & 88 deletions packages/curd/src/image.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,97 @@
// import { z } from "zod";
import { z } from "zod";

// import { CONSTANT } from "@acme/constant";
// import { prisma, type Prisma } from "@acme/db";
import { CONSTANT } from "@acme/constant";
import { prisma, type Prisma } from "@acme/db";

// export const ImageInput = {
// create: z.object({
// libraryId: z.number(),
// name: z.string(),
// size: z.number(),
// createTime: z.date(),
// lastTime: z.date(),
// ext: z.enum(CONSTANT.EXT),
// width: z.number(),
// height: z.number(),
// duration: z.number().optional(),
// noThumbnail: z.boolean().optional(),
// extendId: z.string().optional(),
// folders: z
// .array(
// z.object({
// id: z.string().optional(),
// name: z.string(),
// }),
// )
// .optional(),
// tags: z.array(z.string()).optional(),
// colors: z
// .array(
// z.object({
// rgb: z.number(),
// ratio: z.number(),
// }),
// )
// .optional(),
// }),
// };
export const ImageInput = {
create: z.object({
libraryId: z.number(),
path: z.string(),
thumbnailPath: z.string(),
name: z.string(),
size: z.number(),
createTime: z.date(),
lastTime: z.date(),
ext: z.enum(CONSTANT.EXT),
width: z.number(),
height: z.number(),
duration: z.number().optional(),
extendId: z.string().optional(),
folders: z
.array(
z.object({
id: z.string().optional(),
name: z.string(),
}),
)
.optional(),
tags: z.array(z.string()).optional(),
colors: z
.array(
z.object({
rgb: z.number(),
ratio: z.number(),
}),
)
.optional(),
}),
};

// export const Image = {
// create: (input: z.infer<(typeof ImageInput)["create"]>) => {
// let folders: Prisma.FolderUncheckedCreateNestedManyWithoutImagesInput | undefined;
// let tags: Prisma.TagUncheckedCreateNestedManyWithoutImagesInput | undefined;
// let colors: Prisma.ColorUncheckedCreateNestedManyWithoutImageInput | undefined;
export const Image = {
create: (input: z.infer<(typeof ImageInput)["create"]>) => {
let folders: Prisma.FolderUncheckedCreateNestedManyWithoutImagesInput | undefined;
let tags: Prisma.TagUncheckedCreateNestedManyWithoutImagesInput | undefined;
let colors: Prisma.ColorUncheckedCreateNestedManyWithoutImageInput | undefined;

// if (input.folders) {
// folders = {
// connectOrCreate: input.folders.map((folder) => ({
// where: { name: folder.name },
// create: {
// name: folder.name,
// // 传入 id 时,使用 id 作为 folder 的 id,否则使用 name 作为 id
// id: folder.id || folder.name,
// library: { connect: { id: input.libraryId } },
// },
// })),
// };
// }
if (input.folders) {
folders = {
connectOrCreate: input.folders.map((folder) => ({
where: { name: folder.name },
create: {
name: folder.name,
// 传入 id 时,使用 id 作为 folder 的 id,否则使用 name 作为 id
id: folder.id || folder.name,
library: { connect: { id: input.libraryId } },
},
})),
};
}

// if (input.tags) {
// tags = {
// connectOrCreate: input.tags.map((tag) => ({
// where: { name: tag },
// create: { name: tag, library: { connect: { id: input.libraryId } } },
// })),
// };
// }
if (input.tags) {
tags = {
connectOrCreate: input.tags.map((tag) => ({
where: { name: tag },
create: { name: tag, library: { connect: { id: input.libraryId } } },
})),
};
}

// if (input.colors) {
// colors = {
// connectOrCreate: input.colors.map((color) => ({
// where: { rgb: color.rgb },
// create: { rgb: color.rgb, ratio: color.ratio },
// })),
// };
// }
if (input.colors) {
colors = {
connectOrCreate: input.colors.map((color) => ({
where: { rgb: color.rgb },
create: { rgb: color.rgb, ratio: color.ratio },
})),
};
}

// return prisma.image.create({
// data: {
// libraryId: input.libraryId,
// name: input.name,
// size: input.size,
// createTime: input.createTime,
// lastTime: input.lastTime,
// ext: input.ext,
// width: input.width,
// height: input.height,
// duration: input.duration,
// noThumbnail: input.noThumbnail,
// folders: folders,
// tags,
// colors,
// },
// });
// },
// };
return prisma.image.create({
data: {
libraryId: input.libraryId,
path: input.path,
thumbnailPath: input.thumbnailPath,
name: input.name,
size: input.size,
createTime: input.createTime,
lastTime: input.lastTime,
ext: input.ext,
width: input.width,
height: input.height,
duration: input.duration,
folders: folders,
tags,
colors,
},
});
},
};
37 changes: 20 additions & 17 deletions packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,31 @@ datasource db {
}

model Image {
id String @id @unique
id Int @id @unique @default(autoincrement())
// library.dir + path => originalPath
path String @unique
// UserData 缓存目录 + thumbnailPath => thumbnailPath
thumbnailPath String? @unique
// 名字
name String
name String
// 文件大小
size Int
size Int
// 创建时间
createTime DateTime
createTime DateTime
// 修改时间
lastTime DateTime
lastTime DateTime
// 扩展名
ext String
width Int
height Int
ext String
width Int
height Int
// 视频时长
duration Float?
// 缩略图
noThumbnail Boolean?
duration Float?
// 文件夹
folders Folder[]
tags Tag[]
colors Color[]
library Library @relation(fields: [libraryId], references: [id])
libraryId Int
folders Folder[]
tags Tag[]
colors Color[]
library Library @relation(fields: [libraryId], references: [id])
libraryId Int
}

model Color {
Expand Down Expand Up @@ -66,8 +68,9 @@ model Tag {
}

model Folder {
// id = name,特殊情况 egale folder 有自己的id
id String @id @unique
name String
name String @unique
images Image[]
library Library @relation(fields: [libraryId], references: [id])
libraryId Int
Expand Down

0 comments on commit b07c34f

Please sign in to comment.