Model Helpers

Model Helpers

Create

import (
	"github.com/totoval/framework/helpers/m"
    "fmt"
)

func Create(){
	user := User{
		Username: ptr.String("totoval"),
		Password: ptr.String("passw0rd"),
	}
	if err := m.H().Create(&user); err != nil{
		panic(err)
	}
	
	fmt.Println(user)
}

Save

import (
	"github.com/totoval/framework/helpers/m"
	"github.com/totoval/framework/helpers/ptr"
	"github.com/totoval/framework/model/types/null"
    "fmt"
)

func Save(){
	user := User{
		Id: 1,
		Username: "totoval",
		Password: "passw0rd",
		Nickname: "mynick",
	}
	userModified := User{
		Password: "passw0rd123", // modify password to `passw0rd123`
	}
	if err := m.H().Save(&user, userModified, User{
		Nickname: null.String(), // set nickname to null
	}); err != nil{
		panic(err)
	}
	
	fmt.Println(user)
	// the user will be saved as User{Id: 1, Username: "totoval", Password: "passw0rd123", Nickname: null}
}

Query

import (
	"github.com/totoval/framework/helpers/m"
	"fmt"
)

func Query(){
	user := User{
		Id: 1,
	}
	if err := m.H().First(&user); err != nil{
		panic(err)
	}
	
	fmt.Println(user)
}

Delete

m.H().Delete(user, false)
````

## Restore

```go
m.H().Restore(user)