Giter VIP home page Giter VIP logo

crudiator's Introduction

crudiator

Supercharge your web backend development with crudiator.

Usage

  1. Define the schema
var (
	studentCrudiator = crudiator.MustNewEditor(
		"students",
		crudiator.POSTGRESQL,
		crudiator.NewField("id", crudiator.IsPrimaryKey, crudiator.IncludeOnRead),
		crudiator.NewField("name", crudiator.IncludeAlways),
		crudiator.NewField("age", crudiator.IncludeAlways),
		crudiator.NewField("created_at", crudiator.IncludeOnCreate, crudiator.IncludeOnRead),
		crudiator.NewField("updated_at", crudiator.IncludeOnUpdate, crudiator.IncludeOnRead),
		crudiator.NewField("school_id", crudiator.IncludeOnCreate, crudiator.IncludeOnRead, crudiator.IsSelectionFilter),
	).SoftDelete(true, "deleted_at").
		MustPaginate(crudiator.KEYSET, "id").
		Build()
)

studentCrudiator can be used concurrently since it does not store any state data.

  1. Call the CRUD functions by passing two things:
    • a form from which to pull values from
    • the *sql.DB handle to write and read values from.
studentCrudiator.Create(form, db)

Refer to tests for additional use cases

Pagination

Pagination is supported when reading data.

The following pagination strategies are implemented:

  1. Offset pagination - Uses OFFSET and LIMIT which allows skipping to specific offsets. However, as the size of data grows, it becomes significally slow since the entire table needs to be scanned in order to determine the offset.
  2. Keyset pagination - Utilizses an index to prevent full table scan. Implementation at the moment uses the following since most tables will have numeric keys:
WHERE indexedColumn > :lastIndexColumnValue:
ORDER BY indexedColumn ASC
FETCH NEXT 200 ROWS ONLY;

:lastIndexColumnValue: is the value of the previous value, initially 0, which automatically will return the first 200 rows.

The disadvantage of keyset pagination is that you cannot skip to a specific row offset.

Customization callbacks

There are two callback functions:

  • PreActionCallback
  • PostActionCallback

Each of these is invoked before and after each function of the CRUD is called.

Web Service Framework Support

Library Status Adapter
net/http YES nethttp
go-fiber NO gofiber
gin NO

Database support

Database Create Read Update Delete TESTED
PostgreSQL YES YES YES YES YES
MYSQL YES YES YES YES NO
SQLITE YES YES YES YES NO

Validation

crudiator does not provide data validation. Validation is not appropriate at this layer. It should be trivial to use data validators at a higher layer before passing data to crudiator.

crudiator's People

Contributors

dependabot[bot] avatar sharkfoursix avatar

Stargazers

 avatar

Watchers

 avatar  avatar

crudiator's Issues

Add type converters

Add support for type conversion.

Currently, conversion can be done manually through postRead/preCreate/preUpdate callbacks.

Having pre-defined re-usable converters would be ideal.

Example:

type FieldConverter func(src any) (any, error)

type Field struct {
    ...
    Converter FieldConverter
}

var ConvertTo FieldOption = func(converter FieldConverter) FieldOption {
    return func(f *Field) {
        f.Converter = converter;
    }
}

var uuidStringType FieldConverter = func(src any) (any, error){
    return string(src), nil
}

crudiator.NewField("user_uuid", crudiator.ConvertTo(uuidStringType))

FEATURE: Add Context support

Add *Context methods (i.e CreateWithContext, ReadWithContext, UpdateWithContext, DeleteWithContext to call sql.DB.ExecContext())

FEATURE: Add table joins

Add support for table joins.

  1. Start by prefixing column names with table name/alias
fooCrudiator = crudiator.MustNewEditor(
		"foo",
		crudiator.POSTGRESQL,
		crudiator.NewField("id", crudiator.IsPrimaryKey, crudiator.IncludeOnRead),
		crudiator.NewField("name", crudiator.IncludeAlways),
                crudiator.NewField("bar_id", crudiator.IncludeAlways),
                crudiator.NewField("some_x", crudiator.IncludeAlways),
                crudiator.NewField("some_y", crudiator.IncludeAlways)).
                Join("bar", "optionalTableAlias", crudiator.NewJoinField("optionalTableAlias.id", "foo.bar_id"), crudiator.NewJoinField("optionalTableAlias.some_x", "foo.some_x")). // JOIN bar AS optionalTableAlias ON foo.bar_id = optionalTableAlias.id AND optionalTableAlias.some_x = foo.some_x
                Join("bar", nil, crudiator.NewJoinField("bar.id", "foo.some_y")).
		Build()

Add custom SQL filters support

Improve field definitions by adding proper custom column filter SQL

Example

Called during create

crudiator.NewField("field",  crudiator.CreateFilter("$1 || UPPER($3)"))
// INSERT INTO foo (field,f2,f3) VALUES($1 || UPPER($3), $2, $3)

Called during read

crudiator.NewField("field",  crudiator.ReadFilter("IS NOT NULL AND upper(field) = upper($3)"))
// SELECT * FROM foo WHERE field IS NOT NULL AND upper(field) = upper($3)

Called during update

crudiator.NewField("field",  crudiator.UpdateFilter("upper($3)"))
// UPDATE foo SET field = upper($3)

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.