Пример #1
0
// Copy to.
//
// @param  dest   string
// @param  destRev string
// @param  args... bool
// @return map[string]interface{}, error
// @panics
func (this *Document) CopyTo(dest, destRev string, args ...bool) (map[string]interface{}, error) {
	id, rev := this.GetId(), this.GetRev()
	if id == "" || rev == "" {
		panic("Both _id & _rev fields could not be empty!")
	}
	if dest == "" || destRev == "" {
		panic("Destination & destination revision could not be empty!")
	}

	query, headers := util.Map(), util.Map()
	headers["If-Match"] = rev
	headers["Destination"] = util.StringFormat("%s?rev=%s", dest, destRev)

	if args != nil {
		if args[0] == true {
			query["batch"] = "ok"
		}
		if args[1] == true {
			headers["X-Couch-Full-Commit"] = "true"
		}
	}

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

	return map[string]interface{}{
		"ok":  util.DigBool("ok", data),
		"id":  util.DigString("id", data),
		"rev": util.DigString("rev", data),
	}, nil
}
Пример #2
0
// Remove.
//
// @param  args... bool
// @return map[string]interface{}, error
// @panics
func (this *Document) Remove(args ...bool) (map[string]interface{}, error) {
	id, rev := this.GetId(), this.GetRev()
	if id == "" || rev == "" {
		panic("Both _id & _rev fields could not be empty!")
	}

	query, headers := util.Map(), util.Map()
	headers["If-Match"] = rev

	if args != nil {
		if args[0] == true {
			query["batch"] = "ok"
		}
		if args[1] == true {
			headers["X-Couch-Full-Commit"] = "true"
		}
	}

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

	return map[string]interface{}{
		"ok":  util.DigBool("ok", data),
		"id":  util.DigString("id", data),
		"rev": util.DigString("rev", data),
	}, nil
}
Пример #3
0
// Copy.
//
// @param  dest string
// @param  args... bool
// @return map[string]interface{}, error
// @panics
func (this *Document) Copy(dest string, args ...bool) (map[string]interface{}, error) {
	id := this.GetId()
	if id == "" {
		panic("_id field could not be empty!")
	}
	if dest == "" {
		panic("Destination could not be empty!")
	}

	query, headers := util.Map(), util.Map()
	headers["Destination"] = dest

	if args != nil {
		if args[0] == true {
			query["batch"] = "ok"
		}
		if args[1] == true {
			headers["X-Couch-Full-Commit"] = "true"
		}
	}
	data, err := this.Database.Client.Copy(this.Database.Name+"/"+util.UrlEncode(id), query, headers).
		GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"ok":  util.DigBool("ok", data),
		"id":  util.DigString("id", data),
		"rev": util.DigString("rev", data),
	}, nil
}
Пример #4
0
// View cleanup.
//
// @return bool, error
func (this *Database) ViewCleanup() (bool, error) {
	data, err := this.Client.Post(this.Name+"/_view_cleanup", nil, nil, nil).GetBodyData(nil)
	if err != nil {
		return false, err
	}

	return util.DigBool("ok", data), nil
}
Пример #5
0
// Set revision limit.
//
// @param  limit int
// @return bool, error
func (this *Database) SetRevisionLimit(limit int) (bool, error) {
	data, err := this.Client.Put(this.Name+"/_revs_limit", nil, limit, nil).GetBodyData(limit)
	if err != nil {
		return false, err
	}

	return util.DigBool("ok", data), nil
}
Пример #6
0
// Compact.
//
// @param  ddoc string
// @return bool, error
func (this *Database) Compact(ddoc string) (bool, error) {
	data, err := this.Client.Post(this.Name+"/_compact/"+ddoc, nil, nil, nil).GetBodyData(nil)
	if err != nil {
		return false, err
	}

	return util.DigBool("ok", data), nil
}
Пример #7
0
// Ensure full commit.
//
// @return bool, uint, error
func (this *Database) EnsureFullCommit() (bool, uint, error) {
	data, err := this.Client.Post(this.Name+"/_ensure_full_commit", nil, nil, nil).GetBodyData(nil)
	if err != nil {
		return false, 0, err
	}

	return util.DigBool("ok", data),
		util.DigUInt("instance_start_time", data),
		nil
}
Пример #8
0
// Save.
//
// @param  args... bool
// @return map[string]interface{}, error
func (this *Document) Save(args ...bool) (map[string]interface{}, error) {
	query, headers, body := util.Map(), util.Map(), this.GetData()
	if args != nil {
		if args[0] == true {
			query["batch"] = "ok"
		}
		if args[1] == true {
			headers["X-Couch-Full-Commit"] = "true"
		}
	}

	if this.Rev != "" {
		headers["If-Match"] = this.Rev
	}

	if this.Attachments != nil {
		body["_attachments"] = util.Map()
		for name, attachment := range this.Attachments {
			body["_attachments"].(map[string]interface{})[name] = attachment.ToArray(true)
		}
	}

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

		id, rev := util.DigString("id", data), util.DigString("rev", data)
		// set id & rev for next save() instant calls
		if id != "" && this.Id == nil {
			this.SetId(id)
		}
		if rev != "" {
			this.SetRev(rev)
		}

		return map[string]interface{}{
			"ok":  util.DigBool("ok", data),
			"id":  id,
			"rev": rev,
		}, nil
	}

	if this.Id == nil {
		return _func( // insert action
			this.Database.Client.Post(this.Database.Name, query, body, headers).
				GetBodyData(nil))
	} else {
		return _func( // update action
			this.Database.Client.Put(this.Database.Name+"/"+this.GetId(), query, body, headers).
				GetBodyData(nil))
	}
}
Пример #9
0
// Get database updates.
//
// @param  query map[string]interface{}
// @return map[string]interface{}, error
func (this *Server) GetDatabaseUpdates(query interface{}) (map[string]interface{}, error) {
	data, err := this.Client.Get("/_db_updates", query, nil).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"ok":      util.DigBool("ok", data),
		"type":    util.DigString("type", data),
		"db_name": util.DigString("db_name", data),
	}, nil
}
Пример #10
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
}
Пример #11
0
// Remove.
//
// @return map[string]interface{}, error
// @panics
func (this *DocumentAttachment) Remove(args ...bool) (map[string]interface{}, error) {
	if this.Document == nil {
		panic("Attachment document is not defined!")
	}

	docId := this.Document.GetId()
	docRev := this.Document.GetRev()
	if docId == "" {
		panic("Attachment document _id is required!")
	}
	if docRev == "" {
		panic("Attachment document _rev is required!")
	}
	if this.FileName == "" {
		panic("Attachment file name is required!")
	}

	query, headers := util.Map(), util.Map()
	if args != nil {
		if args[0] {
			query["batch"] = "ok"
		}
		if args[1] {
			headers["X-Couch-Full-Commit"] = "true"
		}
	}
	headers["If-Match"] = docRev

	data, err := this.Document.Database.Client.Delete(util.StringFormat(
		"%s/%s/%s", this.Document.Database.Name, docId, util.UrlEncode(this.FileName),
	), nil, headers,
	).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"ok":  util.DigBool("ok", data),
		"id":  util.DigString("id", data),
		"rev": util.DigString("rev", data),
	}, nil
}
Пример #12
0
// Save.
//
// @return map[string]interface{}, error
// @panics
func (this *DocumentAttachment) Save() (map[string]interface{}, error) {
	if this.Document == nil {
		panic("Attachment document is not defined!")
	}

	docId := this.Document.GetId()
	docRev := this.Document.GetRev()
	if docId == "" {
		panic("Attachment document _id is required!")
	}
	if docRev == "" {
		panic("Attachment document _rev is required!")
	}
	if this.FileName == "" {
		panic("Attachment file name is required!")
	}

	// read file contents
	this.ReadFile(false)

	headers := util.Map()
	headers["If-Match"] = docRev
	headers["Content-Type"] = this.ContentType

	data, err := this.Document.Database.Client.Put(util.StringFormat(
		"%s/%s/%s", this.Document.Database.Name, docId, util.UrlEncode(this.FileName),
	), nil, this.Data, headers,
	).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"ok":  util.DigBool("ok", data),
		"id":  util.DigString("id", data),
		"rev": util.DigString("rev", data),
	}, nil
}