Skip to content

kislerdm/neon-sdk-go

Repository files navigation

Go SDK for Neon Postgres SaaS Platform


⭐ The project needs your support! Please leave a star and become a GitHub sponsor! ⭐

💖 Thank you @neondatabase for sponsoring the project! 💖

logo

Go Report Card codecov Licenses Check

An SDK to manage the Neon Platform programmatically.

Neon is a serverless Postgres platform designed to help you build reliable and scalable applications faster. Neon separates compute and storage to offer modern developer features such as autoscaling, branching, point-in-time restore, and more. Neon is open source and written in Rust.

Find more about Neon here.

How to use

Prerequisites

Installation

Add the SDK as a module dependency:

go get github.com/kislerdm/neon-sdk-go

Run to specify the release version:

go get github.com/kislerdm/neon-sdk-go@{{.Ver}}

Where {{.Ver}} is the release version.

Code Snippets

Default HTTP Client

The following snippet demonstrates how to initialize SDK which will use default HTTP client.

package main

import (
	"log"

	neon "github.com/kislerdm/neon-sdk-go"
)

func main() {
	client, err := neon.NewClient(neon.Config{Key: "{{.NeonApiKey}}"})
	if err != nil {
		panic(err)
	}

	v, err := client.ListProjects(nil, nil, nil)
	if err != nil {
		panic(err)
	}

	log.Printf("%d projects found", len(v.Projects))
}

Custom HTTP client

The SDK can be initialized with a custom HTTP client.

package main

import (
	"net/http"
	"log"
	"time"

	neon "github.com/kislerdm/neon-sdk-go"
)

func main() {
	myHTTPClient := &http.Client{Timeout: 30 * time.Second}

	client, err := neon.NewClient(neon.Config{Key: "{{.NeonApiKey}}", HTTPClient: myHTTPClient})
	if err != nil {
		panic(err)
	}

	v, err := client.ListProjects(nil, nil, nil)
	if err != nil {
		panic(err)
	}

	log.Printf("%d projects found", len(v.Projects))
}

Mock

The SDK provides the http client's mock for unit tests. An example snippet is shown below.

package main

import (
	"log"

	neon "github.com/kislerdm/neon-sdk-go"
)

func main() {
	client, err := neon.NewClient(neon.Config{HTTPClient: neon.NewMockHTTPClient()})
	if err != nil {
		panic(err)
	}

	v, err := client.ListProjects(nil, nil, nil)
	if err != nil {
		panic(err)
	}

	log.Printf("%d projects found", len(v.Projects))
}

Find here the example of how to use the SDK to create a Neon project and use its default database afterward.

Development

The SDK codebase is generated using the OpenAPI from the Neon API reference page. The generator application codebase can be found here.

Commands

Prerequisites:

  • go ~> 1.18
  • gnuMake / cmake

Run to see all available commands:

make help

Run to generate the SDK codebase and store it to PWD, given the OpenAPI spec is available in the file openAPIDefinition.json:

make generate-sdk

Set the flag SKIP_TEST=1 to generate the codebase without running the unit tests afterward:

SKIP_TEST=1 make generate-sdk

Run to customise the locations:

make generate-sdk PATH_SDK=##/PATH/TO/OUTPUT/SDK/CODE## PATH_SPEC=##/PATH/TO/SPEC.json##

Run to test generated SDK:

make tests

Run to test generated SDK stored to /PATH/TO/OUTPUT/SDK/CODE:

make tests DIR=/PATH/TO/OUTPUT/SDK/CODE

Run to test the code generator:

make tests DIR=generator

Run to build the code generator:

make build DIR=generator

Contribution

The SDK is distributed under the MIT license. You can find a full list of dependency licenses here.

Please feel free to open an issue ticket or PR to contribute.