@sakura-soft/common-category-model 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +89 -0
- package/package.json +27 -0
package/index.js
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
import mongoose from "mongoose";
|
2
|
+
|
3
|
+
const categorySchema = new mongoose.Schema(
|
4
|
+
{
|
5
|
+
name: {
|
6
|
+
type: String,
|
7
|
+
required: true,
|
8
|
+
trim: true,
|
9
|
+
unique: true,
|
10
|
+
},
|
11
|
+
slug: {
|
12
|
+
type: String,
|
13
|
+
unique: true,
|
14
|
+
lowercase: true,
|
15
|
+
trim: true,
|
16
|
+
index: true,
|
17
|
+
},
|
18
|
+
description: {
|
19
|
+
type: String,
|
20
|
+
trim: true,
|
21
|
+
},
|
22
|
+
isActive: {
|
23
|
+
type: Boolean,
|
24
|
+
default: true,
|
25
|
+
index: true,
|
26
|
+
},
|
27
|
+
image: {
|
28
|
+
type: String,
|
29
|
+
},
|
30
|
+
forProduct: {
|
31
|
+
type: Boolean,
|
32
|
+
default: false,
|
33
|
+
index: true,
|
34
|
+
},
|
35
|
+
allowedAttributes: [
|
36
|
+
{
|
37
|
+
type: mongoose.Schema.Types.ObjectId,
|
38
|
+
ref: "Attribute",
|
39
|
+
},
|
40
|
+
],
|
41
|
+
parent: {
|
42
|
+
type: mongoose.Schema.Types.ObjectId,
|
43
|
+
ref: "ProductCategory",
|
44
|
+
default: null,
|
45
|
+
index: true,
|
46
|
+
},
|
47
|
+
createdBy: {
|
48
|
+
type: mongoose.Schema.Types.ObjectId,
|
49
|
+
required: true,
|
50
|
+
ref: "Admin",
|
51
|
+
},
|
52
|
+
updatedBy: {
|
53
|
+
type: mongoose.Schema.Types.ObjectId,
|
54
|
+
ref: "Admin",
|
55
|
+
},
|
56
|
+
},
|
57
|
+
{ timestamps: true }
|
58
|
+
);
|
59
|
+
|
60
|
+
productCategorySchema.pre("save", function (next) {
|
61
|
+
if (!this.slug) {
|
62
|
+
this.slug = this.name
|
63
|
+
.toLowerCase()
|
64
|
+
.trim()
|
65
|
+
.replace(/[^a-z0-9\s-]/g, "")
|
66
|
+
.replace(/\s+/g, "-")
|
67
|
+
.replace(/-+/g, "-")
|
68
|
+
.replace(/^-+|-+$/g, "");
|
69
|
+
}
|
70
|
+
next();
|
71
|
+
});
|
72
|
+
|
73
|
+
categorySchema.pre("save", async function (next) {
|
74
|
+
if (this.isModified("allowedAttributes")) {
|
75
|
+
const attributeIds = this.allowedAttributes.map((id) => id.toString());
|
76
|
+
const uniqueIds = new Set(attributeIds);
|
77
|
+
if (uniqueIds.size !== attributeIds.length) {
|
78
|
+
throw new Error("Duplicate attribute IDs found in allowedAttributes.");
|
79
|
+
}
|
80
|
+
}
|
81
|
+
next();
|
82
|
+
});
|
83
|
+
|
84
|
+
categorySchema.index({ parent: 1, isActive: 1, forProduct: 1 });
|
85
|
+
|
86
|
+
const Category =
|
87
|
+
mongoose.models.Category || mongoose.model("Category", categorySchema);
|
88
|
+
|
89
|
+
export default Category;
|
package/package.json
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
{
|
2
|
+
"name": "@sakura-soft/common-category-model",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"main": "index.js",
|
5
|
+
"type": "module",
|
6
|
+
"publishConfig": {
|
7
|
+
"access": "public"
|
8
|
+
},
|
9
|
+
"scripts": {
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
11
|
+
},
|
12
|
+
"keywords": [
|
13
|
+
"mongoose",
|
14
|
+
"models",
|
15
|
+
"schemas",
|
16
|
+
"common"
|
17
|
+
],
|
18
|
+
"author": "AJ",
|
19
|
+
"license": "ISC",
|
20
|
+
"description": "Shared Mongoose schemas for multiple microservices",
|
21
|
+
"files": [
|
22
|
+
"*.js"
|
23
|
+
],
|
24
|
+
"peerDependencies": {
|
25
|
+
"mongoose": "^8.19.0"
|
26
|
+
}
|
27
|
+
}
|