示例#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
}
示例#2
0
// Get UUID.
//
// @param  body map[string]interface{}
// @return map[string]interface{}, error
// @panics
func (this *Server) Replicate(body map[string]interface{}) (map[string]interface{}, error) {
	body = util.Param(body)
	if body["source"] == nil || body["target"] == nil {
		panic("Both source & target required!")
	}

	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["history"] = util.MapList(value)
			for i, history := range value.([]interface{}) {
				ret["history"].([]map[string]interface{})[i] = util.Map()
				for kkey, vvalue := range history.(map[string]interface{}) {
					ret["history"].([]map[string]interface{})[i][kkey] = vvalue
				}
			}
			continue
		}
		ret[key] = value
	}

	return ret, nil
}
示例#3
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{}))
	}
}
示例#4
0
// Find as struct.
//
// @param  data interface{}
// @param  query map[string]interface{}
// @return interface{}, error
func (this *Document) FindStruct(
	data interface{}, query map[string]interface{}) (interface{}, error) {
	id := this.GetId()
	if id == "" {
		panic("_id field is could not be empty!")
	}
	if data == nil {
		panic("You should pass your data struct!")
	}

	query = util.Param(query)
	if query["rev"] == "" && this.Rev != "" {
		query["rev"] = this.Rev
	}

	data, err := this.Database.Client.Get(this.Database.Name+"/"+util.UrlEncode(id), query, nil).
		GetBodyData(data)
	if err != nil {
		return nil, err
	}

	return data, nil
}
示例#5
0
// Find.
//
// @param  query map[string]interface{}
// @return map[string]interface{}, error
// @panics
func (this *Document) Find(query map[string]interface{}) (map[string]interface{}, error) {
	id := this.GetId()
	if id == "" {
		panic("_id field is could not be empty!")
	}

	query = util.Param(query)
	if query["rev"] == "" && this.Rev != "" {
		query["rev"] = this.Rev
	}

	data, err := this.Database.Client.Get(this.Database.Name+"/"+util.UrlEncode(id), query, nil).
		GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	ret := util.Map()
	for key, value := range data.(map[string]interface{}) {
		ret[key] = value
	}

	return ret, nil
}