Skip to content

Latest commit

 

History

History
53 lines (39 loc) · 1.91 KB

File metadata and controls

53 lines (39 loc) · 1.91 KB

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