GithubHelp home page GithubHelp logo

wearepointers / sqlboiler-erg Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 90 KB

Generate exposed relations from your sqlboiler models for your api.

License: MIT License

Go 97.92% Smarty 2.08%
api api-rest generator sqlboiler

sqlboiler-erg's Introduction

SQLBoiler Expose Relations Generator

If you are a fan of SQLBoiler you know that the relations are not exposed but place in a struct called .R. This works fine if you don't need expose the relations with json. This package is made, so you don't have to manually make custom functions to expose them as stated here: volatiletech/sqlboiler#272.

Installation

First you have to install the code generator binaries. Keep in mind that you still have to install sqlboiler separately.

go install github.com/wearepointers/sqlboiler-erg@latest

Usage

import (
  "github.com/yourpackage/models/dm" // Database Models (from sqlboiler)
  "github.com/yourpackage/models/am" // Api Models (from sqlboiler-erg)
)

func (r *Router) GetUsers(c *framework.Context) {
  users, err := dm.Users().All(r.db, c)
  if err != nil {
    c.JSON(http.StatusInternalServerError, err)
    return
  }
  c.JSON(http.StatusOK, am.ToUsers(users, nil)) // Convert SQLBoiler model to API model
}

Config options

You can also pass these options as flags.

Name Defaults Description
pkgname "erg" The name you wish to assign to your generated package
output "erg_models" The name of the folder to output to
output-ts The name of the ts file (models.ts), no file is no typescript generation. Can also be ../../models.ts or dir/other_dir/filename.ts
wipe false Delete the output folder before generation to ensure sanity
inline false Whether to inline the slqboiler structs or not
blacklist [] Can be table or column. Valid: "table", "table.column", "*.column"

Example

// ... existing sqlboiler.toml config
[erg]
output = "models/am"
output-ts = "models/models.ts"
pkgname = "am"
wipe    = true
inline = false
blacklist = ["*.password", "table.token", "*.secret_column", "*.*ends_with", "table"]

Initial Generation

After generating your models with sqlboiler, you can run the following command to generate the exposed models.

SQL Boiler generates a Go ORM from template files, tailored to your database schema.
Complete documentation is available at http://github.com/volatiletech/sqlboiler

Usage:
  sqlboiler-erg [flags]

Examples:
sqlboiler-erg -c sqlboiler-erg.toml 

Flags:
  -c, --config string              Filename of config file to override default lookup
  -h, --help                       help for sqlboiler-erg

Reasoning of the blacklist/inline options

In the original question it was proposed to just add the model like this:

type User struct {
    *sqlboilerModel.User
    Books BookSlice `json:"books,omitempty"`
}

However sqlboiler does not omit empty fields and the tag-ignore option is only for specific fields. I wanted to disable certain fields like this: *.password or *.*hash (ends with hash). If you want to have the structs inlined you can use the inline option.

type User struct {
    ID:               a.ID,
    // ... other fields
    CreatedAt:        ConvertTime(a.CreatedAt),
    UpdatedAt:        ConvertTime(a.UpdatedAt),
    CreatedAt:        ConvertNullTime(a.UpdatedAt),
    Books BookSlice `json:"books,omitempty"`
}

sqlboiler-erg's People

Contributors

arjendevos avatar

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.