Git Product home page Git Product logo

goprisma's Introduction

GoPrisma - a Go wrapper for the Prisma Engines

What's this?

Introspect a database and use it as a GraphQL API using Go.

Supported Databases:

  • SQLite
  • PostgreSQL
  • MySQL
  • MongoDB
  • MS SQL Server
  • CockroachDB

How to use this in production?

It might be obvious, but I just want to make it clear that you should NOT publicly expose your database with this library. This is only intended for use cases where the GraphQL API is shielded by some other component.

We're using goprisma internally in WunderGraph which adds such a shielding layer, with authentication, authorization, role-based access control, caching and more.

Additionally, WunderGraph comes with a TypeScript SDK, so you can introspect multiple databases as well as GraphQL and REST APIs and join them together.

Check out WunderGraph if you're interested in a production-grade solution. Here's a link to the docs on all supported DataSources.

Otherwise, feel free to use this as a go package in your own projects.

go get

go get github.com/jensneuse/goprisma

Introspect a database

prismaSchema := "datasource db {provider = \"postgresql\" url = \"postgresql://admin:admin@localhost:54321/example?schema=public&connection_limit=20&pool_timeout=5\"}"
schema, sdl, err := Introspect(prismaSchema)

Make a Query

query := "{\"query\": \"query Messages {findManymessages(take: 20 orderBy: [{id: desc}]){id message users {id name}}}","variables\": {}}"
engine, err := NewEngine(schema)
if err != nil {
	return
}
defer engine.Close()
response := engine.Execute(query)

Why?

GraphQL is a nice abstraction layer on top of other APIs. Making databases available can be a productivity gainer when no direct database access is required and GraphQL clients already exist in the codebase.

How does it work

Prisma is a really powerful ORM. If you look closely, you'll realize that internally, Prisma 2 is powered by a GraphQL Engine to abstract away the database layer.

The GraphQL Engine is written in Rust.

This library is a CGO wrapper to make the Prisma Rust GraphQL Engine available to write Go programs on top of it.

Is it fast?

I've measured ~0.3ms latency (Go -> Rust -> Database -> Go) on my laptop using a database in docker.

goos: darwin
goarch: amd64
pkg: github.com/jensneuse/goprisma/pkg/prisma
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkEngine_Execute
BenchmarkEngine_Execute-16    	   43815	    304507 ns/op	     144 B/op	       3 allocs/op
PASS

You can find the benchmark in the prisma package tests and run it yourself.

What architectures are supported?

  • darwin x86_64
  • darwin aarch64
  • linux x86_64
  • windows x86_64 (needs verification)

Other targets can be added.

Where is the Rust wrapper?

https://github.com/jensneuse/prisma-engines/tree/goprisma

Example / How to run the tests?

First, clone the repo.

cd docker-example
docker-compose up

Now you're able to run the tests.

How can you help?

Testing

You can help by running the tests and architectures that need verification. Please report any issues and tell me if it works.

Automate building the C-Wrapper

You might want to increase the level of trust by adding a github action to automate building the Rust C-Wrapper. Currently, I'm building it on my MacBook.

Ideally, this github action would fetch the latest stable of prisma-engines, compile the C-Wrapper and updates the lib folder via PR. A blueprint how to do this can be found here: https://github.com/rogchap/v8go/blob/master/.github/workflows/v8build.yml

If you're interested in adding this, please open a PR. That said, you can always build the Rust part yourself to increase the level of trust.

goprisma's People

Contributors

jensneuse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

goprisma's Issues

go get failed

Hey โ€“ this project is really exciting. I just gave it a go but unfortunately ran into the following error:

go get github.com/jensneuse/goprisma/pkg/prisma

go: downloading github.com/jensneuse/goprisma v0.0.0-20210531103550-6521fb3d61b3
# github.com/jensneuse/goprisma/pkg/prisma
ld: warning: ignoring file /Users/m/dev/pkg/mod/github.com/jensneuse/[email protected]/lib/darwin/libquery_engine_c_api.dylib, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x76 0x65 0x72 0x73 0x69 0x6F 0x6E 0x20 0x68 0x74 0x74 0x70 0x73 0x3A 0x2F 0x2F )
Undefined symbols for architecture x86_64:
  "_free_introspection_result", referenced from:
      __cgo_2dba20b15c43_Cfunc_free_introspection_result in _x002.o
     (maybe you meant: __cgo_2dba20b15c43_Cfunc_free_introspection_result)
  "_free_prisma", referenced from:
      __cgo_2dba20b15c43_Cfunc_free_prisma in _x002.o
     (maybe you meant: __cgo_2dba20b15c43_Cfunc_free_prisma)
  "_prisma_execute", referenced from:
      __cgo_2dba20b15c43_Cfunc_prisma_execute in _x002.o
     (maybe you meant: __cgo_2dba20b15c43_Cfunc_prisma_execute)
  "_prisma_introspect", referenced from:
      __cgo_2dba20b15c43_Cfunc_prisma_introspect in _x002.o
     (maybe you meant: __cgo_2dba20b15c43_Cfunc_prisma_introspect)
  "_prisma_new", referenced from:
      __cgo_2dba20b15c43_Cfunc_prisma_new in _x002.o
     (maybe you meant: __cgo_2dba20b15c43_Cfunc_prisma_new)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/m/Library/Caches/go-build"
GOENV="/Users/m/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/m/dev/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/m/dev"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.3"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/m/dev/src/github.com/matthewmueller/hack/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/4f/tcxcr6_55v9bp38d8g4hjlf80000gn/T/go-build2413581743=/tmp/go-build -gno-record-gcc-switches -fno-common"

Any ideas? Thanks!

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.