Giter Club home page Giter Club logo

jubaerhossain / gosql Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 16 KB

This is Go code for a GraphQL API that interacts with a database. The code includes functions for querying and retrieving data from the database. The code uses the Go programming language and the GraphQL Go library to handle GraphQL queries. It also uses the database/sql library to interact with the database.

Home Page: https://github.com/JubaerHossain/gosql/wiki

Go 100.00%
go-mysql graphql-server mysql

gosql's Introduction

GoSQL

GoDoc Go Report Card

GoSQL is a package that provides a simple way to query and manipulate SQL databases from GraphQL resolvers in Go.

Features - Query models by ID or with a where clause. - Specify which fields to select from the database using GraphQL queries. - Automatically convert database rows into Go struct instances. - Easily build a where clause with a map of column names and values. - Supports PostgreSQL, MySQL, and SQLite databases.

Installation

To use this package in your Go project, you can install it using the go get command:

go get github.com/JubaerHossain/gosql

Getting Started

To use GoSQL, first, you need to import the package:

import "github.com/JubaerHossain/gosql"

Then, define your model struct and make sure that the field names match the column names in your database table:

type User struct {
    ID        int
    FirstName string
    LastName  string
    Email     string
}

You can then use GoSQL to query the database and get the results as a slice of struct instances:

func GetUser(params graphql.ResolveParams) (interface{}, error) {
    // Query the database for users
    users, err := gosql.QueryModel(reflect.TypeOf(User{}), "users", params)
    if err != nil {
        return nil, err
    }

    // Return the results
    return users, nil
}

You can also query a single record by ID:

func GetUserByID(params graphql.ResolveParams) (interface{}, error) {
    // Query the database for a user by ID
    user, err := gosql.FindByID(reflect.TypeOf(User{}), "users", params)
    if err != nil {
        return nil, err
    }

    // Return the result
    return user, nil
}

Where Clauses You can also build a where clause using a map of column names and values:

func GetUsersWithLastName(params graphql.ResolveParams) (interface{}, error) {
    // Build the where clause
    where := make(map[string]interface{})
    where["last_name"] = params.Args["lastName"].(string)

    // Query the database for users
    users, err := gosql.QueryModel(reflect.TypeOf(User{}), "users", params, where)
    if err != nil {
        return nil, err
    }

    // Return the results
    return users, nil
}

Selecting Fields You can specify which fields to select from the database using GraphQL queries:

{
    users {
        firstName
        email
    }
}
func GetUser(params graphql.ResolveParams) (interface{}, error) {
    // Get the columns to select from the query
    selectColumn := gosql.GetColumns(params)

    // Query the database for users
    users, err := gosql.QueryModel(reflect.TypeOf(User{}), "users", selectColumn, params)
    if err != nil {
        return nil, err
    }

    // Return the results
    return users, nil
}

Supported Databases GoSQL supports PostgreSQL, MySQL, and SQLite databases. To connect to your database, you need to set the DB variable in the database package to a database handle.

import "database/sql"
import _ "github.com/lib/pq"
import "lms/database"

func init() {
    db, err := sql.Open("postgres", "user=postgres dbname=lms sslmode=disable")
    if err != nil {
        panic(err)
    }

    database.DB = db
}

Contributing Contributions are welcome! If you find a bug or want to add a feature, please open an issue or submit a pull request.

gosql's People

Contributors

jubaerhossain avatar

Watchers

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