Before writing any code for a library app, someone has to decide what a book, a member and a loan look like. How do you model real-world information well?
Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.
02Start with working code
Records are linked by id, like rows in a database.
Learning objective: Model real-world entities as objects and collections, choose property names and types, link records by id, write factory and validation functions, and derive values instead of storing duplicates.
Start by listing the entities (things) in the problem, such as Book, Member and Loan, and the properties each one needs.
Pick clear names and consistent types: price is always a number, dueDate always an ISO date string, tags always an array.
Link records by id instead of copying data: a loan stores memberId, not the member's name, so a name change is made in one place.
Do not store what you can calculate: store quantity and unitPrice, and derivelineTotal with a function.
Factory functions (createBook(input)) centralise defaults and validation, so every object of a kind has the same shape. Classes (Lesson 61+) offer the same with methods attached.
Normalise data that is shared: an byId object or Map of records plus arrays of ids avoids duplicated nested copies that drift out of sync.
Model states explicitly: status: "active" | "returned" | "overdue" is clearer and safer than several booleans that can contradict each other.
Store money in integer cents and dates as ISO strings or timestamps in data; format them only for display.
05Definitions
Entity
A kind of thing in the problem, such as a book or a customer.
Data model
The set of entities, their properties and how they are linked.
Foreign key
A property that stores another record's id to link them.
Factory function
A function that creates objects with a consistent shape.
Derived value
A value calculated from stored data instead of stored itself.
Before building screens, a team models patients, practitioners and appointments as linked records with ids, decides that an appointment has one status field, and agrees that durations and totals are derived. Every later feature builds on this model.
Every created object has the same shape, defaults and validation
Objects cannot change
Unique emails
5. How should money be stored to avoid rounding errors?
As strings like "$4.50"
As integer cents
As floats only
As booleans
6. Which property name and type is best for when a loan is due?
due: "next Tuesday"
dueDate: "2026-12-10"
d: 12
dueDateText: "10/12"
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Link by id: Prints the current name; Uses customerId
Derive the totals: Prints 10 then 13; No stored total
Event booking model: Prints 1 then the error; Two bookings stored; seatsLeft is derived
ex-040-01: output matches the documented expected output
ex-040-02: output matches the documented expected output
ex-040-03: output matches the documented expected output
ex-040-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-02-core-development/module-08-objects-and-structured-data/lesson-040-modeling-real-world-information/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
List entities and their properties first.
Link records by id; do not copy shared data.
Use factories for consistent shapes and validation.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationdata modelling
Context: I am learning data modelling in JavaScript.
Task: Help me model a small library or booking system as entities, properties and id links, explaining each choice.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictiondata modelling drill
Context: I have practised data modelling.
Task: Show me five data models and ask me to spot duplicated or contradictory data.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingdata modelling bug
Context: My stored totals and statuses keep going out of date.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationdata modelling practice
Context: I finished the data modelling exercises.
Task: Specify two small domains for me to model, with the queries the model must support.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewdata modelling review
Context: Here is my code that uses data modelling.
Task: Review my data model for duplicated data, unclear names and values that should be derived.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.