Beispiel #1
0
// Get changes.
//
// @param  query map[string]interface{}
// @return []map[string]interface{}, error
func (this *Database) GetChanges(
	query map[string]interface{}, docIds []string) (map[string]interface{}, error) {
	query = util.Param(query)
	if docIds != nil {
		query["filter"] = "_doc_ids"
	}

	body := util.ParamList("doc_ids", docIds)
	data, err := this.Client.Post(this.Name+"/_changes", query, body, nil).
		GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	ret := util.Map()
	ret["last_seq"] = util.Dig("last_seq", data)
	ret["results"] = util.MapList(0) // set empty as default
	if results := data.(map[string]interface{})["results"].([]interface{}); results != nil {
		ret["results"] = util.MapList(results) // @overwrite
		for i, result := range results {
			ret["results"].([]map[string]interface{})[i] = map[string]interface{}{
				"id":      util.Dig("id", result),
				"seq":     util.Dig("seq", result),
				"deleted": util.Dig("deleted", result),
				"changes": util.Dig("changes", result),
			}
		}
	}

	return ret, nil
}
Beispiel #2
0
// View temp.
//
// @param  _map string
// @param  _red interface{}
// @return map[string]interface{}, error
func (this *Database) ViewTemp(_map string, _red interface{}) (map[string]interface{}, error) {
	body := util.ParamList(
		"map", _map,
		"reduce", util.IsEmptySet(_red, nil), // prevent "missing function" error
	)

	// short?
	type ddl DatabaseDocumentList

	data, err := this.Client.Post(this.Name+"/_temp_view", nil, body, nil).GetBodyData(&ddl{})
	if err != nil {
		return nil, err
	}

	ret := util.Map()
	ret["offset"] = data.(*ddl).Offset
	ret["total_rows"] = data.(*ddl).TotalRows

	rows := data.(*ddl).Rows
	ret["rows"] = util.MapList(len(rows))

	// append docs
	for i, row := range rows {
		ret["rows"].([]map[string]interface{})[i] = map[string]interface{}{
			"id":    row.Id,
			"key":   row.Key,
			"value": row.Value,
		}
	}

	return ret, nil
}
Beispiel #3
0
// Update documents.
//
// @param  document []map[string]interface{}
// @return []map[string]interface{}, error
// @panics
func (this *Database) UpdateDocumentAll(
	documents []interface{}) ([]map[string]interface{}, error) {
	docs := util.MapList(documents)
	for i, doc := range documents {
		if docs[i] == nil {
			docs[i] = util.Map()
		}
		for key, value := range doc.(map[string]interface{}) {
			docs[i][key] = value
		}
		// these are required params
		if docs[i]["_id"] == nil || docs[i]["_rev"] == nil {
			panic("Both _id & _rev fields are required!")
		}
	}

	body := util.ParamList("docs", docs)
	data, err := this.Client.Post(this.Name+"/_bulk_docs", nil, body, nil).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	ret := util.MapList(data)
	for i, doc := range data.([]interface{}) {
		if ret[i] == nil {
			ret[i] = util.Map()
		}

		for key, value := range doc.(map[string]interface{}) {
			ret[i][key] = value
		}
	}

	return ret, nil
}
Beispiel #4
0
// Get document.
//
// @param  key string
// @return map[string]interface{}, error
func (this *Database) GetDocument(key string) (map[string]interface{}, error) {
	// prepare query
	query := util.ParamList(
		"include_docs", true,
		"key", util.Quote(key),
	)

	data, err := this.Client.Get(this.Name+"/_all_docs", query, nil).
		GetBodyData(&DatabaseDocumentList{})
	if err != nil {
		return nil, err
	}

	ret := util.Map()

	for _, doc := range data.(*DatabaseDocumentList).Rows {
		ret["id"] = doc.Id
		ret["key"] = doc.Key
		ret["value"] = map[string]string{"rev": doc.Value["rev"].(string)}
		ret["doc"] = map[string]interface{}{}
		// fill doc field
		for key, value := range doc.Doc {
			ret["doc"].(map[string]interface{})[key] = value
		}
	}

	return ret, nil
}
Beispiel #5
0
// Create database.
//
// @param  target      string
// @param  targetCreate bool
// @return bool
func (this *Database) Replicate(
	target string, targetCreate bool) (map[string]interface{}, error) {
	// prepare body
	body := util.ParamList(
		"source", this.Name,
		"target", target,
		"create_target", targetCreate,
	)

	data, err := this.Client.Post("/_replicate", nil, body, nil).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	ret := util.Map()
	for key, value := range data.(map[string]interface{}) {
		// grap, set & pass history field
		if key == "history" {
			ret[key] = util.MapList(value)
			for i, history := range value.([]interface{}) {
				ret[key].([]map[string]interface{})[i] = util.Map()
				for kkey, vvalue := range history.(map[string]interface{}) {
					ret[key].([]map[string]interface{})[i][kkey] = vvalue
				}
			}
			continue
		}
		ret[key] = value
	}

	return ret, nil
}
Beispiel #6
0
// Setter.
//
// @param  data... interface{}
// @return *couch.Document
// @panics
func (this *Document) Set(data ...interface{}) *Document {
	if data == nil {
		panic("Provide at least a key=>value match as param!")
	}
	this.SetData(util.ParamList(data...))

	return this
}
Beispiel #7
0
// Constructor.
//
// @param  database *couch.Database
// @param  data... interface{}
// @return *couch.Document
func NewDocument(database *Database, data ...interface{}) *Document {
	this := &Document{
		Data:     util.Map(),
		Database: database,
	}

	if data != nil {
		this.SetData(util.ParamList(data...))
	}

	return this
}
Beispiel #8
0
// Find revisions.
//
// @return map[string]interface{}, error
func (this *Document) FindRevisions() (map[string]interface{}, error) {
	data, err := this.Find(util.ParamList("revs", true))
	if err != nil {
		return nil, err
	}

	ret := util.Map()
	if data["_revisions"] != nil {
		ret["start"] = util.DigInt("_revisions.start", data)
		ret["ids"] = util.DigSliceString("_revisions.ids", data)
	}

	return ret, nil
}
Beispiel #9
0
// Set security.
//
// @param  map[string]interface{}
// @param  map[string]interface{}
// @return bool, error
// @panics
func (this *Database) SetSecurity(admins, members map[string]interface{}) (bool, error) {
	// check required fields
	if admins["names"].([]string) == nil || admins["roles"].([]string) == nil ||
		members["names"].([]string) == nil || members["roles"].([]string) == nil {
		panic("Specify admins and/or members with names=>roles fields!")
	}

	body := util.ParamList("admins", admins, "members", members)
	data, err := this.Client.Put(this.Name+"/_security", nil, body, nil).GetBodyData(nil)
	if err != nil {
		return false, err
	}

	return util.DigBool("ok", data), nil
}
Beispiel #10
0
// Get UUIDs.
//
// @return []string, error
func (this *Server) GetUuids(count int) ([]string, error) {
	query := util.ParamList(
		"count", count,
	)
	data, err := this.Client.Get("/_uuids", query, nil).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	ret := util.MapSliceString(count)
	for i, uuid := range data.(map[string]interface{})["uuids"].([]interface{}) {
		ret[i] = uuid.(string)
	}

	return ret, nil
}
Beispiel #11
0
// Get documents.
//
// @param  query map[string]interface{}
// @param  keys  []string
// @return map[string]interface{}, error
func (this *Database) GetDocumentAll(
	query map[string]interface{}, keys []string) (map[string]interface{}, error) {
	query = util.Param(query)
	if query["include_docs"] == nil {
		query["include_docs"] = true
	}

	// short?
	type ddl DatabaseDocumentList

	// make a reusable lambda
	_func := func(data interface{}, err error) (map[string]interface{}, error) {
		if err != nil {
			return nil, err
		}

		ret := util.Map()
		ret["offset"] = data.(*ddl).Offset
		ret["total_rows"] = data.(*ddl).TotalRows

		rows := data.(*ddl).Rows
		ret["rows"] = util.MapList(len(rows))

		// append docs
		for i, row := range rows {
			ret["rows"].([]map[string]interface{})[i] = map[string]interface{}{
				"id":    row.Id,
				"key":   row.Key,
				"value": map[string]string{"rev": row.Value["rev"].(string)},
				"doc":   row.Doc,
			}
		}

		return ret, nil
	}

	if keys == nil {
		return _func( // get all
			this.Client.Get(this.Name+"/_all_docs", query, nil).GetBodyData(&ddl{}))
	} else {
		body := util.ParamList("keys", keys)
		return _func( // get all only matched keys
			this.Client.Post(this.Name+"/_all_docs", query, body, nil).GetBodyData(&ddl{}))
	}
}
Beispiel #12
0
// Find revisions extended.
//
// @return []map[string]interface{}, error
func (this *Document) FindRevisionsExtended() ([]map[string]string, error) {
	data, err := this.Find(util.ParamList("revs_info", true))
	if err != nil {
		return nil, err
	}

	ret := util.MapListString(nil)
	if data["_revs_info"] != nil {
		ret = util.MapListString(data["_revs_info"]) // @overwrite
		for i, info := range data["_revs_info"].([]interface{}) {
			ret[i] = map[string]string{
				"rev":    util.DigString("rev", info),
				"status": util.DigString("status", info),
			}
		}
	}

	return ret, nil
}
Beispiel #13
0
// Create documents.
//
// @param  document []map[string]interface{}
// @return []map[string]interface{}, error
func (this *Database) CreateDocumentAll(
	documents []interface{}) ([]map[string]interface{}, error) {
	docs := util.MapList(documents)
	for i, doc := range documents {
		if docs[i] == nil {
			docs[i] = util.Map()
		}
		// filter documents
		for key, value := range doc.(map[string]interface{}) {
			// this is create method, no update allowed
			if key == "_rev" || key == "_deleted" {
				continue
			}
			docs[i][key] = value
		}
	}

	body := util.ParamList("docs", docs)
	data, err := this.Client.Post(this.Name+"/_bulk_docs", nil, body, nil).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	ret := util.MapList(data)
	for i, doc := range data.([]interface{}) {
		if ret[i] == nil {
			ret[i] = util.Map()
		}

		for key, value := range doc.(map[string]interface{}) {
			ret[i][key] = value
		}
	}

	return ret, nil
}