-
Notifications
You must be signed in to change notification settings - Fork 109
feat: Add datastore mode data transforms #1369
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
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
b7f4a1c
Work in progress property transforms
danieljbruce 73b8e15
Change entity back to any
danieljbruce 1179e4e
Merge branch 'main' of https://github.com/googleapis/nodejs-datastore…
danieljbruce 35ee5a4
Add the new interfaces and middleware code
danieljbruce f181a4c
Pull the build property transform into separate fn
danieljbruce 66e0668
Add arrays to the build transform function
danieljbruce 7abda1f
Add a test for property transforms
danieljbruce 4ae772f
Correct test comments
danieljbruce c5c4484
More modular build function
danieljbruce a1e8e69
Update the test to check for the final result
danieljbruce ef41b73
Add the request spy
danieljbruce ceb3fc4
Fix the bug making property always increment
danieljbruce 81f1898
Fix the test based on the most recent source code
danieljbruce c4ac639
Refactor code for the array transforms
danieljbruce a09119b
Add a guard in case the casted type doesn’t exist
danieljbruce 409f6a6
Remove only
danieljbruce 6f65b43
Add a header
danieljbruce 55b1913
Remove TODO
danieljbruce 2478abc
Remove TODO
danieljbruce 901cbb5
Parameterize the test
danieljbruce c99af00
Merge branch 'main' of https://github.com/googleapis/nodejs-datastore…
danieljbruce a4710ed
Ran linter
danieljbruce ac96630
Run the linter
danieljbruce 499eca9
Add a comment to buildPropertyTransforms
danieljbruce b75897e
Add comments for building the property transforms
danieljbruce c95ac45
Add two more test cases
danieljbruce a019f07
Build the 3 test cases
danieljbruce abcfa40
should process string inputs for max/min/increment
danieljbruce fc79c7e
Produce test cases for different situations
danieljbruce 75a87bd
Delete invalid test cases and run linter
danieljbruce bc60583
Correct the order of operations test
danieljbruce 0fdf74d
Remove only
danieljbruce 904b8ef
Merge branch 'main' into 404540305-datastore-mode-data-transforms
danieljbruce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import {entity, PropertyTransform} from '../../entity'; | ||
| import {google} from '../../../protos/protos'; | ||
| import IValue = google.datastore.v1.IValue; | ||
| import ServerValue = google.datastore.v1.PropertyTransform.ServerValue; | ||
|
|
||
| /** | ||
| * This method takes the user supplied transforms object and returns a list of | ||
| * corresponding transforms that are instead in the shape of the protos so that | ||
| * they can be included in the grpc request to the service directly. | ||
| * | ||
| * @param transforms The transforms supplied by the user that the user intends | ||
| * to apply. | ||
| */ | ||
| export function buildPropertyTransforms(transforms: PropertyTransform[]) { | ||
|
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. nit: this would benefit from some comments 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. Added some comments. |
||
| const propertyTransforms: google.datastore.v1.IPropertyTransform[] = []; | ||
| transforms.forEach((transform: PropertyTransform) => { | ||
| const property = transform.property; | ||
| // If the user's transform has a setToServerValue property then ensure the | ||
| // propertyTransforms sent in the request have a setToServerValue transform. | ||
| if (transform.setToServerValue) { | ||
| propertyTransforms.push({ | ||
| property, | ||
| setToServerValue: ServerValue.REQUEST_TIME, | ||
| }); | ||
| } | ||
| // If the transform has an 'increment', 'maximum' or 'minimum' property then | ||
| // add the corresponding property transform to the propertyTransforms in the | ||
| // request. | ||
| ['increment', 'maximum', 'minimum'].forEach(type => { | ||
| const castedType = type as 'increment' | 'maximum' | 'minimum'; | ||
| if (transform[castedType]) { | ||
| propertyTransforms.push({ | ||
| property, | ||
| [castedType]: entity.encodeValue( | ||
| parseFloat(transform[castedType]), | ||
| property, | ||
| ) as IValue, | ||
| }); | ||
| } | ||
| }); | ||
| // If the transform has an 'appendMissingElements' or 'removeAllFromArray' | ||
| // property then add the corresponding property transform to the | ||
| // propertyTransforms in the request. | ||
| ['appendMissingElements', 'removeAllFromArray'].forEach(type => { | ||
| const castedType = type as 'appendMissingElements' | 'removeAllFromArray'; | ||
| if (transform[castedType]) { | ||
| propertyTransforms.push({ | ||
| property, | ||
| [castedType]: { | ||
| values: transform[castedType].map(element => { | ||
| return entity.encodeValue(element, property) as IValue; | ||
| }), | ||
| }, | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
| return propertyTransforms; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do these have to be any?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maximum/minimum/increment/appendMissingElements/removeAllFromArray proto values can technically accept any type so there really are tons of combinations. In this feature we use encodeValue function to take the user supplied value and encode it into the generic proto IValue format. The proto supports the generic
IValuetype so to match we allow users to supplyanytype to support all the different input possibilities.