File tree Expand file tree Collapse file tree 9 files changed +351
-0
lines changed Expand file tree Collapse file tree 9 files changed +351
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { Schema , model } from "mongoose" ;
2
+
3
+ const brandSchema = new Schema ( {
4
+ name : {
5
+ type : String ,
6
+ required : true ,
7
+ unique : true ,
8
+ trim : true ,
9
+ } ,
10
+ slug : {
11
+ type : String ,
12
+ lowercase : true ,
13
+ } ,
14
+ logo : {
15
+ type : String ,
16
+ } ,
17
+ } ,
18
+ { timestamps : true }
19
+ ) ;
20
+
21
+ export const brandModel = model ( "brand" , brandSchema ) ;
Original file line number Diff line number Diff line change
1
+ import { Schema , model } from "mongoose" ;
2
+
3
+ const cartSchema = new Schema ( {
4
+ userId : {
5
+ type : Schema . ObjectId ,
6
+ ref : "user" ,
7
+ } ,
8
+ cartItem : [
9
+ {
10
+ productId : { type : Schema . ObjectId , ref : "product" } ,
11
+ quantity : {
12
+ type : Number ,
13
+ default : 1
14
+ } ,
15
+ price : Number ,
16
+ totalProductDiscount : Number
17
+ }
18
+ ] ,
19
+ totalPrice : Number ,
20
+ totalPriceAfterDiscount : Number ,
21
+ discount : Number
22
+ } , {
23
+ timestamps : true ,
24
+ }
25
+ ) ;
26
+
27
+ export let cartModel = model ( "cart" , cartSchema ) ;
Original file line number Diff line number Diff line change
1
+ import { Schema , model } from "mongoose" ;
2
+
3
+ const categorySchema = new Schema ( {
4
+ name : {
5
+ type : String ,
6
+ required : true ,
7
+ minLength : [ 4 , "Too Short" ] ,
8
+ unique : true ,
9
+ trim : true ,
10
+ } ,
11
+ slug : {
12
+ type : String ,
13
+ lowercase : true ,
14
+ } ,
15
+ Image : {
16
+ type : String ,
17
+ } ,
18
+ } ,
19
+ { timestamps : true }
20
+ ) ;
21
+ categorySchema . post ( 'init' , function ( doc ) {
22
+ doc . Image = `${ process . env . BASE_URL } category/${ doc . Image } `
23
+ console . log ( doc ) ;
24
+
25
+ } )
26
+ export const categoryModel = model ( "category" , categorySchema ) ;
Original file line number Diff line number Diff line change
1
+ import { Schema , model } from "mongoose" ;
2
+
3
+ const couponSchema = new Schema ( {
4
+ code : {
5
+ type : String ,
6
+ required : true ,
7
+ trim : true ,
8
+ unique : true
9
+ } ,
10
+ expires : {
11
+ type : Date ,
12
+ required : true ,
13
+ } ,
14
+ discount : {
15
+ type : Number ,
16
+ required : true ,
17
+ min : 0 ,
18
+ } ,
19
+ } ,
20
+ { timestamps : true }
21
+ ) ;
22
+
23
+ export const couponModel = model ( "coupon" , couponSchema ) ;
Original file line number Diff line number Diff line change
1
+ import { Schema , model } from "mongoose" ;
2
+
3
+
4
+ const orderSchema = new Schema ( {
5
+ userId : {
6
+ type : Schema . ObjectId ,
7
+ required : true ,
8
+ ref : 'user'
9
+ } ,
10
+ cartItems : [
11
+ {
12
+ productId : { type : Schema . ObjectId , ref : "product" } ,
13
+ quantity : {
14
+ type : Number ,
15
+ default : 1
16
+ } ,
17
+ price : Number ,
18
+ totalProductDiscount : Number
19
+ }
20
+ ] ,
21
+ shippingAddress : {
22
+ street : String ,
23
+ city : String ,
24
+ phone : Number
25
+ } ,
26
+ paymentMethod : {
27
+ type : String ,
28
+ enum : [ 'card' , 'cash' ] ,
29
+ default : 'cash'
30
+ } ,
31
+ isPaid : {
32
+ type : Boolean ,
33
+ default : false
34
+ } ,
35
+ isDelivered : {
36
+ type : Boolean ,
37
+ default : false
38
+ } ,
39
+ paidAt : Date ,
40
+ deliveredAt : Date
41
+ } )
42
+
43
+ export const orderModel = model ( 'order' , orderSchema )
Original file line number Diff line number Diff line change
1
+ import { Schema , model } from "mongoose" ;
2
+
3
+ const productSchema = new Schema ( {
4
+ title : {
5
+ type : String ,
6
+ required : true ,
7
+ unique : true ,
8
+ trim : true ,
9
+ minLength : [ 3 , "Too Short product Name" ] ,
10
+ } ,
11
+ imgCover : {
12
+ type : String ,
13
+ } ,
14
+ images : {
15
+ type : [ String ] ,
16
+ } ,
17
+ descripton : {
18
+ type : String ,
19
+ maxlength : [ 100 , "Description should be less than or equal to 100" ] ,
20
+ minlength : [ 10 , "Description should be more than or equal to 10" ] ,
21
+ required : true ,
22
+ trim : true ,
23
+ } ,
24
+ price : {
25
+ type : Number ,
26
+ default : 0 ,
27
+ min : 0 ,
28
+ required : true ,
29
+ } ,
30
+ priceAfterDiscount : {
31
+ type : Number ,
32
+ default : 0 ,
33
+ min : 0 ,
34
+ } ,
35
+ quantity : {
36
+ type : Number ,
37
+ default : 0 ,
38
+ min : 0 ,
39
+ } ,
40
+ sold : {
41
+ type : Number ,
42
+ default : 0 ,
43
+ min : 0 ,
44
+ } ,
45
+ category : {
46
+ type : Schema . ObjectId ,
47
+ ref : "category" ,
48
+ required : true ,
49
+ } ,
50
+ subcategory : {
51
+ type : Schema . ObjectId ,
52
+ ref : "subcategory" ,
53
+ required : true ,
54
+ } ,
55
+ brand : {
56
+ type : Schema . ObjectId ,
57
+ ref : "brand" ,
58
+ required : true ,
59
+ } ,
60
+ ratingAvg : {
61
+ type : Number ,
62
+ min : 1 ,
63
+ max : 5 ,
64
+ } ,
65
+ ratingCount : {
66
+ type : Number ,
67
+ min : 0 ,
68
+ } ,
69
+ } ,
70
+ { timestamps : true , toJSON : { virtuals : true } , toObject : { virtuals : true } }
71
+ ) ;
72
+
73
+ productSchema . post ( 'init' , function ( doc ) {
74
+
75
+ if ( doc . imgCover && doc . images ) {
76
+
77
+ doc . imgCover = `${ process . env . BASE_URL } products/${ doc . imgCover } `
78
+ doc . images = doc . images . map ( ( ele ) => {
79
+ return `${ process . env . BASE_URL } products/${ ele } `
80
+ } )
81
+ }
82
+
83
+
84
+ } )
85
+
86
+ productSchema . virtual ( 'reviews' , {
87
+ ref : 'review' ,
88
+ localField : '_id' ,
89
+ foreignField : 'productId' ,
90
+ } ) ;
91
+
92
+ productSchema . pre ( [ 'find' , 'findOne' ] , function ( ) {
93
+ this . populate ( 'reviews' )
94
+ } )
95
+
96
+ export const productModel = model ( "product" , productSchema ) ;
Original file line number Diff line number Diff line change
1
+ import { Schema , model } from "mongoose" ;
2
+
3
+ const reviewSchema = new Schema ( {
4
+ text : {
5
+ type : String ,
6
+ trim : true ,
7
+ required : true ,
8
+ } ,
9
+ productId : {
10
+ type : Schema . ObjectId ,
11
+ ref : "product" ,
12
+ required : true ,
13
+ } ,
14
+ userId : {
15
+ type : Schema . ObjectId ,
16
+ ref : "user" ,
17
+ required : true ,
18
+ } ,
19
+ rate : {
20
+ type : Number ,
21
+ default : 1 ,
22
+ } ,
23
+ } ,
24
+ { timestamps : true }
25
+ ) ;
26
+
27
+ reviewSchema . pre ( [ 'find' , 'findOne' ] , function ( ) {
28
+ this . populate ( 'userId' , 'name -_id' )
29
+ } )
30
+
31
+ export const reviewModel = model ( "review" , reviewSchema ) ;
Original file line number Diff line number Diff line change
1
+ import { Schema , model } from "mongoose" ;
2
+
3
+ const subCategorySchema = new Schema ( {
4
+ name : {
5
+ type : String ,
6
+ required : true ,
7
+ minLength : [ 2 , "Too Short" ] ,
8
+ unique : true ,
9
+ trim : true ,
10
+ } ,
11
+ slug : {
12
+ type : String ,
13
+ lowercase : true ,
14
+ } ,
15
+ category : {
16
+ type : Schema . ObjectId ,
17
+ required : true ,
18
+ ref : "category" ,
19
+ } ,
20
+ } ,
21
+ { timestamps : true }
22
+ ) ;
23
+
24
+ export const subCategoryModel = model ( "subcategory" , subCategorySchema ) ;
Original file line number Diff line number Diff line change
1
+ import mongoose , { Schema , model } from "mongoose" ;
2
+ import bcrypt from "bcrypt" ;
3
+
4
+ const userSchema = new Schema ( {
5
+ name : {
6
+ type : String ,
7
+ required : true ,
8
+ trim : true ,
9
+ } ,
10
+ email : {
11
+ type : String ,
12
+ required : true ,
13
+ unique : true ,
14
+ trim : true ,
15
+ } ,
16
+ password : {
17
+ type : String ,
18
+ required : true ,
19
+ } ,
20
+ passwordChangedAt : Date ,
21
+ role : {
22
+ type : String ,
23
+ enum : [ "admin" , "user" ] ,
24
+ default : "user" ,
25
+ } ,
26
+ isActive : {
27
+ type : Boolean ,
28
+ default : true ,
29
+ } ,
30
+ verified : {
31
+ type : Boolean ,
32
+ default : false ,
33
+ } ,
34
+ blocked : {
35
+ type : Boolean ,
36
+ default : false ,
37
+ } ,
38
+
39
+ wishlist : [ { type : Schema . ObjectId , ref : 'product' } ] ,
40
+ addresses : [ {
41
+ city : String ,
42
+ street : String ,
43
+ phone : String
44
+ } ]
45
+ } ,
46
+ { timestamps : true }
47
+ ) ;
48
+
49
+ userSchema . pre ( "save" , function ( ) {
50
+ this . password = bcrypt . hashSync ( this . password , 8 ) ;
51
+ } ) ;
52
+
53
+ userSchema . pre ( "findOneAndUpdate" , function ( ) {
54
+ if ( this . _update . password ) {
55
+ this . _update . password = bcrypt . hashSync ( this . _update . password , 8 ) ;
56
+ }
57
+
58
+ } ) ;
59
+
60
+ export const userModel = model ( "user" , userSchema ) ;
You can’t perform that action at this time.
0 commit comments