Skip to main content

User Family Query Documentation

This code provides functions for querying and manipulating user family data in the database. It uses the PocketBase DAO for database interactions and includes functions for inserting, updating, and synchronizing user family records.

Functions

InsertUserFamily

func InsertUserFamily(dao *daos.Dao, userProfileID string, family *entity.UserProfileTable) (*models.Record, error)

Inserts a user relationship into the database.

  • dao: The PocketBase DAO.
  • userProfileID: The ID of the user profile.
  • family: A pointer to a UserProfileTable struct containing the family member's data.
  • Returns: A pointer to a models.Record containing the inserted record, or an error if the operation fails.

UpdateUserFamily

func UpdateUserFamily(dao *daos.Dao, req *entity.UserProfileTable) (*models.Record, error)

Updates an existing user family record in the database.

  • dao: The PocketBase DAO.
  • req: A pointer to a UserProfileTable struct containing the updated family member's data.
  • Returns: A pointer to a models.Record containing the updated record, or an error if the operation fails.

SyncUserFamilies

func SyncUserFamilies(dao *daos.Dao, userProfileID string, req []*entity.UserProfileTable) error

Synchronizes user family records for a specific user profile by deleting existing records and inserting new ones.

  • dao: The PocketBase DAO.
  • userProfileID: The ID of the user profile.
  • req: A slice of pointers to UserProfileTable structs containing the new family member data.
  • Returns: An error if the operation fails.

updateRelationFamily

func updateRelationFamily(dao *daos.Dao, familyId, relation string) error

Updates the relation field in the user_family table for a given family profile ID.

  • dao: The PocketBase DAO.
  • familyId: The ID of the family profile.
  • relation: The new relation type.
  • Returns: An error if the operation fails.

Example Usage

Initializing the DAO

Before using the functions, you need to initialize the PocketBase DAO.

package main

import (
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
)

func main() {
// Initialize PocketBase
pb := pocketbase.New()

// Initialize the DAO
dao := daos.New(pb.DB())
}

Inserting a User Family

package main

import (
"fio-backend/entity"
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"fmt"
)

func main() {
// Initialize PocketBase
pb := pocketbase.New()

// Initialize the DAO
dao := daos.New(pb.DB())

// Create a new family member profile
newFamilyMember := &entity.UserProfileTable{
FullName: "Jane Doe",
Gender: entity.FEMALE,
BirthDate: "1990-01-01",
RelationAs: entity.SPOUSE,
}

// Insert the new family member
userProfileID := "user123"
record, err := queries.InsertUserFamily(dao, userProfileID, newFamilyMember)
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("Inserted Family Member Record:", record)
}

Updating a User Family

package main

import (
"fio-backend/entity"
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"fmt"
)

func main() {
// Initialize PocketBase
pb := pocketbase.New()

// Initialize the DAO
dao := daos.New(pb.DB())

// Retrieve the family member profile to update
familyMemberID := "family123"
familyMember, err := queries.GetViewUserProfileByID(dao, familyMemberID)
if err != nil {
fmt.Println("Error:", err)
return
}

// Update the family member profile fields
familyMember.FullName = "Jane Doe Updated"
familyMember.RelationAs = entity.MOTHER

// Update the family member profile in the database
updatedRecord, err := queries.UpdateUserFamily(dao, familyMember)
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("Updated Family Member Record:", updatedRecord)
}

Synchronizing User Families

package main

import (
"fio-backend/entity"
"fio-backend/queries"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/daos"
"fmt"
)

func main() {
// Initialize PocketBase
pb := pocketbase.New()

// Initialize the DAO
dao := daos.New(pb.DB())

// Create new family member data
newFamilyMembers := []*entity.UserProfileTable{
{
FullName: "Jane Doe",
Gender: entity.FEMALE,
BirthDate: "1990-01-01",
RelationAs: entity.SPOUSE,
},
{
FullName: "John Doe Jr.",
Gender: entity.MALE,
BirthDate: "2015-01-01",
RelationAs: entity.CHILD,
},
}

// Synchronize user families for a user profile
userProfileID := "user123"
err := queries.SyncUserFamilies(dao, userProfileID, newFamilyMembers)
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("User families synchronized successfully")
}

This documentation provides an overview of the user family query functions, their parameters, and example usage. By following the examples, you can insert, update, and synchronize user family records in the database.