initial commit
This commit is contained in:
commit
e3da5d83b6
49 files changed
+10011
No files matched your search
@@ -0,0 +1,248 @@
|
||||
import mongoose, { Schema, HydratedDocument } from 'mongoose';
|
||||
import { ModelDefinition } from './types';
|
||||
|
||||
export type MetadataDataType =
|
||||
| 'string'
|
||||
| 'number'
|
||||
| 'boolean'
|
||||
| 'array'
|
||||
| 'json';
|
||||
|
||||
export type MetadataValue =
|
||||
| string
|
||||
| number
|
||||
| boolean
|
||||
| unknown[]
|
||||
| Record<string, unknown>
|
||||
| null;
|
||||
|
||||
export interface MongooseTimestamps {
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export interface Metadata {
|
||||
key: string;
|
||||
value: MetadataValue;
|
||||
dataType: MetadataDataType;
|
||||
isReserved: boolean;
|
||||
}
|
||||
|
||||
export interface TaxonomyChildItem {
|
||||
title: string;
|
||||
order: number;
|
||||
articleId?: mongoose.Types.ObjectId;
|
||||
slug?: string; //article document의 slug 참조 (임베딩)
|
||||
children?: TaxonomyChildItem[];
|
||||
}
|
||||
|
||||
export interface Taxonomy extends MongooseTimestamps {
|
||||
label: string;
|
||||
items: TaxonomyChildItem[];
|
||||
metadatas: Metadata[];
|
||||
}
|
||||
|
||||
export type TaxonomyDocument = HydratedDocument<Taxonomy>;
|
||||
|
||||
export interface ContentSection {
|
||||
headline: string;
|
||||
taxonomies: ContentSectionEmbeddedTaxonomy[];
|
||||
}
|
||||
|
||||
export interface ContentSectionEmbeddedTaxonomy {
|
||||
taxonomyId: mongoose.Types.ObjectId;
|
||||
label: string; // 성능 최적화를 위해 임베딩된 label
|
||||
}
|
||||
|
||||
export interface Content extends MongooseTimestamps {
|
||||
key: string;
|
||||
label: string;
|
||||
sections: ContentSection[];
|
||||
}
|
||||
|
||||
export type ContentDocument = HydratedDocument<Content>;
|
||||
|
||||
export interface Article extends MongooseTimestamps {
|
||||
title: string;
|
||||
slug: string;
|
||||
content: string;
|
||||
status: 'draft' | 'published' | 'archived';
|
||||
metadatas: Metadata[];
|
||||
}
|
||||
|
||||
export type ArticleDocument = HydratedDocument<Article>;
|
||||
|
||||
const metadataValueDefinition =
|
||||
{
|
||||
type: mongoose.SchemaTypes.Mixed,
|
||||
required: false, // required: true로 하면 value가 null인 경우 에러 발생
|
||||
default: null,
|
||||
validate: {
|
||||
validator: function (this: Metadata, v: MetadataValue) {
|
||||
if (v === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (this.dataType) {
|
||||
case 'string':
|
||||
return typeof v === 'string';
|
||||
case 'number':
|
||||
return typeof v === 'number';
|
||||
case 'boolean':
|
||||
return typeof v === 'boolean';
|
||||
case 'array':
|
||||
return Array.isArray(v);
|
||||
case 'json':
|
||||
return typeof v === 'object' && !Array.isArray(v);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
},
|
||||
message: function (this: Metadata, props: { value: MetadataValue }) {
|
||||
return `Value "${props.value}" does not match the expected data type "${this.dataType}" for key "${this.key}".`;
|
||||
},
|
||||
},
|
||||
} as mongoose.SchemaDefinitionProperty<MetadataValue>;
|
||||
|
||||
export const MetadataSubSchema = new mongoose.Schema<Metadata>(
|
||||
{
|
||||
key: { type: String, required: true },
|
||||
value: metadataValueDefinition,
|
||||
dataType: {
|
||||
type: String,
|
||||
required: false,
|
||||
default: 'string',
|
||||
enum: ['string', 'number', 'boolean', 'array', 'json'],
|
||||
},
|
||||
isReserved: { type: Boolean, required: false, default: false },
|
||||
},
|
||||
{
|
||||
_id: false,
|
||||
},
|
||||
);
|
||||
|
||||
export const TaxonomyChildItemSubSchema = new Schema<TaxonomyChildItem>(
|
||||
{
|
||||
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 배열로 추가합니다.
|
||||
TaxonomyChildItemSubSchema.add({
|
||||
children: { type: [TaxonomyChildItemSubSchema], default: [] },
|
||||
});
|
||||
|
||||
// 2. 메인 도큐먼트 스키마 (Root)
|
||||
export const TaxonomySchema = new Schema<Taxonomy>(
|
||||
{
|
||||
// items 배열은 위에서 정의한 재귀 스키마를 따름
|
||||
label: { type: String, required: true },
|
||||
items: {
|
||||
type: [TaxonomyChildItemSubSchema],
|
||||
default: [],
|
||||
},
|
||||
|
||||
// metadata는 구조가 가변적이므로 Mixed 타입 사용
|
||||
metadatas: {
|
||||
type: [MetadataSubSchema],
|
||||
required: true,
|
||||
default: [],
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true, // createAt, updatedAt 자동 생성
|
||||
collection: 'taxonomy', // 컬렉션 이름 지정
|
||||
},
|
||||
);
|
||||
|
||||
TaxonomySchema.index({ label: 1 }, { unique: true });
|
||||
|
||||
export const ContentSectionEmbeddedTaxonomySubSchema =
|
||||
new Schema<ContentSectionEmbeddedTaxonomy>(
|
||||
{
|
||||
taxonomyId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'taxonomy',
|
||||
required: true,
|
||||
},
|
||||
label: { type: String, required: true },
|
||||
},
|
||||
{ _id: false },
|
||||
);
|
||||
|
||||
export const ContentSectionSubSchema = new Schema<ContentSection>(
|
||||
{
|
||||
headline: { type: String, required: true },
|
||||
taxonomies: {
|
||||
type: [ContentSectionEmbeddedTaxonomySubSchema],
|
||||
required: true,
|
||||
default: [],
|
||||
},
|
||||
},
|
||||
{ _id: false },
|
||||
);
|
||||
|
||||
export const ContentSchema = new Schema<Content>(
|
||||
{
|
||||
key: { type: String, required: true, unique: true },
|
||||
label: { type: String, required: true },
|
||||
sections: {
|
||||
type: [ContentSectionSubSchema],
|
||||
default: [],
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
collection: 'content',
|
||||
},
|
||||
);
|
||||
|
||||
export const ArticleSchema = new Schema<Article>(
|
||||
{
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user