Skip to content
Merged
Show file tree
Hide file tree
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 Mar 18, 2025
73b8e15
Change entity back to any
danieljbruce Mar 18, 2025
1179e4e
Merge branch 'main' of https://github.com/googleapis/nodejs-datastore…
danieljbruce Mar 18, 2025
35ee5a4
Add the new interfaces and middleware code
danieljbruce Mar 18, 2025
f181a4c
Pull the build property transform into separate fn
danieljbruce Mar 18, 2025
66e0668
Add arrays to the build transform function
danieljbruce Mar 18, 2025
7abda1f
Add a test for property transforms
danieljbruce Mar 18, 2025
4ae772f
Correct test comments
danieljbruce Mar 18, 2025
c5c4484
More modular build function
danieljbruce Mar 18, 2025
a1e8e69
Update the test to check for the final result
danieljbruce Mar 18, 2025
ef41b73
Add the request spy
danieljbruce Mar 18, 2025
ceb3fc4
Fix the bug making property always increment
danieljbruce Mar 19, 2025
81f1898
Fix the test based on the most recent source code
danieljbruce Mar 19, 2025
c4ac639
Refactor code for the array transforms
danieljbruce Mar 19, 2025
a09119b
Add a guard in case the casted type doesn’t exist
danieljbruce Mar 19, 2025
409f6a6
Remove only
danieljbruce Mar 19, 2025
6f65b43
Add a header
danieljbruce Mar 24, 2025
55b1913
Remove TODO
danieljbruce Mar 24, 2025
2478abc
Remove TODO
danieljbruce Mar 24, 2025
901cbb5
Parameterize the test
danieljbruce May 29, 2025
c99af00
Merge branch 'main' of https://github.com/googleapis/nodejs-datastore…
danieljbruce May 29, 2025
a4710ed
Ran linter
danieljbruce May 29, 2025
ac96630
Run the linter
danieljbruce May 30, 2025
499eca9
Add a comment to buildPropertyTransforms
danieljbruce Sep 10, 2025
b75897e
Add comments for building the property transforms
danieljbruce Sep 10, 2025
c95ac45
Add two more test cases
danieljbruce Sep 10, 2025
a019f07
Build the 3 test cases
danieljbruce Sep 11, 2025
abcfa40
should process string inputs for max/min/increment
danieljbruce Sep 11, 2025
fc79c7e
Produce test cases for different situations
danieljbruce Sep 11, 2025
75a87bd
Delete invalid test cases and run linter
danieljbruce Sep 11, 2025
bc60583
Correct the order of operations test
danieljbruce Sep 11, 2025
0fdf74d
Remove only
danieljbruce Sep 11, 2025
904b8ef
Merge branch 'main' into 404540305-datastore-mode-data-transforms
danieljbruce Sep 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1452,8 +1452,28 @@
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;

Check warning on line 1464 in src/entity.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

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?

Copy link
Contributor Author

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 supply any type to support all the different input possibilities.

image

maximum: any;

Check warning on line 1465 in src/entity.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
minimum: any;

Check warning on line 1466 in src/entity.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
appendMissingElements: any[];

Check warning on line 1467 in src/entity.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
removeAllFromArray: any[];

Check warning on line 1468 in src/entity.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
};

interface EntityWithTransforms {
transforms?: PropertyTransform[];
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Entity = any;
export type Entity = any & EntityWithTransforms;
export type Entities = Entity | Entity[];

interface KeyProtoPathElement extends google.datastore.v1.Key.IPathElement {
Expand Down
28 changes: 24 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ import {
ServiceError,
} from 'google-gax';
import * as is from 'is';
import {Transform, pipeline} from 'stream';
import {pipeline, Transform} from 'stream';

import {entity, Entities, Entity, EntityProto, ValueProto} from './entity';
import {
entity,
Entities,
Entity,
EntityProto,
ValueProto,
PropertyTransform,
} from './entity';
import {AggregateField} from './aggregate';
import Key = entity.Key;
export {Entity, Key, AggregateField};
Expand Down Expand Up @@ -70,6 +77,10 @@ import {AggregateQuery} from './aggregate';
import {SaveEntity} from './interfaces/save';
import {extendExcludeFromIndexes} from './utils/entity/extendExcludeFromIndexes';
import {buildEntityProto} from './utils/entity/buildEntityProto';
import IValue = google.datastore.v1.IValue;
import IEntity = google.datastore.v1.IEntity;
import ServerValue = google.datastore.v1.PropertyTransform.ServerValue;
import {buildPropertyTransforms} from './utils/entity/buildPropertyTransforms';

const {grpc} = new GrpcClient();

Expand Down Expand Up @@ -1098,7 +1109,8 @@ class Datastore extends DatastoreRequest {
entities
.map(DatastoreRequest.prepareEntityObject_)
.forEach((entityObject: Entity, index: number) => {
const mutation: Mutation = {};
const mutation: google.datastore.v1.IMutation = {};

let method = 'upsert';

if (entityObject.method) {
Expand All @@ -1120,7 +1132,15 @@ class Datastore extends DatastoreRequest {

entityProto.key = entity.keyToKeyProto(entityObject.key);

mutation[method] = entityProto;
mutation[method as 'upsert' | 'update' | 'insert' | 'delete'] =
entityProto as IEntity;

// We built the entityProto, now we should add the data transforms:
if (entityObject.transforms) {
mutation.propertyTransforms = buildPropertyTransforms(
entityObject.transforms,
);
}
mutations.push(mutation);
});

Expand Down
73 changes: 73 additions & 0 deletions src/utils/entity/buildPropertyTransforms.ts
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[]) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this would benefit from some comments

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
Loading
Loading