diff --git a/packages/curd/index.ts b/packages/curd/index.ts index f345d9a1..0cdaf19f 100644 --- a/packages/curd/index.ts +++ b/packages/curd/index.ts @@ -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"; @@ -8,6 +9,7 @@ export const ZodInput = { tag: TagInput, config: ConfigInput, library: LibraryInput, + image: ImageInput, }; // library id => libraryId @@ -31,6 +33,7 @@ const curd = { tag: Tag, config: Config, library: Library, + image: Image, }; export default curd; diff --git a/packages/curd/src/image.ts b/packages/curd/src/image.ts index ce23e0e4..fddffe8a 100644 --- a/packages/curd/src/image.ts +++ b/packages/curd/src/image.ts @@ -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, + }, + }); + }, +}; diff --git a/packages/db/prisma/schema.prisma b/packages/db/prisma/schema.prisma index 51752998..84912854 100644 --- a/packages/db/prisma/schema.prisma +++ b/packages/db/prisma/schema.prisma @@ -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 { @@ -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