A compact package for organizing and maintaining your entity database metadata.
morph
organizes and maintains the necessary metadata to map your entities to
and from relational database tables. This is accomplished using the
Metadata Mapping pattern popularized by Martin Fowler.
With these metadata mappings, your application is empowered to construct SQL
queries dynamically using the entities themselves.
With morph
, your application reaps several benefits:
- dynamic construction of queries using entities and their fields.
- metadata generation using files in several formats, including YAML and JSON.
- decoupling of code responsible for manufacturing queries from code tasked with SQL generation.
Using morph
is super straightforward. You utilize Table
and
Column
to organize metadata for your entities and their
associated relational representations. Let's suppose you have a User
entity
in your application (user
):
var usernameCol morph.Column
usernameCol.SetName("username")
usernameCol.SetField(Fields.Username)
var passwordCol morph.Column
passwordCol.SetName("password")
passwordCol.SetField(Fields.Password)
var userTable morph.Table
userTable.SetName("user")
userTable.SetAlias("U")
userTable.SetType(user)
userTable.AddColumns(usernameCol, passwordCol)
Capturing the metadata mappings can be tedious, especially if your application has many entities with corresponding relational representations. Instead of constructing them manually, we recommend loading in a file that specifies the metadata mapping configuration:
{
"tables": [
{
"typeName": "example.User",
"name": "user",
"alias": "U",
"columns": [
{
"name": "username",
"field": "Username"
},
{
"name": "password",
"field": "Password"
}
]
}
]
}
configuration, err := morph.Load("./metadata.json")
if err != nil {
panic(err)
}
tables := configuration.AsMetadata()
At this time, we currently support YAML (.yaml
, .yml
) and JSON (.json
)
configuration files. However, if you would like to utilize a different file
format, you can construct a type that implements morph.Loader
and add the appropriate entries in morph.Loaders
. The
morph.Load
function will leverage morph.Loaders
by extracting
the file extension using the path provided to it.
Want to lend us a hand? Check out our guidelines for contributing.
We are rocking an Apache 2.0 license for this project.
Please check out our code of conduct to get up to speed how we do things.