Giter Club home page Giter Club logo

datastore's Introduction

Data Store

Basing on gist of RA9dev I've created a simple data store for my projects using v5/pgxpool

This package provides a simple data store that supports CRUD operations for a user and product. It uses a database connection pool to manage the connections to the database and provides transaction management using pgx.TxOptions.

Usage

To create a new data store, call the NewConnectedPostgresDB function with a valid database URL. This will create a new connection pool and connect to the database using the provided URL.

db, err := storage.NewConnectedPostgresDB("putYourURLHere")
if err != nil {
    panic(err)
}

The data store provides methods for working with users and products. To create a new user, call the User method to get a user repository and use the Create method to create a new user.

user := storage.User{
    Name: "John Doe",
}
user, err := db.User().Create(context.TODO(), user)
if err != nil {
    panic(err)
}

The data store also provides a InTransaction method that allows you to run a function in the context of a transaction. If the function returns an error, the transaction is rolled back. Otherwise, the transaction is committed.

err := db.InTransaction(context.TODO(), func(ctx context.Context, txStore storage.DataStore) error {
    user, err := txStore.User().One(ctx, storage.UserFilter{IDs: []uint64{1}})
    if err != nil {
        return err
    }


    product, err := txStore.Product().Create(ctx, storage.Product{
        Name: "Book for John Doe",
    })
    if err != nil {
        return err
    }
    return nil
}, pgx.TxOptions{})
if err != nil {
    panic(err)
}

When you are done with the data store, be sure to call the Close method to close the underlying database connections.

datastore's People

Contributors

ayiskakov 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.