School Lunch Data Model
School Lunch Data Model
Data Model#
It's time to go deeper into our application's data model. The purpose of our application is to allow registered users to create and publish a weekly school lunch menu. We will use a lunchWeek
entity that will have a list of lunchDay
child entities for each school day of the week.
In the lunchWeek
entity, we have the following properties:
(int) lunchWeekId
: The primary key of the lunch week entity (more on this later)(date) weekOf
: The start date of the school week, e.g. 2020-01-01(bool) isPublished
: Whether or not the week is published for public users to see(array) lunchDays
: A list of child lunchDay objects
Then for the lunchDay
entity, we have these properties:
(int) lunchDayId
: The primary key of the lunch day entity(int) lunchWeekId
: Foreign key to the parent lunch week entity(date) day
: The date of the given lunch day(string) menuDetails
: The details of the daily lunch menu
Here's a representative JavaScript object example putting it all together:
{
lunchWeek: {
lunchWeekId: 1,
weekOf: '2020-10-05',
isPublished: false,
lunchDays: [
{
lunchDayId: 1,
lunchWeekId: 1,
day: '2020-10-05',
menuDetails: 'Beef tacos or cheese quesadillas with apple slices, rice and black beans'
},
{
lunchDayId: 2,
lunchWeekId: 1,
day: '2020-10-06',
menuDetails: 'Cheese or pepperoni pizza with grapes and celery sticks'
}
]
}
}
Our menuDetails property is just a single string. We may discover later on that we need more properties to subdivide or organize the menuDetails such as keeping the entree and side items in separate properties. For now, we'll keep things simple and defer that decision until later.
The Lunch-Week Route#
The next thing we will tackle is creating the lunch-week route in Express so that we can create-read-update-delete (CRUD) data about lunch weeks.
Start off by creating a new file called lunch-week.js
in the routes
directory. Go ahead and stub out a GET endpoint.
var express = require('express')
var router = express.Router()
router.get('/', function (req, res) {
res.send('lunch week data')
})
module.exports = router
Then, we need to import the route into app.js
and apply it to the Express middleware. Update app.js
with the router code:
var express = require('express')
var path = require('path')
var cookieParser = require('cookie-parser')
var logger = require('morgan')
var indexRouter = require('./routes/index')
var usersRouter = require('./routes/users')
var lunchWeekRouter = require('./routes/lunch-week') // HERE!!
var app = express()
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
app.use('/', indexRouter)
app.use('/users', usersRouter)
app.use('/lunch-week', lunchWeekRouter) // AND HERE!!
module.exports = app
After updating app.js
, make sure the local Express environment is running and test the http://localhost:3000/lunch-week/
route in your browser. You should see the message "lunch week data".

We are going to hard-code a JSON response, just for now. Hard-coding the data obviously won't work for production, but it will help us learn the overall concepts faster. The GET /lunch-week
endpoint will return a list of lunchWeek
objects.
Add a lunchWeekList
object to the lunch-week
file and update the get
endpoint to return the object. We've defined the object list separately from the endpoint so that we can access it from multiple routes:
const lunchWeekList = [
{
lunchWeekId: 1,
weekOf: '2020-10-05',
isPublished: true,
},
{
lunchWeekId: 2,
weekOf: '2020-10-12',
isPublished: true,
},
{
lunchWeekId: 3,
weekOf: '2020-10-19',
isPublished: false,
},
]
router.get('/', function (req, res) {
res.send(lunchWeekList)
})
This page is a preview of Fullstack Svelte