> For the complete documentation index, see [llms.txt](https://medellin-js.gitbook.io/workshop-fullstack-js-developer/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://medellin-js.gitbook.io/workshop-fullstack-js-developer/backend/mongoose-js/esquemas-y-modelos.md).

# Esquemas y Modelos

Una vez entendidos los conceptos principales para utilizar moongose, procedemos con crear nuestros modelos y esquemas de la API del carrito de compras, por lo cual necesitamos crear un modelo de productos. Dentro de la carpeta `/api/product` creamos un archivo: `product.model.js`, con el siguiente código.

{% code title="api/product/product.model.js" %}

```javascript
/**
 * Product model
 */
 
const mongoose = require('mongoose');

const { Schema } = mongoose;

const ProductSchema = new Schema({
  name: { type: String, uppercase: true, required: true },
  availableQuantity: { type: Number, required: true },
  price: { type: Number, required: true },
  description: { type: String, required: true },
  image: { type: String },
  slug: { type: String, trim: true },
}, { timestamps: true });

ProductSchema
  .path('availableQuantity')
  .validate((availableQuantity) => {
    if (availableQuantity > 0) {
      return true;
    }
    return false;
  }, 'The available quantity must be greater than 0');

ProductSchema
  .path('price')
  .validate((price) => {
    if (price >= 0) {
      return true;
    }
    return false;
  }, 'The price cannot be a negative value');

module.exports = mongoose.model('Product', ProductSchema);

```

{% endcode %}

Ya hemos creado nuestro primer modelo ahora pasemos a crear el controlador con cada uno de los métodos que el modelo podrá ejecutar.

### Repositorio: [Add the Product's model](https://github.com/colombia-dev/medjs-workshop-fullstack-js-backend/commit/f62e9c9cc0e9fa0cc226c9675c06a5a4b9476b7d)
