Files
airtable/table.go
2024-06-22 21:46:58 -07:00

60 lines
1.3 KiB
Go

package airtable
import (
"context"
"fmt"
)
type Table struct {
ID string `json:"id"`
PrimaryFieldID string `json:"primaryFieldId"`
Name string `json:"name"`
Description string `json:"description"`
Fields []*Field `json:"fields"`
Views []*View `json:"views"`
c *Client
b *Base
}
type Field struct {
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Options map[string]any `json:"options"`
}
type View struct {
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
}
func (b *Base) ListTables(ctx context.Context) ([]*Table, error) {
return listAll[Table](ctx, b.c, fmt.Sprintf("meta/bases/%s/tables", b.ID), nil, "tables", func(t *Table) error {
t.c = b.c
t.b = b
return nil
})
}
func (b *Base) GetTableByName(ctx context.Context, name string) (*Table, error) {
tables, err := b.ListTables(ctx)
if err != nil {
return nil, err
}
for _, table := range tables {
if table.Name == name {
return table, nil
}
}
return nil, fmt.Errorf("table '%s' not found", name)
}
func (t Table) String() string {
return fmt.Sprintf("%s.%s", t.b, t.Name)
}