|
| 1 | +// Code generated by goctl. DO NOT EDIT. |
| 2 | +// goctl {{.version}} |
| 3 | + |
| 4 | +package model |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/eddieowens/opts" |
| 11 | + "github.com/jzero-io/jzero/core/stores/monx" |
| 12 | + "github.com/zeromicro/go-zero/core/stores/mon" |
| 13 | + "github.com/zeromicro/go-zero/core/stores/monc" |
| 14 | + "go.mongodb.org/mongo-driver/v2/bson" |
| 15 | + "go.mongodb.org/mongo-driver/v2/mongo" |
| 16 | + "go.mongodb.org/mongo-driver/v2/mongo/options" |
| 17 | +) |
| 18 | + |
| 19 | +{{if .Cache}}var prefix{{.Type}}CacheKey = "{{if .Prefix}}{{.Prefix}}:{{end}}cache:{{.lowerType}}:"{{end}} |
| 20 | + |
| 21 | +type {{.lowerType}}Model interface{ |
| 22 | + Insert(ctx context.Context,data *{{.Type}}, opts ...options.Lister[options.InsertOneOptions]) error |
| 23 | + FindOne(ctx context.Context,id string) (*{{.Type}}, error) |
| 24 | + Update(ctx context.Context,data *{{.Type}}) (*mongo.UpdateResult, error) |
| 25 | + Delete(ctx context.Context,id string) (int64, error) |
| 26 | + |
| 27 | + // custom interface generated by jzero |
| 28 | + BulkInsert(ctx context.Context, data []*{{.Type}}, opts ...options.Lister[options.InsertManyOptions]) error |
| 29 | + FindOneByCondition(ctx context.Context, condition any, opts ...options.Lister[options.FindOneOptions]) (*{{.Type}}, error) |
| 30 | + FindByCondition(ctx context.Context, condition any, opts ...options.Lister[options.FindOptions]) ([]*{{.Type}}, error) |
| 31 | + DeleteByCondition(ctx context.Context, condition any, opts ...options.Lister[options.DeleteManyOptions]) (int64, error) |
| 32 | +} |
| 33 | + |
| 34 | +type default{{.Type}}Model struct { |
| 35 | + conn *mon.Model |
| 36 | + cachedConn *monc.Model |
| 37 | +} |
| 38 | + |
| 39 | +func newDefault{{.Type}}Model(url, db string, op ...opts.Opt[monx.MonOpts]) *default{{.Type}}Model { |
| 40 | + o := opts.DefaultApply(op...) |
| 41 | + |
| 42 | + var ( |
| 43 | + cachedConn *monc.Model |
| 44 | + ) |
| 45 | + |
| 46 | + conn := mon.MustNewModel(url, db, {{.Type}}CollectionName) |
| 47 | + if len(o.CacheConf) > 0 { |
| 48 | + cachedConn = monc.MustNewModel(url,db, {{.Type}}CollectionName, o.CacheConf, o.CacheOpts...) |
| 49 | + } |
| 50 | + |
| 51 | + return &default{{.Type}}Model{conn: conn, cachedConn: cachedConn} |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +func (m *default{{.Type}}Model) Insert(ctx context.Context, data *{{.Type}}, opts ...options.Lister[options.InsertOneOptions]) error { |
| 56 | + if data.ID.IsZero() { |
| 57 | + data.ID = bson.NewObjectID() |
| 58 | + data.CreateAt = time.Now() |
| 59 | + data.UpdateAt = time.Now() |
| 60 | + } |
| 61 | + |
| 62 | + {{if .Cache}}key := prefix{{.Type}}CacheKey + data.ID.Hex() |
| 63 | + _, err := m.cachedConn.InsertOne(ctx, {{if .Cache}}key, {{end}} data, opts...) {{else}}_, err := m.conn.InsertOne(ctx, data, opts...) {{end}} |
| 64 | + return err |
| 65 | +} |
| 66 | + |
| 67 | +func (m *default{{.Type}}Model) BulkInsert(ctx context.Context, data []*{{.Type}}, opts ...options.Lister[options.InsertManyOptions]) error { |
| 68 | + for _, d := range data { |
| 69 | + if d.ID.IsZero() { |
| 70 | + d.ID = bson.NewObjectID() |
| 71 | + d.CreateAt = time.Now() |
| 72 | + d.UpdateAt = time.Now() |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + var documents []any |
| 77 | + for _, d := range data { |
| 78 | + documents = append(documents, d) |
| 79 | + } |
| 80 | + {{if .Cache}}_, err := m.cachedConn.InsertMany(ctx, {{if .Cache}}key, {{end}} documents, opts...) {{else}}_, err := m.conn.InsertMany(ctx, documents, opts...) {{end}} |
| 81 | + return err |
| 82 | +} |
| 83 | + |
| 84 | +func (m *default{{.Type}}Model) FindOne(ctx context.Context, id string) (*{{.Type}}, error) { |
| 85 | + oid, err := bson.ObjectIDFromHex(id) |
| 86 | + if err != nil { |
| 87 | + return nil, ErrInvalidObjectId |
| 88 | + } |
| 89 | + |
| 90 | + var data {{.Type}} |
| 91 | + {{if .Cache}}key := prefix{{.Type}}CacheKey + id |
| 92 | + err = m.cachedConn.FindOne(ctx, {{if .Cache}}key, {{end}}&data, bson.M{"_id": oid}) {{else}}err = m.conn.FindOne(ctx, &data, bson.M{"_id": oid}) {{end}} |
| 93 | + switch err { |
| 94 | + case nil: |
| 95 | + return &data, nil |
| 96 | + case {{if .Cache}}monc{{else}}mon{{end}}.ErrNotFound: |
| 97 | + return nil, ErrNotFound |
| 98 | + default: |
| 99 | + return nil, err |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func (m *default{{.Type}}Model) FindOneByCondition(ctx context.Context, condition any, opts ...options.Lister[options.FindOneOptions]) (*{{.Type}}, error) { |
| 104 | + var data {{.Type}} |
| 105 | + {{if .Cache}}err := m.cachedConn.FindOne(ctx, {{if .Cache}}key, {{end}}&data, condition, opts...) {{else}}err := m.conn.FindOne(ctx, &data, condition, opts...) {{end}} |
| 106 | + switch err { |
| 107 | + case nil: |
| 108 | + return &data, nil |
| 109 | + case {{if .Cache}}monc{{else}}mon{{end}}.ErrNotFound: |
| 110 | + return nil, ErrNotFound |
| 111 | + default: |
| 112 | + return nil, err |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func (m *default{{.Type}}Model) FindByCondition(ctx context.Context, condition any, opts ...options.Lister[options.FindOptions]) ([]*{{.Type}}, error) { |
| 117 | + var data []*{{.Type}} |
| 118 | + {{if .Cache}}err := m.cachedConn.Find(ctx, {{if .Cache}}key, {{end}}&data, condition, opts...) {{else}}err := m.conn.Find(ctx, &data, condition, opts...) {{end}} |
| 119 | + switch err { |
| 120 | + case nil: |
| 121 | + return data, nil |
| 122 | + case {{if .Cache}}monc{{else}}mon{{end}}.ErrNotFound: |
| 123 | + return nil, ErrNotFound |
| 124 | + default: |
| 125 | + return nil, err |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func (m *default{{.Type}}Model) Update(ctx context.Context, data *{{.Type}}) (*mongo.UpdateResult, error) { |
| 130 | + data.UpdateAt = time.Now() |
| 131 | + {{if .Cache}}key := prefix{{.Type}}CacheKey + data.ID.Hex() |
| 132 | + res, err := m.cachedConn.UpdateOne(ctx, {{if .Cache}}key, {{end}}bson.M{"_id": data.ID}, bson.M{"$set": data}) {{else}}res, err := m.conn.UpdateOne(ctx, bson.M{"_id": data.ID}, bson.M{"$set": data}) {{end}} |
| 133 | + return res, err |
| 134 | +} |
| 135 | + |
| 136 | +func (m *default{{.Type}}Model) Delete(ctx context.Context, id string) (int64, error) { |
| 137 | + oid, err := bson.ObjectIDFromHex(id) |
| 138 | + if err != nil { |
| 139 | + return 0, ErrInvalidObjectId |
| 140 | + } |
| 141 | + {{if .Cache}}key := prefix{{.Type}}CacheKey +id |
| 142 | + res, err := m.cachedConn.DeleteOne(ctx, {{if .Cache}}key, {{end}}bson.M{"_id": oid}) {{else}}res, err := m.conn.DeleteOne(ctx, bson.M{"_id": oid}) {{end}} |
| 143 | + return res, err |
| 144 | +} |
| 145 | + |
| 146 | +func (m *default{{.Type}}Model) DeleteByCondition(ctx context.Context, condition any, opts ...options.Lister[options.DeleteManyOptions]) (int64, error) { |
| 147 | + {{if .Cache}}res, err := m.cachedConn.DeleteMany(ctx, {{if .Cache}}key, {{end}}condition, opts...) {{else}}res, err := m.conn.DeleteMany(ctx, condition, opts...) {{end}} |
| 148 | + return res, err |
| 149 | +} |
0 commit comments