fauna / faunadb-go Goto Github PK
View Code? Open in Web Editor NEWGo driver for Fauna v4 (deprecated)
Home Page: https://docs.fauna.com/fauna/v4/
License: Other
Go driver for Fauna v4 (deprecated)
Home Page: https://docs.fauna.com/fauna/v4/
License: Other
I am trying to get faunadb-go with go get ./... but it always get this:
github.com/fauna/faunadb-go v2.0.0+incompatible // indirect
gopkg.in/fauna/faunadb-go.v2 v2.0.0
Queries like Cloud Firestore given that it has Documents. ie db.collection("users").doc("id").get()
I'm a noob at this though
hi, i think relations in golang are of type:
faunadb.refFn
but since type refFn is not exported i can't use it in a struct
type Profile2 struct {
Text string `fauna:"text"`
Relation f.refFn `fauna:"relation"`
}
i am doing it with f.Ref()
there is another way?
How to fetch an array of items and unmarshal them to struct?
I tried using a Lambda, but I couldn't unmarshal the result to an array of my structs.
I ended up with this, but I guess this is doing too many requests to the DB, yes?
func (db DB) Users() ([]User, error) {
res, err := db.client.Query(f.Paginate(f.Match(f.Index("all_users")), f.Size(99999)))
if err != nil {
return nil, err
}
var refs []f.RefV
res.At(f.ObjKey("data")).Get(&refs)
var users []User
for _, ref := range refs {
res, err := db.client.Query(f.Get(ref))
if err != nil {
return nil, err
}
var user User
res.At(f.ObjKey("data")).Get(&user)
users = append(users, user)
}
return users, nil
}
This is my code:
client := f.NewFaunaClient(os.Getenv("FAUNADB_SERVER_SECRET"))
res, err := client.Query(f.Get(f.Ref("classes/Card")))
if err != nil {
panic(err)
}
var cards []*m.Card
if err := res.At(f.ObjKey("data")).Get(cards); err != nil {
log.Println(err)
return &events.APIGatewayProxyResponse{
StatusCode: http.StatusMethodNotAllowed,
Body: "Error fetch data",
}, nil
}
This is the error message:
Error while decoding fauna value at: <root>. Can not decode map into a value of type "[]*card.Card"
When I click the link to go to the future version of godoc, I see that there is no documentation visible.
https://pkg.go.dev/github.com/fauna/faunadb-go/faunadb
Seems like there is a license issue or something.
Hello,
I want to evaluate FaunaDB but it's not possible due to #77
Error:
go get github.com/fauna/[email protected]: github.com/fauna/[email protected]: invalid version: module contains a go.mod file, so major version must be compatible: should be v0 or v1, not v2
Therefore the CI wasn't fixed for more than 20 days. Is this module still maintained?
Related: golang/go#35732
In the godocs, collections are referred by f.RefCollection("someRef")
Method. However, in practice, f.Ref("collections/someRef")
is the right way to get the reference of a specific collection. Is it miss-documented?
The new documentation site for go (pkg.go.dev) does not detect the LICENSE file correctly, causing documenation to not be displayed.
Missing docs: https://pkg.go.dev/github.com/fauna/faunadb-go/v3/faunadb
The License is MPL-2.0, but somehow pkg.go.dev does not detect this.
More info: https://pkg.go.dev/license-policy
Sorry if this has been answered! Thanks!
I ran into an error running go get
:
$ go get github.com/fauna/faunadb-go
package github.com/fauna/faunadb-go: no buildable Go source files in /$GOPATH/src/github.com/fauna/faunadb-go
This works:
$ go get github.com/fauna/faunadb-go/...
To have go get github.com/fauna/faunadb-go
work as stated, you'd need to move everything from /fauna/*
up a directory into the root.
Vercel supports Go, and FaunaDB + Vercel is a promising setup. However, Vercel uses a go.mod file to setup the runtime environment. As per other issues, such as #90, the go mod dependency setup produces some version issues.
The main issue is that Vercel grabs version 2.0.0+incompatible from 2018. I can't the docs for this version. Could you provide link to the docs. Otherwise what is the course of action so that go mod grabs the current version?
Howdy!
I'm using this lib in conjunction w/ Fauna functions to make a GraphQL server. I know Fauna has a product for that - this is more of a learning project than anything else.
Anyway, I've created a create_user
function that takes a lot of inspiration from the website's authentication tutorial:
// create_user
Query(
Lambda(
["email", "handle", "password"],
If(
Or(
Exists(Match(Index("user_ref_by_username"), LowerCase(Var("handle")))),
Exists(Match(Index("users_by_email"), LowerCase(Var("email"))))
),
Abort("the handle or username already exists"),
Create(Collection("users"), {
credentials: { password: Var("password") },
data: {
id: NewId(),
email: LowerCase(Var("email")),
displayName: Var("handle"),
username: LowerCase(Var("handle"))
}
})
)
)
)
It creates a user only if the handle and email address is not unique. Otherwise, it Aborts
with a message describing the problem. Calling this from the shell is helpful - I get exactly what I expect:
> Call("create_user", ["[email protected]", "Alker", "1234"])
Error: call error
{
errors: [
{
position: [],
code: 'call error',
description: 'Calling the function resulted in an error.',
cause: [
{
position: [
'expr',
'then'
],
code: 'transaction aborted',
description: 'the handle or username already exists'
}
]
}
]
}
However, the err
from the call via this lib's client.Query(...)
method only provides a synthesis of the top-level message:
Response error 400. Errors: [](call error): Calling the function resulted in an error."
This makes it hard to handle the error unambiguously. Does this library support either:
If neither, does this make sense to add to the library? If so, where would be a good place to start? I don't mind contributing, but I started learning Go (and FaunaDB) a couple of weeks ago so I'm apprehensive about it at the moment.
I apologize, but I can't figure this out. I did a map over an index to search for specific items. However, I cannot figure out how to get the data out of the faunadb.Value mapping. I have a struct called Cookie and I want to get all of the results from the index and map them into a slice of structs.
type Cookie struct {
ID string `json:"id"`
Name string `json:"name"`
Cost float64 `json:"cost"`
Unit string `json:"unit"`
Ingredients []string `json:"ingredients"`
}
type Cookies []Cookie
resp, err := client.Query(
f.Map(
f.Paginate(
f.MatchTerm(f.Index("ByIngredients"), ingredient)),
f.Lambda("cookie", f.Get(f.Var("cookie"))),
),
)
Here's the data I get back:
map[data:[map[data:map[ingredients:[chocolate chips] name:Chocolate Chip price:10 unit:dozen] ref:{265047217107108372 0xc000584240 <nil>} ts:1589028306980000] map[data:map[ingredients:[chocolate love] name:Brownie price:10 unit:dozen] ref:{265245928522252820 0xc000584640 <nil>} ts:1589217079570000]]]
I just can't figure out how to unmap this with resp.Get()
I am trying to convert the following code into golang, we have an existing firebase app but we are attempting to convert it into faunadb. I have more complex authentication requirements and therefore cannot use faunadb's built-in system. My solution is to write a go Lamba function that can handle login/sign up functionality.
Create(
Tokens(), {
instance: Select("ref", Get(Match(Index('users_by_email'), "[email protected]")))
}
)
func loginUser(email string) {
expr := fauna.Create(fauna.Tokens(), fauna.Select("ref",
fauna.Get(
fauna.MatchTerm(fauna.Index("users_by_email"), email),
),
))
value, err := client.Query(expr)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(value)
}
}
I'm very new to faunadb, I get this error when running the above code snippet.
Response error 400. Errors: [params](invalid argument): Object expected, Ref provided.
This isn't the best to help debug, what would be more helpful is to return a FQL string with the error section highlighted in red.
Would this be a feature you can implement?
P.S.
Would appreciate any help with the above issue
auth::go run main.go
.\main.go:36:28: undefined: faunadb.CreateCollection
.\main.go:45:15: undefined: faunadb.Collection
.\main.go:58:15: undefined: faunadb.Collection
.\main.go:70:4: undefined: faunadb.Collection
.\main.go:87:6: undefined: faunadb.Collection
.\main.go:97:45: undefined: faunadb.RefCollection
.\main.go:97:61: undefined: faunadb.Collection
When I isolate the file, it successfully works without giving me any errors, but when I add it to a project, it results in an error. This is just a small section of the problem. I found mostt others when I was using it resulted in some weird errors.
I have tried my best, but I just can't seem to solve it, please help me.
for the v2+
version, the go modules need that the module path to sync with the major version.
https://github.com/golang/go/wiki/Modules#releasing-modules-v2-or-higher
module github.com/yourusername/project
go 1.12
require (
github.com/fauna/faunadb-go v2.12.0
)
Run go mod tidy
require github.com/fauna/faunadb-go: version "v2.12.0" invalid: module contains a go.mod file, so major version must be compatible: should be v0 or v1, not v2
module github.com/yourusername/project
go 1.13
require (
github.com/fauna/faunadb-go v2.12.0
)
To be able to use the faunadb client, we are forced to use hash (pseudo-version) instead a real version
module github.com/yourusername/project
go 1.13
require (
github.com/fauna/faunadb-go v1.0.1-0.20200506155252-ce35719ad41e
)
Change the module name and update the package inside this repo.
module github.com/fauna/faunadb-go/v2
go 1.11
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
)
package faunadb_test
import (
"testing"
"time"
f "github.com/fauna/faunadb-go/faunadb/v2"
"github.com/stretchr/testify/suite"
)
This example from the tutorial isn't valid Go syntax:
func main() {
res, err := client.Query(f.Concat(["Hello", "World"], " "))
if err != nil {
panic(err)
}
fmt.Println(res)
}
I changed it to this and it worked:
res, err := client.Query(f.Concat([]string{"Hello", "World"}))
if err != nil {
panic(err)
}
fmt.Println(res)
In the documentation for Create
I can see that it supports a TTL
parameter, which takes a Fauna Timestamp object: https://docs.fauna.com/fauna/current/api/fql/functions/create
In the documentation for Timestamps, it says the code to use for the Go library is faunadb.Time
but when I do so, I get an error saying that faunadb.Time is not a type
.
I can't find Time
in the code, but I have found some similar things, like TimeV. Would that be the type to use for timestamps? I see that it's basically just a wrapper around the Go stdlib time.Time
object.
i've just got this error following the example. i've created a server key for this code
https://docs.fauna.com/fauna/current/drivers/go
but i can't read or create data
Response error 401. Errors: : Unauthorized, details: []
always is the same error, i am using v4 and golang v1.16
Currently fauna-go sets values in the document even though they have a zero value.
running the following query will result in the following document:
type MyObject struct {
MyString string
MyInt int
}
myObj := MyObject{
MyString: "",
MyInt: 0,
}
res, err := s.fdb.Query(f.Create(f.Collection("my_collection"), f.Obj{"data": myObj}))
if err != nil {
return nil, err
}
document:
{
"ref": Ref(Collection("my_collection"), "286888125244899841"),
"ts": 1609856686740000,
"data": {
"MyString": "",
"MyInt": 0
}
}
Fauna-db should implement functionality to discard zero valued fields. I.e. string values of "", int values of 0, float values of 0 should be discared.
I think this should be implemented via field struct tags, either via a opt-in to use serialize the zero values, or via a opt out. This is how serialization/deserialization of zero values in handling in other db drivers and in JSON.
For example the JSON field tags have the option to specify omitempty, which discards zero valued fields from serialization.
type MyObject struct {
MyString string `json:",omitempty"`
MyInt int `json:",omitempty"`
}
Conversely pg-go defaults to discarded zero values, but allows you to specify use_zero
type MyObject struct {
MyString string `pg:",use_zero"`
MyInt int `pg:",use_zero"`
}
I would propose that this is implemented using the omitempty option in the struct field tags. E.g:
type MyObject struct {
MyString string `fauna:",omitempty"`
MyInt int `fauna:",omitempty"`
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.