Skip to content

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
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 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
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
23 changes: 22 additions & 1 deletion src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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;
// TODO: Call out this interface change

Choose a reason for hiding this comment

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

What is this TODO here for?

export type Entity = any & EntityWithTransforms;

Check warning on line 1477 in src/entity.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
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
57 changes: 57 additions & 0 deletions src/utils/entity/buildPropertyTransforms.ts
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[]) {

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

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;
}
232 changes: 232 additions & 0 deletions system-test/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import {Query, RunQueryInfo, ExecutionStats} from '../src/query';
import KEY_SYMBOL = entity.KEY_SYMBOL;
import {transactionExpiredError} from '../src/request';
const sinon = require('sinon');

const async = require('async');

Expand Down Expand Up @@ -3295,6 +3296,237 @@
assert.strictEqual(entity, undefined);
});
});
describe.only('Datastore mode data transforms', () => {

Check failure on line 3299 in system-test/datastore.ts

View workflow job for this annotation

GitHub Actions / lint

'describe.only' is restricted from being used
const key = datastore.key(['Post', 'post1']);
async.each(
[
{
name: 'should perform a basic data transform',
saveArg: {
key: key,
data: {
name: 'test',
p1: 3,
p2: 4,
p3: 5,
a1: [3, 4, 5],
},
transforms: [
{
property: 'p1',
setToServerValue: true,
},
{
property: 'p2',
increment: 4,
},
{
property: 'p3',
maximum: 9,
},
{
property: 'p2',
minimum: 6,
},
{
property: 'a1',
appendMissingElements: [5, 6],
},
{
property: 'a1',
removeAllFromArray: [3],
},
],
},
saveResult: [
{
mutationResults: [
{
transformResults: [
{
meaning: 0,
excludeFromIndexes: false,
valueType: 'timestampValue',
},
{
meaning: 0,
excludeFromIndexes: false,
integerValue: '8',
valueType: 'integerValue',
},
{
meaning: 0,
excludeFromIndexes: false,
integerValue: '9',
valueType: 'integerValue',
},
{
meaning: 0,
excludeFromIndexes: false,
integerValue: '6',
valueType: 'integerValue',
},
{
meaning: 0,
excludeFromIndexes: false,
nullValue: 'NULL_VALUE',
valueType: 'nullValue',
},
{
meaning: 0,
excludeFromIndexes: false,
nullValue: 'NULL_VALUE',
valueType: 'nullValue',
},
],
key: null,
conflictDetected: false,
},
],
commitTime: null,
},
],
serverValue: {
name: 'test',
a1: [4, 5, 6],
p2: 6,
p3: 9,
},
gapicRequest: {
client: 'DatastoreClient',
method: 'commit',
reqOpts: {
mutations: [
{
upsert: {
key: {
path: [
{
kind: 'Post',
name: 'post1',
},
],
partitionId: {},
},
properties: {
name: {
stringValue: 'test',
},
p1: {
integerValue: '3',
},
p2: {
integerValue: '4',
},
p3: {
integerValue: '5',
},
a1: {
arrayValue: {
values: [
{
integerValue: '3',
},
{
integerValue: '4',
},
{
integerValue: '5',
},
],
},
},
},
},
propertyTransforms: [
{
property: 'p1',
setToServerValue: 1,
},
{
property: 'p2',
increment: {
integerValue: '4',
},
},
{
property: 'p3',
maximum: {
integerValue: '9',
},
},
{
property: 'p2',
minimum: {
integerValue: '6',
},
},
{
property: 'a1',
appendMissingElements: {
values: [
{
integerValue: '5',
},
{
integerValue: '6',
},
],
},
},
{
property: 'a1',
removeAllFromArray: {
values: [
{
integerValue: '3',
},
],
},
},
],
},
],
},
gaxOpts: {},
},
},
],
async (testParameters: any) => {
it(testParameters.name, async () => {
const requestSpy = sinon.spy(datastore.request_);
datastore.request_ = requestSpy;
const result = await datastore.save(testParameters.saveArg);
// Clean the data from the server first before comparing:
result.forEach(serverResult => {
delete serverResult['indexUpdates'];
serverResult.mutationResults?.forEach(mutationResult => {
delete mutationResult['updateTime'];
delete mutationResult['createTime'];
delete mutationResult['version'];
mutationResult.transformResults?.forEach(transformResult => {
delete transformResult['timestampValue'];
});
});
});
// Now the data should have fixed values.
// Do a comparison against the expected result.
assert.deepStrictEqual(result, testParameters.saveResult);
// Now check the value that was actually saved to the server:
const [entity] = await datastore.get(key);
const parsedResult = JSON.parse(JSON.stringify(entity));
delete parsedResult['p1']; // This is a timestamp so we can't consistently test this.
assert.deepStrictEqual(parsedResult, testParameters.serverValue);
delete requestSpy.args[0][0].reqOpts.mutations[0].upsert.key
.partitionId['namespaceId'];
assert.deepStrictEqual(
requestSpy.args[0][0],
testParameters.gapicRequest,
);
});
},
);
});
});
},
);
Loading