import mongoose, { Schema, Document } from 'mongoose'; import { ModelDefinition } from './types'; export interface Metadata { key: string; value: string | any[] | null; dataType: string; isReserved: boolean; } export interface TaxonomyChildItem { title: string; order: number; articleId?: mongoose.Types.ObjectId; slug?: string; //article document의 slug 참조 (임베딩) children?: TaxonomyChildItem[]; } export interface Taxonomy extends Document { label: string; items: TaxonomyChildItem[]; metadatas: Metadata[]; } export interface ContentSection { heading: string; taxonomies: mongoose.Types.ObjectId[]; } export interface Content extends Document { key: string; label: string; sections: ContentSection[]; } export interface Article extends Document { title: string; slug: string; content: string; status: 'draft' | 'published' | 'archived'; metadatas: Metadata[]; } export const MetadataSubSchema = new mongoose.Schema({ key: { type: String, required: true }, value: { type: mongoose.SchemaTypes.Mixed, required: false, // required: true로 하면 value가 null인 경우 에러 발생 default: null, validate: { validator: function (v: string | any[] | null) { // 특정 키인 경우 value가 null 또는 빈 문자열이 아니어야 함 // if (this.key === 'requiredKey') { // return typeof v === 'string' && v.trim().length > 0; // } // 그렇지 않은 경우 value는 null 또는 문자열, Array이어야 함 return v === null || typeof v === 'string' || Array.isArray(v); }, message: function (props: any) { if (this.key === 'requiredKey') { return `Value is required for key "${this.key}" and cannot be null or empty.`; } return `Value must be a string, an array, or null for key "${this.key}".`; }, }, }, dataType: { type: String, required: false, default: 'string' }, isReserved: { type: Boolean, required: false, default: false }, }); export const TaxonomyChildItemSchema = new Schema( { title: { type: String, required: true }, order: { type: Number, required: true, default: 100 }, slug: { type: String }, // slug는 없을 수도 있으므로 required 제외 articleId: { type: Schema.Types.ObjectId, ref: 'article' }, }, { _id: false, // 하위 아이템마다 _id가 생성되는 것을 방지 (선택 사항) }, ); // **중요: 재귀 참조 설정** // 스키마 정의 후 .add()를 통해 자기 자신을 children 배열로 추가합니다. TaxonomyChildItemSchema.add({ children: { type: [TaxonomyChildItemSchema], default: [] }, }); // 2. 메인 도큐먼트 스키마 (Root) export const TaxonomySchema = new Schema( { // items 배열은 위에서 정의한 재귀 스키마를 따름 label: { type: String, required: true }, items: { type: [TaxonomyChildItemSchema], default: [], }, // metadata는 구조가 가변적이므로 Mixed 타입 사용 metadatas: { type: [MetadataSubSchema], required: true, default: [], validate: { validator: function (metas: Metadata[]) { const keys = metas.map((m) => m.key); return keys.length === new Set(keys).size; }, message: 'Duplicate metadata keys are not allowed.', }, }, }, { timestamps: true, // createAt, updatedAt 자동 생성 collection: 'taxonomy', // 컬렉션 이름 지정 }, ); TaxonomySchema.index({ label: 1 }, { unique: true }); export const ContentSchema = new Schema( { key: { type: String, required: true, unique: true }, label: { type: String, required: true }, sections: { type: [ { heading: { type: String, required: true }, taxonomies: [{ type: Schema.Types.ObjectId, ref: 'taxonomy' }], }, ], default: [], }, }, { timestamps: true, collection: 'content', }, ); export const ArticleSchema = new Schema( { title: { type: String, required: true }, content: { type: String, required: true }, slug: { type: String, required: true, unique: true }, status: { type: String, enum: ['draft', 'published', 'archived'], default: 'draft', }, metadatas: { type: [MetadataSubSchema], required: true, default: [], validate: { validator: function (metas: Metadata[]) { const keys = metas.map((m) => m.key); return keys.length === new Set(keys).size; }, message: 'Duplicate metadata keys are not allowed.', }, }, }, { timestamps: true, collection: 'article', }, ); ArticleSchema.index({ title: 'text', content: 'text' }); export const moeumModelDefinitions: ModelDefinition[] = [ { name: 'taxonomy', schema: TaxonomySchema, schemaName: 'taxonomy' }, { name: 'content', schema: ContentSchema, schemaName: 'content' }, { name: 'article', schema: ArticleSchema, schemaName: 'article' }, ]; export interface MoeumModelTypes { taxonomy: Taxonomy; content: Content; article: Article; }