-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(firestore): update and delete pipeline DML stages #14331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -822,3 +822,94 @@ func (p *Pipeline) RawStage(name string, args []any, opts ...RawStageOptions) *P | |
| } | ||
| return p.append(stage) | ||
| } | ||
|
|
||
| // UpdateOption is an option for an Update pipeline stage. | ||
| // | ||
| // Experimental: Firestore Pipelines is currently in preview and is subject to potential breaking changes in future versions, | ||
| // regardless of any other documented package stability guarantees. | ||
| type UpdateOption interface { | ||
| isUpdateOption() | ||
| } | ||
|
|
||
| type updateTransformationsOption struct { | ||
| fields []Selectable | ||
| } | ||
|
|
||
| func (updateTransformationsOption) isUpdateOption() {} | ||
|
|
||
| // WithUpdateTransformations specifies the list of field transformations to apply in an update operation. | ||
| // | ||
| // Experimental: Firestore Pipelines is currently in preview and is subject to potential breaking changes in future versions, | ||
| // regardless of any other documented package stability guarantees. | ||
| func WithUpdateTransformations(field Selectable, additionalFields ...Selectable) UpdateOption { | ||
| return updateTransformationsOption{ | ||
| fields: append([]Selectable{field}, additionalFields...), | ||
| } | ||
| } | ||
|
|
||
| // Update performs an update operation using documents from previous stages. | ||
| // | ||
| // This method updates the documents in place based on the data flowing through the pipeline. | ||
| // You can optionally specify a list of [Selectable] field transformations using [WithUpdateTransformations]. | ||
| // If no transformations are provided, it performs the update in-place without any changes. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This seems a little unclear to me. Doesn't it replace the whole doc if no selectables are given?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in #14322 |
||
| // | ||
| // Example: | ||
| // | ||
| // // In-place update | ||
| // client.Pipeline().Literals(updateData).Update() | ||
| // | ||
| // // Update with transformations | ||
| // client.Pipeline().Collection("books"). | ||
| // Where(GreaterThan("price", 50)). | ||
| // Update(WithUpdateTransformations(ConstantOf("Discounted").As("status"))) | ||
| // | ||
| // Experimental: Firestore Pipelines is currently in preview and is subject to potential breaking changes in future versions, | ||
| // regardless of any other documented package stability guarantees. | ||
| func (p *Pipeline) Update(opts ...UpdateOption) *Pipeline { | ||
| if p.err != nil { | ||
| return p | ||
| } | ||
|
|
||
| var transformations []Selectable | ||
| for _, opt := range opts { | ||
| if opt != nil { | ||
| switch o := opt.(type) { | ||
| case updateTransformationsOption: | ||
| transformations = append(transformations, o.fields...) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| stage, err := newUpdateStage(transformations) | ||
| if err != nil { | ||
| p.err = err | ||
| return p | ||
| } | ||
| return p.append(stage) | ||
| } | ||
|
|
||
| // DeleteOption is an option for a Delete pipeline stage. | ||
| // | ||
| // Experimental: Firestore Pipelines is currently in preview and is subject to potential breaking changes in future versions, | ||
| // regardless of any other documented package stability guarantees. | ||
| type DeleteOption interface { | ||
| isDeleteOption() | ||
| } | ||
|
|
||
| // Delete deletes the documents from previous stages. | ||
| // | ||
| // Example: | ||
| // | ||
| // client.Pipeline().Collection("logs"). | ||
| // Where(Equal("status", "archived")). | ||
| // Delete() | ||
| // | ||
| // Experimental: Firestore Pipelines is currently in preview and is subject to potential breaking changes in future versions, | ||
| // regardless of any other documented package stability guarantees. | ||
| func (p *Pipeline) Delete(opts ...DeleteOption) *Pipeline { | ||
| if p.err != nil { | ||
| return p | ||
| } | ||
| stage := newDeleteStage() | ||
| return p.append(stage) | ||
| } | ||
|
bhshkh marked this conversation as resolved.
|
||
Uh oh!
There was an error while loading. Please reload this page.