בשלב הזה אנחנו עוברים מדמה (Dummy Responses) לשמירה אמיתית במסד הנתונים.
נשתמש ב־Mongoose כדי להגדיר Schema ו־Model עבור ה־Key-Value שלנו, ונממש בפועל את פעולות ה־POST וה־GET.
ניצור תיקייה חדשה תחת src:
models/
ובתוכה קובץ:
keyValue.js
בתוך keyValue.js:
mongooseלכל אובייקט יהיו:
key → String, Required, Uniquevalue → String, Requiredconst 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;