Giter Club home page Giter Club logo

mro's Introduction

mro

Introspect a PostgreSQL database to generate Go marshalling boilerplate

Installation

go get -u github.com/wttw/mro

Usage

mro --bootstrap pgx will create four files in the current directory, suitable for use with pgx.

mro.cfg is a HCL format configuration file. It's hopefully self-documenting. If nothing else you'll need to edit the ConnectionString setting to point at the database containing the schema you want to work from.

pgx.go specifies the interface that mro generated code will use to access the database. It's implemented by pgx.Conn, pgx.ConnPool and pgx.Tx.

table.pgx.tpl and enum.pgx.tpl are Go format templates used to generate code.

mro or mro -package <packagename> will generate marshaling and unmarshaling code for the database schema. For each table it will generate a struct that represents a row of the table, with a name based on the name of the table converted into PascalCase: a table called "email_source" will map on to a struct called "EmailSource". That struct has an Insert() method and, if there's a single-column primary key, Update(), Upsert() and Delete() methods.

Functions to retrieve data from each table are also created. AllEmailSource() will return the entire table, and functions named like EmailSourceByID() will be created for each primary key or unique index on the table.

Additional SQL queries can be added to the Queries section of the configuration file. These must retrieve columns from a single table, and will generate functions to retrieve those as slices of that table's struct.

It will also generate mro.json containing all the information retrieved from the database, as is passed in to the templates to generate code.

Not supported

Any database other than PostgreSQL.

Marshaling of arbitrary SQL queries. This could be added fairly simply, but I've not needed it so far.

Any use of, for example, foreign keys to record structure at a higher level than a table.

Anything ORM-ish, beyond basic marshaling code. If you're looking for programatic generation of SQL queries or automatic loading of related rows this isn't the best place to start.

Known bugs

Table or column names that use unicode characters outside the basic multilingual plane aren't handled correctly.

Other issues

This is mostly untested code. I'm using it in a large production-grade project, so I'll be dealing with bugs (and maybe adding regression test) as I come across them.

It's also a fairly quick hack, so while the code quality isn't terrible it's definitely not great. Global variables, everything lives in main ...

Similar libraries

gnorm

xo

sqlboiler - much more ORMy

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.