Beispiel #1
0
// Save - Save generic record in couchDB.
func (this CouchDb) Save(record interface{}) bool {
	if record == nil {
		return false
	}
	if reflect.TypeOf(record).String() == "string" {
		record = entity.Json(record.(string)).ToObject() //convert Json to Map object
	}
	id, rev, err := this.Conn.Insert(record)
	if err == nil && id != "" && rev != "" {
		return true
	}
	return false
}
Beispiel #2
0
// Update - Update record in mongoDB.
func (this MongoDb) Update(record interface{}) bool {
	if record == nil {
		return false
	}
	if reflect.TypeOf(record).String() == "string" {
		record = entity.Json(record.(string)).ToObject()
		record.(entity.Map)["_id"] = bson.ObjectIdHex(record.(entity.Map)["_id"].(string))
	}
	err := this.Conn.UpdateId(record.(entity.Map)["_id"], record)
	if err == nil {
		return true
	}
	return false
}
Beispiel #3
0
// Delete - Delete generic record in couchDB.
func (this CouchDb) Delete(record interface{}) bool {
	if record == nil {
		return false
	}
	if reflect.TypeOf(record).String() == "string" {
		record = entity.Json(record.(string)).ToObject()
	}
	rev, err := this.Conn.Retrieve(fmt.Sprintf("%s", record.(entity.Map)["_id"]), &record)
	if err == nil && rev != "" {
		err = this.Conn.Delete(fmt.Sprintf("%s", record.(map[string]interface{})["_id"]), rev)
		if err == nil {
			return true
		}
	}
	return false
}
Beispiel #4
0
// Delete - Delete generic record in mongoDB.
func (this MongoDb) Delete(record interface{}) bool {
	if record == nil {
		return false
	}
	var err error
	if reflect.TypeOf(record).String() == "string" {
		record = entity.Json(record.(string)).ToObject()
		err = this.Conn.Remove(bson.M{"_id": bson.ObjectIdHex(record.(entity.Map)["_id"].(string))})
	} else {
		err = this.Conn.Remove(bson.M{"_id": record.(entity.Map)["_id"]})
	}
	if err == nil {
		return true
	}
	return false
}
Beispiel #5
0
// Update - Update record in couchDB.
func (this CouchDb) Update(record interface{}) bool {
	if record == nil {
		return false
	}
	if reflect.TypeOf(record).String() == "string" {
		record = entity.Json(record.(string)).ToObject()
	}
	var r map[string]interface{}
	rev, err := this.Conn.Retrieve(fmt.Sprintf("%s", record.(entity.Map)["_id"]), &r)
	if err == nil {
		rev, err = this.Conn.EditWith(record, fmt.Sprintf("%s", record.(entity.Map)["_id"]), rev)
		if err == nil {
			return true
		}
	} else {
		panic(err)
	}
	return false
}
Beispiel #6
0
// NewJson - Create and return a new object of type Json.
// Ex. jsn := NewJson(`{"name":"abc","age":24}`)
// or
// jsn := NewJson()
// jsn = `{"name":"abc","age":24}`
func NewJson(s ...string) entity.Json {
	if len(s) == 0 {
		return entity.Json("")
	}
	return entity.Json(s[0])
}