1
0
Fork 0
mirror of synced 2024-07-03 21:50:34 +12:00
appwrite/app/sdks/server-go/database.go

168 lines
5.5 KiB
Go
Raw Normal View History

2020-01-29 03:22:13 +13:00
package appwrite
import (
"strings"
)
// Database service
type Database struct {
2020-01-29 09:24:42 +13:00
client Client
2020-01-29 03:22:13 +13:00
}
2020-01-29 18:08:39 +13:00
func NewDatabase(clt Client) Database {
2020-01-29 12:14:30 +13:00
service := Database{
client: clt,
}
2020-01-29 10:01:07 +13:00
return service
}
2020-01-29 03:22:13 +13:00
// ListCollections get a list of all the user collections. You can use the
// query params to filter your results. On admin mode, this endpoint will
// return a list of all of the project collections. [Learn more about
// different API modes](/docs/admin).
func (srv *Database) ListCollections(Search string, Limit int, Offset int, OrderType string) (map[string]interface{}, error) {
2020-01-31 05:18:59 +13:00
path := "/database/collections"
2020-01-29 03:22:13 +13:00
params := map[string]interface{}{
"search": Search,
"limit": Limit,
"offset": Offset,
"orderType": OrderType,
}
2020-01-29 11:56:24 +13:00
return srv.client.Call("GET", path, nil, params)
2020-01-29 03:22:13 +13:00
}
// CreateCollection create a new Collection.
func (srv *Database) CreateCollection(Name string, Read []interface{}, Write []interface{}, Rules []interface{}) (map[string]interface{}, error) {
2020-01-31 05:18:59 +13:00
path := "/database/collections"
2020-01-29 03:22:13 +13:00
params := map[string]interface{}{
"name": Name,
"read": Read,
"write": Write,
"rules": Rules,
}
2020-01-29 11:56:24 +13:00
return srv.client.Call("POST", path, nil, params)
2020-01-29 03:22:13 +13:00
}
// GetCollection get collection by its unique ID. This endpoint response
// returns a JSON object with the collection metadata.
func (srv *Database) GetCollection(CollectionId string) (map[string]interface{}, error) {
r := strings.NewReplacer("{collectionId}", CollectionId)
2020-01-31 05:18:59 +13:00
path := r.Replace("/database/collections/{collectionId}")
2020-01-29 03:22:13 +13:00
params := map[string]interface{}{
}
2020-01-29 11:56:24 +13:00
return srv.client.Call("GET", path, nil, params)
2020-01-29 03:22:13 +13:00
}
// UpdateCollection update collection by its unique ID.
func (srv *Database) UpdateCollection(CollectionId string, Name string, Read []interface{}, Write []interface{}, Rules []interface{}) (map[string]interface{}, error) {
r := strings.NewReplacer("{collectionId}", CollectionId)
2020-01-31 05:18:59 +13:00
path := r.Replace("/database/collections/{collectionId}")
2020-01-29 03:22:13 +13:00
params := map[string]interface{}{
"name": Name,
"read": Read,
"write": Write,
"rules": Rules,
}
2020-01-29 11:56:24 +13:00
return srv.client.Call("PUT", path, nil, params)
2020-01-29 03:22:13 +13:00
}
// DeleteCollection delete a collection by its unique ID. Only users with
// write permissions have access to delete this resource.
func (srv *Database) DeleteCollection(CollectionId string) (map[string]interface{}, error) {
r := strings.NewReplacer("{collectionId}", CollectionId)
2020-01-31 05:18:59 +13:00
path := r.Replace("/database/collections/{collectionId}")
2020-01-29 03:22:13 +13:00
params := map[string]interface{}{
}
2020-01-29 11:56:24 +13:00
return srv.client.Call("DELETE", path, nil, params)
2020-01-29 03:22:13 +13:00
}
// ListDocuments get a list of all the user documents. You can use the query
// params to filter your results. On admin mode, this endpoint will return a
// list of all of the project documents. [Learn more about different API
// modes](/docs/admin).
func (srv *Database) ListDocuments(CollectionId string, Filters []interface{}, Offset int, Limit int, OrderField string, OrderType string, OrderCast string, Search string, First int, Last int) (map[string]interface{}, error) {
r := strings.NewReplacer("{collectionId}", CollectionId)
2020-01-31 05:18:59 +13:00
path := r.Replace("/database/collections/{collectionId}/documents")
2020-01-29 03:22:13 +13:00
params := map[string]interface{}{
"filters": Filters,
"offset": Offset,
"limit": Limit,
"order-field": OrderField,
"order-type": OrderType,
"order-cast": OrderCast,
"search": Search,
"first": First,
"last": Last,
}
2020-01-29 11:56:24 +13:00
return srv.client.Call("GET", path, nil, params)
2020-01-29 03:22:13 +13:00
}
// CreateDocument create a new Document.
2020-02-14 19:28:54 +13:00
func (srv *Database) CreateDocument(CollectionId string, Data object, Read []interface{}, Write []interface{}, ParentDocument string, ParentProperty string, ParentPropertyType string) (map[string]interface{}, error) {
2020-01-29 03:22:13 +13:00
r := strings.NewReplacer("{collectionId}", CollectionId)
2020-01-31 05:18:59 +13:00
path := r.Replace("/database/collections/{collectionId}/documents")
2020-01-29 03:22:13 +13:00
params := map[string]interface{}{
"data": Data,
"read": Read,
"write": Write,
"parentDocument": ParentDocument,
"parentProperty": ParentProperty,
"parentPropertyType": ParentPropertyType,
}
2020-01-29 11:56:24 +13:00
return srv.client.Call("POST", path, nil, params)
2020-01-29 03:22:13 +13:00
}
// GetDocument get document by its unique ID. This endpoint response returns a
// JSON object with the document data.
func (srv *Database) GetDocument(CollectionId string, DocumentId string) (map[string]interface{}, error) {
r := strings.NewReplacer("{collectionId}", CollectionId, "{documentId}", DocumentId)
2020-01-31 05:18:59 +13:00
path := r.Replace("/database/collections/{collectionId}/documents/{documentId}")
2020-01-29 03:22:13 +13:00
params := map[string]interface{}{
}
2020-01-29 11:56:24 +13:00
return srv.client.Call("GET", path, nil, params)
2020-01-29 03:22:13 +13:00
}
// UpdateDocument
2020-02-14 19:28:54 +13:00
func (srv *Database) UpdateDocument(CollectionId string, DocumentId string, Data object, Read []interface{}, Write []interface{}) (map[string]interface{}, error) {
2020-01-29 03:22:13 +13:00
r := strings.NewReplacer("{collectionId}", CollectionId, "{documentId}", DocumentId)
2020-01-31 05:18:59 +13:00
path := r.Replace("/database/collections/{collectionId}/documents/{documentId}")
2020-01-29 03:22:13 +13:00
params := map[string]interface{}{
"data": Data,
"read": Read,
"write": Write,
}
2020-01-29 11:56:24 +13:00
return srv.client.Call("PATCH", path, nil, params)
2020-01-29 03:22:13 +13:00
}
// DeleteDocument delete document by its unique ID. This endpoint deletes only
// the parent documents, his attributes and relations to other documents.
// Child documents **will not** be deleted.
func (srv *Database) DeleteDocument(CollectionId string, DocumentId string) (map[string]interface{}, error) {
r := strings.NewReplacer("{collectionId}", CollectionId, "{documentId}", DocumentId)
2020-01-31 05:18:59 +13:00
path := r.Replace("/database/collections/{collectionId}/documents/{documentId}")
2020-01-29 03:22:13 +13:00
params := map[string]interface{}{
}
2020-01-29 11:56:24 +13:00
return srv.client.Call("DELETE", path, nil, params)
2020-01-29 03:22:13 +13:00
}