sql-nullstring

command
Version: v0.7.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2021 License: MIT Imports: 6 Imported by: 0

README

Go GraphQL SQL null string example

database/sql Nullstring implementation, with JSON marshalling interfaces.

To run the program, go to the directory
cd examples/sql-nullstring

Run the example
go run main.go

sql.NullString

On occasion you will encounter sql fields that are nullable, as in

CREATE TABLE persons (
    id INT PRIMARY KEY,
    name TEXT NOT NULL,
    favorite_dog TEXT -- this field can have a NULL value
)

For the struct

import "database/sql"

type Person struct {
    ID          int             `json:"id" sql:"id"`
    Name        string          `json:"name" sql:"name"`
    FavoriteDog sql.NullString  `json:"favorite_dog" sql:"favorite_dog"`
}

But graphql would render said field as an object {{ false}} or {{Bulldog true}}, depending on their validity.

With this implementation, graphql would render the null items as an empty string (""), but would be saved in the database as NULL, appropriately.

The pattern can be extended to include other database/sql null types.

Documentation

The Go Gopher

There is no documentation for this package.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL