-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblueprints.go
More file actions
62 lines (56 loc) · 1.68 KB
/
blueprints.go
File metadata and controls
62 lines (56 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package sqlc
import (
"context"
"sync/atomic"
"github.com/mhiro2/seedling"
)
// idSeq simulates auto-increment IDs that would normally come from the database.
var idSeq atomic.Int64
func nextID() int64 {
return idSeq.Add(1)
}
// RegisterBlueprints registers Organization and Member blueprints in reg.
//
// In a real project, the Insert functions would call sqlc-generated query methods:
//
// Insert: func(ctx context.Context, db seedling.DBTX, v Organization) (Organization, error) {
// return queries.InsertOrganization(ctx, db.(*sql.DB), v)
// }
//
// Here we use mock inserts that assign incrementing IDs.
func RegisterBlueprints(reg *seedling.Registry) {
seedling.MustRegisterTo(reg, seedling.Blueprint[Organization]{
Name: "organization",
Table: "organizations",
PKField: "ID",
Defaults: func() Organization {
return Organization{Name: "test-org"}
},
Insert: func(ctx context.Context, db seedling.DBTX, v Organization) (Organization, error) {
// In production: return queries.InsertOrganization(ctx, db.(*sql.DB), v)
v.ID = nextID()
return v, nil
},
})
seedling.MustRegisterTo(reg, seedling.Blueprint[Member]{
Name: "member",
Table: "members",
PKField: "ID",
Defaults: func() Member {
return Member{Name: "test-member", Email: "member@example.com"}
},
Relations: []seedling.Relation{
{
Name: "organization",
Kind: seedling.BelongsTo,
LocalField: "OrganizationID",
RefBlueprint: "organization",
},
},
Insert: func(ctx context.Context, db seedling.DBTX, v Member) (Member, error) {
// In production: return queries.InsertMember(ctx, db.(*sql.DB), v)
v.ID = nextID()
return v, nil
},
})
}