GRO (GoRADD ORM) is a code-generated data access layer for Go programs that provides a consistent interface to an application and supports a large number of databases.
The basic idea is this:
- Describe your database structure in a JSON file.
- Use the provided tools to create the structure in your database of choice.
- Run the code generation tool to generate the data access layer that mirrors the database structure.
- Write your application, using the API provided by the code generated functions to access the database.
One huge advantage to this approach is that your application accesses the database through Go functions and structures. You do not have to write any database specific code, and naming errors are caught early by the compiler. Your IDE can help with your coding, because it will already have access to all the tables, fields and relationships in the database through the generated functions. The process of moving data from your application to the database has already been thoroughly tested. Additionally, the code generator generates code coverage tests for the generated code.
In addition to basic table (or document/entity in NoSQL), and column (or field in NoSQL) descriptions, you will also describe one-to-one, one-to-many, and many-to-many relationships in the structure file. The code generated will allow you to easily query through these relationships to build a multi-level structure of data presented in Go code. This model is sometimes referred to as a graph database and the approach is similar to the ELM project. Optimistic and lazy loading of relationships are both supported. SQL databases will use foreign keys and association tables to implement the relationships. NoSQL databases will depend on the capabilities of the database to determine how they are structured. Special fields can be specified to provide optimistic locking support for a table, and to auto-generate unique ids and timestamps.
MySQL and Postgres databases can be exported to produce the database structure file. Names used in the generated code will be based on names used in the database by default, but these defaults can be overridden in the structure file to map any Go identifier to a data field name.
The API generated by the code generator will generally be consistent regardless of the backend database used. This allows you to begin designing your application and data now, and later decide what is the best database to deploy your application on. Moving from one database to another will involve pushing the data strcuture to the new database type, and then exporting the data from the old database and importing to the new database. Fortunately, GRO provides tools for all of that.
The generated code uses a layered approach, giving you a way to customize the API presented to your application. If the data changes, and you generate new code, your customizations are preserved.
The generated code uses templates in the GOT template language, a simple and easy to use template language that lets you insert basic values, and even Go code within the template. A benefit to GOT templates is that they are easily overridden, so that if you want to customize how code is generated, or generate additional files, you can do that.
Currently supported databases are MySQL, Postgres and SQLite.