בשלב הזה אנחנו עוברים מדמה (Dummy Responses) לשמירה אמיתית במסד הנתונים.

נשתמש ב־Mongoose כדי להגדיר Schema ו־Model עבור ה־Key-Value שלנו, ונממש בפועל את פעולות ה־POST וה־GET.


1️⃣ יצירת Model עבור Key-Value

ניצור תיקייה חדשה תחת src:

models/

ובתוכה קובץ:

keyValue.js

הגדרת Schema

בתוך keyValue.js:

מבנה הנתונים

לכל אובייקט יהיו:

const mongoose = require('mongoose');

const keyValueSchema = new mongoose.Schema({
  key: {
    type: String,
    required: true,
    unique: true
  },
  value: {
    type: String,
    required: true
  }
});

const KeyValue = mongoose.model('KeyValue', keyValueSchema);

module.exports = KeyValue;