-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ tasks: | |
|
||
new_post: | ||
cmds: | ||
- script/add {{.CLI_ARGS}} | ||
- scripts/add "{{.CLI_ARGS}}" | ||
|
||
new_talk: | ||
cmds: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] | ||
--- |
4 changes: 2 additions & 2 deletions
4
...ent/posts/2023-03-03-til-how-to-run-parallel-jobs-on-gitlab-ci/images/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...-03-08--til-how-to-set-a-relationship-on-golang-pocketbase-/images/bookmark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...-08--til-how-to-set-a-relationship-on-golang-pocketbase-/images/bookmark_id.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...il-how-to-set-a-relationship-on-golang-pocketbase-/images/bookmark_metadata.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...023-03-08--til-how-to-set-a-relationship-on-golang-pocketbase-/images/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions
54
.../posts/2023-03-08--til-how-to-set-a-relationship-on-golang-pocketbase-/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters