2

Hello I am using pgx to use my postgres, and I have doubts as to how I can transform a row in the database into an aggregate

I am using entities and value objects without value object it seems easy using marshal, but using value object I think it is not a good idea to have fields exported and then my question comes in, how can I convert my line into a struct of my aggregate

my aggregrate :

type Email struct {
    address string
}

type Password struct {
    value string
}

type Name struct {
    firstName string
    lastName  string
}

type Person struct {
    Id       string
    Name     valueObject.Name
    Email    valueObject.Email
    Password valueObject.Password
    Created  time.Time
    Updated  time.Time
}

func NewPerson(name valueObject.Name, email valueObject.Email, password valueObject.Password) *Person {
    id := uuid.New()
    return &Person{
        Id:       id.String(),
        Name:     name,
        Email:    email,
        Password: password,
        Created:  time.Now(),
        Updated:  time.Now(),
    }
}

all my value objects have a method to get the private value through a function simulating a get, I didn’t put the rest of the code of my value objects so it wouldn’t get big

func to get all rows from table:

func (r *personRepository) GetAll() (persons []*entities.Person, err error) {
    qry := `select id, first_name, last_name, email, password created_at, updated_at from persons`
    rows, err := r.conn.Query(context.Background(), qry)

    return nil, fmt.Errorf("err")
}

If someone can give me a glimpse of how I can pass this line from the bank to a struct of my aggregate using this value object

1 Answer 1

1

You can use something like this (not yet tested and need optimization):

func (r *personRepository) GetAll() (persons []*entities.Person, err error) {
    qry := `select id, first_name, last_name, email, password, created_at, updated_at from persons`
    rows, err := r.conn.Query(context.Background(), qry)

  var items []*entities.Person
  if err != nil {
    // No result found with the query.
    if err == pgx.ErrNoRows {
        return items, nil
    }
    
    // Error happened
    log.Printf("can't get list person: %v\n", err)
    return items, err
  }

  defer rows.Close()

  for rows.Next() {
    // Build item Person for earch row.
    // must be the same with the query column position.

    var id, firstName, lastName, email, password string
    var createdAt, updatedAt time.Time
    
    err = rows.Scan(&id, &firstName, &lastName, &email,
                    &createdAt, updatedAt)
    if err != nil {
        log.Printf("Failed to build item: %v\n", err)
        return items, err
    }

    item := &entities.Person{
      Id: id,
      FirstName: firstName,
      // fill other value
    }

    // Add item to the list.
    items = append(items, item)
  }

  return items, nil
}

Don't forget to add the comma after text password in your query.


I am using entities and value objects without value object it seems easy using marshal,

Sorry, I don't know about the value object in your question.

Sign up to request clarification or add additional context in comments.

3 Comments

I can only create a valuable object, like password, name, by a like constructor NewPassword (p string), NewEmail (and string) why the fields of my structs of my value objects are not exported
You need to uppercase the first character of your fields to make it exportable.
I understand, but that way I would lose the sense of using valuables, I was trying to see if I had an alternative to making the marshal with non-exported fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.