-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
150 lines (140 loc) · 3.98 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
InventoryCategory
├── title: "Electronics"
├── items: [ // Directly associated items with InventoryCategory
│ ├── { name: "Smartphone" }
│ └── { name: "Tablet" }
├── subCategories: [ // Subcategories related to InventoryCategory
│ ├── InventorySubCategory
│ │ ├── title: "Laptops"
│ │ ├── parent: "InventoryCategory" (ref to Electronics)
│ │ └── items: [ // Items related to this SubCategory
│ │ ├── { name: "MacBook Pro" }
│ │ └── { name: "Dell XPS" }
│ │ ]
│ └── InventorySubCategory
│ ├── title: "Smartphones"
│ ├── parent: "InventoryCategory" (ref to Electronics)
│ └── items: [
│ ├── { name: "iPhone 14" }
│ └── { name: "Samsung Galaxy S23" }
│ ]
└── ]
*/
import mongoose from "mongoose";
mongoose.connect("mongodb://127.0.0.1:27017/polymorphic").then(() => {
console.log("connected");
});
// Inventory Category Schema
const inventoryCategorySchema = new mongoose.Schema(
{ title: String },
{ timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } }
);
inventoryCategorySchema.virtual("subCategories", {
ref: "InventorySubCategory",
localField: "_id",
foreignField: "parent",
});
inventoryCategorySchema.virtual("items", {
ref: "InventoryItem",
localField: "_id",
foreignField: "category",
});
const InventoryCategory = mongoose.model(
"InventoryCategory",
inventoryCategorySchema
);
const InventorySubCategorySchema = new mongoose.Schema(
{
title: String,
parent: {
type: mongoose.Schema.Types.ObjectId,
ref: "InventoryCategory",
required: true,
},
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
strict: false,
}
);
InventorySubCategorySchema.virtual("items", {
ref: "InventoryItem",
localField: "_id",
foreignField: "category",
});
const InventorySubCategory = mongoose.model(
"InventorySubCategory",
InventorySubCategorySchema
);
// InventoryItem Schema
const inventoryItemSchema = new mongoose.Schema(
{
name: String,
categoryType: {
type: String,
required: true,
enum: ["InventoryCategory", "InventorySubCategory"],
},
category: {
type: mongoose.Schema.Types.ObjectId,
refPath: "categoryType", // Dynamically reference either InventoryCategory or InventorySubCategory
required: true,
},
},
{ timestamps: true }
);
const InventoryItem = mongoose.model("InventoryItem", inventoryItemSchema);
const main = async () => {
// delete all data
await InventoryCategory.deleteMany({});
await InventorySubCategory.deleteMany({});
await InventoryItem.deleteMany({});
// Creating a new InventoryCategory
const category = new InventoryCategory({ title: "Category" });
await category.save();
// Creating a new InventorySubCategory
const subCategory = new InventorySubCategory({
title: "sub-category-1",
parent: category._id,
});
await subCategory.save();
// Creating a new InventoryItem
InventoryItem.create({
name: "root-item-1",
categoryType: "InventoryCategory",
category: category._id,
});
InventoryItem.create({
name: "root-item-2",
categoryType: "InventoryCategory",
category: category._id,
});
InventoryItem.create({
name: "root-item-3",
categoryType: "InventoryCategory",
category: category._id,
});
// create 2 items under subCategory
InventoryItem.create({
name: "item-1",
categoryType: "InventorySubCategory",
category: subCategory._id,
});
InventoryItem.create({
name: "item-2",
categoryType: "InventorySubCategory",
category: subCategory._id,
});
// get all categories
const categories = await InventoryCategory.find()
.populate("items")
.populate({
path: "subCategories",
populate: { path: "items" },
});
console.log(JSON.stringify(categories, null, 2));
};
main();