cm

package module
v0.0.0-...-d631984 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2019 License: MIT Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

cm.go

Status: not sure... making it open source for now.

persistent collection management for golang, via declarative structs.

A few of the benifits of this approach are:

Separate model struct from persistence logic via a 'Collection structure':

import cm "github.com/sheenobu/cm.go"

var Albums AlbumsCollection

type AlbumsCollection struct {
	cm.Collection
	ID       cm.ValueColumn
	Artist   cm.ValueColumn
	Name     cm.ValueColumn
	Year     cm.ValueColumn
	Explicit cm.ValueColumn
}

DSL for operating on these structures:

// Querying for all released in the year 2011
var albumList []albums.Album
err = Albums.Filter(Albums.Year.Eq(2011)).List(ctx, &albumList)

Get

go get github.com/sheenobu/cm.go
go get github.com/sheenobu/cm.go/sql

Usage

albums/albums.go:

import cm "github.com/sheenobu/cm.go"

// Album is the model for music album database
type Album struct {
	ID       *int
	Artist   string
	Name     string
	Explicit bool
	Year     int64
}

// AlbumsCollection defines the columns and operations for
// the Album model
type AlbumsCollection struct {
	cm.Collection
	ID       cm.ValueColumn
	Artist   cm.ValueColumn
	Name     cm.ValueColumn
	Year     cm.ValueColumn
	Explicit cm.ValueColumn
}

albums/albums_sqlite.go:

import (
	"github.com/jmoiron/sqlx"
	"github.com/sheenobu/cm.go/sql"
)

// Collection is the database attached reference
// of the albums collection
var Collection *AlbumsCollection

func init() {
	db, _ := sqlx.Connect("sqlite3", "albums.db")

	// initialize the fields, declaring what they do and how they map
	Collection = &AlbumsCollection{
		Collection: sql.New(db, "ALBUMS"), // db connection, table name

		// if you want to use uuid's or some other generated key
		//ID:		sql.Varchar("id", 32).PrimaryKey().FromFunction(uuidGen)

		ID:       sql.Integer().PrimaryKey(),
		Artist:   sql.Varchar("artist", 100).NotNull(),
		Name:     sql.Varchar("name", 100).NotNull(),
		Year:     sql.Column("year", "number not null"),
		Explicit: sql.Column("explicit", "bool not null default false"),
	}

	// Init performs the heavy lifting of creating the tables,
	// pre-caching reflection results, building the querys.
	_ = Collection.Init(Collection)
}

main.go:

import (
	"context"
	"github.com/sheenobu/cm.go/doc/albums"
)

// Albums is an optional shortcut for our example code
var Albums = albums.Collection

func main() {
	// for potential transactions
	ctx := context.Background()

	// insertion
	err = Albums.Insert(ctx, &albums.Album{
		Artist:   "Childish Gambino",
		Name:     "Camp",
		Year:     2011,
		Explicit: true,
	})

	err = Albums.Insert(ctx, &albums.Album{
		Artist:   "Sleater-Kinney",
		Name:     "No Cities To Love",
		Year:     2015,
		Explicit: false,
	})

	// Querying for all released in the year 2011
	var albumList []albums.Album
	err = Albums.Filter(Albums.Year.Eq(2011)).List(ctx, &albumList)

	// Updating all explicit albums to non-explicit
	filter := Albums.Explicit.Eq(true)
	setter := Albums.Explicit.Set(false)
	err = Albums.Filter(filter).Edit(setter).Update(ctx)

	// Deletion by ID
	err = Albums.Filter(Albums.ID.Eq(0)).Delete(ctx)
}

Backends

  • sqlx - In Progress
  • database/sql - TODO
  • others - TBD

Implemented

  • Filtering
  • Fetching
  • Deleting via filtering
  • Updating via filtering
  • Simple schema generation
  • Inserting
  • Pagination
  • Automatic column generation on insert
  • (Optional) Transactions

TODO

  • [DONE] Nullable column types
  • [DONE] Remove/Replace build tool to support importing from other projects.
  • [DONE] sql.Varchar
  • [DONE] In-place updates: c.Edit(c.MyColumn.Set("value"))
  • [DONE] Point to $GOPATH/../www for demo file root
  • [DONE] sql.Integer
  • [DONE] PrimaryKey, AutoIncrement builder functions
  • [DONE] Automatic ID generation via function
  • [DONE] Transactions
  • sql.DateTime
  • Raw Query API
  • Remove reflection code.
  • Updating by entity (Not sure if we want to support this)
  • Deleting by entity
  • Database Versioning
  • Relations / SQL joins
  • Caching
  • In-place updates:
    • c.Edit(c.MyColumn.Append("_appended_string") - TODO
    • c.Edit(c.MyIntegerColumn.Add(1)) - TODO
    • c.Edit(c.MyColumn.SetFunc(fn)) - TODO

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

type Collection interface {
	Init(interface{}) error

	ExecRaw(context.Context, string) error

	// Modify Operations
	Filter(Predicate) Collection
	Edit(Operation) Collection

	Insert(context.Context, interface{}) error
	Delete(context.Context) error
	Update(context.Context) error

	// Read Operations
	List(context.Context, interface{}) error
	Page(context.Context, int) (Paginator, error)
	Single(context.Context, interface{}) error

	// C returns the internal composite type, if it exists. Nil otherwise
	C() Collection
}

Collection defines a grouping of related entities that can be created, searched, filtered, updated, and deleted.

type Operation

type Operation interface {
	Apply(c Collection) error
}

Operation defines a piece of code that modifies a writing statement

type Paginator

type Paginator interface {

	// Apply pushes the current page of results into the given slice
	Apply(context.Context, interface{}) error

	// PageCount returns the number of pages
	PageCount() int

	// ItemCount returns the total number of items
	ItemCount() int

	// CurrentPage returns the current page
	CurrentPage() int

	// PerPageCount returns how many items are going to be on each page
	PerPageCount() int

	// Next moves to the next page, returning false if we are out of pages
	Next() bool

	// Prev moves to the previous page, returning false if we are out of pages
	Prev() bool
}

Paginator is the stateful object for allowing page based iteration of a collection.

type Predicate

type Predicate interface {
	Apply(c Collection) error
}

Predicate defines a piece of code that modifies a reading statement.

type Transaction

type Transaction interface {
	Commit() error
	Rollback() error
	Active() bool
}

A Transaction is a unit of work against a database.

type Transactioner

type Transactioner interface {
	BeginTx() (Transaction, error)
}

Transactioner marks a collection as something that supports starting transactions.

type ValueColumn

type ValueColumn interface {

	// Name is the column name
	Name() string

	// Type is the column type (sql type, redis type, etc)
	Type() string

	// Eq returns a predicate that, when applied to a Collection, filters out matching elements
	Eq(interface{}) Predicate

	// NotEq returns a predicate that, when applied to a Collection, filters out non-matching elements
	NotEq(interface{}) Predicate

	// Like returns a predicate that, when applied to a Collection, filters out wildcard matching elements
	Like(bool, interface{}) Predicate

	// Set returns the operation that, when applied to a collection, updates the elements
	Set(interface{}) Operation
}

ValueColumn defines a column defined on a collection.

Directories

Path Synopsis
demos

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL