Skip to content
/ gormcls Public

gormcls - Isolate the scope of temporary variables when using GORM, making the code more concise

License

Notifications You must be signed in to change notification settings

yyle88/gormcls

Repository files navigation

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

gormcls - Isolate the scope of temporary variables when using GORM, making the code more concise

gormcls isolates the scope of temporary variables when working with GORM, simplifying database operations and making the code more concise.

gormcls works in conjunction with gormcnm and gormcngen, simplifying GORM development and optimizing the management of temporary variable scopes.


CHINESE README

中文说明


Installation

go get github.com/yyle88/gormcls

Quick Start

Example Code

Query Data

var example Example
if cls := gormcls.Cls(&Example{}); cls.OK() {
	err := db.Table(example.TableName()).Where(cls.Name.Eq("test")).First(&example).Error
    must.Done(err)
    fmt.Println("Fetched Name:", example.Name)
}

Update Data

if one, cls := gormcls.Use(&Example{}); cls.OK() {
    err := db.Model(one).Where(cls.Name.Eq("test")).Update(cls.Age.Kv(30)).Error
    must.Done(err)
    fmt.Println("Age updated to:", 30)
}

Get Maximum Value

var maxAge int
if one, cls := gormcls.Use(&Example{}); cls.OK() {
	err := db.Model(one).Select(cls.Age.COALESCE().MaxStmt("max_age")).First(&maxAge).Error
	must.Done(err)
    fmt.Println("Max Age:", maxAge)
}

API Overview

Function Param Return Description
Cls MOD CLS Returns the column information (cls), useful when only column data is needed.
Use MOD MOD, CLS Returns the model (mod) and its associated columns (cls), ideal for queries or operations that need both.
Umc MOD MOD, CLS Returns the model (mod) and its associated columns (cls), functioning identically to the Use function.
Usc MOD []MOD, CLS Returns a slice of models (MOD) and the associated columns (cls), suitable for queries returning multiple models (e.g., Find queries).
Msc MOD MOD, []MOD, CLS Returns the model (mod), the model slice ([]MOD), and the associated columns (cls), useful for queries requiring both model and column data.
One MOD MOD Returns the model (mod), ensuring type safety by checking whether the argument is a pointer type at compile-time.
Ums MOD []MOD Returns a slice of models (MOD), useful for queries that expect a slice of models (e.g., Find queries).
Uss - []MOD Returns an empty slice of models (MOD), typically used for initialization or preparing for future object population without needing the columns.
Usn int []MOD Returns a slice of models (MOD) with a specified initial capacity, optimizing memory allocation based on the expected number of objects (MOD).

License

gormcls is open-source and released under the MIT License. See the LICENSE file for more information.


Support

Welcome to contribute to this project by submitting pull requests or reporting issues.

If you find this package helpful, give it a star on GitHub!

Thank you for your support!

Happy Coding with gormcls! 🎉

Give me stars. Thank you!!!

See stars

see stars

About

gormcls - Isolate the scope of temporary variables when using GORM, making the code more concise

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published