-
Notifications
You must be signed in to change notification settings - Fork 108
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
Open
danieljbruce
wants to merge
23
commits into
main
Choose a base branch
from
404540305-datastore-mode-data-transforms
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1452,8 +1452,29 @@ | |
excludeFromIndexes?: boolean; | ||
} | ||
|
||
/* | ||
* This is the interface the user would provide transform operations in before | ||
* they are converted to the google.datastore.v1.IPropertyTransform | ||
* interface. | ||
* | ||
*/ | ||
export type PropertyTransform = { | ||
property: string; | ||
setToServerValue: boolean; | ||
increment: any; | ||
maximum: any; | ||
minimum: any; | ||
appendMissingElements: any[]; | ||
removeAllFromArray: any[]; | ||
}; | ||
|
||
interface EntityWithTransforms { | ||
transforms?: PropertyTransform[]; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type Entity = any; | ||
// TODO: Call out this interface change | ||
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. What is this TODO here for? |
||
export type Entity = any & EntityWithTransforms; | ||
export type Entities = Entity | Entity[]; | ||
|
||
interface KeyProtoPathElement extends google.datastore.v1.Key.IPathElement { | ||
|
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,57 @@ | ||
// 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; | ||
|
||
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 |
||
const propertyTransforms: google.datastore.v1.IPropertyTransform[] = []; | ||
transforms.forEach((transform: PropertyTransform) => { | ||
const property = transform.property; | ||
if (transform.setToServerValue) { | ||
propertyTransforms.push({ | ||
property, | ||
setToServerValue: ServerValue.REQUEST_TIME, | ||
}); | ||
} | ||
['increment', 'maximum', 'minimum'].forEach(type => { | ||
const castedType = type as 'increment' | 'maximum' | 'minimum'; | ||
if (transform[castedType]) { | ||
propertyTransforms.push({ | ||
property, | ||
[castedType]: entity.encodeValue( | ||
transform[castedType], | ||
property, | ||
) as IValue, | ||
}); | ||
} | ||
}); | ||
['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; | ||
} |
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
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
IValue
type so to match we allow users to supplyany
type to support all the different input possibilities.