MCPcopy Create free account
hub / github.com/efectn/go-orm-benchmarks / Only

Method Only

bench/ent/model_query.go:108–121  ·  view source on GitHub ↗

Only returns a single Model entity found by the query, ensuring it only returns one. Returns a *NotSingularError when more than one Model entity is found. Returns a *NotFoundError when no Model entities are found.

(ctx context.Context)

Source from the content-addressed store, hash-verified

106// Returns a *NotSingularError when more than one Model entity is found.
107// Returns a *NotFoundError when no Model entities are found.
108func (mq *ModelQuery) Only(ctx context.Context) (*Model, error) {
109 nodes, err := mq.Limit(2).All(setContextOp(ctx, mq.ctx, "Only"))
110 if err != nil {
111 return nil, err
112 }
113 switch len(nodes) {
114 case 1:
115 return nodes[0], nil
116 case 0:
117 return nil, &NotFoundError{model.Label}
118 default:
119 return nil, &NotSingularError{model.Label}
120 }
121}
122
123// OnlyX is like Only, but panics if an error occurs.
124func (mq *ModelQuery) OnlyX(ctx context.Context) *Model {

Callers 2

OnlyXMethod · 0.95
GetMethod · 0.80

Calls 3

LimitMethod · 0.95
setContextOpFunction · 0.85
AllMethod · 0.45

Tested by

no test coverage detected