Generating Models

Manually defining models is cumbersome, so Formn’s CLI provides a helper tool to generate models from an existing database schema.

Prerequisites

If you haven’t already done so, install the formn-cli package globally.

npm install -g formn-cli

You’ll also need to define a connections.json file as described in the Connecting section. Formn generates models using database metadata defined in the INFORMATION_SCHEMA tables, so the credentials supplied in the connections.json file must have sufficient permission.

Generating Models

The Formn CLI command for generating models is generate, aliased g. You can see all the commands and options using the --help switch.

formn --help
formn generate --help

Options include:

  • --connections-file, aliased as -c. The path to a connections.json file, which defaults to ./connections.json.
  • --flavor, aliased -f. The database type, which defaults to mysql.

The generate command requires one argument: the path where model classes should be written. Here’s an example.

formn g src/entity/

Assuming you’ve been following along with the tutorial, running the above command will generate two model classes: person.entity.ts and phone-number.entity.ts.

Customizing Generated Models

You may have noticed that the class and property names in the generated models do not correspond directly to the database table and column names. For example, the class names are singular and pascal case (Person and PhoneNumber), whereas the database tables names are plural and snake case (people and phone_numbers). Also, the properties associated with primary keys have been aliased as Person.id and PhoneNumber.id. If that naming convention is okay with you, then feel free to skip this section, but do note that the formatting can be customized to your liking.

The code responsible for model generation in Formn is the ModelGenerator class. (There’s one implementation per database flavor, for example MySQLModelGenerator). The ModelGenerator constructor takes three parameters that are used for formatting class names, column-decorated property names, and relationship-decorated property names. Those parameters are instances of TableFormatter, ColumnFormatter, and RelationshipFormatter, respectively. The Formn CLI uses the defaults: DefaultTableFormatter, DefaultColumnFormatter, and DefaultRelationshipFormatter.

If you want to customize the generated models, then you’ll need to implement one or more of the above interfaces, and then instantiate a ModelGenerator manually. Take a look at the formn-cli code for an example: cli-model-generator.ts.