Go Cookbook
Getting Started
To get started with the Go driver, visit the
Github Page or Package Page to view the source code, tests and basic usage examples.
You can install the driver via the command line:
go get github.com/gitana/cloudcms-go-driver
Connecting to Cloud CMS
There are two ways to connect with the Go driver: By finding a gitana.json
file in your working directory, or by providing a config configuration.
// Connect to CloudCMS using gitana.json in working directory
session, err := cloudcms.ConnectDefault()
session, err := cloudcms.Connect(&cloudcms.CloudcmsConfig{
Client_id: "clientId",
Client_secret: "clientSecret",
Username: "username",
Password: "password",
BaseURL: "baseURL",
})
Code Samples
Here are some code samples to help you get started. In these scenarios, we assume you're using the most simple connection method (providing the path to gitana.json
).
In Cloud CMS, you can have as many repositories as you'd like. And each repository can multiple branches (similar to Git).
The first thing you should do is connect and get the branch that you want to work on.
For the purpose of most of these examples, we'll assume the repository ID is 1234567890abcdef1234
and the branch
is master
.
Node creation
{
"title": "Custom Article",
"_qname": "custom:article",
"type": "object",
"properties": {
"title": {
"type": "string"
},
"body": {
"type": "string"
},
"categories": {
"type": "array",
"items": {
"type": "string"
}
},
"rating": {
"type": "number"
}
}
}
Create a content instance of this type like this:
session, _ := cloudcms.ConnectDefault()
repositoryId := "1234567890abcdef1234"
branchId := "master"
nodeObj := cloudcms.JsonObject{
"_type": "custom:article",
"title": "Article #1",
"body": "A still more glorious dawn awaits",
"categories": []string{
"category1",
"category2",
"category3",
},
"rating": 5,
}
node1Id, _ := session.CreateNode(repositoryId, branchId, nodeObj, nil)
Create a Folder
In Cloud CMS, folders are just nodes with the f:container
feature on them. It must be associated to the root node to be considered part of file folder tree.
session, _ := cloudcms.ConnectDefault()
repositoryId := "1234567890abcdef1234"
branchId := "master"
nodeObj := cloudcms.JsonObject{
"title": "My Folder",
}
optsObj := cloudcms.JsonObject{
"parentPath": "/",
}
node := session.CreateNode(repositoryId, branchId, nodeObj, optsObj)
session.AddNodeFeature(repositoryId, branchId, nodeId, "f:container", JsonObject{})
Associations
In Cloud CMS, any two nodes can be connected via an association. You can use an out-of-the-box association type, such
as a:linked
or a:owned
or you can create your own.
Let's imagine that we have two articles that look like this:
- Article #1
{
"_doc": "1234567890abcdef1111",
"_type": "custom:article",
"title": "Article #1",
"body": "a mote of dust suspended in a sunbeam",
"categories": ["category1", "category2"],
"rating": 1
}
- Article #2
{
"_doc": "1234567890abcdef2222",
"_doc": "",
"_type": "custom:article",
"title": "Article #2",
"body": "harvesting star light",
"categories": ["category2", "category3"],
"rating": 2
}
Let's create an association that points from the first article (1234567890abcdef1111
) to the second article
(1234567890abcdef2222
), like this:
session, _ := cloudcms.ConnectDefault()
repositoryId := "1234567890abcdef1234"
branchId := "master"
article1Id := "1234567890abcdef1111"
article2Id := "1234567890abcdef2222"
association, _ := session.Associate(repositoryId, branchId, article1Id, article2Id, "a:linked", "OUTGOING", nil)
You can also find all associations around article1
. This will include associations that are INCOMING
, OUTGOING
and MUTUAL
.
associations, err := session.ListNodeAssociations(repositoryId, branchId, node1Id, "", "", nil)
Or find only the INCOMING
associations:
associations, err := session.ListIncomingAssociations(repositoryId, branchId, node1Id, "", nil)
Or find only the OUTGOING
associations of type a:linked:
associations, err := session.ListOutgoingAssociations(repositoryId, branchId, node1Id, "a:linked", nil)
Query
Let's now look at querying for content. This makes use of MongoDB's query language to express some pretty powerful
queries. We recommend further reading on Query within Cloud CMS.
Let's assume that we have the following:
- Article #1
{
"_type": "custom:article",
"title": "Article #1",
"body": "a mote of dust suspended in a sunbeam",
"categories": ["category1", "category2"],
"rating": 1
}
- Article #2
{
"_type": "custom:article",
"title": "Article #2",
"body": "harvesting star light",
"categories": ["category2", "category3"],
"rating": 2
}
- Article 3
{
"_type": "custom:article",
"title": "Article #3",
"body": "we are star stuff",
"categories": ["category3", "category1"],
"rating": 3
}
Basic Querying
Find all of the nodes that have a category
with the value category1
.
session, _ := cloudcms.ConnectDefault()
repositoryId := "1234567890abcdef1234"
branchId := "master"
query := cloudcms.JsonObject{
"_type": "custom:article",
"category": "category1",
}
nodes, _ := session.QueryNodes(repositoryId, branchId, query, nil)
Pagination
If you have a lot of potential query hits, you'll want to paginate. Here is an example where we return results starting
at index 2 and hand back at most 5 results.
session, _ := cloudcms.ConnectDefault()
repositoryId := "1234567890abcdef1234"
branchId := "master"
query := cloudcms.JsonObject{
"_type": "custom:article",
"category": "category1",
}
pagination := cloudcms.JsonObject{
"skip": 2,
"limit": 5,
}
nodes, _ := session.QueryNodes(repositoryId, branchId, query, pagination)
Sorting
Use the pagination option to sort the results as you see fit. Here we sort descending on rating
.
session, _ := cloudcms.ConnectDefault()
repositoryId := "1234567890abcdef1234"
branchId := "master"
query := cloudcms.JsonObject{
"_type": "custom:article",
"category": "category1",
}
pagination := cloudcms.JsonObject{
"sort": cloudcms.JsonObject{
"rating": -1
}
}
nodes, _ := session.QueryNodes(repositoryId, branchId, query, pagination)
Query for values in a range
Find all of the articles where the rating is greater than or equal to 2.
This demonstrates the power of MongoDB's query language.
session, _ := cloudcms.ConnectDefault()
repositoryId := "1234567890abcdef1234"
branchId := "master"
query := cloudcms.JsonObject{
"_type": "custom:article",
"rating": cloudcms.JsonObject{
"$gte": 2,
},
}
nodes, _ := session.QueryNodes(repositoryId, branchId, query, nil)
Search for Nodes
Let's now look at searching for content. This makes use of Elastic Search's DSL to express some very powerful
full text and structured searches. We recommend further reading on Search within Cloud CMS.
We also suggest reading up on Query Strings to understand how you can write searches textually (in addition to structuring them as JSON objects):
Let's assume that we have the following:
- Article #1
{
"_type": "custom:article",
"title": "Article #1",
"body": "a mote of dust suspended in a sunbeam",
"categories": ["category1", "category2"],
"rating": 1
}
- Article #2
{
"_type": "custom:article",
"title": "Article #2",
"body": "harvesting star light",
"categories": ["category2", "category3"],
"rating": 2
}
- Article 3
{
"_type": "custom:article",
"title": "Article #3",
"body": "we are star stuff",
"categories": ["category3", "category1"],
"rating": 3
}
Full Text Search
Find all of the nodes with the word star
. Simple enough!
session, _ := cloudcms.ConnectDefault()
repositoryId := "1234567890abcdef1234"
branchId := "master"
searchNodes, _ := session.SearchNodes(repositoryId, branchId, "star", nil)
GraphQL
You can make queries for specific fields or content in related items by using GraphQL. Here we do a simple query for just the title and body of our article type:
session, _ := cloudcms.ConnectDefault()
repositoryId := "1234567890abcdef1234"
branchId := "master"
query := `
query{
n_nodes {
title
}
}`
result, _ := session.GraphQLQuery(repositoryId, branchId, query, "", nil)