Skip to content
/ gqb Public

Golang and SQL query build generation

License

Notifications You must be signed in to change notification settings

jobstoit/gqb

Repository files navigation

gQB

coverage Build Status Go Report Card

The generated Query Builder creates a query builder and if needed a migration using a yaml configuration.

The gqb takes takes the following flags as arguments:

-migrate		Writes the configuration to the specified sql file
			takes the output file as argument

-db			Directly inserts the configured structure as migration
			into the database using the DB_DRIVER and DB_URL enviroment
			variables as flags for this mode

-model			Writes the configruration to git.ultraware.nl/NiseVoid/qb
			models and takes the output as argument

-pkg			Specifies the package name for the qbmodels (default model)

-dvr			Specifies the driver for sql generation (default postgres)

Configuration

Create a configuration file based on the following yaml structure (this is an example configuration):

pkg: models                             # optional
driver: mysql                           # optional (default postgres)
tables:
  users:
    id: int, primary
    email: varchar, unique
    password: varchar
    first_name: varchar(50)
    last_name: varchar(100)
    role: role                          # foreign key (enum)

  posts:
    id: int, primary
    created_by: users.id                # foreign key
    created_at: datetime, default(NOW)
    updated_at: datetime, default(NOW)
    title: varchar
    subtitle: varchar, nullable
    context: text

  images:
    id: int, primary
    title: varchar
    created_at: datetime, default(NOW)
    updated_at: datetime, default(NOW)
    path: varchar

  post_images:
    id: int, primary
    image_id: images.id                 # foreign key
    post_id: posts(id)                  # another foreign key
    description: varchar

enums:
  role:
  - ADMIN
  - MODERATOR
  - GENERAL_USER

The configuration has the following type constraints:

primary			sets the type as primary key in the table
unique			set a UNIQUE constraint on the type
nullable		undoes the default NOT NULL constraint on a type
default(%)		sets the default constraint and uses a parameter to set a default value
constraint(%)		sets one or more constraints, constraints can be seperated using ;

Installation

Install this using go get:

$ go get -u github.com/jobstoit/gqb

Then start using it, try:

$ gqb

Todo

  • Create configuration reader
  • Create SQL generator
  • Create NiseVoid/qb generator
  • Create a database differential inspector (config against the current state of the database)
  • Create a diffential SQL generator & executor
  • Create a seeder & seeder configuration