Created
August 8, 2018 15:08
-
-
Save CaryBourgeois/8e5712566a2aa547abf1104a2c9e06b5 to your computer and use it in GitHub Desktop.
Learn-Fauna-Go Part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 = "You're 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 dbSecret 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(&dbSecret) | |
if err != nil { | |
panic(err) | |
} | |
log.Printf("Database: %s, specific key: %s\n%s", dbName, dbSecret, res) | |
dbClient = adminClient.NewSessionClient(dbSecret) | |
return | |
} | |
/* | |
* Create the a class object within our database | |
*/ | |
func createClass(className string, dbClient *f.FaunaClient) { | |
res, err := dbClient.Query( | |
f.CreateClass(f.Obj{"name": className})) | |
if err != nil { | |
panic(err) | |
} | |
log.Printf("Created Class: %s\n%s", className, res) | |
} | |
func main() { | |
createDatabase() | |
dbClient := getDbClient() | |
createClass("SampleClass", dbClient) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment