From 29ede650334228564d9d880b394e209879a48bf1 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Sat, 3 Dec 2022 15:02:07 -0900 Subject: [PATCH] add date format and orDefault helpers --- backend/internal/data/repo/query_helpers.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 backend/internal/data/repo/query_helpers.go diff --git a/backend/internal/data/repo/query_helpers.go b/backend/internal/data/repo/query_helpers.go new file mode 100644 index 0000000..2205d81 --- /dev/null +++ b/backend/internal/data/repo/query_helpers.go @@ -0,0 +1,18 @@ +package repo + +import "time" + +func sqliteDateFormat(t time.Time) string { + return t.Format("2006-01-02 15:04:05") +} + +// orDefault returns the value of the pointer if it is not nil, otherwise it returns the default value +// +// This is used for nullable or potentially nullable fields (or aggregates) in the database when running +// queries. If the field is null, the pointer will be nil, so we return the default value instead. +func orDefault[T any](v *T, def T) T { + if v == nil { + return def + } + return *v +}