2. Make a JavaScript Data Model

The starting point for making a JavaScript data model is an association-free information design model like the following one:

How to make this design model has been discussed in the previous chapter (about unidirectional associations).

The meaning of the design model and its reference property publisher can be illustrated by a sample data population for the two model classes Book and Publisher:

Table 12.1. Sample data for Publisher

Name Address
Bantam Books New York, USA
Basic Books New York, USA

Table 12.2. Sample data for Book

ISBN Title Year Publisher
0553345842 The Mind's I 1982 Bantam Books
1463794762 The Critique of Pure Reason 2011
1928565379 The Critique of Practical Reason 2009
0465030793 I Am A Strange Loop 2000 Basic Books

We now show how to derive a JavaScript data model from the design model in three steps.

  1. Create a check operation for each non-derived property in order to have a central place for implementing property constraints. For a standard identifier property (such as Book::isbn), three check operations are needed:

    1. A basic check operation, such as checkIsbn, for checking all syntactic constraints, but not the mandatory value and the uniqueness constraints.

    2. A standard ID check operation, such as checkIsbnAsId, for checking the mandatory value and uniqueness constraints that are required for an identifier (or primary key) attribute.

    3. An ID reference check operation, such as checkIsbnAsIdRef, for checking the referential integrity constraint that is required for an ID reference (IdRef) (or foreign key) attribute.

    For a reference property, such as Book::publisher, the check operation, Book.checkPublisher, has to check the corresponding referential integrity constraint, and possibly also a mandatory value constraint, if the property is mandatory.

  2. Create a setter operation for each non-derived single-valued property. In the setter, the corresponding check operation is invoked and the property is only set, if the check does not detect any constraint violation.

This leads to the following JavaScript data model class Book, where the class-level ('static') methods are shown underlined:

We have to perform a similar transformation also for the class Publisher. This gives us the complete JavaScript data model derived from the above association-free model, as depicted in the following class diagram.

Figure 12.1.  The complete JavaScript data model