I am trying to build a rest api with node, express and mongodb for webapp where I have three routes or section on frontend
- Quotes
- Stories
- News
When user will click Quotes he will see only quotes, same will goes with Stories and News.
I am confused how to build schema for this kind of API.
This is my approach First I build separate schemas for each section then merge all of them in main ApiSchema... I am not sure if this approach is right
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const QuotesSchema = new Schema({
author: {
type: String,
required: true
},
categories: {
type: [String],
required: true
}
})
module.exports = Quotes = mongoose.model('quotes', QuotesSchema)
const StoriesSchema = new Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
}
})
module.exports = Stories = mongoose.model('stories', StoriesSchema)
const NewsSchema = new Schema({
headline: {
type: String,
required: true
}
date: {
type: Date,
default: Date.now
}
})
module.exports = News = mongoose.model('news', NewsSchema)
const Quotes = require('./Quotes')
const Stories = require('./Stories')
const News = require('./News')
const ApiSchema = new Schema({
quotes: [Quotes],
Stories: [Stories],
News: [News]
})
module.exports = Api = mongoose.model('api', ApiSchema)
I want my api like below example
[
{
quotes: [
{ author: 'John', quote: 'be happy', tags: ['life', 'heart'] },
{ author: 'Mark', quote: 'be creative', tags: ['motivation', 'education'] }
]
},
{
story: [
{ title: 'broken heart', description: 'sit ae Ipsa, laboriosam!', category: ['lorem1', 'lorem2'] },
{ title: 'lorem ipsum', description: 'Lorem ipsum dolor sit.', category: ['lorem1', 'lorem2'] }
]
},
{
news: [
{ headline: 'ipsum Lorem', city: 'Jehigi' },
{ headline: 'Lorem, ipsum', city: 'Fahmori' }
]
}
]
I hope I am clear. will so kind of you if you help me out.