Install database pipelines to your templates. Perform Mongo database actions with Golang templates.
Add this import
tag within your .gxml
file.
<import src="github.com/cheikhshift/db/gos.gxml"/>
#Test it Make sure MongoDb is running!
go get github.com/cheikhshift/gopher-sauce-database
$GOPATH/bin/gopher-sauce-database
-
Connect to database. Add this within your
main
tag in the root of your.gxml
file.defer dbs.Close() dbs,_ = db.Connect("localhost", "dbtest")
-
Declare a struct within your
header
tag in yourGoS
file. This struct will be namedSample
. It is a must to have theId
field.<struct name="Sample"> Id bson.ObjectId `bson:"_id,omitempty"` TestField string `valid:"unique"` FieldTwo string `valid:"email,unique,required"` Created time.Time //timestamp local format Updated time.Time </struct>
-
Create a new file named
index.tmpl
in yourtmpl
directory. Contents of file :<!DOCTYPE html> <html lang="en"> <head> <title>DB Example</title> </head> <body> <!-- pipelines will go here --> </body> </html>
-
Add these following lines in
<!-- pipelines will go here -->
: -
to create a new
Sample
within yourindex.tmpl
. This will auto-populateId
andCreated
.{{ $obj := New Sample }}
-
Update values of
$obj
to prevent validation errors{{with $obj }} {{ .|u "FieldTwo" "email@gmail.com" }} {{end}}
-
Save
$obj
to database :{{ $err := $obj | Update }}
-
Check if error occurred and display :
{{ if $err | neq "" }} <h1>Error {{$err}}</h1> {{end}}
-
Add these following lines in
<!-- pipelines will go here -->
: -
Load a new query map :
{{ $query := search }}
-
Set query keys :
{{with $query }} {{ . | k "fieldtwo" "email@gmail.com" }} {{end}}
-
Perform query and display one item :
{{ with $query | Q Sample}} {{ . | One }} {{ end }}
-
Perform a query and attempt update. The pipeline
isSample
will convert the JSON database object to aSample
struct reader.{{ with $query | Q Sample}} {{ range . | All }} {{ . | k "fieldtwo" "newmail" }} <!-- Cast similar to with $obj --> {{ with . | isSample }} FieldTwo : {{ .FieldTwo }} <!-- will display invalid email --> {{ . | Update }} {{end }} {{ end }} {{ end }}
Returns Struct with Id
and Created
initialized.
- param :
Struct
name without strings or any braces. For example a struct declared in your.gxml
file namedSample
will be initialized with pipeline{{ $obj := New Sample }}
If an error occurs while updating your object this pipeline will return a string error as to why.
- $param : Struct to save to database.
If an error occurs while updating your object this pipeline will return a string error as to why.
- $param : Struct to remove from database. Requires a valid ObjectId in field
Id
to work.
Update a field value within your struct.
- $param : Struct to update.
- $param1 : Struct field name to update.
- $param2 : New value to set to specified struct field.
Returns a new query map (= json ).
Set a key within your query map. This is the equivalent of updating a map value within Go
.
- $search : Query map.
- $param1 : String of key to set.
- $param2 : Value to set.
Remove key from query map.
- $search : Query map.
- $param1 : String of key to remove.
Return value of specified query map key.
- $search : Query map.
- $param1 : String of key to retrieve.
Return new *mgo.Query
.
- $search : Query map.
- $param1 :
Struct
name without strings or any braces. For example a struct declared in your.gxml
file namedSample
will be initialized with pipeline{{ $search | Q Sample }}
Limit query to specified number.
- $param : *mgo.Query to limit.
- $param1 : int value to limit query by.
Skip query by specified number.
- $param : *mgo.Query to limit.
- $param1 : int value to skip query by.
Sort query by specified field. Any struct field must be in lower case.
- $param : *mgo.Query to limit.
- $param1 : string of field and sort syntax. For example
{{ Sort $query "-firstname" }}
Return int length of query.
- $param : *mgo.Query to count.
Return slice
of maps of your query.
- $param : *mgo.Query to count.
Return one result of your query as a map.
- $param : *mgo.Query to find one of.
Use the following syntax to convert a map to a struct for item update or removal. Only structs declared within your project's gxml
files are available to your templates
{{ $map | is<GoS Struct name> }}