Skip to content

Commit

Permalink
PR(CONST): Make a constant for "_id".
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Jun 15, 2023
1 parent ac2b59f commit 925eed4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
4 changes: 4 additions & 0 deletions client/request/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const (
// https://spec.graphql.org/October2021/#sec-Type-Name-Introspection
TypeNameFieldName = "__typename"

// This is appended to the related object name to give us the field name
// that corresponds to the related object's join relation id, i.e. `Author_id`.
RelatedObjectID = "_id"

Cid = "cid"
Data = "data"
DocKey = "dockey"
Expand Down
2 changes: 1 addition & 1 deletion client/request/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *Select) validateGroupBy() []error {
if typedChildSelection.Name == groupByField {
fieldExistsInGroupBy = true
break
} else if typedChildSelection.Name == groupByField+"_id" {
} else if typedChildSelection.Name == groupByField+RelatedObjectID {
isAliasFieldInGroupBy = true
break
}
Expand Down
6 changes: 4 additions & 2 deletions db/collection_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,9 @@ func (c *collection) isSecondaryIDField(fieldDesc client.FieldDescription) (clie
return client.FieldDescription{}, false
}

relationFieldDescription, valid := c.Description().GetField(strings.TrimSuffix(fieldDesc.Name, "_id"))
relationFieldDescription, valid := c.Description().GetField(
strings.TrimSuffix(fieldDesc.Name, request.RelatedObjectID),
)
return relationFieldDescription, valid && !relationFieldDescription.IsPrimaryRelation()
}

Expand Down Expand Up @@ -431,7 +433,7 @@ func (c *collection) patchPrimaryDoc(
_, err = primaryCol.UpdateWithKey(
ctx,
primaryDockey,
fmt.Sprintf(`{"%s": "%s"}`, primaryField.Name+"_id", docKey),
fmt.Sprintf(`{"%s": "%s"}`, primaryField.Name+request.RelatedObjectID, docKey),
)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions planner/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func toSelect(
if _, fieldHasMapping := mapping.IndexesByName[groupByField]; fieldHasMapping {
// Not an alias as is already mapped.
continue
} else if _, isAlias := mapping.IndexesByName[groupByField+"_id"]; isAlias {
} else if _, isAlias := mapping.IndexesByName[groupByField+request.RelatedObjectID]; isAlias {
// Remap the alias to it's' actual internal name.
groupByFields[index] = groupByField + "_id"
groupByFields[index] = groupByField + request.RelatedObjectID
} else {
// Field is not mapped nor is an alias, then is invalid field to group on. This can be
// incase of when an alias might have been used on groupBy relation from the single side.
Expand Down
6 changes: 3 additions & 3 deletions planner/type_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (n *typeJoinOne) Next() (bool, error) {

func (n *typeJoinOne) valuesSecondary(doc core.Doc) core.Doc {
fkIndex := &mapper.PropertyIndex{
Index: n.subType.DocumentMap().FirstIndexOfName(n.subTypeFieldName + "_id"),
Index: n.subType.DocumentMap().FirstIndexOfName(n.subTypeFieldName + request.RelatedObjectID),
}
filter := map[connor.FilterKey]any{
fkIndex: doc.GetKey(),
Expand Down Expand Up @@ -386,7 +386,7 @@ func (n *typeJoinOne) valuesSecondary(doc core.Doc) core.Doc {

func (n *typeJoinOne) valuesPrimary(doc core.Doc) core.Doc {
// get the subtype doc key
subDocKey := n.docMapper.documentMapping.FirstOfName(doc, n.subTypeName+"_id")
subDocKey := n.docMapper.documentMapping.FirstOfName(doc, n.subTypeName+request.RelatedObjectID)

subDocKeyStr, ok := subDocKey.(string)
if !ok {
Expand Down Expand Up @@ -546,7 +546,7 @@ func (n *typeJoinMany) Next() (bool, error) {
// @todo: handle index for one-to-many setup
} else {
fkIndex := &mapper.PropertyIndex{
Index: n.subSelect.FirstIndexOfName(n.rootName + "_id"),
Index: n.subSelect.FirstIndexOfName(n.rootName + request.RelatedObjectID),
}
filter := map[connor.FilterKey]any{
fkIndex: n.currentValue.GetKey(), // user_id: "bae-ALICE" | user_id: "bae-CHARLIE"
Expand Down

0 comments on commit 925eed4

Please sign in to comment.