Giter Site home page Giter Site logo

briamoe / go-planetscale-serverless Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 37 KB

Makes doing queries to PlanetScale over HTTP in Go possible, allowing for the use of PlanetScale in serverless applications.

License: MIT License

Go 100.00%
go mysql database planetscale serverless vitess golang

go-planetscale-serverless's Introduction

🪐 PlanetScale Serverless Driver for Go

Makes doing queries to PlanetScale over HTTP in Go possible, allowing for the use of PlanetScale in serverless applications. Based on the PlanetScale Serverless Driver for JavaScript.

⚠️ Experimental

This library is incredibly experimental, as it uses undocumented API's only exposed by the aforementioned JavaScript driver. Things are subject to change as they update the driver.

Quick Start

Here's the essentials you need to get up and running with the library.

package main

import (
	"github.com/briamoe/go-planetscale-serverless"
)

type User struct {
	ID   int    `ps:"id"`
	Name string `ps:"name"`
}

func main() {
	c, err := planetscale.NewConnection(&planetscale.Config{
		Username: "<username>",
		Password: "<password>",
		Host:     "<host>",
	})
	if err != nil {
		panic(err)
	}

	e, err := c.Execute("SELECT * FROM users WHERE id=?", 1);
	if err != nil {
		panic(err)
	}

	var u []*User
	if err = e.Decode(&u); err != nil {
		panic(err)
	}
}

Installation

Requires Go version 1.18 or higher.

go get -u github.com/briamoe/go-planetscale-serverless

Examples

Listed below are a few examples of how to use this library on different popular serverless providers.

Connection

Creating a new connection creates a shared session that can be used across queries to PlanetScale.

To generate the credentials highlighted in <> below, head to your planetscale dashboard and select your database. Then go to Settings > Passwords > New password, and paste in the values it gives you.

planetscale.NewConnection(&planetscale.Config{
	Username: "<username>",
	Password: "<password>",
	Host:     "<host>",
})

Executing

Executing performs a query to a PlanetScale Database.

connection.Execute("SELECT * FROM users WHERE id=?", 1)

Selects

If a statement includes a select, you can grab the rows returned by using Decode(out interface{})

type User struct {
	ID   int    `ps:"id"`
	Name string `ps:"name"`
}

var []*User
executed.Decode(&u)

The use of ps tags are an imporant part of the decoding process, as it determines which fields inside sql are mapped to fields in a struct. Make sure to incude these!

Inserts or Updates

If a statement includes a insert or an update statement, you can access how many rows were affected or the insert id. These are set to 0 if a query doesn't include any of those.

fmt.Println(executed.RowsAffected) // 1
fmt.Println(executed.InsertID) // 200

go-planetscale-serverless's People

Contributors

briamoe avatar

Stargazers

 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.