Skip to content

crossworth/enthstore

Repository files navigation

Hstore type and predicates for Ent.

This package defines the Postgres hstore type to be used with ent and a few predicates.

Using the type:

func (User) Fields() []ent.Field {
	return []ent.Field{
		field.Other("attributes", enthstore.Hstore{}).
			SchemaType(enthstore.Hstore{}.SchemaType()).
			Default(func() enthstore.Hstore {
				return enthstore.Hstore{}
			}),
	}
}

Using the predicates:

users, err := client.User.Query().Where(func(selector *sql.Selector) {
    selector.Where(enthstore.HasKey(user.FieldAttributes, "a"))
}).All(context.Background)

List of predicates:

  • HasKey
  • HasAllKeys
  • ValueIsNull
  • ValueEQ
  • ValueNEQ
  • ValueGT
  • ValueGTE
  • ValueLT
  • ValueLTE
  • ValueContains
  • ValueHasPrefix
  • ValueHasSuffix

Using with GQLGen:

Define a custom scalar:

scalar Hstore

And declare the type mapping on gqlgen.yml:

models:
  Hstore:
    model: github.com/crossworth/enthstore.Hstore