diff --git a/src/api/structures/common/IRecordMerge.ts b/src/api/structures/common/IRecordMerge.ts index bd8b987..837e4e5 100644 --- a/src/api/structures/common/IRecordMerge.ts +++ b/src/api/structures/common/IRecordMerge.ts @@ -5,9 +5,9 @@ import { tags } from "typia"; * * `IRecordMerge` is a structure for merging records. * - * The `merge` means that merging multiple {@link IRecordMerge.merged} + * The `merge` means that merging multiple {@link IRecordMerge.absorbed} * records into {@link IRecordMerge.keep} instead of deleting - * {@link IRecordMerge.merged} records. + * {@link IRecordMerge.absorbed} records. * * If there're some dependent tables of the target `table` having * unique constraint on foriegn key column, such dependent tables @@ -24,13 +24,13 @@ export interface IRecordMerge { /** * Target record to keep after merging. * - * After merge process, {@link merged} records would be merged into + * After merge process, {@link absorbed} records would be merged into * this {@link keep} record. */ keep: string & tags.Format<"uuid">; /** - * To be merged to {@link keep} after merging. + * To be absorbed to {@link keep} after merging. */ - merged: Array>; + absorbed: Array>; } diff --git a/src/providers/common/AttachmentFileProvider.ts b/src/providers/common/AttachmentFileProvider.ts index 64caeea..6d28d1d 100644 --- a/src/providers/common/AttachmentFileProvider.ts +++ b/src/providers/common/AttachmentFileProvider.ts @@ -19,15 +19,12 @@ export namespace AttachmentFileProvider { Prisma.validator()({}); } - export function collect( - input: IAttachmentFile.IStore, - ): Prisma.attachment_filesCreateInput { - return { + export const collect = (input: IAttachmentFile.IStore) => + Prisma.validator()({ id: v4(), name: input.name, extension: input.extension, url: input.url, created_at: new Date(), - }; - } + }); } diff --git a/src/providers/common/BbsArticleCommentSnapshotProvider.ts b/src/providers/common/BbsArticleCommentSnapshotProvider.ts index a3fa86b..e7b9f5f 100644 --- a/src/providers/common/BbsArticleCommentSnapshotProvider.ts +++ b/src/providers/common/BbsArticleCommentSnapshotProvider.ts @@ -17,7 +17,7 @@ export namespace BbsArticleCommentSnapshotProvider { id: input.id, format: input.format as any, body: input.body, - files: input.files + files: input.to_files .sort((a, b) => a.sequence - b.sequence) .map((p) => AttachmentFileProvider.json.transform(p.file)), created_at: input.created_at.toISOString(), @@ -26,7 +26,7 @@ export namespace BbsArticleCommentSnapshotProvider { Prisma.validator()( { include: { - files: { + to_files: { include: { file: AttachmentFileProvider.json.select(), }, @@ -52,21 +52,22 @@ export namespace BbsArticleCommentSnapshotProvider { return json.transform(snapshot); }; - export const collect = ( - input: IBbsArticleComment.IStore, - ): Omit => ({ - id: v4(), - format: input.format, - body: input.body, - created_at: new Date(), - files: { - create: input.files.map((file, i) => ({ - id: v4(), - file: { - create: AttachmentFileProvider.collect(file), - }, - sequence: i, - })), - }, - }); + export const collect = (input: IBbsArticleComment.IStore) => + Prisma.validator< + Omit + >()({ + id: v4(), + format: input.format, + body: input.body, + created_at: new Date(), + to_files: { + create: input.files.map((file, i) => ({ + id: v4(), + file: { + create: AttachmentFileProvider.collect(file), + }, + sequence: i, + })), + }, + }); } diff --git a/src/providers/common/BbsArticleProvider.ts b/src/providers/common/BbsArticleProvider.ts index a45044c..1fe7975 100644 --- a/src/providers/common/BbsArticleProvider.ts +++ b/src/providers/common/BbsArticleProvider.ts @@ -36,7 +36,7 @@ export namespace BbsArticleProvider { format: input.mv_last!.snapshot.format as IBbsArticle.Format, created_at: input.created_at.toISOString(), updated_at: input.mv_last!.snapshot.created_at.toISOString(), - files: input.mv_last!.snapshot.files.map((p) => + files: input.mv_last!.snapshot.to_files.map((p) => AttachmentFileProvider.json.transform(p.file), ), }); @@ -47,7 +47,7 @@ export namespace BbsArticleProvider { include: { snapshot: { include: { - files: { + to_files: { include: { file: AttachmentFileProvider.json.select(), }, @@ -99,14 +99,18 @@ export namespace BbsArticleProvider { : null; export const collect = - ( - snapshotFactory: ( - input: Input, - ) => Omit, + < + Input extends IBbsArticle.IStore, + Snapshot extends Omit< + Prisma.bbs_article_snapshotsCreateInput, + "article" + >, + >( + snapshotFactory: (input: Input) => Snapshot, ) => - (input: Input): Prisma.bbs_articlesCreateInput => { + (input: Input) => { const snapshot = snapshotFactory(input); - return { + return Prisma.validator()({ id: v4(), snapshots: { create: [snapshot], @@ -118,6 +122,6 @@ export namespace BbsArticleProvider { snapshot: { connect: { id: snapshot.id } }, }, }, - }; + }); }; } diff --git a/src/providers/common/BbsArticleSnapshotProvider.ts b/src/providers/common/BbsArticleSnapshotProvider.ts index 505ee83..6ca1680 100644 --- a/src/providers/common/BbsArticleSnapshotProvider.ts +++ b/src/providers/common/BbsArticleSnapshotProvider.ts @@ -18,7 +18,7 @@ export namespace BbsArticleSnapshotProvider { title: input.title, format: input.format as any, body: input.body, - files: input.files + files: input.to_files .sort((a, b) => a.sequence - b.sequence) .map((p) => AttachmentFileProvider.json.transform(p.file)), created_at: input.created_at.toISOString(), @@ -26,7 +26,7 @@ export namespace BbsArticleSnapshotProvider { export const select = () => Prisma.validator()({ include: { - files: { + to_files: { include: { file: AttachmentFileProvider.json.select(), }, @@ -56,22 +56,23 @@ export namespace BbsArticleSnapshotProvider { return json.transform(snapshot); }; - export const collect = ( - input: IBbsArticle.IStore, - ): Omit => ({ - id: v4(), - title: input.title, - format: input.format, - body: input.body, - created_at: new Date(), - files: { - create: input.files.map((file, i) => ({ - id: v4(), - file: { - create: AttachmentFileProvider.collect(file), - }, - sequence: i, - })), - }, - }); + export const collect = (input: IBbsArticle.IStore) => + Prisma.validator< + Omit + >()({ + id: v4(), + title: input.title, + format: input.format, + body: input.body, + created_at: new Date(), + to_files: { + create: input.files.map((file, i) => ({ + id: v4(), + file: { + create: AttachmentFileProvider.collect(file), + }, + sequence: i, + })), + }, + }); } diff --git a/src/providers/common/EntityMergeProvider.ts b/src/providers/common/EntityMergeProvider.ts index a14da2f..8b722d7 100644 --- a/src/providers/common/EntityMergeProvider.ts +++ b/src/providers/common/EntityMergeProvider.ts @@ -26,10 +26,12 @@ export namespace EntityMergeProvider { ? await finder(input) : await (SGlobal.prisma[table] as any).count({ where: { - [primary.name]: { in: [input.keep, ...input.merged] }, + [primary.name]: { + in: [input.keep, ...input.absorbed], + }, }, }); - if (count !== input.merged.length + 1) + if (count !== input.absorbed.length + 1) throw new nest.NotFoundException( "Unable to find matched record(s).", ); diff --git a/src/schema.prisma b/src/schema.prisma index faaee1b..c4f021b 100644 --- a/src/schema.prisma +++ b/src/schema.prisma @@ -167,7 +167,7 @@ model bbs_article_snapshots { article bbs_articles @relation(fields: [bbs_article_id], references: [id], onDelete: Cascade) /// List of wrappers of attachment files. - files bbs_article_snapshot_files[] + to_files bbs_article_snapshot_files[] mv_last mv_bbs_article_last_snapshots? @@ -336,7 +336,7 @@ model bbs_article_comment_snapshots { comment bbs_article_comments @relation(fields: [bbs_article_comment_id], references: [id], onDelete: Cascade) /// List of wrappers of attachment files. - files bbs_article_comment_snapshot_files[] + to_files bbs_article_comment_snapshot_files[] @@index([bbs_article_comment_id, created_at]) } diff --git a/src/utils/EntityUtil.ts b/src/utils/EntityUtil.ts index 89de028..b6b1b29 100644 --- a/src/utils/EntityUtil.ts +++ b/src/utils/EntityUtil.ts @@ -17,21 +17,21 @@ export namespace EntityUtil { /** * Target record to keep after merging. * - * After merge process, {@link merged} records would be merged into + * After merge process, {@link absorbed} records would be merged into * this {@link keep} record. */ keep: Key; /** - * To be merged to {@link keep} after merging. + * To be absorbed to the {@link keep} record after merging. */ - merged: Key[]; + absorbed: Key[]; } /** * Merge multiple records into one. * - * Merge multiple {@link IMergeProps.merged} records into + * Merge multiple {@link IMergeProps.absorbed} records into * {@link IMergeProps.keep} record, instead of deleting them. * * If there're some dependent tables of the target `table` having @@ -112,7 +112,7 @@ export namespace EntityUtil { else await (client[table] as any).updateMany({ where: { - [foreign.name]: { in: props.merged }, + [foreign.name]: { in: props.absorbed }, }, data: { [foreign.name]: props.keep, @@ -123,7 +123,7 @@ export namespace EntityUtil { // REMOVE TO BE MERGED RECORD await (client[table] as any).deleteMany({ where: { - [key.name]: { in: props.merged }, + [key.name]: { in: props.absorbed }, }, }); }; @@ -156,7 +156,9 @@ export namespace EntityUtil { primary.dbName } AS VARCHAR(36))) AS UUID) AS ${primary.dbName} FROM ${current.model.dbName} - WHERE ${current.foreign.dbName} IN ($1, ${parent.merged + WHERE ${ + current.foreign.dbName + } IN ($1, ${parent.absorbed .map((_, index) => `$${index + 2}`) .join(", ")}) GROUP BY ${group.join(", ")} @@ -166,7 +168,7 @@ export namespace EntityUtil { )`; await client.$executeRawUnsafe(sql, [ parent.keep, - ...parent.merged, + ...parent.absorbed, ]); } @@ -179,7 +181,7 @@ export namespace EntityUtil { ].findMany({ where: { [current.foreign.name]: { - in: [parent.keep, ...parent.merged], + in: [parent.keep, ...parent.absorbed], }, }, orderBy: [ @@ -203,7 +205,7 @@ export namespace EntityUtil { await merge(client)(current.model.name as Prisma.ModelName)({ keep: master[primary.name], - merged: slaves.map((slave) => slave[primary.name]), + absorbed: slaves.map((slave) => slave[primary.name]), }); } };