-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
54 lines (49 loc) · 1.27 KB
/
schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package airtablesql
import (
"fmt"
"github.com/alvarowolfx/gamer-journal-wrapped/src/util"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"
"github.com/mehanizm/airtable"
)
const (
recordIDFieldName = "record_id"
)
func tableSchemaFromAirtable(tableSchema *airtable.TableSchema) sql.Schema {
tableName := util.ToSnakecase(tableSchema.Name)
schema := sql.Schema{
&sql.Column{
Name: recordIDFieldName,
Type: types.Text,
Nullable: false,
Source: tableName,
PrimaryKey: true,
},
}
for _, field := range tableSchema.Fields {
col := &sql.Column{
Name: util.ToSnakecase(field.Name),
Type: fromAirtableType(field.Type),
Comment: fmt.Sprintf("airtable type: %s; airtable field: %s", field.Type, field.Name),
Nullable: true,
Source: tableName,
PrimaryKey: field.ID == tableSchema.PrimaryFieldID,
}
schema = append(schema, col)
}
return schema
}
func fromAirtableType(ftype string) sql.Type {
switch ftype {
case "date":
return types.Date
case "autoNumber":
return types.Float64
case "singleSelect", "multilineText", "singleLineText":
return types.Text
case "multipleRecordLinks", "multipleLookupValues":
return types.JSON
default:
return types.Text
}
}