user_create.go
1 // Code generated by ent, DO NOT EDIT. 2 3 package ent 4 5 import ( 6 "context" 7 "errors" 8 "fmt" 9 "time" 10 11 "entgo.io/ent/dialect" 12 "entgo.io/ent/dialect/sql" 13 "entgo.io/ent/dialect/sql/sqlgraph" 14 "entgo.io/ent/schema/field" 15 "github.com/google/uuid" 16 "github.com/mrusme/journalist/ent/feed" 17 "github.com/mrusme/journalist/ent/item" 18 "github.com/mrusme/journalist/ent/read" 19 "github.com/mrusme/journalist/ent/subscription" 20 "github.com/mrusme/journalist/ent/token" 21 "github.com/mrusme/journalist/ent/user" 22 ) 23 24 // UserCreate is the builder for creating a User entity. 25 type UserCreate struct { 26 config 27 mutation *UserMutation 28 hooks []Hook 29 conflict []sql.ConflictOption 30 } 31 32 // SetUsername sets the "username" field. 33 func (uc *UserCreate) SetUsername(s string) *UserCreate { 34 uc.mutation.SetUsername(s) 35 return uc 36 } 37 38 // SetPassword sets the "password" field. 39 func (uc *UserCreate) SetPassword(s string) *UserCreate { 40 uc.mutation.SetPassword(s) 41 return uc 42 } 43 44 // SetRole sets the "role" field. 45 func (uc *UserCreate) SetRole(s string) *UserCreate { 46 uc.mutation.SetRole(s) 47 return uc 48 } 49 50 // SetNillableRole sets the "role" field if the given value is not nil. 51 func (uc *UserCreate) SetNillableRole(s *string) *UserCreate { 52 if s != nil { 53 uc.SetRole(*s) 54 } 55 return uc 56 } 57 58 // SetCreatedAt sets the "created_at" field. 59 func (uc *UserCreate) SetCreatedAt(t time.Time) *UserCreate { 60 uc.mutation.SetCreatedAt(t) 61 return uc 62 } 63 64 // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. 65 func (uc *UserCreate) SetNillableCreatedAt(t *time.Time) *UserCreate { 66 if t != nil { 67 uc.SetCreatedAt(*t) 68 } 69 return uc 70 } 71 72 // SetUpdatedAt sets the "updated_at" field. 73 func (uc *UserCreate) SetUpdatedAt(t time.Time) *UserCreate { 74 uc.mutation.SetUpdatedAt(t) 75 return uc 76 } 77 78 // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. 79 func (uc *UserCreate) SetNillableUpdatedAt(t *time.Time) *UserCreate { 80 if t != nil { 81 uc.SetUpdatedAt(*t) 82 } 83 return uc 84 } 85 86 // SetDeletedAt sets the "deleted_at" field. 87 func (uc *UserCreate) SetDeletedAt(t time.Time) *UserCreate { 88 uc.mutation.SetDeletedAt(t) 89 return uc 90 } 91 92 // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil. 93 func (uc *UserCreate) SetNillableDeletedAt(t *time.Time) *UserCreate { 94 if t != nil { 95 uc.SetDeletedAt(*t) 96 } 97 return uc 98 } 99 100 // SetID sets the "id" field. 101 func (uc *UserCreate) SetID(u uuid.UUID) *UserCreate { 102 uc.mutation.SetID(u) 103 return uc 104 } 105 106 // SetNillableID sets the "id" field if the given value is not nil. 107 func (uc *UserCreate) SetNillableID(u *uuid.UUID) *UserCreate { 108 if u != nil { 109 uc.SetID(*u) 110 } 111 return uc 112 } 113 114 // AddTokenIDs adds the "tokens" edge to the Token entity by IDs. 115 func (uc *UserCreate) AddTokenIDs(ids ...uuid.UUID) *UserCreate { 116 uc.mutation.AddTokenIDs(ids...) 117 return uc 118 } 119 120 // AddTokens adds the "tokens" edges to the Token entity. 121 func (uc *UserCreate) AddTokens(t ...*Token) *UserCreate { 122 ids := make([]uuid.UUID, len(t)) 123 for i := range t { 124 ids[i] = t[i].ID 125 } 126 return uc.AddTokenIDs(ids...) 127 } 128 129 // AddSubscribedFeedIDs adds the "subscribed_feeds" edge to the Feed entity by IDs. 130 func (uc *UserCreate) AddSubscribedFeedIDs(ids ...uuid.UUID) *UserCreate { 131 uc.mutation.AddSubscribedFeedIDs(ids...) 132 return uc 133 } 134 135 // AddSubscribedFeeds adds the "subscribed_feeds" edges to the Feed entity. 136 func (uc *UserCreate) AddSubscribedFeeds(f ...*Feed) *UserCreate { 137 ids := make([]uuid.UUID, len(f)) 138 for i := range f { 139 ids[i] = f[i].ID 140 } 141 return uc.AddSubscribedFeedIDs(ids...) 142 } 143 144 // AddReadItemIDs adds the "read_items" edge to the Item entity by IDs. 145 func (uc *UserCreate) AddReadItemIDs(ids ...uuid.UUID) *UserCreate { 146 uc.mutation.AddReadItemIDs(ids...) 147 return uc 148 } 149 150 // AddReadItems adds the "read_items" edges to the Item entity. 151 func (uc *UserCreate) AddReadItems(i ...*Item) *UserCreate { 152 ids := make([]uuid.UUID, len(i)) 153 for j := range i { 154 ids[j] = i[j].ID 155 } 156 return uc.AddReadItemIDs(ids...) 157 } 158 159 // AddSubscriptionIDs adds the "subscriptions" edge to the Subscription entity by IDs. 160 func (uc *UserCreate) AddSubscriptionIDs(ids ...uuid.UUID) *UserCreate { 161 uc.mutation.AddSubscriptionIDs(ids...) 162 return uc 163 } 164 165 // AddSubscriptions adds the "subscriptions" edges to the Subscription entity. 166 func (uc *UserCreate) AddSubscriptions(s ...*Subscription) *UserCreate { 167 ids := make([]uuid.UUID, len(s)) 168 for i := range s { 169 ids[i] = s[i].ID 170 } 171 return uc.AddSubscriptionIDs(ids...) 172 } 173 174 // AddReadIDs adds the "reads" edge to the Read entity by IDs. 175 func (uc *UserCreate) AddReadIDs(ids ...uuid.UUID) *UserCreate { 176 uc.mutation.AddReadIDs(ids...) 177 return uc 178 } 179 180 // AddReads adds the "reads" edges to the Read entity. 181 func (uc *UserCreate) AddReads(r ...*Read) *UserCreate { 182 ids := make([]uuid.UUID, len(r)) 183 for i := range r { 184 ids[i] = r[i].ID 185 } 186 return uc.AddReadIDs(ids...) 187 } 188 189 // Mutation returns the UserMutation object of the builder. 190 func (uc *UserCreate) Mutation() *UserMutation { 191 return uc.mutation 192 } 193 194 // Save creates the User in the database. 195 func (uc *UserCreate) Save(ctx context.Context) (*User, error) { 196 uc.defaults() 197 return withHooks(ctx, uc.sqlSave, uc.mutation, uc.hooks) 198 } 199 200 // SaveX calls Save and panics if Save returns an error. 201 func (uc *UserCreate) SaveX(ctx context.Context) *User { 202 v, err := uc.Save(ctx) 203 if err != nil { 204 panic(err) 205 } 206 return v 207 } 208 209 // Exec executes the query. 210 func (uc *UserCreate) Exec(ctx context.Context) error { 211 _, err := uc.Save(ctx) 212 return err 213 } 214 215 // ExecX is like Exec, but panics if an error occurs. 216 func (uc *UserCreate) ExecX(ctx context.Context) { 217 if err := uc.Exec(ctx); err != nil { 218 panic(err) 219 } 220 } 221 222 // defaults sets the default values of the builder before save. 223 func (uc *UserCreate) defaults() { 224 if _, ok := uc.mutation.Role(); !ok { 225 v := user.DefaultRole 226 uc.mutation.SetRole(v) 227 } 228 if _, ok := uc.mutation.CreatedAt(); !ok { 229 v := user.DefaultCreatedAt() 230 uc.mutation.SetCreatedAt(v) 231 } 232 if _, ok := uc.mutation.UpdatedAt(); !ok { 233 v := user.DefaultUpdatedAt() 234 uc.mutation.SetUpdatedAt(v) 235 } 236 if _, ok := uc.mutation.ID(); !ok { 237 v := user.DefaultID() 238 uc.mutation.SetID(v) 239 } 240 } 241 242 // check runs all checks and user-defined validators on the builder. 243 func (uc *UserCreate) check() error { 244 if _, ok := uc.mutation.Username(); !ok { 245 return &ValidationError{Name: "username", err: errors.New(`ent: missing required field "User.username"`)} 246 } 247 if v, ok := uc.mutation.Username(); ok { 248 if err := user.UsernameValidator(v); err != nil { 249 return &ValidationError{Name: "username", err: fmt.Errorf(`ent: validator failed for field "User.username": %w`, err)} 250 } 251 } 252 if _, ok := uc.mutation.Password(); !ok { 253 return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "User.password"`)} 254 } 255 if v, ok := uc.mutation.Password(); ok { 256 if err := user.PasswordValidator(v); err != nil { 257 return &ValidationError{Name: "password", err: fmt.Errorf(`ent: validator failed for field "User.password": %w`, err)} 258 } 259 } 260 if _, ok := uc.mutation.Role(); !ok { 261 return &ValidationError{Name: "role", err: errors.New(`ent: missing required field "User.role"`)} 262 } 263 if v, ok := uc.mutation.Role(); ok { 264 if err := user.RoleValidator(v); err != nil { 265 return &ValidationError{Name: "role", err: fmt.Errorf(`ent: validator failed for field "User.role": %w`, err)} 266 } 267 } 268 if _, ok := uc.mutation.CreatedAt(); !ok { 269 return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "User.created_at"`)} 270 } 271 if _, ok := uc.mutation.UpdatedAt(); !ok { 272 return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "User.updated_at"`)} 273 } 274 return nil 275 } 276 277 func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { 278 if err := uc.check(); err != nil { 279 return nil, err 280 } 281 _node, _spec := uc.createSpec() 282 if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { 283 if sqlgraph.IsConstraintError(err) { 284 err = &ConstraintError{msg: err.Error(), wrap: err} 285 } 286 return nil, err 287 } 288 if _spec.ID.Value != nil { 289 if id, ok := _spec.ID.Value.(*uuid.UUID); ok { 290 _node.ID = *id 291 } else if err := _node.ID.Scan(_spec.ID.Value); err != nil { 292 return nil, err 293 } 294 } 295 uc.mutation.id = &_node.ID 296 uc.mutation.done = true 297 return _node, nil 298 } 299 300 func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { 301 var ( 302 _node = &User{config: uc.config} 303 _spec = sqlgraph.NewCreateSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID)) 304 ) 305 _spec.OnConflict = uc.conflict 306 if id, ok := uc.mutation.ID(); ok { 307 _node.ID = id 308 _spec.ID.Value = &id 309 } 310 if value, ok := uc.mutation.Username(); ok { 311 _spec.SetField(user.FieldUsername, field.TypeString, value) 312 _node.Username = value 313 } 314 if value, ok := uc.mutation.Password(); ok { 315 _spec.SetField(user.FieldPassword, field.TypeString, value) 316 _node.Password = value 317 } 318 if value, ok := uc.mutation.Role(); ok { 319 _spec.SetField(user.FieldRole, field.TypeString, value) 320 _node.Role = value 321 } 322 if value, ok := uc.mutation.CreatedAt(); ok { 323 _spec.SetField(user.FieldCreatedAt, field.TypeTime, value) 324 _node.CreatedAt = value 325 } 326 if value, ok := uc.mutation.UpdatedAt(); ok { 327 _spec.SetField(user.FieldUpdatedAt, field.TypeTime, value) 328 _node.UpdatedAt = value 329 } 330 if value, ok := uc.mutation.DeletedAt(); ok { 331 _spec.SetField(user.FieldDeletedAt, field.TypeTime, value) 332 _node.DeletedAt = &value 333 } 334 if nodes := uc.mutation.TokensIDs(); len(nodes) > 0 { 335 edge := &sqlgraph.EdgeSpec{ 336 Rel: sqlgraph.O2M, 337 Inverse: false, 338 Table: user.TokensTable, 339 Columns: []string{user.TokensColumn}, 340 Bidi: false, 341 Target: &sqlgraph.EdgeTarget{ 342 IDSpec: sqlgraph.NewFieldSpec(token.FieldID, field.TypeUUID), 343 }, 344 } 345 for _, k := range nodes { 346 edge.Target.Nodes = append(edge.Target.Nodes, k) 347 } 348 _spec.Edges = append(_spec.Edges, edge) 349 } 350 if nodes := uc.mutation.SubscribedFeedsIDs(); len(nodes) > 0 { 351 edge := &sqlgraph.EdgeSpec{ 352 Rel: sqlgraph.M2M, 353 Inverse: false, 354 Table: user.SubscribedFeedsTable, 355 Columns: user.SubscribedFeedsPrimaryKey, 356 Bidi: false, 357 Target: &sqlgraph.EdgeTarget{ 358 IDSpec: sqlgraph.NewFieldSpec(feed.FieldID, field.TypeUUID), 359 }, 360 } 361 for _, k := range nodes { 362 edge.Target.Nodes = append(edge.Target.Nodes, k) 363 } 364 createE := &SubscriptionCreate{config: uc.config, mutation: newSubscriptionMutation(uc.config, OpCreate)} 365 createE.defaults() 366 _, specE := createE.createSpec() 367 edge.Target.Fields = specE.Fields 368 if specE.ID.Value != nil { 369 edge.Target.Fields = append(edge.Target.Fields, specE.ID) 370 } 371 _spec.Edges = append(_spec.Edges, edge) 372 } 373 if nodes := uc.mutation.ReadItemsIDs(); len(nodes) > 0 { 374 edge := &sqlgraph.EdgeSpec{ 375 Rel: sqlgraph.M2M, 376 Inverse: false, 377 Table: user.ReadItemsTable, 378 Columns: user.ReadItemsPrimaryKey, 379 Bidi: false, 380 Target: &sqlgraph.EdgeTarget{ 381 IDSpec: sqlgraph.NewFieldSpec(item.FieldID, field.TypeUUID), 382 }, 383 } 384 for _, k := range nodes { 385 edge.Target.Nodes = append(edge.Target.Nodes, k) 386 } 387 createE := &ReadCreate{config: uc.config, mutation: newReadMutation(uc.config, OpCreate)} 388 createE.defaults() 389 _, specE := createE.createSpec() 390 edge.Target.Fields = specE.Fields 391 if specE.ID.Value != nil { 392 edge.Target.Fields = append(edge.Target.Fields, specE.ID) 393 } 394 _spec.Edges = append(_spec.Edges, edge) 395 } 396 if nodes := uc.mutation.SubscriptionsIDs(); len(nodes) > 0 { 397 edge := &sqlgraph.EdgeSpec{ 398 Rel: sqlgraph.O2M, 399 Inverse: true, 400 Table: user.SubscriptionsTable, 401 Columns: []string{user.SubscriptionsColumn}, 402 Bidi: false, 403 Target: &sqlgraph.EdgeTarget{ 404 IDSpec: sqlgraph.NewFieldSpec(subscription.FieldID, field.TypeUUID), 405 }, 406 } 407 for _, k := range nodes { 408 edge.Target.Nodes = append(edge.Target.Nodes, k) 409 } 410 _spec.Edges = append(_spec.Edges, edge) 411 } 412 if nodes := uc.mutation.ReadsIDs(); len(nodes) > 0 { 413 edge := &sqlgraph.EdgeSpec{ 414 Rel: sqlgraph.O2M, 415 Inverse: true, 416 Table: user.ReadsTable, 417 Columns: []string{user.ReadsColumn}, 418 Bidi: false, 419 Target: &sqlgraph.EdgeTarget{ 420 IDSpec: sqlgraph.NewFieldSpec(read.FieldID, field.TypeUUID), 421 }, 422 } 423 for _, k := range nodes { 424 edge.Target.Nodes = append(edge.Target.Nodes, k) 425 } 426 _spec.Edges = append(_spec.Edges, edge) 427 } 428 return _node, _spec 429 } 430 431 // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause 432 // of the `INSERT` statement. For example: 433 // 434 // client.User.Create(). 435 // SetUsername(v). 436 // OnConflict( 437 // // Update the row with the new values 438 // // the was proposed for insertion. 439 // sql.ResolveWithNewValues(), 440 // ). 441 // // Override some of the fields with custom 442 // // update values. 443 // Update(func(u *ent.UserUpsert) { 444 // SetUsername(v+v). 445 // }). 446 // Exec(ctx) 447 func (uc *UserCreate) OnConflict(opts ...sql.ConflictOption) *UserUpsertOne { 448 uc.conflict = opts 449 return &UserUpsertOne{ 450 create: uc, 451 } 452 } 453 454 // OnConflictColumns calls `OnConflict` and configures the columns 455 // as conflict target. Using this option is equivalent to using: 456 // 457 // client.User.Create(). 458 // OnConflict(sql.ConflictColumns(columns...)). 459 // Exec(ctx) 460 func (uc *UserCreate) OnConflictColumns(columns ...string) *UserUpsertOne { 461 uc.conflict = append(uc.conflict, sql.ConflictColumns(columns...)) 462 return &UserUpsertOne{ 463 create: uc, 464 } 465 } 466 467 type ( 468 // UserUpsertOne is the builder for "upsert"-ing 469 // one User node. 470 UserUpsertOne struct { 471 create *UserCreate 472 } 473 474 // UserUpsert is the "OnConflict" setter. 475 UserUpsert struct { 476 *sql.UpdateSet 477 } 478 ) 479 480 // SetUsername sets the "username" field. 481 func (u *UserUpsert) SetUsername(v string) *UserUpsert { 482 u.Set(user.FieldUsername, v) 483 return u 484 } 485 486 // UpdateUsername sets the "username" field to the value that was provided on create. 487 func (u *UserUpsert) UpdateUsername() *UserUpsert { 488 u.SetExcluded(user.FieldUsername) 489 return u 490 } 491 492 // SetPassword sets the "password" field. 493 func (u *UserUpsert) SetPassword(v string) *UserUpsert { 494 u.Set(user.FieldPassword, v) 495 return u 496 } 497 498 // UpdatePassword sets the "password" field to the value that was provided on create. 499 func (u *UserUpsert) UpdatePassword() *UserUpsert { 500 u.SetExcluded(user.FieldPassword) 501 return u 502 } 503 504 // SetRole sets the "role" field. 505 func (u *UserUpsert) SetRole(v string) *UserUpsert { 506 u.Set(user.FieldRole, v) 507 return u 508 } 509 510 // UpdateRole sets the "role" field to the value that was provided on create. 511 func (u *UserUpsert) UpdateRole() *UserUpsert { 512 u.SetExcluded(user.FieldRole) 513 return u 514 } 515 516 // SetCreatedAt sets the "created_at" field. 517 func (u *UserUpsert) SetCreatedAt(v time.Time) *UserUpsert { 518 u.Set(user.FieldCreatedAt, v) 519 return u 520 } 521 522 // UpdateCreatedAt sets the "created_at" field to the value that was provided on create. 523 func (u *UserUpsert) UpdateCreatedAt() *UserUpsert { 524 u.SetExcluded(user.FieldCreatedAt) 525 return u 526 } 527 528 // SetUpdatedAt sets the "updated_at" field. 529 func (u *UserUpsert) SetUpdatedAt(v time.Time) *UserUpsert { 530 u.Set(user.FieldUpdatedAt, v) 531 return u 532 } 533 534 // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create. 535 func (u *UserUpsert) UpdateUpdatedAt() *UserUpsert { 536 u.SetExcluded(user.FieldUpdatedAt) 537 return u 538 } 539 540 // SetDeletedAt sets the "deleted_at" field. 541 func (u *UserUpsert) SetDeletedAt(v time.Time) *UserUpsert { 542 u.Set(user.FieldDeletedAt, v) 543 return u 544 } 545 546 // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create. 547 func (u *UserUpsert) UpdateDeletedAt() *UserUpsert { 548 u.SetExcluded(user.FieldDeletedAt) 549 return u 550 } 551 552 // ClearDeletedAt clears the value of the "deleted_at" field. 553 func (u *UserUpsert) ClearDeletedAt() *UserUpsert { 554 u.SetNull(user.FieldDeletedAt) 555 return u 556 } 557 558 // UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field. 559 // Using this option is equivalent to using: 560 // 561 // client.User.Create(). 562 // OnConflict( 563 // sql.ResolveWithNewValues(), 564 // sql.ResolveWith(func(u *sql.UpdateSet) { 565 // u.SetIgnore(user.FieldID) 566 // }), 567 // ). 568 // Exec(ctx) 569 func (u *UserUpsertOne) UpdateNewValues() *UserUpsertOne { 570 u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) 571 u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) { 572 if _, exists := u.create.mutation.ID(); exists { 573 s.SetIgnore(user.FieldID) 574 } 575 })) 576 return u 577 } 578 579 // Ignore sets each column to itself in case of conflict. 580 // Using this option is equivalent to using: 581 // 582 // client.User.Create(). 583 // OnConflict(sql.ResolveWithIgnore()). 584 // Exec(ctx) 585 func (u *UserUpsertOne) Ignore() *UserUpsertOne { 586 u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore()) 587 return u 588 } 589 590 // DoNothing configures the conflict_action to `DO NOTHING`. 591 // Supported only by SQLite and PostgreSQL. 592 func (u *UserUpsertOne) DoNothing() *UserUpsertOne { 593 u.create.conflict = append(u.create.conflict, sql.DoNothing()) 594 return u 595 } 596 597 // Update allows overriding fields `UPDATE` values. See the UserCreate.OnConflict 598 // documentation for more info. 599 func (u *UserUpsertOne) Update(set func(*UserUpsert)) *UserUpsertOne { 600 u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { 601 set(&UserUpsert{UpdateSet: update}) 602 })) 603 return u 604 } 605 606 // SetUsername sets the "username" field. 607 func (u *UserUpsertOne) SetUsername(v string) *UserUpsertOne { 608 return u.Update(func(s *UserUpsert) { 609 s.SetUsername(v) 610 }) 611 } 612 613 // UpdateUsername sets the "username" field to the value that was provided on create. 614 func (u *UserUpsertOne) UpdateUsername() *UserUpsertOne { 615 return u.Update(func(s *UserUpsert) { 616 s.UpdateUsername() 617 }) 618 } 619 620 // SetPassword sets the "password" field. 621 func (u *UserUpsertOne) SetPassword(v string) *UserUpsertOne { 622 return u.Update(func(s *UserUpsert) { 623 s.SetPassword(v) 624 }) 625 } 626 627 // UpdatePassword sets the "password" field to the value that was provided on create. 628 func (u *UserUpsertOne) UpdatePassword() *UserUpsertOne { 629 return u.Update(func(s *UserUpsert) { 630 s.UpdatePassword() 631 }) 632 } 633 634 // SetRole sets the "role" field. 635 func (u *UserUpsertOne) SetRole(v string) *UserUpsertOne { 636 return u.Update(func(s *UserUpsert) { 637 s.SetRole(v) 638 }) 639 } 640 641 // UpdateRole sets the "role" field to the value that was provided on create. 642 func (u *UserUpsertOne) UpdateRole() *UserUpsertOne { 643 return u.Update(func(s *UserUpsert) { 644 s.UpdateRole() 645 }) 646 } 647 648 // SetCreatedAt sets the "created_at" field. 649 func (u *UserUpsertOne) SetCreatedAt(v time.Time) *UserUpsertOne { 650 return u.Update(func(s *UserUpsert) { 651 s.SetCreatedAt(v) 652 }) 653 } 654 655 // UpdateCreatedAt sets the "created_at" field to the value that was provided on create. 656 func (u *UserUpsertOne) UpdateCreatedAt() *UserUpsertOne { 657 return u.Update(func(s *UserUpsert) { 658 s.UpdateCreatedAt() 659 }) 660 } 661 662 // SetUpdatedAt sets the "updated_at" field. 663 func (u *UserUpsertOne) SetUpdatedAt(v time.Time) *UserUpsertOne { 664 return u.Update(func(s *UserUpsert) { 665 s.SetUpdatedAt(v) 666 }) 667 } 668 669 // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create. 670 func (u *UserUpsertOne) UpdateUpdatedAt() *UserUpsertOne { 671 return u.Update(func(s *UserUpsert) { 672 s.UpdateUpdatedAt() 673 }) 674 } 675 676 // SetDeletedAt sets the "deleted_at" field. 677 func (u *UserUpsertOne) SetDeletedAt(v time.Time) *UserUpsertOne { 678 return u.Update(func(s *UserUpsert) { 679 s.SetDeletedAt(v) 680 }) 681 } 682 683 // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create. 684 func (u *UserUpsertOne) UpdateDeletedAt() *UserUpsertOne { 685 return u.Update(func(s *UserUpsert) { 686 s.UpdateDeletedAt() 687 }) 688 } 689 690 // ClearDeletedAt clears the value of the "deleted_at" field. 691 func (u *UserUpsertOne) ClearDeletedAt() *UserUpsertOne { 692 return u.Update(func(s *UserUpsert) { 693 s.ClearDeletedAt() 694 }) 695 } 696 697 // Exec executes the query. 698 func (u *UserUpsertOne) Exec(ctx context.Context) error { 699 if len(u.create.conflict) == 0 { 700 return errors.New("ent: missing options for UserCreate.OnConflict") 701 } 702 return u.create.Exec(ctx) 703 } 704 705 // ExecX is like Exec, but panics if an error occurs. 706 func (u *UserUpsertOne) ExecX(ctx context.Context) { 707 if err := u.create.Exec(ctx); err != nil { 708 panic(err) 709 } 710 } 711 712 // Exec executes the UPSERT query and returns the inserted/updated ID. 713 func (u *UserUpsertOne) ID(ctx context.Context) (id uuid.UUID, err error) { 714 if u.create.driver.Dialect() == dialect.MySQL { 715 // In case of "ON CONFLICT", there is no way to get back non-numeric ID 716 // fields from the database since MySQL does not support the RETURNING clause. 717 return id, errors.New("ent: UserUpsertOne.ID is not supported by MySQL driver. Use UserUpsertOne.Exec instead") 718 } 719 node, err := u.create.Save(ctx) 720 if err != nil { 721 return id, err 722 } 723 return node.ID, nil 724 } 725 726 // IDX is like ID, but panics if an error occurs. 727 func (u *UserUpsertOne) IDX(ctx context.Context) uuid.UUID { 728 id, err := u.ID(ctx) 729 if err != nil { 730 panic(err) 731 } 732 return id 733 } 734 735 // UserCreateBulk is the builder for creating many User entities in bulk. 736 type UserCreateBulk struct { 737 config 738 err error 739 builders []*UserCreate 740 conflict []sql.ConflictOption 741 } 742 743 // Save creates the User entities in the database. 744 func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) { 745 if ucb.err != nil { 746 return nil, ucb.err 747 } 748 specs := make([]*sqlgraph.CreateSpec, len(ucb.builders)) 749 nodes := make([]*User, len(ucb.builders)) 750 mutators := make([]Mutator, len(ucb.builders)) 751 for i := range ucb.builders { 752 func(i int, root context.Context) { 753 builder := ucb.builders[i] 754 builder.defaults() 755 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { 756 mutation, ok := m.(*UserMutation) 757 if !ok { 758 return nil, fmt.Errorf("unexpected mutation type %T", m) 759 } 760 if err := builder.check(); err != nil { 761 return nil, err 762 } 763 builder.mutation = mutation 764 var err error 765 nodes[i], specs[i] = builder.createSpec() 766 if i < len(mutators)-1 { 767 _, err = mutators[i+1].Mutate(root, ucb.builders[i+1].mutation) 768 } else { 769 spec := &sqlgraph.BatchCreateSpec{Nodes: specs} 770 spec.OnConflict = ucb.conflict 771 // Invoke the actual operation on the latest mutation in the chain. 772 if err = sqlgraph.BatchCreate(ctx, ucb.driver, spec); err != nil { 773 if sqlgraph.IsConstraintError(err) { 774 err = &ConstraintError{msg: err.Error(), wrap: err} 775 } 776 } 777 } 778 if err != nil { 779 return nil, err 780 } 781 mutation.id = &nodes[i].ID 782 mutation.done = true 783 return nodes[i], nil 784 }) 785 for i := len(builder.hooks) - 1; i >= 0; i-- { 786 mut = builder.hooks[i](mut) 787 } 788 mutators[i] = mut 789 }(i, ctx) 790 } 791 if len(mutators) > 0 { 792 if _, err := mutators[0].Mutate(ctx, ucb.builders[0].mutation); err != nil { 793 return nil, err 794 } 795 } 796 return nodes, nil 797 } 798 799 // SaveX is like Save, but panics if an error occurs. 800 func (ucb *UserCreateBulk) SaveX(ctx context.Context) []*User { 801 v, err := ucb.Save(ctx) 802 if err != nil { 803 panic(err) 804 } 805 return v 806 } 807 808 // Exec executes the query. 809 func (ucb *UserCreateBulk) Exec(ctx context.Context) error { 810 _, err := ucb.Save(ctx) 811 return err 812 } 813 814 // ExecX is like Exec, but panics if an error occurs. 815 func (ucb *UserCreateBulk) ExecX(ctx context.Context) { 816 if err := ucb.Exec(ctx); err != nil { 817 panic(err) 818 } 819 } 820 821 // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause 822 // of the `INSERT` statement. For example: 823 // 824 // client.User.CreateBulk(builders...). 825 // OnConflict( 826 // // Update the row with the new values 827 // // the was proposed for insertion. 828 // sql.ResolveWithNewValues(), 829 // ). 830 // // Override some of the fields with custom 831 // // update values. 832 // Update(func(u *ent.UserUpsert) { 833 // SetUsername(v+v). 834 // }). 835 // Exec(ctx) 836 func (ucb *UserCreateBulk) OnConflict(opts ...sql.ConflictOption) *UserUpsertBulk { 837 ucb.conflict = opts 838 return &UserUpsertBulk{ 839 create: ucb, 840 } 841 } 842 843 // OnConflictColumns calls `OnConflict` and configures the columns 844 // as conflict target. Using this option is equivalent to using: 845 // 846 // client.User.Create(). 847 // OnConflict(sql.ConflictColumns(columns...)). 848 // Exec(ctx) 849 func (ucb *UserCreateBulk) OnConflictColumns(columns ...string) *UserUpsertBulk { 850 ucb.conflict = append(ucb.conflict, sql.ConflictColumns(columns...)) 851 return &UserUpsertBulk{ 852 create: ucb, 853 } 854 } 855 856 // UserUpsertBulk is the builder for "upsert"-ing 857 // a bulk of User nodes. 858 type UserUpsertBulk struct { 859 create *UserCreateBulk 860 } 861 862 // UpdateNewValues updates the mutable fields using the new values that 863 // were set on create. Using this option is equivalent to using: 864 // 865 // client.User.Create(). 866 // OnConflict( 867 // sql.ResolveWithNewValues(), 868 // sql.ResolveWith(func(u *sql.UpdateSet) { 869 // u.SetIgnore(user.FieldID) 870 // }), 871 // ). 872 // Exec(ctx) 873 func (u *UserUpsertBulk) UpdateNewValues() *UserUpsertBulk { 874 u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) 875 u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) { 876 for _, b := range u.create.builders { 877 if _, exists := b.mutation.ID(); exists { 878 s.SetIgnore(user.FieldID) 879 } 880 } 881 })) 882 return u 883 } 884 885 // Ignore sets each column to itself in case of conflict. 886 // Using this option is equivalent to using: 887 // 888 // client.User.Create(). 889 // OnConflict(sql.ResolveWithIgnore()). 890 // Exec(ctx) 891 func (u *UserUpsertBulk) Ignore() *UserUpsertBulk { 892 u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore()) 893 return u 894 } 895 896 // DoNothing configures the conflict_action to `DO NOTHING`. 897 // Supported only by SQLite and PostgreSQL. 898 func (u *UserUpsertBulk) DoNothing() *UserUpsertBulk { 899 u.create.conflict = append(u.create.conflict, sql.DoNothing()) 900 return u 901 } 902 903 // Update allows overriding fields `UPDATE` values. See the UserCreateBulk.OnConflict 904 // documentation for more info. 905 func (u *UserUpsertBulk) Update(set func(*UserUpsert)) *UserUpsertBulk { 906 u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { 907 set(&UserUpsert{UpdateSet: update}) 908 })) 909 return u 910 } 911 912 // SetUsername sets the "username" field. 913 func (u *UserUpsertBulk) SetUsername(v string) *UserUpsertBulk { 914 return u.Update(func(s *UserUpsert) { 915 s.SetUsername(v) 916 }) 917 } 918 919 // UpdateUsername sets the "username" field to the value that was provided on create. 920 func (u *UserUpsertBulk) UpdateUsername() *UserUpsertBulk { 921 return u.Update(func(s *UserUpsert) { 922 s.UpdateUsername() 923 }) 924 } 925 926 // SetPassword sets the "password" field. 927 func (u *UserUpsertBulk) SetPassword(v string) *UserUpsertBulk { 928 return u.Update(func(s *UserUpsert) { 929 s.SetPassword(v) 930 }) 931 } 932 933 // UpdatePassword sets the "password" field to the value that was provided on create. 934 func (u *UserUpsertBulk) UpdatePassword() *UserUpsertBulk { 935 return u.Update(func(s *UserUpsert) { 936 s.UpdatePassword() 937 }) 938 } 939 940 // SetRole sets the "role" field. 941 func (u *UserUpsertBulk) SetRole(v string) *UserUpsertBulk { 942 return u.Update(func(s *UserUpsert) { 943 s.SetRole(v) 944 }) 945 } 946 947 // UpdateRole sets the "role" field to the value that was provided on create. 948 func (u *UserUpsertBulk) UpdateRole() *UserUpsertBulk { 949 return u.Update(func(s *UserUpsert) { 950 s.UpdateRole() 951 }) 952 } 953 954 // SetCreatedAt sets the "created_at" field. 955 func (u *UserUpsertBulk) SetCreatedAt(v time.Time) *UserUpsertBulk { 956 return u.Update(func(s *UserUpsert) { 957 s.SetCreatedAt(v) 958 }) 959 } 960 961 // UpdateCreatedAt sets the "created_at" field to the value that was provided on create. 962 func (u *UserUpsertBulk) UpdateCreatedAt() *UserUpsertBulk { 963 return u.Update(func(s *UserUpsert) { 964 s.UpdateCreatedAt() 965 }) 966 } 967 968 // SetUpdatedAt sets the "updated_at" field. 969 func (u *UserUpsertBulk) SetUpdatedAt(v time.Time) *UserUpsertBulk { 970 return u.Update(func(s *UserUpsert) { 971 s.SetUpdatedAt(v) 972 }) 973 } 974 975 // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create. 976 func (u *UserUpsertBulk) UpdateUpdatedAt() *UserUpsertBulk { 977 return u.Update(func(s *UserUpsert) { 978 s.UpdateUpdatedAt() 979 }) 980 } 981 982 // SetDeletedAt sets the "deleted_at" field. 983 func (u *UserUpsertBulk) SetDeletedAt(v time.Time) *UserUpsertBulk { 984 return u.Update(func(s *UserUpsert) { 985 s.SetDeletedAt(v) 986 }) 987 } 988 989 // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create. 990 func (u *UserUpsertBulk) UpdateDeletedAt() *UserUpsertBulk { 991 return u.Update(func(s *UserUpsert) { 992 s.UpdateDeletedAt() 993 }) 994 } 995 996 // ClearDeletedAt clears the value of the "deleted_at" field. 997 func (u *UserUpsertBulk) ClearDeletedAt() *UserUpsertBulk { 998 return u.Update(func(s *UserUpsert) { 999 s.ClearDeletedAt() 1000 }) 1001 } 1002 1003 // Exec executes the query. 1004 func (u *UserUpsertBulk) Exec(ctx context.Context) error { 1005 if u.create.err != nil { 1006 return u.create.err 1007 } 1008 for i, b := range u.create.builders { 1009 if len(b.conflict) != 0 { 1010 return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the UserCreateBulk instead", i) 1011 } 1012 } 1013 if len(u.create.conflict) == 0 { 1014 return errors.New("ent: missing options for UserCreateBulk.OnConflict") 1015 } 1016 return u.create.Exec(ctx) 1017 } 1018 1019 // ExecX is like Exec, but panics if an error occurs. 1020 func (u *UserUpsertBulk) ExecX(ctx context.Context) { 1021 if err := u.create.Exec(ctx); err != nil { 1022 panic(err) 1023 } 1024 }