Despite the plethora of libraries and frameworks I still find It time consuming, error prone and a repetitive work to write model classes for the ORMs I am using in my different projects including Django model, Hibernate, Doctrine or Room persistence library. Most of the time they have similar architecture with a set of imports, class definition, annotations, attributes, constructors and getters and setters.
This has led me to think of a way to speed up this process. I started brainstorming and then working on a tool that given a particular JSON object, would generate a code that corresponds to the ORM of interest, a sort of mapper, that maps the given JSON object to an ORM tables with its attributes and methods if applicable.
You can visit the end result here
The approach was quite simple. Let's consider the JSON in Fig 1.1 representing a Person with several attributes. The Person Object is converted into a table with Person as the table’s name and the key-value pairs as the table’s attributes. Below is the JSON object transformed into its Django Model class equivalent.
We have several exceptions though.
- If we have a JSON Object as an embedded object of another JSON Object as shown in Fig 1.3, the embedded JSON Object, in this case Address, will be transformed into another table. A One to One relationship will be set between the two tables which in our example are Person and Address as shown in Fig 1.4
person_id = models.OneToOneField(Person,on_delete=models.CASCADE)
- In the case we have a JSON array as an embedded key-value pair of another JSON Object, and that the value of the JSON array are JSON Object as shown in Fig 1.5, the first element of the array is considered and will be transformed into a table, in this case visited cities. A Many to One relationship will be set between the two tables, in our case Person table and the Visited cities table. Fig 1.6 shows the output for the conversion into Django Model
person_id = models.ForeignKey(Person, on_delete=models.CASCADE)
- The last exception is when the embedded JSON array is a list of simple data types such as strings, numbers or boolean. The first element will be used to determine the type of that attribute.
So far the tool has basic support for Django Model and Hibernate and can also generate SQL create statements for PostgreSQL database. The plan in the near future is to support Doctrine and Room persistence library and be able to generate SQL create statements for MySQL database.
To identify the attribute type to use in our models, the JSON value types are mapped into their corresponding SQL types. We therefore have
- Boolean: “false” or false in the JSON value is converted into a Boolean;
- Numbers: “23” or 23 in the JSON value is converted into an Integer;
- Date: Will mapped string in a format “MM-dd-yyyy” into a Date;
- String: if a field is neither a Boolean nor a Number it will be mapped into a String field.
- Field types: The tool is quite limited to a few attribute types at the moment. Knowing that more fields types are supported by ORMs we tool can evolve to take them into consideration.
- Relationships: The same thing applies to relationship between tables. So far only One To One and Many to One relationships are available
- Other configuration: Cascade deletion is used by default with One To One and Many To One relationships while there are not always the best choices. The tool should somehow allow users to determine which configuration they want to use.
- Take into consideration more attribute types, relationships and configuration
- Generate fixtures using the data passed in the JSON Object