Git Product home page Git Product logo

pgx-protobuf-timestamp's Introduction

Go Reference Build Status

pgx-protobuf-timestamp - Protobuf Timestamp Scanning for github.com/jackc/pgx

Are you using github.com/jackc/pgx/v5? Do you need to Scan/insert protobuf Timestamps? Then this is the library for you!

Setup

You will need to add this to your project of course:

go get github.com/manniwood/pgx-protobuf-timestamp

Then, if you are using a single connection to postgres, do this:

import (
	"github.com/jackc/pgx/v5"
	"github.com/manniwood/pgx-protobuf-timestamp/pgxpbts"
)

...

config, err := pgx.ParseConfig(dbURL)
if err != nil {
	return nil, err
}

conn, err := pgx.ConnectConfig(context.Background(), config)
if err != nil {
	return nil, err
}
pgxpbts.Register(conn.TypeMap())
pgxpbts.RegisterTZ(conn.TypeMap())

or, if you are using pgxpool:

import (
	"github.com/jackc/pgx/v5"
	"github.com/jackc/pgx/v5/pgxpool"
	"github.com/manniwood/pgx-protobuf-timestamp/pgxpbts"
)

...

config, err := pgxpool.ParseConfig(dbURL)
if err != nil {
	return nil, err
}
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
	pgxpbts.Register(conn.TypeMap())
	pgxpbts.RegisterTZ(conn.TypeMap())
	return nil
}
pool, err := pgxpool.NewWithConfig(context.Background(), config)
if err != nil {
	return nil, err
}

Now, you will be able to serialize/deserialize Go protobuf *timestamppb.Timestamp types to/from Postgres timestamp and timestamptz types.

See the tests for examples.

Inspiration

See https://github.com/jackc/pgx-gofrs-uuid and https://github.com/jackc/pgx-shopspring-decimal for more examples of how to write type serializers/deserializers for pgx.

pgx-protobuf-timestamp's People

Contributors

manniwood avatar

Stargazers

Martin Löper avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.