Skip to content

Commit 6912b46

Browse files
committed
feat(predicate): add ChildPredicate type and Children method for composite predicates
1 parent f1358f6 commit 6912b46

4 files changed

Lines changed: 113 additions & 1 deletion

File tree

‎generator/templates/client.gotpl‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,17 @@ type PredicateData struct {
327327
Children []PredicateData
328328
}
329329

330+
type ChildPredicate struct {
331+
Column string
332+
Value any
333+
}
334+
330335
type PredicateOf[M any] interface {
331336
ToPredicateData() PredicateData
332337
Validate() error
333338
Column() string
334339
Value() any
340+
Children() []ChildPredicate
335341
phantom(M)
336342
}
337343

@@ -355,6 +361,17 @@ func (p Predicate[M]) Value() any {
355361
return p.Data.Value
356362
}
357363

364+
func (p Predicate[M]) Children() []ChildPredicate {
365+
if !p.Data.IsLogical || len(p.Data.Children) == 0 {
366+
return nil
367+
}
368+
res := make([]ChildPredicate, len(p.Data.Children))
369+
for i, c := range p.Data.Children {
370+
res[i] = ChildPredicate{Column: c.Column, Value: c.Value}
371+
}
372+
return res
373+
}
374+
358375
func (p Predicate[M]) phantom(M) {}
359376

360377
type UniquePredicate[M any] struct {
@@ -380,6 +397,17 @@ func (p UniquePredicate[M]) Value() any {
380397
return p.Data.Value
381398
}
382399

400+
func (p UniquePredicate[M]) Children() []ChildPredicate {
401+
if !p.Data.IsLogical || len(p.Data.Children) == 0 {
402+
return nil
403+
}
404+
res := make([]ChildPredicate, len(p.Data.Children))
405+
for i, c := range p.Data.Children {
406+
res[i] = ChildPredicate{Column: c.Column, Value: c.Value}
407+
}
408+
return res
409+
}
410+
383411
func (p UniquePredicate[M]) phantom(M) {}
384412

385413
func validateValue(col string, val any) error {

‎integration/composite_test.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,47 @@ func TestCompositeKeys(t *testing.T) {
213213
}
214214
})
215215

216+
t.Run("hook_inspects_composite_Children", func(t *testing.T) {
217+
db, cleanup := setupTestDB(t)
218+
defer cleanup()
219+
220+
var children []phi.ChildPredicate
221+
222+
db.User.Use(user.Extension{
223+
FindUnique: func(ctx context.Context, args *user.FindUniqueArgs, next user.FindUniqueQuery) (*phi.User, error) {
224+
if len(args.Where) > 0 {
225+
children = args.Where[0].Children()
226+
}
227+
return next(ctx, args)
228+
},
229+
})
230+
231+
_, err := db.User.Create().
232+
SetEmail("hook-children@example.com").
233+
SetPhoneNum("composite-005b").
234+
Exec(ctx)
235+
if err != nil {
236+
t.Fatalf("create failed: %v", err)
237+
}
238+
239+
_, err = db.User.FindUnique(
240+
user.EmailPhone.EQ("hook-children@example.com", "composite-005b"),
241+
).Exec(ctx)
242+
if err != nil {
243+
t.Fatalf("find unique failed: %v", err)
244+
}
245+
246+
if len(children) != 2 {
247+
t.Fatalf("expected 2 children, got %d", len(children))
248+
}
249+
if children[0].Column != "email" || children[0].Value != "hook-children@example.com" {
250+
t.Errorf("unexpected child 0: %+v", children[0])
251+
}
252+
if children[1].Column != "phoneNum" || children[1].Value != "composite-005b" {
253+
t.Errorf("unexpected child 1: %+v", children[1])
254+
}
255+
})
256+
216257
t.Run("hook_replaces_composite_predicate", func(t *testing.T) {
217258
db, cleanup := setupTestDB(t)
218259
defer cleanup()

‎integration/main.go‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ func main() {
7272
// runExtensionExamples(db, ctx)
7373
// runCTP(db, ctx)
7474
//
75-
7675
usr, err := db.User.Create().SetEmail("xx@yy.com").SetPhoneNum("11").Exec(ctx)
7776
if err != nil {
7877
panic(err)
@@ -84,6 +83,22 @@ func main() {
8483
panic(err)
8584
}
8685
printJSON(posts)
86+
87+
db.User.Use(user.Extension{
88+
FindUnique: func(ctx context.Context, args *phi.UserFindUniqueArgs, next phi.UserFindUniqueQuery) (*phi.User, error) {
89+
for _, w := range args.Where {
90+
switch w.Column() {
91+
case user.Email.Column:
92+
// Scalar unique predicate
93+
case user.EmailPhone.Column:
94+
for _, child := range w.Children() {
95+
fmt.Printf("Composite field: %s = %v\n", child.Column, child.Value)
96+
}
97+
}
98+
}
99+
return next(ctx, args)
100+
},
101+
})
87102
}
88103

89104
// =============================================================================

‎integration/phi/client.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,11 +1293,17 @@ type PredicateData struct {
12931293
Children []PredicateData
12941294
}
12951295

1296+
type ChildPredicate struct {
1297+
Column string
1298+
Value any
1299+
}
1300+
12961301
type PredicateOf[M any] interface {
12971302
ToPredicateData() PredicateData
12981303
Validate() error
12991304
Column() string
13001305
Value() any
1306+
Children() []ChildPredicate
13011307
phantom(M)
13021308
}
13031309

@@ -1321,6 +1327,17 @@ func (p Predicate[M]) Value() any {
13211327
return p.Data.Value
13221328
}
13231329

1330+
func (p Predicate[M]) Children() []ChildPredicate {
1331+
if !p.Data.IsLogical || len(p.Data.Children) == 0 {
1332+
return nil
1333+
}
1334+
res := make([]ChildPredicate, len(p.Data.Children))
1335+
for i, c := range p.Data.Children {
1336+
res[i] = ChildPredicate{Column: c.Column, Value: c.Value}
1337+
}
1338+
return res
1339+
}
1340+
13241341
func (p Predicate[M]) phantom(M) {}
13251342

13261343
type UniquePredicate[M any] struct {
@@ -1346,6 +1363,17 @@ func (p UniquePredicate[M]) Value() any {
13461363
return p.Data.Value
13471364
}
13481365

1366+
func (p UniquePredicate[M]) Children() []ChildPredicate {
1367+
if !p.Data.IsLogical || len(p.Data.Children) == 0 {
1368+
return nil
1369+
}
1370+
res := make([]ChildPredicate, len(p.Data.Children))
1371+
for i, c := range p.Data.Children {
1372+
res[i] = ChildPredicate{Column: c.Column, Value: c.Value}
1373+
}
1374+
return res
1375+
}
1376+
13491377
func (p UniquePredicate[M]) phantom(M) {}
13501378

13511379
func validateValue(col string, val any) error {

0 commit comments

Comments
 (0)