https://github.com/datumbrain/nulltypes
GORM null-types JSON marshalling and unmarshalling mixins.
https://github.com/datumbrain/nulltypes
hacktoberfest
Last synced: about 1 year ago
JSON representation
GORM null-types JSON marshalling and unmarshalling mixins.
- Host: GitHub
- URL: https://github.com/datumbrain/nulltypes
- Owner: datumbrain
- License: mit
- Created: 2020-10-13T06:28:11.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-09-03T09:01:35.000Z (over 1 year ago)
- Last Synced: 2025-04-23T22:05:37.170Z (about 1 year ago)
- Topics: hacktoberfest
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 17
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nulltypes [](https://travis-ci.com/github/datumbrain/nulltypes) [](https://goreportcard.com/report/github.com/datumbrain/nulltypes) 

`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:
```go
nulltypes.DatabaseLocation, _ = time.LoadLocation([YOUR_TIME_ZONE])
```
## Import
```go
import "github.com/datumbrain/nulltypes"
```
## Usage
Here is an example usage with _GORM_.
```go
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"`
}
```
```go
user := User{
ID: 0,
Name: "John Doe",
Address: nulltypes.String("221B Baker Street"),
CreationDate: time.Now(),
UpdationDate: nulltypes.Now(),
ManagerID: nulltypes.Int64(5),
}
```
## Author
- [Faizan Khalid](https://github.com/iamfaizankhalid)
- [Fahad Siddiqui](https://github.com/fahadsiddiqui)