Update one or multiple records. Also update `updated_at` if `timestamps` is `true`. * * await Flight.where("departure", "Dublin").update("departure", "Tokyo"); * * await Flight.where("departure", "Dublin").update({ destination: "Tokyo" });
(fieldOrFields: string | Values, fieldValue?: FieldValue)
| 643 | * await Flight.where("departure", "Dublin").update({ destination: "Tokyo" }); |
| 644 | */ |
| 645 | static update(fieldOrFields: string | Values, fieldValue?: FieldValue) { |
| 646 | let fieldsToUpdate: Values = {}; |
| 647 | |
| 648 | if (this.timestamps) { |
| 649 | fieldsToUpdate[ |
| 650 | this.formatFieldToDatabase("updated_at") as string |
| 651 | ] = new Date(); |
| 652 | } |
| 653 | |
| 654 | if (typeof fieldOrFields === "string") { |
| 655 | fieldsToUpdate[ |
| 656 | this.formatFieldToDatabase(fieldOrFields) as string |
| 657 | ] = fieldValue!; |
| 658 | } else { |
| 659 | fieldsToUpdate = { |
| 660 | ...fieldsToUpdate, |
| 661 | ...(this.formatFieldToDatabase(fieldOrFields) as { |
| 662 | [fieldName: string]: any; |
| 663 | }), |
| 664 | }; |
| 665 | } |
| 666 | |
| 667 | return this._runQuery( |
| 668 | this._currentQuery |
| 669 | .table(this.table) |
| 670 | .update(fieldsToUpdate) |
| 671 | .toDescription(), |
| 672 | ) as Promise<Model | Model[]>; |
| 673 | } |
| 674 | |
| 675 | /** Delete a record by a primary key value. |
| 676 | * |
no test coverage detected