- Notifications
You must be signed in to change notification settings - Fork0
eduardolat/nocodbgo
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A Zero-Dependency Go client for theNocoDB API.This client provides a simple and intuitive way to interact with the NocoDB APIusing a fluent chain pattern.
Warning
This client is not yet stable and the API signature may change in the futureuntil it reaches version 1.0.0, so be careful when upgrading. However, the APIsignature should not change too much.
Go version 1.22 or higher is required.
go get github.com/eduardolat/nocodbgo
Most modern code editors will provide autocompletion to guide you through theavailable options as you type. This makes using the SDK intuitive and easy tolearn.
You can find examples of how to use the client in theexamplesdirectory.
You can also hover over the methods provided by the client to read more aboutthem.
package mainimport ("github.com/eduardolat/nocodbgo")funcmain() {// Create a new client using the chain patternclient,err:=nocodbgo.NewClient().WithBaseURL("https://example.com").WithAPIToken("your-api-token").Create()iferr!=nil {// Handle error}}
// Get a tabletable:=client.Table("your-table-id")// Create a record (can be a map[string]any or a struct with JSON tags)user:=map[string]any{"Name":"John Doe","Email":"john@example.com","Age":30,}// Create a recorduserID,err:=table.CreateRecord(user).Execute()// Read a recordreadResponse,err:=table.ReadRecord(userID).// Optional, if not provided a context.Background() will be usedWithContext(context.Background()).ReturnFields("Name","Email","Age").Execute()// Decode into a structtypeUserstruct {IDint`json:"Id"`Namestring`json:"Name"`Emailstring`json:"Email"`Ageint`json:"Age"`}varuserStructUsererr=readResponse.DecodeInto(&userStruct)// Update a recordupdateUser:=map[string]any{"Id":userID,// ID must be included"Name":"John Smith",}err=table.UpdateRecord(updateUser).Execute()// Delete a recorderr=table.DeleteRecord(userID).Execute()
// List records with options using the chain patternresult,err:=table.ListRecords().Where("(Age,gt,18)").SortAscBy("Name").Limit(10).Execute()// Decode the list into a structvarusers []Usererr=result.DecodeInto(&users)// Count recordscount,err:=table.CountRecords().Where("(Age,gt,18)").Execute()
// Query with complex filters using specific methodsresult,err:=table.ListRecords().WhereIsEqualTo("Name","John Smith").WhereIsGreaterThan("Age","18").WhereIsLessThan("Age","30").SortAscBy("Name").Limit(10).Execute()// Query with custom filtersresult,err:=table.ListRecords().Where("(Age,gt,20)~or(Email,like,%@example.com)").SortDescBy("Age").Limit(5).Execute()
// Create multiple recordsusers:= []map[string]any{ {"Name":"Jane Doe","Email":"jane@example.com","Age":25, }, {"Name":"Bob Smith","Email":"bob@example.com","Age":40, },}createdIDs,err:=table.CreateRecords(users).Execute()// Update multiple recordsupdateUsers:= []map[string]any{ {"Id":createdIDs[0],"Name":"Jane Smith", }, {"Id":createdIDs[1],"Name":"Robert Smith", },}err=table.UpdateRecords(updateUsers).Execute()// Delete multiple recordserr=table.DeleteRecords(createdIDs).Execute()
// List linked recordslinkedRecords,err:=table.ListLinks("link-field-id",recordID).ReturnFields("Name","Email").SortAscBy("Name").Limit(10).Where("(Age,gt,18)").Execute()// Decode the linked recordsvarlinkedUsers []Usererr=linkedRecords.DecodeInto(&linkedUsers)// Create a linkerr=table.CreateLink("link-field-id",recordID,targetID).Execute()// Create multiple linkserr=table.CreateLinks("link-field-id",recordID, []int{1,2,3}).Execute()// Delete a linkerr=table.DeleteLink("link-field-id",recordID,targetID).Execute()// Delete multiple linkserr=table.DeleteLinks("link-field-id",recordID, []int{1,2}).Execute()
// Use a specific viewresult,err:=table.ListRecords().WithViewId("view-id").Execute()// Shuffle results randomlyresult,err:=table.ListRecords().Shuffle().Limit(5).Execute()// Paginationresult,err:=table.ListRecords().Page(2,10).// Page 2, 10 records per pageExecute()
All operations support the use ofcontext.Context
for cancellation and timeoutcontrol:
ctx,cancel:=context.WithTimeout(context.Background(),5*time.Second)defercancel()result,err:=table.ListRecords().WithContext(ctx).Where("(Age,gt,18)").Execute()
If you don't provide a context, a context.Background() will be used.
This project is licensed under the MIT License - see theLICENSE filefor details.