Skip to content

Commit

Permalink
til: pocketbase relations
Browse files Browse the repository at this point in the history
Fixes: #77
  • Loading branch information
hmajid2301 committed Mar 2, 2023
1 parent f5adc72 commit 906b7be
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tasks:

new_post:
cmds:
- script/add {{.CLI_ARGS}}
- scripts/add "{{.CLI_ARGS}}"

new_talk:
cmds:
Expand Down
2 changes: 1 addition & 1 deletion archetypes/post-bundle/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: {{ slicestr (replace .Name "-" " ") 11 | title }}
date: {{ dateFormat "2006-01-02" .Date }}
canonicalUrl: https://haseebmajid.dev/posts/{{.Name}}
canonicalURL: https://haseebmajid.dev/posts/{{.Name}}
tags: []
---
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
title: "TIL: How to Set a Relationship on Golang Pocketbase"
date: 2023-03-08
canonicalURL: https://haseebmajid.dev/posts/2023-03-08--til-how-to-set-a-relationship-on-golang-pocketbase-
tags:
- golang
- pocketbase
series:
- TIL
---

**TIL: How to Set a Relationship on Golang Pocketbase**

[Pocketbase](https://pocketbase.io/) is a backend as a service, it is very nice because it is written in Golang
it can compile down to a single binary. It also allows us to extend it using Golang and use it as a framework.
In my case, I needed some extra logic before adding data to the database so I decided to extend Pocketbase
and write some of my business logic.

Part of the code involved adding some entries to the Database including a relationship. However, I noticed that the relationship
looked different in the Pocketbase GUI as compared with the ones I created using the Pocketbase client in the SvelteKit part of my app.

The other relationships showed up a bit differently, they didn't include the id of the relationship (as a foreign key) but rather a field
on the other collection. In the example below the relationship field I am referencing is the `bookmark_metadata`. Which uses the `title` field.

![Bookmark](images/bookmark.png)

Where the bookmark metadata may look like:

![Bookmark Metadata](images/bookmark_metadata.png)

We can achieve this using the following code:

```golang {hl_lines=[2]}
bookmarkRecord := models.NewRecord(collection)
bookmarkRecord.Set("bookmark_metadata", []string{metadataRecord.Id})
bookmarkRecord.Set("favourite", false)
bookmarkRecord.Set("collection", collectionID)
bookmarkRecord.Set("custom_order", math.MaxInt32)
```

The key being that we set `bookmark_metadata` as a slice (array).
However, if we did:

```golang {hl_lines=[2]}
bookmarkRecord.Set("bookmark_metadata", metadataRecord.Id)
```

In the GUI the record would look like this:

![Bookmark ID](images/bookmark_id.png)

Both options will work for setting up a relationship between two collections. I just wanted to show you how you can set it
so it shows up like relationship created via the JS SDK. You can read more about creating records with Golang
[here](https://pocketbase.io/docs/record-methods/#create-new-record).
2 changes: 1 addition & 1 deletion scripts/add
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ TITLE_SLUG="$(echo -n "$TITLE" | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-
DATE="$(date +"%F")"
SLUG="$DATE-$TITLE_SLUG"

# git checkout -b "$SLUG"
git checkout -b "$SLUG"
hugo new --kind post-bundle posts/$SLUG

0 comments on commit 906b7be

Please sign in to comment.