func (col *Collection) Delete(key string) error { var err error var res *nap.Response if key == "" { return errors.New("Key must not be empty") } if col.Type == 2 { res, err = col.db.get("document", col.Name+"/"+key, "DELETE", nil, nil, nil) } else { res, err = col.db.get("edge", col.Name+"/"+key, "DELETE", nil, nil, nil) } if err != nil { return err } switch res.Status() { case 202, 200: return nil default: return errors.New("Document don't exist or revision error") } }
func (col *Collection) Patch(key string, doc interface{}) error { var err error var res *nap.Response if key == "" { return errors.New("Key must not be empty") } if col.Type == 2 { res, err = col.db.send("document", col.Name+"/"+key, "PATCH", doc, &doc, &doc) } else { res, err = col.db.send("edge", col.Name+"/"+key+"?rev=", "PATCH", doc, &doc, &doc) } if err != nil { return err } switch res.Status() { case 201: return nil case 202: return nil case 400: return errors.New("Body does not contain a valid JSON representation of a document.") case 404: return errors.New("Collection or document was not found") default: return nil } }
// Replace document func (col *Collection) Replace(key string, doc interface{}) error { var err error var res *nap.Response if key == "" { return errors.New("Key must not be empty") } if col.Type == 2 { res, err = col.db.send("document", col.Name+"/"+key, "PUT", doc, &doc, &doc) } else { res, err = col.db.send("edge", col.Name+"/"+key, "PUT", doc, &doc, &doc) } if err != nil { return err } switch res.Status() { case 201: return nil case 202: return nil case 400: return errors.New("Invalid json") case 404: return errors.New("Collection or document was not found") default: return nil } }
// Save saves doc into collection, doc should have Document Embedded to retrieve error and Key later. func (col *Collection) Save(doc interface{}) error { var err error var res *nap.Response if col.Type == 2 { res, err = col.db.send("document?collection="+col.Name, "", "POST", doc, &doc, &doc) } else { return errors.New("Trying to save doc into EdgeCollection") } if err != nil { return err } switch res.Status() { case 201: return nil case 202: return nil case 400: return errors.New("Invalid document json") case 404: return errors.New("Collection does not exist") default: return nil } }
// Get list of collections from any database func Collections(db *Database) error { var err error var res *nap.Response // get all non-system collections res, err = db.get("collection?excludeSystem=true", "", "GET", nil, db, db) if err != nil { return err } if res.Status() == 200 { return nil } else { return errors.New("Failed to retrieve collections from Database") } }
// Save Edge into Edges collection func (col *Collection) SaveEdge(doc interface{}, from string, to string) error { var err error var res *nap.Response if col.Type == 3 { res, err = col.db.send("edge?collection="+col.Name+"&from="+from+"&to="+to, "", "POST", doc, &doc, &doc) } else { return errors.New("Trying to save document into Edge-Collection") } if err != nil { return err } if res.Status() != 201 && res.Status() != 202 { return errors.New("Unable to save document error") } return nil }