Last active
October 15, 2020 00:33
-
-
Save juanhuttemann/fd25672100d285f8aba18e1b1cea2506 to your computer and use it in GitHub Desktop.
Item - Category - Mongoose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require("mongoose"); | |
const slugify = require("slugify"); | |
const CategorySchema = new mongoose.Schema({ | |
name: { | |
type: String, | |
unique: true, | |
trim: true, | |
required: [true, "Por favor agregue un nombre"], | |
}, | |
slug: String, | |
description: { | |
type: String, | |
}, | |
createdAt: { | |
type: Date, | |
default: Date.now, | |
}, | |
parent: { | |
type: mongoose.Schema.ObjectId, | |
ref: "Category", | |
default: null, | |
}, | |
tag: { | |
type: String, | |
}, | |
}); | |
CategorySchema.pre("save", function () { | |
this.slug = slugify(this.name, { lower: true }); | |
next(); | |
}); | |
module.exports = mongoose.model("Category", CategorySchema); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require("mongoose"); | |
const slugify = require("slugify"); | |
const ItemSchema = new mongoose.Schema({ | |
name: { | |
type: String, | |
required: [true, "Por favor agregue un nombre"], | |
unique: true, | |
trim: true, | |
maxlength: [60, "El nombre no puede tener más de 60 caracteres"], | |
}, | |
slug: String, | |
description: { | |
type: String, | |
trim: true, | |
maxlength: [400, "La descripción no puede tener más de 400 caracteres"], | |
}, | |
price: { | |
type: Number, | |
required: [true, "Por favor agregue un precio"], | |
}, | |
quantity: { | |
type: Number, | |
default: 0, | |
}, | |
code: { | |
type: String, | |
}, | |
url: { | |
type: String, | |
}, | |
createdAt: { | |
type: Date, | |
default: Date.now, | |
}, | |
category: { | |
type: mongoose.Schema.ObjectId, | |
ref: "Category", | |
}, | |
tag: { | |
type: String, | |
}, | |
}); | |
ItemSchema.pre("save", function () { | |
this.slug = slugify(this.name, { lower: true }); | |
next(); | |
}); | |
module.exports = mongoose.model("Item", ItemSchema); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": [ | |
{ | |
"_id": "5f8734cffc3d1d001796c557", | |
"name": "Heineken", | |
"category": { | |
"_id": "5f7e5d0afbb7b4001750c5c5", | |
"name": "Cerveza", | |
} | |
}, | |
{ | |
"_id": "5f8734cffc3d1d001796c557", | |
"name": "Pilsen", | |
"category": { | |
"_id": "5f7e5d0afbb7b4001750c5c5", | |
"name": "Cerveza", | |
} | |
}, | |
{ | |
"_id": "5f8734cffc3d1d001796c557", | |
"name": "Carne", | |
"category": { | |
"_id": "5f7e5d0afbb7b4001750c5c5", | |
"name": "Alimentos", | |
} | |
}, | |
{ | |
"_id": "5f8734cffc3d1d001796c557", | |
"name": "Fideos", | |
"category": { | |
"_id": "5f7e5d0afbb7b4001750c5c5", | |
"name": "Alimentos", | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment