Skip to content

Instantly share code, notes, and snippets.

@CaryBourgeois
Last active August 17, 2023 11:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaryBourgeois/4d5e7eab118dd17703a2b8cc5096e0fa to your computer and use it in GitHub Desktop.
Save CaryBourgeois/4d5e7eab118dd17703a2b8cc5096e0fa to your computer and use it in GitHub Desktop.
Learn-Fauna-Go Part 3
/*
* Copyright 2018 Fauna, Inc.
*
* Licensed under the Mozilla Public License, Version 2.0 (the "License"); you may
* not use this software except in compliance with the License. You may obtain a
* copy of the License at
*
* http://mozilla.org/MPL/2.0/
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package main
import (
f "github.com/fauna/faunadb-go/faunadb"
"log"
)
var (
secret = "Your-Secret-Here"
endpoint = f.Endpoint("https://db.fauna.com")
adminClient = f.NewFaunaClient(secret, endpoint)
dbName = "learn-fauna-go"
)
/*
* Check for the existence of the database. If it exists return true.
* If database does not exist create it.
*/
func createDatabase() {
res, err := adminClient.Query(
f.If(
f.Exists(f.Database(dbName)),
true,
f.CreateDatabase(f.Obj{"name": dbName})))
if err != nil {
panic(err)
}
if res != f.BooleanV(true) {
log.Printf("Created Database: %s\n %s", dbName, res)
} else {
log.Printf("Database: %s, Already Exists\n %s", dbName, res)
}
}
/*
* Create a Database specific client that we can use to create objects within
* the target database. In this case we will give the client the role of "server"
* which will allow us create/delete/read/write access to all objects in the database.
*/
func getDbClient() (dbClient *f.FaunaClient) {
var res f.Value
var err error
var secret string
res, err = adminClient.Query(
f.CreateKey(f.Obj{
"database": f.Database(dbName),
"role": "server"}))
if err != nil {
panic(err)
}
err = res.At(f.ObjKey("secret")).Get(&secret)
if err != nil {
panic(err)
}
log.Printf("Database: %s, specifc key: %s\n%s", dbName, secret, res)
dbClient = adminClient.NewSessionClient(secret)
return
}
/*
* Check for the existence of the class. If it exists return true.
* If class does not exist create it.
*/
func createClass( dbClient *f.FaunaClient, className string) {
res, err := dbClient.Query(
f.If(
f.Exists(f.Class(className)),
true,
f.CreateClass(f.Obj{"name": className})))
if err != nil {
panic(err)
}
if res != f.BooleanV(true) {
log.Printf("Created Class: %s\n %s", className, res)
} else {
log.Printf("Class: %s, Already Exists\n %s", className, res)
}
}
/*
* Create a new instance of a class with "id" and "name"
*/
func createInstance(dbClient *f.FaunaClient, className string, id int, name string) {
var res f.Value
var err error
var ref f.RefV
res, err = dbClient.Query(
f.Create(f.Class(className), f.Obj{"data": f.Obj{"id": id, "name": name}}))
if err != nil {
panic(err)
}
if err = res.At(f.ObjKey("ref")).Get(&ref); err == nil {
log.Printf("Created '%s': %v \n%s", className, id, ref)
} else {
panic(err)
}
res, err = dbClient.Query(f.Select(f.Arr{"data", "name"}, f.Get(ref)))
if err != nil {
panic(err)
}
log.Printf("Read '%s': %v \n%s", className, id, res)
}
func main() {
createDatabase()
dbClient := getDbClient()
className := "Customers"
createClass(dbClient, className)
createInstance(dbClient, className, 1, "Adam Smith")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment