Skip to content

GORM null-types JSON marshalling and unmarshalling mixins.

License

Notifications You must be signed in to change notification settings

datumbrain/nulltypes

Repository files navigation

nulltypes Build Status Go Report Card License

nulltypes is a golang module that provides an alternative to nullable data types from database/sql with proper JSON marshalling and unmarshalling.

It also provides a wrapper for time.Time to format time to use with timestamp of SQL databases, i.e. mysql, postgres.

The default database time zone is set to UTC, but it can easily be changed with:

nulltypes.DatabaseLocation, _ = time.LoadLocation([YOUR_TIME_ZONE])

Import

import "github.com/datumbrain/nulltypes"

Usage

Here is an example usage with GORM.

package models

type User struct {
	ID              uint                 `json:"id" gorm:"primary_key"`
	Name            string               `json:"name"`
	Address         nulltypes.NullString `json:"address,omitempty"`
	CreationDate    time.Time            `json:"-" gorm:"autoCreateTime:milli;default:current_timestamp"`
	UpdationDate    nulltypes.NullTime   `json:"-" gorm:"autoUpdateTime:milli"` 
	TerminationDate nulltypes.NullTime   `json:"termination_date,omitempty"`
	ManagerID       nulltypes.NullInt64  `json:"manager_id,omitempty" gorm:"OnUpdate:CASCADE,OnDelete:SET NULL"`
}
user := User{
	ID:           0,
	Name:         "John Doe",
	Address:      nulltypes.String("221B Baker Street"),
	CreationDate: time.Now(),
	UpdationDate: nulltypes.Now(),
	ManagerID:    nulltypes.Int64(5),
}

Author

About

GORM null-types JSON marshalling and unmarshalling mixins.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages