freerware / morph

A compact package for organizing and maintaining your entity database metadata.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

morph

A compact package for organizing and maintaining your entity database metadata.

GoDoc Build Status Coverage Status Release License

What is it?

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.

Why use it?

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.

How to use it?

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)

Loading

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()

Custom Loader

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.

Contribute

Want to lend us a hand? Check out our guidelines for contributing.

License

We are rocking an Apache 2.0 license for this project.

Code of Conduct

Please check out our code of conduct to get up to speed how we do things.

About

A compact package for organizing and maintaining your entity database metadata.

License:Apache License 2.0


Languages

Language:Go 99.1%Language:Makefile 0.9%